# How stan deal with overflow or underflow issue?

#1

At present, I have developed my before code to more patches. It has initialization failure problem. I think it is because I am using exponential for my likelihood function. The value are very small, so it might underflow. I know one way ‘log-sum-exp’ trick. Each likelihood minus the maximum likelihood and get the new likelihood.
So anyone can provide me an example how stan use that? Thanks.

#2

Stan has a log_sum_exp function. The mixture chapter of the Stan book describes what the function is doing.

#3

Thanks, I’ll try that!

#4

Hi,

I think the log_sum _ exp function is doing

log(a+b) = log \underline sum \underline exp(log(a),log(b)).

So for mixture model, it can be written in log.sum.exp(log(lambda)+normal_log(y,mu[1],sigma[1]),loglm(lambda)+normal_log(y,mu[2],sigma[2])).

But for my problem, my likelihood is a exponential distribution. The likelihood is like below:

f(y)\sim mixture\space gaussian(\theta)
Likelihood(y,c|\theta) = \prod \space exp((1/f(y))*Ci)

So my loglikelihood would be

\sum log(lamda[i])-lamda[i]*Ci
How could I forbid the underflow for that kind of likelihood function? Thanks.