Calculate Scipy LOGNORM.CDF() and get the same answer as MS Excel LOGNORM.DIST

Question:

I am reproducing a chart in a paper using the LOGNORM.DIST in Microsoft Excel 2013 and would like to get the same chart in Python. I am getting the correct answer in excel, but not in python.

In excel the I have,

mean of ln(KE)      4.630495093
std dev of ln(KE)       0.560774853

I then plot x (KE) from 10 to 1000 and using the Excel LOGNORM.DIST and calculate the probability of the event. I’m getting the exact answers from the paper so I’m confident in the calculation. The plot is below:

MS Excel 2013 Plot of LOGNORM.DIST

In python I’m using Python 3.4 and Scipy 0.16.0 and my code is as follows:

%matplotlib inline
from scipy.stats import lognorm
import numpy as np
import matplotlib.pyplot as plt

shape = 0.560774853 #standard deviation
scale = 4.630495093 #mean
loc = 0

dist=lognorm(shape, loc, scale)
x=np.linspace(10,1000,200)

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.set_xscale('log')
ax.set_xlim([10., 1000.])
ax.set_ylim([0., 1.])
ax.plot(x,dist.cdf(x)), dist.cdf(103)

and the plot is,

Python Plot of LOGNORM

I have messed around a lot with the loc parameter, but nothing works. The last line in the python code

dist.cdf(103)

should give me a 50% probability, but obviously I’m doing something wrong.

Asked By: Shonn McNeill

||

Answers:

The scale parameter of the scipy lognorm distribution is exp(mean), where mean is the mean of the underlying normal distribution. So you should write:

scale = np.exp(mean)

Here’s a script that generates a plot like the Excel plot:

import numpy as np
from scipy.stats import lognorm
import matplotlib.pyplot as plt

shape = 0.560774853
scale = np.exp(4.630495093)
loc = 0

dist = lognorm(shape, loc, scale)

x = np.linspace(10, 1000, 500)
plt.semilogx(x, dist.cdf(x))
plt.grid(True)
plt.grid(True, which='minor')
plt.show()

plot of lognorm cdf

Answered By: Warren Weckesser

This post could be improved by specifying the question in the context of the latest evolution of lognorm.dist in excel which requires a 4th parameter:

  • LOGNORM.DIST(x,mean,standard_dev,cumulative) Cumulative :Required.
    If cumulative is TRUE, LOGNORM.DIST returns the cumulative
    distribution function; If FALSE, it returns the probability
    density function.

I believe that the current question and answer here is for the case where that cumulative parameter is false.

This is a good answer for the case where the cumulative parameter is true:

https://stackoverflow.com/a/22617052/17203549

Answered By: Shane