Hello,
I ran a GLM using CMDSTANPY. I’ve ran my data sample through it and got the posterior predictive samples. How do I pull out the 5%, 95% and mean of the posterior predictive samples like I get when plotting my posterior parameters?
Hello,
I ran a GLM using CMDSTANPY. I’ve ran my data sample through it and got the posterior predictive samples. How do I pull out the 5%, 95% and mean of the posterior predictive samples like I get when plotting my posterior parameters?
Like getting those values from summary DataFrame (Set hdi_prob to 0.95) or do you want to calculate those values manually (np.quantile(idata.posterior_predictive.theta, [0.05, 0.95])
and np.mean(idata.posterior_predictive.theta)
)?
Getting those values from the posterior predictive. Specifically, loss_ratio_ppc
from the generated quantities block.
functions {
real half_normal_rng(real mu, real sigma) {
real p = normal_cdf(0, mu, sigma); // cdf for bounds
real u = uniform_rng(p, 1);
return (sigma * inv_Phi(u)) + mu; // inverse cdf for value
}
}
data {
int<lower=1> n; // number of observations
vector<lower=0>[n] loss_ratio; //target
vector[n] log_cancellation; //monthly cancel rate
int<lower=1> p;
}
parameters {
real alpha; // intercept
real beta; // slope
real<lower=0> sigma; // scatter
}
transformed parameters {
// Any variable declared here is part of the output produced for draws.
vector[n] mu;
mu = alpha + beta * log_cancellation;
}
model {
// priors
alpha ~ normal(0,5); // prior for intercept
beta ~ normal(0,5); // prior for cancellation rate
sigma ~ normal(0,5); // prior for sigma
// likelihood
for (i in 1:n) {
loss_ratio[i] ~ normal(mu, sigma) T[0,];
}
}
generated quantities {
vector[p] loss_ratio_ppc;
for (i in 1:p) {loss_ratio_ppc[i] = half_normal_rng(mu[i], sigma);}
}
I tried the command print(inf_data.quantiles(q='posterior_predictive'))
but hat doesn’t produce anything.
How did you created the inferencedata object? Did you specify ppc values?
See
https://arviz-devs.github.io/arviz/getting_started/CreatingInferenceData.html
Yes i converted to inference data. When I put said inference data object inside a print statement, I get the following output:
Inference data with groups:
> posterior
> posterior_predictive
> sample_stats
> observed_data
Which led me to try the inf_data.quantiles(q=posterior_predictive)
which didn’t work.
Try
inf_data.posterior_predictive.quantile(q=[0.05,0.95])
You might also want to take a look at this page of the ArviZ documentation: Working with InferenceData — ArviZ dev documentation