# Sum of log-normal distributions and Quadrature Integration

I am trying to model some data as:

Y = log(sum^k (e^X_i))
X_i ~ N(0,sigma)

I had been putting all of the X_i as parameters in the model, which works reasonably well, but it was rather slow. To speed things up, I was using the Schwartz-Yeh approximation, which is a lognormal approximation for the sum of lognormals, however to find the parameters for the approximation, one must use numerical integration, and people normally use quadrature eg:

However, it seems strange to be doing the quadrature within the HMC, and I havenâ€™t been getting the speed up I was expecting. The number of numerical integrations scales O(log2(k)), and with 8 quadrature points itâ€™s not clear that much computation is saved.

Reading the manuals and forum, Iâ€™ve seen HMC described as an integration algorithm, so it seems like it might be possible to somehow replace the quadrature with new latent variables, but Iâ€™m not sure how to proceed.

1 Like

Try defining the X_i values to be an `ordered` parameter rather than real. This will stop swapping of the parameters between modes which is probably killing your performance. The issue is that your original model is unidentified under exchange of parameters. Ie x_1 --> x_2 and x_2 --> x_1 gives you the same likelihood.

That makes sense about the parameters swapping around - but with the ordered constraint is it still the same model? This application seems somewhat different from the cutpoint regression introduced in the manual.

Itâ€™s still the same problem formulation. It just means that your variables are ordered, so the problem is no longer unidentified. You canâ€™t swap X_2 and X_1 in this case.

Iâ€™ve done something similar before in a number of cases and it has worked really well.

Give it a try and let me know how it goesâ€¦

Well, itâ€™s not technically exactly the same model, but you can think of the ordering as doing the normal distribution, then sorting. So yes, itâ€™ll be the same for all intents and purposes. We use this technique to identify mixtures, and now that youâ€™e going down this route, you might want to read @betanalphaâ€™s case study on mixtures (under doc on our web pages).

If you want to code this

``````Y = log(sum^k (e^X_i))
X_i ~ N(0,sigma)
``````

you want to code this in Stan as

``````X[i] ~ normal(0, sigma);
...
Y = log_sum_exp(X);
``````

Thatâ€™ll keep the arithmetic stable and wonâ€™t lose precision.

I couldnâ€™t follow what the integral was you were trying to do, but Iâ€™d stay away from approximations if at all possible.

Thanks! I started to look into it and it seemed like it was taking a while for the parameters to converge. All of the X_i are pretty far into the right tail of the distribution and they seemed to all get smashed together, but not at a value low enough.

The Schwartz-Yeh approximation models
Z = log(e^X + e^Y)
Where Z is approximately normally distributed with mean equal to E[Z] and var = E[Z^2] - E[Z]^2.

Although it is an approximation Iâ€™m looking at sums of 100-500 variables, and by recursively doing the integral, I am only looking at adding O(log(k)) computations to the model rather than O(k) variablesâ€¦

Just a quick update - I was able to sidestep the problem by using a gamma rather than log-normal distribution, and then using the sum of logarithmized Gamma random variables using
the method of Marques, Coelho & Carvalho (2014).

Thanks for writing back with the solution (which I markedâ€”at least I hope it was the solution to this problem).