Coding a translated / 3-parameter Weibull

I’m trying to fit a translated / 3-parameter Weibull (see here: https://en.wikipedia.org/wiki/Weibull_distribution#Related_distributions), but I’ve never coded a PDF in Stan. I can get it to run, but getting it to mix is another issue.

The challenge with this distribution is that the observed values always have to be greater than the location (i.e. x >= theta on the Wikipedia page). This is problematic because it implies a vector-valued constraint. I’ve come up with what I think is a fairly clumsy solution, so any advice on how to improve this would be most gratefully received!

> foo_model <- "
> data{
>   int<lower=0> N;
>   int<lower=0> Nsites;
>   real<lower=0> y[N];
>   int site[N];
> }
> parameters{
>   real<lower=0> scale[Nsites];
>   real<lower=0> shape;
>   real int_mean;
>   real int_by_site[Nsites];
>   real<lower=0> int_by_site_sd;
> }
> transformed parameters{
>   vector[N] mu;
>   for (i in 1:N)
>    mu[i] = int_mean + int_by_site_sd*int_by_site[site[i]];
> }
> model{
>   shape ~ normal(0,1);
>   scale ~ normal(0,1);
>   int_mean ~ normal(0,1);
>   int_by_site ~ normal(0,1);
>   int_by_site_sd ~ normal(0,1);
>   for (i in 1:N){
>    if (y[i] < mu[i])
>     target += negative_infinity();
>    else
>     target += log(shape/scale[site[i]]) + (shape-1)*log((y[i]-mu[i])/scale[site[i]]) - pow((y[i]-mu[i])/scale[site[i]],shape);
>   }
> }
> "

It would be better to have constraint set so that you can make a parameter transformation without the constraint. Your current approach causes sharp boundary, which is difficult for HMC. Could you tell more about your data and the data analysis problem, because I don’t understand in which kind of case you would assume different lower boundary of the distribution for different y.

You could also check the draft case study Case study: Extreme value analysis and user defined probability functions in Stan for how to implement the distribution as user defined function, which would make it easier to test that distribution and make the model code prettier.

1 Like

Thanks for the reply. I did notice your great case study the same day I posted my q. It is very helpful!

I wasn’t sure how to set the constraint because of the varying boundary. I assumed different boundaries because I am modelling the location parameter mu as a function of some covariates that occur at the same level as y. By definition in the 3-parameter Weibull that I’ve implemented: y >= mu. But maybe I shouldn’t be treating the location parameter in this way?

There’s a section of the manual that explains how to deal with arrays of parameters with varying constraints

It’s hard to say without knowing more about the data generating process.