Hyperparameterized beta distribution with support on arbitrary [a,b] (lpdf)

Not tested yet! But I think this should work, and am sharing it in case it can be of use to someone else:

// hyperparameterized beta distribution with support on arbitrary [a,b]

functions {

  // custom log PDF of hyperparameterized transformed beta distribution
  real hyper_trans_beta_lpdf(real p, real alph, real bet, real a, real b) {

    real z = (p - a) / (b - a);

    return -log(b - a) + lgamma(alph + bet) - (lgamma(alph) + lgamma(bet)) + (alph - 1) * log(z) + (bet - 1) * log1m(z);

  }

}

data {

  // bounds (can be calculated in 'transformed data' block instead)
  real a;
  real b;

}

parameters {

  // parameter of interest
  real<lower=a, upper=b> p;

  // hyperparameters
  real<lower=0> alph;
  real<lower=0> bet;

}

model {

  // incrementing statement
  target += hyper_trans_beta_lpdf(p | alph, bet, a, b);

}

Hi, @Corey.Plate:

Did you put this in the Developers category because you think we should add it to Stan or put it in the User’s Guide?

Why not go the other way around and define

real hyper_trans_beta_lpdf(real y, real alpha, real beta, real lb, real ub) {
   real p = (y - lb) / (ub - lb);
   real jacobian_adjust = log(1 / (ub - lb));  // Jacobian adjustment
   return beta_lpdf(p | alpha, beta) + jacobian_adjustment;
}

I think that’s right if you’re thinking of the generative process for y as

p ~ beta(alpha, beta)
y = lb + p * (ub - lb)

We’ve found it a bit more useful to use the mean parameterization with parameters alpha / (alpha + beta) and alpha + beta. It’s a built-in and you could do the same thing here.

1 Like