# Log\_sum\_exp(matrix) by column or by row

**URL:** <https://discourse.mc-stan.org/t/log-sum-exp-matrix-by-column-or-by-row/16162>\
**Category:** Modeling\
**Tags:** hmm\
**Created:** [June 23, 2020, 2:08am UTC](https://discourse.mc-stan.org/t/log-sum-exp-matrix-by-column-or-by-row/16162 "2020-06-23T02:08:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sonicking](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@sonicking](https://discourse.mc-stan.org/u/sonicking)\
**Post date:** [June 23, 2020, 2:08am UTC](https://discourse.mc-stan.org/t/log-sum-exp-matrix-by-column-or-by-row/16162/1 "2020-06-23T02:08:38Z")

</div>

Is it possible to compute log\_sum\_exp(matrix) and get a column (so just apply the operation on each element in every row) or get a row (so apply the operation on each element in every column)? I am trying to speed up the forward algorithm in a hidden Markov model longitudinal problem. But right now it loops thru each person, each period and all the possible states. It is quite slow. I want to see if I can increase the speed if I can do this log\_sum\_exp operation for every person together in a vectorized manner.

This is the code that I am running. The forward-algorithm part under the `model{}` is looping every person (I), every period (T), and every state (S). I want to vectorize that portion of the code. The sticking point is the use of `log_sum_exp`. Thank you.

```
data {
  int<lower=1> S; // num states
  int<lower=1> I; // num customers
  int<lower=1> T; // num calibration periods
  int<lower=0> Y[I,T]; // observed behavior
  int<lower=1> K; // Binomial number trials
}

parameters {
  matrix[S*(S-1), I] z; // indep normals for transition prob.
  cholesky_factor_corr[S*(S-1)] L_Omega; // cholesky corr. for transition prob.
  row_vector[S*(S-1)] mu_theta; // mean of unconstrained transition prob.
  vector<lower=0,upper=pi()/2>[S*(S-1)] tau_unif; // scaled variance of transition prob.
  simplex[S+1] mu_unif_phi; // transformed state dependent prob.
  simplex[S] ppi; // initial prob.
}

transformed parameters{
  matrix[I,S*(S-1)] theta; // individual transition parameters
  vector<lower=0>[S*(S-1)] tau; // variance of transition prob.
  matrix[S,S] log_Q[I]; // log transition prob.

  for (s in 1:S*(S-1))
    tau[s] = 2.5 * tan(tau_unif[s]);
  theta = rep_matrix(mu_theta,I) + (diag_pre_multiply(tau,L_Omega) * z)'; // heterogenity for transition prob.
  for (i in 1:I){
    for (k in 1:S){
      row_vector[S-1] ttheta;
      ttheta = theta[i,((S-1)*(k-1)+1):((S-1)*k)];
      log_Q[i,k,]=to_row_vector(log_softmax(to_vector(append_col(ttheta,0))));
      // add the zero at the end, so vector is size S,
      // and compute softmax (multinomial logistic link)
    }
  }
}

model {
  to_vector(z) ~ normal(0, 1);
  L_Omega ~ lkj_corr_cholesky(2);
  mu_theta ~ normal(0, 2);
  mu_unif_phi~dirichlet(rep_vector(1.0,S+1));
  {
    vector[S] mu_phi = head(cumulative_sum(mu_unif_phi),S);
    // Forward algorithm
    for (i in 1:I){
      real acc[S];
      real gamma[T,S];
      for (k in 1:S)
        gamma[1,k] = log(ppi[k])+ binomial_lpmf(Y[i,1]|K,mu_phi[k]);
      for (t in 2:T) {
        for (k in 1:S) {
          for (j in 1:S){
            acc[j] = gamma[t-1,j] + log_Q[i,j,k] + binomial_lpmf(Y[i,t]|K,mu_phi[k]) ;
          }
          gamma[t,k] = log_sum_exp(acc);
        }
      }
      target +=log_sum_exp(gamma[T]);
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![vianeylb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/vianeylb/32/6195_2.png) [@vianeylb](https://discourse.mc-stan.org/u/vianeylb)\
**Post date:** [June 24, 2020, 1:17pm UTC](https://discourse.mc-stan.org/t/log-sum-exp-matrix-by-column-or-by-row/16162/2 "2020-06-24T13:17:50Z")

</div>

I’m not exactly sure if you can do a whole matrix, but I think there’s a way to speed up at least part of your forward algorithm implementation. Here’s an example in Stan of an HMM with gamma state-dependent distributions:

```
data {
   int<lower=0> T; // length of the time series
   vector[T] steps; // step lengths
   int<lower=1> N; // number of states
}

parameters {
   positive_ordered[N] mu; // mean of gamma - ordered
   vector<lower=0>[N] sigma; // SD of gamma
   //tpm
   simplex[N] gamma[N];
}  

transformed parameters {
   vector<lower=0>[N] shape;
   vector<lower=0>[N] rate;

   // transform mean and SD to shape and rate
   for(n in 1:N)
       shape[n] = mu[n]*mu[n]/(sigma[n]*sigma[n]);
   
   for(n in 1:N)
       rate[n] = mu[n]/(sigma[n]*sigma[n]);
}

model {
   vector[N] logp;
   vector[N] logptemp;
   matrix[N,N] log_gamma_tr;
   
   // priors
   mu[1] ~ normal(1, 1);
   mu[2] ~ normal(5, 1);
   sigma ~ student_t(3, 0, 1);

     
   // transpose
   for(i in 1:N)
       for(j in 1:N)
           log_gamma_tr[j,i] = log(gamma[i,j]);

   for(i in 1:N)
     logp = rep_vector(-log(N), N) + gamma_lpdf(steps[t] | shape[n], rate[n]);
    
   // likelihood computation
   for (t in 2:T) {
     for (n in 1:N) {
         logptemp[n] = log_sum_exp(to_vector(log_gamma_tr[n]) + logp) + 
                                   gamma_lpdf(steps[t] | shape[n], rate[n]);   
    }
    logp = logptemp;
   }
   
 target += log_sum_exp(logp);
}

```

In the likelihood computation, you can use the log\_sum\_exp to perform operations by row. Hope that speeds it up a bit.

---

<div class="post-metadata">

**Author:** ![paul](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul/32/7838_2.png) [@paul](https://discourse.mc-stan.org/u/paul)\
**Post date:** [June 25, 2020, 12:07pm UTC](https://discourse.mc-stan.org/t/log-sum-exp-matrix-by-column-or-by-row/16162/3 "2020-06-25T12:07:18Z")

</div>

You can also use the ’ operator to transpose the matrix. However log\_sum\_exp will work with vectors (columns) and row\_vectors. So you just give it whichever you need to sum.

---

<div class="post-metadata">

**Author:** ![sonicking](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@sonicking](https://discourse.mc-stan.org/u/sonicking)\
**Post date:** [June 25, 2020, 3:41pm UTC](https://discourse.mc-stan.org/t/log-sum-exp-matrix-by-column-or-by-row/16162/4 "2020-06-25T15:41:32Z")

</div>

I am trying to apply log\_sum\_exp to a matrix but my goal is to perform the actual computation by row or by column of that matrix, and then get the corresponding vector or row\_vector as the result.

---

<div class="post-metadata">

**Author:** ![paul](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul/32/7838_2.png) [@paul](https://discourse.mc-stan.org/u/paul)\
**Post date:** [June 25, 2020, 5:25pm UTC](https://discourse.mc-stan.org/t/log-sum-exp-matrix-by-column-or-by-row/16162/5 "2020-06-25T17:25:07Z")

</div>

log\_sum\_exp returns a real, so it will sum an entire matrix. So you need to break it down with a for-loop I think.  
f it was me, I would write as a function. Something like…

```
  vector matrix_logsumexp(matrix Mat) {
    int NR = rows(Mat);
    vector[NR] res;
    for (i in 1:NR) {
      res[i] = log_sum_exp(Mat[i,]);      
    }
    return res;
  } //matrix_logsumexp

```

Then you can expose the function and test it in R to make sure it is doing what you think it should (useful for us non-experts with complex functions). Is this what you were thinking of?

---

<div class="post-metadata">

**Author:** ![Alasdair\_Clarke](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/alasdair_clarke/32/9468_2.png) [@Alasdair\_Clarke](https://discourse.mc-stan.org/u/Alasdair_Clarke)\
**Post date:** [April 29, 2025, 9:11am UTC](https://discourse.mc-stan.org/t/log-sum-exp-matrix-by-column-or-by-row/16162/6 "2025-04-29T09:11:59Z")

</div>

Hello,

did this ever get implemented? I’ve been trying to vectorise my foraging model and I have hit the same stumbling block.

---

<div class="post-metadata">

**Author:** ![sonicking](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@sonicking](https://discourse.mc-stan.org/u/sonicking)\
**Post date:** [April 29, 2025, 2:59pm UTC](https://discourse.mc-stan.org/t/log-sum-exp-matrix-by-column-or-by-row/16162/7 "2025-04-29T14:59:08Z")

</div>

Unfortunately, no. I just a loop over each row or column for the matrix.

There is a R function that can take a matrix as an input. It is

```no-highlight
TargetScore::logsumexp(matrix, 2)

```

`TargetScore` is part of the Bioconductor libraries.

---

<div class="post-metadata">

**Author:** ![Alasdair\_Clarke](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/alasdair_clarke/32/9468_2.png) [@Alasdair\_Clarke](https://discourse.mc-stan.org/u/Alasdair_Clarke)\
**Post date:** [April 29, 2025, 3:11pm UTC](https://discourse.mc-stan.org/t/log-sum-exp-matrix-by-column-or-by-row/16162/8 "2025-04-29T15:11:59Z")

</div>

ah. that’s a shame. Thanks for replying!

I spent yesterday trying to vectorise my slow model, and I think I managed it all except for this final stage. And sadly, the resulting model was slower rather than faster!

I guess I can live with slow.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [May 2, 2025, 9:39pm UTC](https://discourse.mc-stan.org/t/log-sum-exp-matrix-by-column-or-by-row/16162/9 "2025-05-02T21:39:24Z")

</div>

> [@Alasdair\_Clarke](#):
>
> I spent yesterday trying to vectorise my slow model, and I think I managed it all except for this final stage. And sadly, the resulting model was slower rather than faster!

> [@paul](#):
>
> So you need to break it down with a for-loop I think.

We don’t have a function that applies functions row-wise to matrices. But even if we did, it wouldn’t be faster than the function @paul wrote above. That’s because there’s no shared computations among the rows.
