Hello everyone!
I am using Stan.jl and DifferentialEquations.jl to estimate the parameters of the differential equations. In particular, I am taking LotkaVolterra Differential Equation which has 4 parameters.
As the starting step, I am estimating all the 4 parameters using the code below:
using OrdinaryDiffEq, ParameterizedFunctions, Mamba, Stan
f4 = @ode_def_nohes LotkaVolterraTest4 begin
dx = a*x - b*x*y
dy = -c*y + d*x*y
end a=>1.5 b=>1.0 c=>3.0 d=>1.0
u0 = [1.0,1.0]
tspan = (0.0,10.0)
prob4 = ODEProblem(f4,u0,tspan)
sol = solve(prob4,Tsit5())
t = collect(linspace(1,10,10))
using RecursiveArrayTools # for VectorOfArray
randomized = VectorOfArray([(sol(t[i]) + .01randn(2)) for i in 1:length(t)])
data = convert(Array,randomized)
const parameter_estimation_lotka_volterra4_model = "
functions {
real[] sho(real t,real[] y,real[] theta,real[] x_r,int[] x_i) {
real dydt[2];
dydt[1] = theta[1]*y[1] - theta[2]*y[1]*y[2];
dydt[2] = -theta[3]*y[1] - theta[4] * y[1]* y[2];
return dydt;
}
}
data {
real y0[2];
int<lower=1> T;
real y[T,2];
real t0;
real ts[T];
}
transformed data {
real x_r[0];
int x_i[0];
}
parameters {
vector<lower=0>[2] sigma;
real theta[4];
}
model{
real y_hat[T,2];
sigma ~ inv_gamma(2,3);
theta[1] ~ normal(1.5, 1);
theta[2] ~ normal(1.0, 1);
theta[3] ~ normal(3.0, 1);
theta[4] ~ normal(1.0, 1);
y_hat = integrate_ode_rk45(sho, y0, t0, ts, theta, x_r, x_i);
for (t in 1:T){
y[t] ~ normal(y_hat[t], sigma);
}
}
"
stanmodel = Stanmodel(num_samples=1000, num_warmup=1000, name="parameter_estimation_lotka_volterra4_model", model=parameter_estimation_lotka_volterra4_model);
const parameter_estimation_lotka_volterra4_data = Dict("y0"=>prob4.u0, "T" => size(t)[1], "y" => data', "t0" => tspan[1], "ts"=>t)
rc4, sim4 = stan(stanmodel, [parameter_estimation_lotka_volterra4_data], "/Users/ayush/tmp"; CmdStanDir=CMDSTAN_HOME)
which is working properly.
However when I am trying to estimate only 1 parameter (namely a) using the code below:
const parameter_estimation_lotka_volterra1_model = "
functions {
real[] sho(real t,real[] y,real[] theta,real[] x_r,int[] x_i) {
real dydt[2];
dydt[1] = theta[1] * y[1] - 1.0*y[1] * y[2];
dydt[2] = 3.0*y[1] - 1.0*y[1] * y[2];
return dydt;
}
}
data {
real y0[2];
int<lower=1> T;
real y[T,2];
real t0;
real ts[T];
}
transformed data {
real x_r[0];
int x_i[0];
}
parameters {
vector<lower=0>[2] sigma;
real theta[1];
}
model{
real y_hat[T,2];
sigma ~ inv_gamma(2, 3);
theta[1] ~ normal(1.5, 1);
y_hat = integrate_ode_rk45(sho, y0, t0, ts, theta, x_r, x_i);
for (t in 1:T){
y[t] ~ normal(y_hat[t], sigma);
}
}
"
using OrdinaryDiffEq, ParameterizedFunctions, Mamba, Stan
f1 = @ode_def_nohes LotkaVolterraTest1 begin
dx = a*x - b*x*y
dy = -c*y + d*x*y
end a=>1.5 b=1.0 c=3.0 d=1.0
u0 = [1.0,1.0]
tspan = (0.0,10.0)
prob1 = ODEProblem(f1,u0,tspan)
sol = solve(prob1,Tsit5())
t = collect(linspace(1,10,10))
using RecursiveArrayTools # for VectorOfArray
randomized = VectorOfArray([(sol(t[i]) + .01randn(2)) for i in 1:length(t)])
data = convert(Array,randomized)
stanmodel = Stanmodel(num_samples=2, num_warmup=2, name="parameter_estimation_lotka_volterra1_model", model=parameter_estimation_lotka_volterra1_model);
const parameter_estimation_lotka_volterra1_data = Dict("y0"=>prob1.u0, "T" => size(t)[1], "y" => data', "t0" => tspan[1], "ts"=>t)
rc1, sim1 = stan(stanmodel, [parameter_estimation_lotka_volterra1_data], "/Users/ayush/tmp"; CmdStanDir=CMDSTAN_HOME)
I am getting the following error:
ERROR: Return code = -5
Stacktrace:
[1] #stan#4(::Type{T} where T, ::Bool, ::Bool, ::String, ::Function, ::Stan.Stanmodel, ::Array{Dict{String,Any},1}, ::String) at /Users/ayush/.julia/v0.6/Stan/src/main/stancode.jl:160
[2] (::Stan.#kw##stan)(::Array{Any,1}, ::Stan.#stan, ::Stan.Stanmodel, ::Array{Dict{String,Any},1}, ::String) at ./:0
Unrecoverable error evaluating the log probability at the initial value.
Any help would be appreciated. TIA :)