0
import scipy.stats as st

samp=st.lognorm(0.6,loc=2,scale=1).rvs(size=2000) # sample
param=st.lognorm.fit(samp, floc = 0) # fit the sample data
print(param) # does not coincide  with shape, loc, scale above!
x=np.linspace(0,10,100)
pdf_fitted = st.lognorm.pdf(x, param[0], loc=param[1], scale=param[2]) # fitted distribution
print(pdf_fitted)
pdf = st.lognorm.pdf(x, 0.5, loc=0, scale=1) # original distribution
plt.plot(x,pdf_fitted,'r-',x,pdf,'g-', color = "red")
plt.hist(samp,bins=30,normed=True,alpha=.3)

enter image description here

I want to do the lognormal fitting. When I used the above code, "two" fitted-curve were plotted. The first (the peak was around 0.5) might be wrong. The second (the peak was around 3) was the only curve I wanted.

How should I do? I've chacked the similar question, but I cannot understand how to solve it.

Python 3.7.2, scipy 1.7.1, Mac 10.14.5 were used.

rrkk
  • 437
  • 1
  • 5
  • 15
  • To generate `samp`, you used `loc=2`. In the next line, you use `floc=0`, which tells the `fit` to fix the `loc` parameter to be 0. So of course it will not match. Try removing `floc=0` from the call of `fit`. – Warren Weckesser Aug 22 '21 at 22:28
  • Thank you for your comment. I tried the below code, and I got the ideal graph. However, why I got always the two curves and I had to hide the unideal curve by setting "loc = 10"? samp=st.lognorm(0.6,loc=2,scale=1).rvs(size=2000) # sample param=st.lognorm.fit(samp) # fit the sample data ・・・ pdf = st.lognorm.pdf(x, 0.5, loc = 10, scale=1) # original distribution ・・・ – rrkk Aug 23 '21 at 04:36

2 Answers2

0

I understood the below code was incorrect.

pdf = st.lognorm.pdf(x, 0.5, loc=0, scale=1) # original distribution

rrkk
  • 437
  • 1
  • 5
  • 15
0
import numpy as np
import scipy.stats as st
import matplotlib.pyplot as plt

samp = st.lognorm(0.5, loc=2, scale=1).rvs(size=1000)  # sample
param = st.lognorm.fit(samp)  # fit the sample data
print(param)  # does not coincide  with shape, loc, scale above!
x = np.linspace(0, 10, 100)
# pdf_fitted = st.lognorm.pdf(x, param[0], loc=param[1], scale=param[2]) # fitted distribution
pdf_fitted = st.lognorm.pdf(x, *param)  # fitted distribution
print(pdf_fitted)
pdf = st.lognorm.pdf(x, 0.5, loc=2, scale=1)  # original distribution
plt.hist(samp, bins=30, alpha=.3, density=True)
plt.plot(x, pdf_fitted, '--', alpha=0.5)
plt.plot(x, pdf, '-', alpha=0.5)
plt.show()

enter image description here

silgon
  • 6,890
  • 7
  • 46
  • 67