-
Notifications
You must be signed in to change notification settings - Fork 0
/
flexible_monster.stan
365 lines (359 loc) · 13.4 KB
/
flexible_monster.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
functions {
vector prepare_unit_log_vector(vector y, int enforce_constraints){
if(!enforce_constraints){return y;}
vector[rows(y)] rv = y;
rv[5] = -sum(rv[2:4]);
rv[7] = -rv[6];
return rv;
}
vector constrain_log_vector(vector y, int enforce_constraints){
if(!enforce_constraints){return y;}
vector[rows(y)] rv = y;
rv[2:5] = log_softmax(rv[2:5]);
rv[6:7] = log_softmax(rv[6:7]);
return rv;
}
vector dydt_exposure(
real t, vector concentration_out,
vector FVP, vector FFPF, real CFFPF,
real VMI, real KMI
){
return FVP .* (
dot_product(FFPF, concentration_out) + CFFPF - concentration_out
) + [
0,0,0,VMI * concentration_out[4] / (KMI + concentration_out[4])
]';
}
vector dydt_nonexposure(
real t, vector concentration_out,
vector FVP, vector FFPF,
real VMI, real KMI
){
return FVP .* (
dot_product(FFPF, concentration_out) - concentration_out
) + [
0,0,0,VMI * concentration_out[4] / (KMI + concentration_out[4])
]';
}
real lambert_w0_exp(real earg){
if(is_nan(earg)){return earg;}
// https://www.wolframalpha.com/input/?i=N%5BTable%5B%7BProductLog%5BExp%5Bx%5D%5D%2CProductLog%5BExp%5Bx%5D%5D%2F%28ProductLog%5BExp%5Bx%5D%5D%2B1%29%7D%2C+%7Bx%2C%7B-40%2C700%7D%7D%5D%2C+32%5D
// (4.2483542552915889772807209044045×10^-18 | 4.2483542552915889592322070259504×10^-18
// 693.45830887902549833674969928122 | 0.99856002874871758528483054395180)
if(earg > 700){
real x0 = 700;
real y0 = lambert_w0(exp(x0));
real dydx = y0/(y0+1);
return y0 + (earg - x0) * dydx;
// real y0 = 693.45830887902549833674969928122;
// real dydx = 0.9985600287487175852848305439518009385629359235843258625030446976;
// // https://www.wolframalpha.com/input/?i=ProductLog%5BExp%5B700%5D%5D%2F%28ProductLog%5BExp%5B700%5D%5D%2B1%29
// return y0 + (earg - x0) * dydx;
}
if(earg < -40){
real x0 = -40;
real y0 = lambert_w0(exp(x0));
// real y0 = 4.2483542552915889772807209044045064097730030864961656941893e-18;
return y0 * exp(earg - x0);
}
return lambert_w0(exp(earg));
}
real min_concentration(){return 1e-12;}
real exact_michaelis_menten_solution(real dt, real C, real V, real K){
if(C <= min_concentration()){return min_concentration();}
if(K == 0){return C - dt * V;}
real earg = (dt*V+C)/K+log(C/K);
return K * lambert_w0_exp(earg);
}
vector ginterpolate(real xi, vector left, vector right){
return exp((1-xi)*log(min_concentration()+left)+xi*log(min_concentration()+right));
}
// vector strang_step(real dt)
real[,] simulate_person(
data real concentration_exposure,
data real[] times,
vector params,
data vector measured_params,
data int no_sub_steps
){
int no_times = size(times);
int no_exposure_times = 1;
real rv[no_times, 2];
real lean_body_mass = measured_params[1]; #[kg]
real mass_fraction_fat = measured_params[2]; #[1]
real volume_flow_pulmonary = measured_params[3]; #[l/min]
// real concentration_exposure = measured_params[4]; #[mug/l]
real body_mass = lean_body_mass / (1 - mass_fraction_fat); #[kg]
real volume_fat = mass_fraction_fat * body_mass / .92; #[l]
real volume_flow_alveolar = .7 * volume_flow_pulmonary; #[l/min]
real mass_flow_exposure = volume_flow_alveolar * concentration_exposure; #[mug/min]
real VPR = params[1]; #[1]
real volume_flow_venous = volume_flow_alveolar / VPR; #[l/min]
vector[4] unit_volume_flow = params[2:5];
vector[4] volume_flow = unit_volume_flow * volume_flow_venous; #[l/min]
# body density = water density = 1kg/l
vector[3] dummy = lean_body_mass * params[6:8]; #[l]
vector[4] volume = append_row( #[l]
dummy[1:2],
[volume_fat, dummy[3]]'
);
real partition_coefficient_alveolar = params[9]; #[1]
vector[4] partition_coefficient = params[10:13]; #[1]
vector[4] effective_volume = volume .* partition_coefficient;
real VMI = pow(lean_body_mass, .7) * params[14]/(effective_volume[4]);
real KMI = params[15]/effective_volume[4];
vector[4] concentration_out = rep_vector(min_concentration(), 4); #[mug/l]
vector[4] FVP = volume_flow ./ (effective_volume); #[1/min]
real FPF = volume_flow_venous + volume_flow_alveolar / partition_coefficient_alveolar; #[l/min]
array[no_times] vector[4] all_concentration_out;
if(no_sub_steps <= 0){
all_concentration_out[1] = ode_bdf_tol(
dydt_exposure, concentration_out, 0, {times[1]},
pow(10, no_sub_steps), pow(10, no_sub_steps-14), 100000000,
FVP, volume_flow / FPF, mass_flow_exposure / FPF,
-VMI, KMI
)[1];
all_concentration_out[2:] = ode_bdf_tol(
dydt_nonexposure, all_concentration_out[1], 0, to_array_1d(
to_vector(times[2:])-times[1]
),
pow(10, no_sub_steps), pow(10, no_sub_steps-14), 100000000,
FVP, volume_flow / FPF,
-VMI, KMI
);
}else{
matrix[4,4] A = add_diag( #[1/min]
FVP * (volume_flow / FPF)',
-FVP
);
vector[4] A_source = mass_flow_exposure / FPF * A \ FVP; #[mug/l]
vector[4] last_concentration_out = concentration_out; #[mug/l]
real dt = times[1] / no_sub_steps; #[min]
matrix[4,4] transition_matrix = matrix_exp(dt * A); #[1]
vector[4] exp_A_source = transition_matrix * A_source - A_source; #[mug/l]
real last_time = 0; #[min]
real next_time = 0;
int time_idx = 1;
real next_checkpoint = times[time_idx];
while(time_idx <= no_times){
next_time = last_time + dt;
concentration_out[4] = exact_michaelis_menten_solution(
dt/2, concentration_out[4], -VMI, KMI
);
if(time_idx <= no_exposure_times){
concentration_out = transition_matrix * concentration_out + exp_A_source;
}else{
concentration_out = transition_matrix * concentration_out;
}
concentration_out[4] = exact_michaelis_menten_solution(
dt/2, concentration_out[4], -VMI, KMI
);
while(next_time >= next_checkpoint){
all_concentration_out[time_idx] = ginterpolate(
(next_checkpoint - last_time)/dt,
last_concentration_out,
concentration_out
);
if(time_idx == no_exposure_times){
concentration_out = all_concentration_out[time_idx];
next_time = times[time_idx];
}
time_idx += 1;
if(time_idx <= no_times){
next_checkpoint = times[time_idx];
}else{
break;
}
}
last_time = next_time;
last_concentration_out = concentration_out;
}
}
for(time_idx in 1:no_times){
concentration_out = all_concentration_out[time_idx];
real concentration_venous = dot_product(unit_volume_flow, concentration_out);
real concentration_inhale = (time_idx <= no_exposure_times ? concentration_exposure : 0);
real concentration_alveolar = (
concentration_inhale + concentration_venous
) / (
VPR + partition_coefficient_alveolar
);
real concentration_exhale = .7 * concentration_alveolar + .3 * concentration_inhale;
rv[time_idx] = {min_concentration()+concentration_venous, min_concentration()+concentration_exhale};
}
return rv;
}
}
data {
int no_persons;
int no_measured_params;
array[no_persons] vector[no_measured_params] measured_params;
int no_experiments;
real exposures[no_experiments];
int no_measurements;
real experiments[no_persons, no_experiments, no_measurements, 3];
real weights[no_persons, no_experiments, no_measurements, 2];
int no_latent_params;
vector<lower=0>[no_latent_params] population_eM_eM;
vector<lower=0>[no_latent_params] population_eM_eS;
vector<lower=1>[no_latent_params] population_eS_mu;
vector<lower=0>[no_latent_params] population_eS_nu;
array[2] vector[no_latent_params] population_truncation;
array[2] vector[no_latent_params] std_truncation;
array[2] vector[no_latent_params] person_truncation;
real noise_scale;
real likelihood;
int no_sub_steps;
int no_sim_sub_steps;
int enforce_constraints;
int include_jacobian;
}
transformed data {
vector[no_latent_params] log_population_eM_eM = log(population_eM_eM);
vector[no_latent_params] log_population_eM_eS = log(population_eM_eS);
vector<lower=0>[no_latent_params] log_population_eS_mu = log(population_eS_mu);
vector[no_latent_params] log_log_population_eS_mu = log(log_population_eS_mu);
real condition_on_experiment[no_persons, no_experiments];
for(person in 1:no_persons){
for(experiment in 1:no_experiments){
condition_on_experiment[person,experiment] = max(to_array_1d(weights[person,experiment]));
}
}
}
parameters {
vector<
lower=population_truncation[1],
upper=population_truncation[2]
>[no_latent_params] unit_log_population_eM;
vector<
lower=std_truncation[1],
upper=std_truncation[2]
>[no_latent_params] unit_log_population_eS;
array[no_persons] vector[no_latent_params] unit_log_person_params;
vector<lower=0>[2] noise;
}
transformed parameters {
vector[no_latent_params] constrained_unit_log_population_eM = prepare_unit_log_vector(
unit_log_population_eM, enforce_constraints
);
vector[no_latent_params] log_population_eM = constrain_log_vector(
log_population_eM_eM + log_population_eM_eS .* constrained_unit_log_population_eM,
enforce_constraints
);
vector<lower=0>[no_latent_params] population_eM = exp(log_population_eM);
vector<lower=0>[no_latent_params] log_population_eS = exp(
log_log_population_eS_mu + unit_log_population_eS
);
vector<lower=1>[no_latent_params] population_eS = exp(
log_population_eS
);
array[no_persons] vector[no_latent_params] constrained_unit_log_person_params;
array[no_persons] vector[no_latent_params] log_person_params;
array[no_persons] vector<lower=0>[no_latent_params] person_params;
real true_states[no_persons, no_experiments, no_measurements, 2];
if(!enforce_constraints){
population_eM[2:5] = population_eM[2:5] / sum(population_eM[2:5]);
population_eM[6:7] = population_eM[6:7] / sum(population_eM[6:7]);
}
population_eM[6:7] = (
.837 - population_eM[8]
) * population_eM[6:7];
for(person in 1:no_persons){
constrained_unit_log_person_params[person] = prepare_unit_log_vector(
unit_log_person_params[person], enforce_constraints
);
log_person_params[person] = constrain_log_vector(
log_population_eM + log_population_eS .* constrained_unit_log_person_params[person],
enforce_constraints
);
person_params[person] = exp(log_person_params[person]);
if(!enforce_constraints){
person_params[person, 2:5] = person_params[person, 2:5] / sum(person_params[person, 2:5]);
person_params[person, 6:7] = person_params[person, 6:7] / sum(person_params[person, 6:7]);
}
person_params[person, 6:7] = (
.837 - person_params[person,8]
) * person_params[person, 6:7];
if(likelihood){
for(experiment in 1:no_experiments){
if(!condition_on_experiment[person, experiment]){
continue;
}
true_states[person, experiment] = simulate_person(
exposures[experiment],
experiments[person, experiment,,1],
person_params[person], measured_params[person],
no_sub_steps
);
}
}
}
}
model {
target += normal_lpdf(constrained_unit_log_population_eM | 0,1);
if(enforce_constraints){
target += normal_lpdf(unit_log_population_eM[5] | 0,1);
target += normal_lpdf(unit_log_population_eM[7] | 0,1);
}
target += scaled_inv_chi_square_lpdf(
pow(log_population_eS, 2) | population_eS_nu, log_population_eS_mu
);
if(include_jacobian){
target += log(pow(log_population_eS,2));
}
for(person in 1:no_persons){
target += normal_lpdf(constrained_unit_log_person_params[person] | 0,1);
if(enforce_constraints){
target += normal_lpdf(unit_log_person_params[person, 5] | 0,1);
target += normal_lpdf(unit_log_person_params[person, 7] | 0,1);
}
}
if(likelihood){
if(include_jacobian){
target += -log(noise);
}else{
target += -2*log(noise);
}
}else{
target += weibull_lpdf(noise/noise_scale | 2,1);
}
if(likelihood){
for(person in 1:no_persons){
for(experiment in 1:no_experiments){
if(!condition_on_experiment[person, experiment]){
continue;
}
for(measurement in 1:no_measurements){
for(i in 1:2){
real weight = weights[person, experiment, measurement, i];
if(weight){
real observation = experiments[person, experiment, measurement, 1+i];
real state = true_states[person, experiment, measurement, i];
target += weight * lognormal_lpdf(
observation | log(state), noise[i]
);
}
}
}
}
}
}
}
generated quantities {
real predicted_states[no_persons, no_experiments, no_measurements, 2];
for(person in 1:no_persons){
for(experiment in 1:no_experiments){
real temp[no_measurements, 2] = simulate_person(
exposures[experiment],
experiments[person, experiment,,1],
person_params[person], measured_params[person],
no_sim_sub_steps
);
for(i in 1:2){
predicted_states[person,experiment,,i] = lognormal_rng(
log(temp[,i]), noise[i]
);
}
}
}
}