# Beta samples in log space for numerically stable model

**URL:** <https://discourse.mc-stan.org/t/beta-samples-in-log-space-for-numerically-stable-model/15246>\
**Category:** Modeling\
**Created:** [May 19, 2020, 7:25pm UTC](https://discourse.mc-stan.org/t/beta-samples-in-log-space-for-numerically-stable-model/15246 "2020-05-19T19:25:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![NTorabi](https://avatars.discourse-cdn.com/v4/letter/n/b487fb/32.png) [@NTorabi](https://discourse.mc-stan.org/u/NTorabi)\
**Post date:** [May 19, 2020, 7:25pm UTC](https://discourse.mc-stan.org/t/beta-samples-in-log-space-for-numerically-stable-model/15246/1 "2020-05-19T19:25:47Z")

</div>

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:

```stan
    data {
       real scale;
       real a;
       real b;
    }
    parameters {
        real p;
        vector<lower=0.0, upper=1.0>[3] labeler_sens[4];
   }
    transformed parameters {
        vector[3] transfomed_labeler_sens[4];
        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:

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

```

---

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [May 28, 2020, 12:11pm UTC](https://discourse.mc-stan.org/t/beta-samples-in-log-space-for-numerically-stable-model/15246/2 "2020-05-28T12:11:08Z")

</div>

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!
