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

2024/10/15 12:34:17

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 pltshape = 0.560774853 #standard deviation
scale = 4.630495093 #mean
loc = 0dist=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.

Answer

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 pltshape = 0.560774853
scale = np.exp(4.630495093)
loc = 0dist = 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

https://en.xdnf.cn/q/117832.html

Related Q&A

Python MySQLdb cursor.execute() insert with varying number of values

Similar questions have been asked, but all of them - for example This One deals only with specified number of values.for example, I tried to do this the following way:def insert_values(table, columns, …

Searching in a .txt file and Comparing the two values of a string in python?

"cadence_regulatable_result": "completeRecognition","appserver_results": {"status": "success","final_response": 0,"payload": {"…

How to perform an HTTP/XML authentication with requests

I am trying to authenticate to Docushare with Python 3.4 using requests 2.7. I am relatively new to Python and to the requests module but Ive done a lot of reading and am not able to make any more prog…

webPy Sessions - Concurrent users use same session and session timeout

I have a webPy app using sessions for user authentication. Sessions are initiated like so:web.config.debug=Falsestore = web.session.DiskStore(/path_to_app/sessions) if web.config.get(_session) is None:…

get text content from p tag

I am trying to get description text content of each block on this page https://twitter.com/search?q=data%20mining&src=typd&vertical=default&f=users. html for p tag looks like<p class=&q…

Python - making a function that would add - between letters

Im trying to make a function, f(x), that would add a "-" between each letter:For example:f("James")should output as:J-a-m-e-s-I would love it if you could use simple python function…

python script keeps converting dates to utc

I have the following:import psycopg2 from openpyxl import Workbook wb = Workbook() wb.active =0 ws = wb.active ws.title = "Repair" ws.sheet_properties.tabColor = "CCFFCC"print(wb.sh…

sklearn tsne with sparse matrix

Im trying to display tsne on a very sparse matrix with precomputed distances values but Im having trouble with it.It boils down to this:row = np.array([0, 2, 2, 0, 1, 2]) col = np.array([0, 0, 1, 2, 2,…

Removing a sublist from a list

I have a list e.g. l1 = [1,2,3,4] and another list: l2 = [1,2,3,4,5,6,7,1,2,3,4]. I would like to check if l1 is a subset in l2 and if it is, then I want to delete these elements from l2 such that l2 …

Python double FOR loops without threading

Basically, I want to make a grid with school subjects and all the test results I got from it, and I want to display about 10 results for every subject.Like this:... ------------------------------------…