Search code examples
pythonnumpyexp

Python won't show me np.exp marketshare values


I'm trying to estimate marketshares with the following formula:

c = np.exp(-Mu*a)/(np.exp(-Mu*a)+np.exp(-Mu*b))

in which a and b are 9x9 matrices with cell values that can be larger than 1000. Because the numbers are so small, Python returns NaN values. In order to enhance precision of the estimation i have already tried np.float128 but all this does is raise the error that numpy doesn't have an attribute called float128. I have also tried longdouble, again without success. Are there other ways to make Python show the actual values of the cells instead of NaN?


Solution

  • You have:

    c = np.exp(-Mu*a)/(np.exp(-Mu*a)+np.exp(-Mu*b))
    

    Multipying the numerator and denominator by e^(Mu*a), you get:

    c = 1/(1+np.exp(Mu*(a-b)))
    

    This is just a reformulation of the same formula.

    Now, if the exp term is still too small, and you do not need a more precise result, then your c is approximately very close to 1. And if you still need to control precision, you can take log on both sides and use the Taylor expansion of log(1+x).