-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiremodel_generate.stan
160 lines (138 loc) · 4.42 KB
/
firemodel_generate.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
data {
int<lower=0> N; // # of pixels * time steps
int<lower=0> J; // # of pixels
int<lower=0> P; // # of environment vars
array[N] int<lower=1,upper=N> pid; // pixel count
// vector<lower=1,upper=N>[N] pid; // pixel count
matrix[J,P] x; // NxP environmental matrix
vector<lower=-1>[N] age; // age at observation N
vector<lower=-1,upper=1>[N] y_obs; // ndvi at observation N
// a switch to evaluate the likelihood following:
// https://khakieconomics.github.io/2017/-6/30/An-easy-way-to-simulate-fake-data-in-stan.html
int<lower = 0, upper = 1> fit; // fit the model? Or just run with the priors
int<lower = 0, upper = 1> predict; // predict NDVI for all pixels?
}
parameters {
vector[J] alpha;
vector[J] gamma;
vector[J] lambda;
real<lower=0, upper=1> alpha_mu;
vector[P] gamma_beta;
vector[P] lambda_beta;
real<lower=0> tau_sq;
real<lower=0> gamma_tau_sq;
real<lower=0> lambda_tau_sq;
real<lower=0> alpha_tau_sq;
}
transformed parameters {
vector[N] mu;
vector[J] gamma_mu;
vector[J] lambda_mu;
real tau = sqrt(tau_sq);
real gamma_tau = sqrt(gamma_tau_sq);
real lambda_tau = sqrt(lambda_tau_sq);
real alpha_tau = sqrt(alpha_tau_sq);
// regressions
gamma_mu = x*gamma_beta;
lambda_mu = x*lambda_beta;
if(fit==1){ // only run if fitting is desired
for (i in 1:N){
mu[i] = exp(alpha[pid[i]])+exp(gamma[pid[i]])-exp(gamma[pid[i]])*exp(-(age[i]/exp(lambda[pid[i]])));
// mu = exp(alpha[pid])+exp(gamma[pid])-exp(gamma[pid])*exp(-(age/exp(lambda[pid])));
}
}
}
model {
// hyperpriors
tau ~ student_t(4,0,1); //#inv_gamma(0.01, 0.01);
gamma_tau ~ student_t(4,0,1); //#inv_gamma(0.01, 0.01);
lambda_tau ~ student_t(4,0,1); //#inv_gamma(0.01, 0.01);
alpha_tau ~ student_t(4,0,1); //#inv_gamma(0.01, 0.01);
// priors
alpha_mu ~ normal(0.15,3);
gamma_beta ~ normal(0,3);
lambda_beta ~ normal(0,3);
// recovery curve
alpha ~ normal(alpha_mu, alpha_tau);
gamma ~ normal(gamma_mu,gamma_tau);
lambda ~ normal(lambda_mu,lambda_tau);
// likelihood
// if(fit==1){ // only run if fitting is desired
y_obs ~ normal(mu, tau);
// }
}
generated quantities {
array[N] real y_pred;
//if(predict==1){ // only run if prediction is desired
y_pred = normal_rng(mu, tau);
// }
}
=======
data {
int<lower=0> N; // # of pixels * time steps
int<lower=0> J; // # of pixels
int<lower=0> P; // # of environment vars
array[N] int<lower=1,upper=N> pid; // pixel count
matrix[J,P] x; // NxP environmental matrix
vector<lower=-1>[N] age; // age at observation N
vector<lower=-1,upper=1>[N] y_obs; // ndvi at observation N
// a switch to evaluate the likelihood following:
// https://khakieconomics.github.io/2017/-6/30/An-easy-way-to-simulate-fake-data-in-stan.html
int<lower = 0, upper = 1> fit; // fit the model? Or just run with the priors
int<lower = 0, upper = 1> predict; // predict NDVI for all pixels?
}
parameters {
vector[J] alpha;
vector[J] gamma;
vector[J] lambda;
real<lower=0, upper=1> alpha_mu;
vector[P] gamma_beta;
vector[P] lambda_beta;
real<lower=0> tau_sq;
real<lower=0> gamma_tau_sq;
real<lower=0> lambda_tau_sq;
real<lower=0> alpha_tau_sq;
}
transformed parameters {
vector[N] mu;
vector[J] gamma_mu;
vector[J] lambda_mu;
real tau = sqrt(tau_sq);
real gamma_tau = sqrt(gamma_tau_sq);
real lambda_tau = sqrt(lambda_tau_sq);
real alpha_tau = sqrt(alpha_tau_sq);
// regressions
gamma_mu = x*gamma_beta;
lambda_mu = x*lambda_beta;
if(fit==1){ // only run if fitting is desired
for (i in 1:N){
mu[i] = exp(alpha[pid[i]])+exp(gamma[pid[i]])-exp(gamma[pid[i]])*exp(-(age[i]/exp(lambda[pid[i]])));
// mu = exp(alpha[pid])+exp(gamma[pid])-exp(gamma[pid])*exp(-(age/exp(lambda[pid])));
}
}
}
model {
// hyperpriors
tau ~ student_t(4,0,1); //#inv_gamma(0.01, 0.01);
gamma_tau ~ student_t(4,0,1); //#inv_gamma(0.01, 0.01);
lambda_tau ~ student_t(4,0,1); //#inv_gamma(0.01, 0.01);
alpha_tau ~ student_t(4,0,1); //#inv_gamma(0.01, 0.01);
// priors
alpha_mu ~ normal(0.15,3);
gamma_beta ~ normal(0,3);
lambda_beta ~ normal(0,3);
// recovery curve
alpha ~ normal(alpha_mu, alpha_tau);
gamma ~ normal(gamma_mu,gamma_tau);
lambda ~ normal(lambda_mu,lambda_tau);
// likelihood
// if(fit==1){ // only run if fitting is desired
y_obs ~ normal(mu, tau);
// }
}
generated quantities {
array[N] real y_pred;
//if(predict==1){ // only run if prediction is desired
y_pred = normal_rng(mu, tau);
// }
}