-
-
Notifications
You must be signed in to change notification settings - Fork 478
/
fire.stan
52 lines (50 loc) · 1.33 KB
/
fire.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
/**
* Fire: Fire insurance claims: data distribution incrementing log prob
* directly because the composite lognormal-pareto distribution
* is not built in.
*
* http://www.openbugs.net/Examples/Fire.html
*
* see D. P. M. Scollnik (2007)
* http://www.tandfonline.com/doi/pdf/10.1080/03461230601110447
*
* the mixture used here is Equation (3.1) in the paper
* with smooth density function so r in (3.1) is given
* in Equation (3.4)
*/
data {
int<lower=0> N;
array[N] real x;
}
parameters {
real<lower=0> alpha;
real<lower=0> sigma;
real<lower=0> theta;
}
transformed parameters {
real mu = log(theta) - alpha * sigma * sigma;
}
model {
array[N] real lpa;
real tmp;
real log_tmp;
real neg_log1p_temp;
real log_Phi_alpha_times_sigma;
theta ~ gamma(.001, .001);
alpha ~ gamma(.001, .001);
sigma ~ gamma(.001, .001);
tmp = sqrt(2 * pi()) * alpha * sigma * Phi(alpha * sigma)
* exp(0.5 * pow(alpha * sigma, 2));
log_tmp = log(tmp);
neg_log1p_temp = -log1p(tmp);
log_Phi_alpha_times_sigma = log(Phi(alpha * sigma));
for (i in 1 : N) {
if (theta > x[i]) {
lpa[i] = log_tmp + neg_log1p_temp - log_Phi_alpha_times_sigma
+ lognormal_lpdf(x[i] | mu, sigma);
} else {
lpa[i] = neg_log1p_temp + pareto_lpdf(x[i] | theta, alpha);
}
}
target += sum(lpa);
}