# Beta samples in log space for numerically stable model

Hi all, I have a question regarding getting samples from Beta distribution in the log space. I want to make the model numerically stable and so I want to have everything in the log space. So, the general idea that I want to implement has two steps: (1) getting samples from Beta, (2) transform samples to the log space and work with them to calculate final target values. So, a toy impl is as follow:

``````    data {
real scale;
real a;
real b;
}
parameters {
real p;
vector<lower=0.0, upper=1.0> labeler_sens;
}
transformed parameters {
vector transfomed_labeler_sens;
for (i in 1:4) {
transfomed_labeler_sens[i, :] = i * labeler_sens[i, :];
}
}
model {
target += beta_lpdf(p | a, b);
for (i in 1:3) {
target += beta_lpdf(labeler_sens[:,i] | scale * p,  scale * (1 - p));
}
}
``````

Now my questions is how I can get Beta samples (I mean p and labeler_sens) in the log space to avoid working with probabilities all together? If I was able to do that, the transformed parameters block will be changed as follow and so I have no variable as a probability:

``````    transformed parameters {
vector log_transfomed_labeler_sens;
for (i in 1:4) {
log_transfomed_labeler_sens[i, :] = log(i) +  log_labeler_sens[i, :];
}
``````

Hi,
sorry we got to your question quite late. If you wanted to have a variant of beta that works on the log scale, e.g. something like `logbeta_lpdf(x | a, b) == beta_lpdf(exp(x) | a, b)` than this is in principle possible by just computing the change of variables needed i.e. you substitute `exp(x)` for `x` into the density formula and then add the Jacobian correction (log - gradient of the transformation) - it may sound scary but is really not after you dig into it a bit :-) )

I would however be surprised if that helped solve some actual problems - why would you want to work on the log scale? What is the computational problem you are trying to solve/avoid by moving to the log scale?

Best of luck with your model!