Question

I want to evaulate a fraction like this:

enter image description here

or

\begin{equation}  
\frac{e^{y_t}}{\sum_{i=1}^T{e^{y_i}}}  
\end{equation}

for large y values. But a statistical software (e.g., R) can not evaluate exp(800) or even larger numbers. Then the problem is that the fraction is evaluated to NaN. But we know that this fraction is always between 0 and 1. Is it possible to use some mathematical or programming tricks to fix the problem? Thank you so much.

Was it helpful?

Solution

I would try

  • put 1 on top, and distribute the exp(-Y[t]) into each term of the sum

  • simplify each term of the sum, by summing the exponents

or some such algebra.

Another way to look at it ...

You say the overall fraction is between 0 and 1. Well, if it's close to 1, that means the denominator is bound to be reasonable.

If it's close to 0, that means the denominator is very large. So if you're summing the denominator, and it gets unreasonably large, just quit. You know it can only increase.

Also, you could sort the terms of the sum, doing the ones with the largest exponent first. That way you get to the unreasonableness sooner.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top