# Worthwhile feature request: log1p\_sum\_exp?

**URL:** <https://discourse.mc-stan.org/t/worthwhile-feature-request-log1p-sum-exp/34072>\
**Category:** Developers\
**Created:** [February 9, 2024, 9:03pm UTC](https://discourse.mc-stan.org/t/worthwhile-feature-request-log1p-sum-exp/34072 "2024-02-09T21:03:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dalton](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/dalton/32/4287_2.png) [@Dalton](https://discourse.mc-stan.org/u/Dalton)\
**Post date:** [February 9, 2024, 9:03pm UTC](https://discourse.mc-stan.org/t/worthwhile-feature-request-log1p-sum-exp/34072/1 "2024-02-09T21:03:01Z")

</div>

I’m working on model where I expect to be using additive log ratios (ALRs) quite a bit. I’m thinking about functions to build up my likelihoods in the spirit of the catergorical\_logit() family of functions, where the softmax is carried out internally and things are kept on the log-scale as much as possible. But rather than including the reference state as a zero within the vector of ALRs, I’d like to just work the reference state as a separate value. So to that end I wrote this function:

```no-highlight
 real log1p_sum_exp(vector alr) {
    return (log_sum_exp(append_row(alr, 0)));
  }

```

Which is all fine and good, but it occurs to me that this might be useful as basic Stan function (and possibly faster/more stable). What I want is ultimately want is -\mathrm{log}(1 + \sum\_{n=1}^N\mathrm{exp}(v\_n)) which is equivalent to the log probability of outcome K, if v is K - 1 vector of ALRs. I’m doing that by appending a zero and then calling log\_sum\_exp.

My thinking is that since `log_sum_exp` is doing \mathrm{max}(v) + \mathrm{log}\sum\_{n=1}^N\mathrm{exp}(v\_n - \mathrm{max}(v)), the `log1p_sum_exp` is doing a bit of redundant/unnecessary math. Within `log_sum_exp`, the zero at the end of the vector will just be replaced by -\mathrm{max}(v) and \mathrm{max}(v) will be replaced by zero, all of the other values would be v\_n - \mathrm{max}(v). So you could instead just define log1p\_sum\_exp base level Stan function that does:

\mathrm{max}(v) + \mathrm{log}(\sum\_{n=1}^N\mathrm{exp}(v\_n - \mathrm{max}(v)) + \mathrm{exp}(-\mathrm{max}(v)))

I don’t know how much faster this would be then the function I wrote, but it’s avoiding the step appending a zero to the end of the row (or a column of zeros as advised in the users guide section on multi-logit regression), which might be advantageous. Is this potentially useful enough/easy enough to request as a new function? It seems to me that this is the extension of the function `log1m_inv_logit` to the additive log ratio realm.

I took a look at the Stan math on Github, but I don’t know C++ and it’d be a learning curve for me to contribute.

---

<div class="post-metadata">

**Author:** ![andre.pfeuffer](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@andre.pfeuffer](https://discourse.mc-stan.org/u/andre.pfeuffer)\
**Post date:** [February 10, 2024, 12:33am UTC](https://discourse.mc-stan.org/t/worthwhile-feature-request-log1p-sum-exp/34072/2 "2024-02-10T00:33:38Z")

</div>

Isn’t the function `log1p_exp` exactly doing that?

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [February 10, 2024, 3:42am UTC](https://discourse.mc-stan.org/t/worthwhile-feature-request-log1p-sum-exp/34072/3 "2024-02-10T03:42:31Z")

</div>

Does `log1p_exp` take the sum when passed a vector? I thought that it was vectorized over its input, yielding a vector out when you pass a vector in.

---

<div class="post-metadata">

**Author:** ![andre.pfeuffer](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@andre.pfeuffer](https://discourse.mc-stan.org/u/andre.pfeuffer)\
**Post date:** [February 10, 2024, 4:03am UTC](https://discourse.mc-stan.org/t/worthwhile-feature-request-log1p-sum-exp/34072/4 "2024-02-10T04:03:39Z")

</div>

Yes sure, you are right. You may delete my post.
