-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlosscurves_sislob.stan
120 lines (85 loc) · 2.47 KB
/
losscurves_sislob.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
functions {
real growth_factor_weibull(real t, real omega, real theta) {
return 1 - exp(-(t/theta)^omega);
}
real growth_factor_loglogistic(real t, real omega, real theta) {
real pow_t_omega = t^omega;
return pow_t_omega / (pow_t_omega + theta^omega);
}
}
data {
int<lower=0,upper=1> growthmodel_id;
int n_data;
int n_time;
int n_cohort;
int cohort_id[n_data];
int t_idx[n_data];
int cohort_maxtime[n_cohort];
vector<lower=0>[n_time] t_value;
vector[n_cohort] premium;
vector[n_data] loss;
}
parameters {
real<lower=0> omega;
real<lower=0> theta;
vector<lower=0>[n_cohort] LR;
real mu_LR;
real<lower=0> sd_LR;
real<lower=0> loss_sd;
}
transformed parameters {
vector[n_time] gf;
vector[n_data] lm;
for(i in 1:n_time) {
gf[i] = growthmodel_id == 1 ?
growth_factor_weibull (t_value[i], omega, theta) :
growth_factor_loglogistic(t_value[i], omega, theta);
}
for (i in 1:n_data) {
lm[i] = LR[cohort_id[i]] * premium[cohort_id[i]] * gf[t_idx[i]];
}
}
model {
mu_LR ~ normal(0, 0.5);
sd_LR ~ lognormal(0, 0.5);
LR ~ lognormal(mu_LR, sd_LR);
loss_sd ~ lognormal(0, 0.7);
omega ~ lognormal(0, 0.5);
theta ~ lognormal(0, 0.5);
loss ~ normal(lm, (loss_sd * premium)[cohort_id]);
}
generated quantities {
vector<lower=0>[n_time] loss_sample[n_cohort];
vector<lower=0>[n_time] loss_prediction[n_cohort];
vector<lower=0>[n_time] step_ratio[n_cohort];
real mu_LR_exp;
real<lower=0> ppc_minLR;
real<lower=0> ppc_maxLR;
real<lower=0> ppc_EFC;
for(i in 1:n_cohort) {
step_ratio[i] = rep_vector(1, n_time);
loss_sample[i] = LR[i] * premium[i] * gf;
}
mu_LR_exp = exp(mu_LR);
for(i in 1:n_data) {
loss_prediction[cohort_id[i], t_idx[i]] = loss[i];
}
for(i in 1:n_cohort) {
for(j in 2:n_time) {
step_ratio[i, j] = gf[t_idx[j]] / gf[t_idx[j-1]];
}
}
for(i in 1:n_cohort) {
for(j in (cohort_maxtime[i]+1):n_time) {
loss_prediction[i,j] = loss_prediction[i,j-1] * step_ratio[i,j];
}
}
// Create PPC distributions for the max/min of LR
ppc_minLR = min(LR);
ppc_maxLR = max(LR);
// Create total reserve PPC
ppc_EFC = 0;
for(i in 1:n_cohort) {
ppc_EFC = ppc_EFC + loss_prediction[i, n_time] - loss_prediction[i, cohort_maxtime[i]];
}
}