I am getting
Rejecting initial value:
Error evaluating the log probability at the initial value.
Exception: lognormal_lpdf: Random variable[3] is -0.150929, but must be >= 0! (in 'modela6b869eb4ad3_LorenzStan' at line 51)
and eventually
Initialization between (-2, 2) failed after 100 attempts.
Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] "Error in sampler$call_sampler(args_list[[i]]) : Initialization failed."
The offending line (51) is
x[1,] ~ lognormal(log(1.0), 0.2);
The full model is below. Aren’t I just asking the prior for x[1,]
to be sampled from a log normal distribution? But it looks like negative values are being sampled.
functions {
real[] ds_dt(real t, // time
real[] s, // system state
real[] theta, // parameters
real[] x_r, // data
int[] x_i) // unused integer data
{
real x = s[1];
real y = s[2];
real z = s[3];
real alpha = theta[1];
real rho = x_r[1];
real beta = x_r[2];
real dx_dt = alpha * (y - x);
real dy_dt = x * (rho - z) - y;
real dz_dt = x * y - beta * z;
return { dx_dt, dy_dt, dz_dt };
}
}
data {
int<lower = 1> N; // Number of measurements
real ts[N]; // Times of the measurements
real y[N]; // The measurements themselves
}
transformed data {
real rho = 45.92;
real beta = 4.00;
real rdummy[2];
rdummy[1] = rho;
rdummy[2] = beta;
}
parameters {
real x[N,3];
real ln_alpha[N];
real mu_alpha;
real<lower = 0> sigma_alpha;
}
model {
real parms[1];
real deltaT;
mu_alpha ~ uniform(12.0, 20.0);
sigma_alpha ~ uniform(0.0, 0.5);
x[1,] ~ lognormal(log(1.0), 0.2);
ln_alpha[1] ~ normal(log(mu_alpha), sigma_alpha);
for (i in 2:N) {
parms[1] = exp(ln_alpha[i-1]);
target += normal_lpdf(x[i,] | (integrate_ode_rk45(ds_dt, x[i-1,], ts[i-1], ts[i:i], parms, rdummy, rep_array(0, 0)))[1], 1.0E-3);
deltaT = ts[i] - ts[i-1];
target += normal_lpdf(ln_alpha[i] | ln_alpha[i - 1] - deltaT * sigma_alpha * sigma_alpha / 2, sqrt(deltaT) * sigma_alpha);
}
y ~ normal(x[,1], 0.2);
}