-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogistic.stan
47 lines (41 loc) · 1.32 KB
/
logistic.stan
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* References:
- Stan functions reference: 11.2 Ordinary differential equation (ODE) solvers
https://mc-stan.org/docs/functions-reference/functions-ode-solver.html
- Ordinary Differential Equations with Stan in R
https://mpopov.com/tutorials/ode-stan-r/
- Predator-Prey Population Dynamics: the Lotka-Volterra model in Stan
https://mc-stan.org/users/documentation/case-studies/lotka-volterra-predator-prey.html
*/
functions {
vector logistic(real t, vector x, array[] real theta) {
vector[1] dx_dt;
real r = theta[1];
real K = theta[2];
dx_dt[1] = r * x[1] * (1 - x[1] / K);
return dx_dt;
}
}
data {
int<lower = 0> N; // number of measurements
array[N] real ts; // measurement times
real<lower = 0> y0; // initial measured value
array[N] real<lower = 0> y; // measured values
}
parameters {
real<lower = 0> r; // intrinsic growth rate
real<lower = 0> K; // carrying capacity
vector<lower = 0>[1] z0; // initial value
real<lower = 0> sigma; // noise scale
}
model {
array[2] real theta = {r, K};
array[N] vector[1] z = ode_rk45(logistic, z0, 0, ts, theta);
y0 ~ lognormal(log(z0), sigma);
for (n in 1:N)
y[n] ~ lognormal(log(z[n]), sigma);
// priors
r ~ normal(0, 5);
K ~ normal(0, 100);
z0 ~ normal(0, 100);
sigma ~ normal(0, 5);
}