Calculate Scipy LOGNORM.CDF() and get the same ans

2019-09-19 11:11发布

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.

1条回答
The star\"
2楼-- · 2019-09-19 11:24

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

查看更多
登录 后发表回答