Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to debug "could not calculate numerical derivatives -- flat or discontinuous region encountered"

    I have the following custom ml problem.

    Code:
    cap program drop censored_likelihood
    program define censored_likelihood
        version 17.0
        args lnf sigma lambda gamma0 gamma1
        tempvar m epsilon Iy0 Iy010 Iy10
        local y "$ML_y1"
        gen double `m' = (1 + (`lambda' - 1) * x * (`gamma0' - exp(-`gamma1' * z)))
        gen double `epsilon' = `y' - `m'
        qui replace `epsilon' = 0 if `y' == 0
        
        gen double `Iy0' = (`y' == 0)
        gen double `Iy010' = (`y' > 0 & `y' < 10)
        gen double `Iy10' = (`y' == 10)
        
        replace `lnf' = -0.5 * ln(2*c(pi)*`sigma'^2) - (`y'-`m')^2/(2*`sigma'^2)  ///
                    + `Iy0'   * ln(normal(-`m'/`sigma')) ///
                    + `Iy010' * ln(normal((10-`m')/`sigma') - normal(-`m'/`sigma')) ///
                    + `Iy10' * ln(1 - normal((10-`m')/`sigma'))
        
    end
    
    ml model lf censored_likelihood (b1: y = x z, nocons) (lambda:) (gamma0:) (gamma1:) (sigma:)
    ml check
    ml maximize

    I received the error: "could not calculate numerical derivatives -- flat or discontinuous region encountered". How do you debug this?

  • #2
    Originally posted by Henry Strawforrd View Post
    I received the error: "could not calculate numerical derivatives -- flat or discontinuous region encountered". How do you debug this?
    What did ml check give you beforehand? It was OK?

    Typically, ml check is the major diagnostic method for debugging, and it is at that step (making ml check happy) where you do most of the debugging of the evaluator.

    Comment


    • #3
      Yes, ml check did not give any error. I also used ml search actually, did not put it in the code.

      Comment


      • #4
        As I mentioned in your parallel thread, you'll want to make sure that the arguments passed from ml model and what your likelihood evaluator expects align.

        One tactic you might want to consider is to start with a simpler likelihood function, get it working and build up progressively.

        Comment

        Working...
        X