计算Scipy LOGNORM.CDF()并获得与MS Excel LOGNORM.DIST相同的答案 [英] Calculate Scipy LOGNORM.CDF() and get the same answer as MS Excel LOGNORM.DIST
问题描述
我正在使用Microsoft Excel 2013中的LOGNORM.DIST在论文中复制图表,并希望在Python中获得相同的图表。我在excel中得到了正确的答案,但是在python中却没有。
在excel中,我有,
ln(KE)的平均值4.630495093
ln(KE)的标准开发0.560774853
然后我使用Excel LOGNORM.DIST将x(KE)从10绘制到1000,并计算事件的概率。我从论文中得到了确切的答案,所以我对计算很有信心。该图如下所示:
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,
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.
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()
这篇关于计算Scipy LOGNORM.CDF()并获得与MS Excel LOGNORM.DIST相同的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!