Error at initial value

Dear users,

I am working with an ODE model and the stan code works fine for estimating all parameters , apart from one (beta) which gives the following error. I would like to know what the error means and how do I overcome the error. I have tried the followings:

  1. provide initial values using function initf (with values for beta changed to 0, 1, 2)
  2. removed constraint on beta
    But still gets the error.

Thanks

SAMPLING FOR MODEL 'myModel-fit' NOW (CHAIN 1).
Chain 1: Unrecoverable error evaluating the log probability at the initial value.
Chain 1: Exception: Max number of iterations exceeded (500).  (in 'model504c2b646c11_myModel_fit' at line 102)

[1] "Error in sampler$call_sampler(args_list[[i]]) : "                                                       
[2] "  Exception: Max number of iterations exceeded (500).  (in 'model504c2b646c11_myModel_fit' at line 102)"
[1] "error occurred during calling the sampler; sampling not done"

myModel-fit.stan (2.8 KB)
R_code.R (1.1 KB)

Thereā€™s an argument ā€œmax_num_stepsā€ you can pass to the ODE solvers: https://mc-stan.org/docs/2_18/functions-reference/functions-ode-solver.html

That is the maximum number of little steps the solver will take between output timepoints in your simulation. Try making that higher.

Youā€™ll need to specify the relative and absolute tolerances as well, like, add a:

1e-6, 1e-6, 1000

To the end of your ODE solver and see if that helps. If things are still blowing up, it might indicate a problem with your ODE specification (the solver is having to take ultra tiny steps ā€“ which might mean the solution is blowing up or something weird).

Thanks @bbbales2.

I tried specifying the tolerances to 1e-6 and changed maximum number of steps to 1000, 1e6, 1e7, 2e6 and other values. I still get the same error.

The data I used for this example is generated from the model and the code seems to estimate all parameters except beta. I actually need the code to run with some patient (actual) data. However, when i tried to run the stan code with actual data, I get the following error for some patient data but not for others.

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: lognormal_lpdf: Location parameter[2] is nan, but must be finite!  (in 'model4adc71ce664f_myModel_fit_upto_omega' at line 131)

Chain 1: Rejecting initial value:
Chain 1:   Error evaluating the log probability at the initial value.
Chain 1: Exception: lognormal_lpdf: Location parameter[3] is nan, but must be finite!  (in 'model4adc71ce664f_myModel_fit_upto_omega' at line 131)

And a warning after the end

Warning message:
In validityMethod(object) :
  The following variables have undefined values:  y_pred[1,1],The following variables have undefined values:  y_pred[2,1],The following variables have undefined values:  y_pred[3,1],The following variables have undefined values:  y_pred[4,1],The following variables have undefined values:  y_pred[5,1],The following variables have undefined values:  y_pred[6,1],The following variables have undefined values:  y_pred[7,1],The following variables have undefined values:  y_pred[8,1],The following variables have undefined values:  y_pred[9,1],The following variables have undefined values:  y_pred[10,1],The following variables have undefined values:  y_pred[11,1],The following variables have undefined values:  y_pred[1,2],The following variables have undefined values:  y_pred[2,2],The following variables have undefined values:  y_pred[3,2],The following variables have undefined values:  y_pred[4,2],The following variables have undefined values:  y_pred[5,2],The following variables have undefined [... truncated]

Line 131 has the code
``R
y_hat ~ lognormal(log(y[,5]), std);

where _y_ is computed from ODE. 
And _y_pred_ is prediction from ODE in the generated quantities block. 

I was wondering if this means sampling for _y_hat_ is not computed as  the location parameter, _log(y[ ,5])_ is nan due to the value of _y[, 5]_ being negative? 
If this is the case, is there a way STANs ODE solver to compute Nonnegative solutions (like MATLAB)? If not, how do I modify the code so that the error is removed and sampling is computed correctly. 

Thanks so much

Can you print the actual values of the parameters that are being used in the simulation that fails to run? ā€˜print(beta);ā€™ etc is valid Stan code. If you know the valid values, then try them in an ODE solver outside of Stan and see what is happening.

This means you havenā€™t assigned values to these variables. Thereā€™s probably a bug in your code here.

This means the mean of the lognormal is a nan. That could happen if y[,5] contains an inf, a nan, or a negative number I think. You should print(y[,5]) and see whatā€™s in there.

Thanks @bbbales2. The error at initialisation is now gone, but the code is very slow (took about 2 and half hours) with even a single chain with 100 iterations. The data used to run stan code is given in the attached .R file. Here is what I did:

  1. use 1e6 as the maximum number of steps in the ODE solver, assumed prior for beta as normal(0.5,0.5) which is reparametrised in the generated quantities block.

The actual value of beta used to generate the data is very small (3.8e-11). I wonder if the longer run time is due to the choice of prior for beta? If so what would be a good prior for a parameter which is very small and positive. I thought of uniform[0,1], but reading through Stan documentation, uniform priors are not recommended in general. My main concern at present is to check if the Stan code recovers the actual values used to generate the data.

Any help is appreciated.

One tip Iā€™ve seen is to reparameterize your model so that parameters are on the unit scale. Maybe instead of trying to recover beta as 3.8e-11, try defining your parameter as lbeta=log(beta), then try to recover lbeta=-11 (roughly).

1 Like

Thanks @ScottAlder. The tip really helped. Run time is reduced.

1 Like