# Estimating parameters from data for skew t distribution

I’m new to stan (loving it so far), and somewhere between a beginner and intermediate with Bayesian modeling in general. I’m trying to fit a skew t distribution with some data. Stan does not have a skew t (or skew generalized t), but I found a couple of related posts on the topic here in forums.

One that seems useful is a chi-square skew normal mixture in this comment:

I’ll copy the code from that comment here for convenience:

``````library(rstan)
scode <- "
data {
real<lower=1> nu;
real alpha;
}
transformed data {
real sqrt_nu = sqrt(nu);
}
parameters {
real<lower=0> V;
real Z;
}
transformed parameters {
real T = Z * sqrt(V) * sqrt_nu;
}
model {
V ~ inv_chi_square(nu);
Z ~ skew_normal(0, 1, alpha);
}
"

foo_data <- list(nu = 4, alpha = 1)
``````

But that does not seem to be designed to learn parameters from data. Instead it looks like parameters are being fed in as data. I was trying to modify it to fit parameters from observed data, but I’m getting errors, and I’m not sure how to proceed. Is this an easy challenge for someone here? Or can you point me to a reference that can help me make the conversion? Or any other suggestions or resources on how to fit a skew t distribution in stan?

What errors are you getting?
I think the code that can learn the parameters looks something like this.

``````data {
int<lower=1> N;
real T[N];
}
parameters {
real alpha;
real<lower=1> nu;
real<lower=0> V[N];
}
transformed parameters {
real sqrt_nu = sqrt(nu);
}
model {
for (n in 1:N) {
V[n] ~ inv_chi_square(nu);
T[n] ~ skew_normal(0, sqrt(V[n]) * sqrt_nu, alpha);
}
}
``````
2 Likes

Yes, that does seem to work. Thank you!

My problem was not looping, and not moving the sqrt(V[n]) * sqrt_nu block within the skew_normal distribution. Like I said, I’m new. :)