-
-
Notifications
You must be signed in to change notification settings - Fork 479
Expand file tree
/
Copy pathtwo_comp_ef_fit.stan
More file actions
71 lines (69 loc) · 1.66 KB
/
two_comp_ef_fit.stan
File metadata and controls
71 lines (69 loc) · 1.66 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// two compartment event and forcing (fit)
functions {
int max_indx (real t, real[] x) {
int N = num_elements(x);
int indx = 1;
while (indx <= N && x[indx] <= t) {
indx = indx + 1;
}
indx = indx - 1;
return indx;
}
real force_constant (real t, real[] x_step, real[] y_step) {
int indx;
real value;
indx = max_indx(t, x_step);
if (indx == 0)
value = 0;
else
value = y_step[indx];
return value;
}
real[] two_comp (real t, real[] y, real[] theta, real[] x_r, int[] x_i) {
real dydt[2];
real c = force_constant(t, {0.0,3,5,7,9}, {0.0,3,0,3,0});
dydt[1] = -theta[1] * y[1] + c;
dydt[2] = theta[1] * y[1] - theta[2] * y[2];
return dydt;
}
}
data {
int<lower=1> T;
int<lower=1> E;
int<lower=1> S;
int<lower=1> K;
real y0[E,S];
real t0[E];
real ts[T];
int start[E];
int end[E];
real y[T,S];
}
transformed data {
real x[0];
int x_int[0];
}
parameters {
real theta[K];
real<lower=0> sigma;
}
model {
real y_hat[T,S];
// 1. stop the integrator at t
// 2. take the last value of y_hat (value at t-1) as the new y0 (initial state)
// 3. combine y0 with the event at t
// 4. restart the integrator
for (i in 1:E) {
real y_state[S];
if (i == 1)
y_state = y0[1,];
else
y_state = to_array_1d(to_vector(y0[i,]) + to_vector(y_hat[end[i-1],]));
y_hat[start[i]:end[i],] = integrate_ode_bdf(two_comp, y_state, t0[i], ts[start[i]:end[i]], theta, x, x_int);
}
for (t in 1:T)
target+= normal_lpdf(y[t] | y_hat[t], sigma);
target+= normal_lpdf(theta[1] | 5, 1);
target+= normal_lpdf(theta[2] | 0, 1);
target+= normal_lpdf(sigma | 0, 0.1);
}