Skip to content

Instantly share code, notes, and snippets.

@nazi123
Last active November 4, 2019 10:44
Show Gist options
  • Save nazi123/be18b51f71e2a049f939ca8036237a5f to your computer and use it in GitHub Desktop.
Save nazi123/be18b51f71e2a049f939ca8036237a5f to your computer and use it in GitHub Desktop.
tweedie in stan
data {
int <lower=1> N; // Sample size
int <lower=1> M; // m ~ poison(lambda)
real<lower=0> Y[N]; // outcome and each Yi=x1+x2+...+xm , and each xi ~ Gamma(alpha,gama) so Y|m ~ Gamma(m*alpha,gama)
}
parameters {
real<lower=0> mu;
real<lower=0> phi;
real<lower=1, upper=2> p;
}
transformed parameters {
real lambda = mu^(2-p)/(phi*(2-p));
real alpha = (2-p)/(p-1);
real gama = 1/phi*mu^(1-p)/(p-1);
}
model {
mu ~ cauchy(0, 5);
phi ~ cauchy(0, 5);
for (n in 1:N) {
if (Y[n] == 0) {
target += -lambda;
} else {
vector[M] ps;
for (m in 1:M)
ps[m] = poisson_lpmf(m | lambda) + gamma_lpdf(Y[n] | m*alpha, gama);
target += log_sum_exp(ps);
}
}
}
@nazi123
Copy link
Author

nazi123 commented Nov 4, 2019

Should I consider “M” as a fixed value regarding to the data (like this codes) or it should be a random variable with Poisson distribution in the model? In each of the cases how should I write the codes? And how should I do this?
I appreciate any suggestion and help.
Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment