SciPy skewnormal fitting

Question:

I am trying to fit data into a skew normal distribution using the SciPy Skewnorm package.

However, I am failing to understand the usage properly as I cannot find proper documentation or examples on this matter.

On the help section I found Documentation and trying to use skewnorm.fit() along with skewnorm.pdf() to fit data into a model and use that model to output a distribution and compare with the original data.

Please let me know if anyone can help with this.

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

# choose some parameters
a, loc, scale = 5.3, -0.1, 2.2
# draw a sample
data = stats.skewnorm(a, loc, scale).rvs(1000)
# estimate parameters from sample
ae, loce, scalee = stats.skewnorm.fit(data)
# Plot the PDF.
plt.figure()
plt.hist(data, bins=100, normed=True, alpha=0.6, color='g')
xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 100)
p = stats.skewnorm.pdf(x,ae, loce, scalee)#.rvs(100)
plt.plot(x, p, 'k', linewidth=2)

Output:

enter image description here

Asked By: Afshin Rahimi

||

Answers:

Here is an example to get you started.

>>> from scipy import stats

# choose some parameters
>>> a, loc, scale = 1.3, -0.1, 2.2
# draw a sample
>>> sample = stats.skewnorm(a, loc, scale).rvs(1000)

# estimate parameters from sample
>>> ae, loce, scalee = stats.skewnorm.fit(sample)
>>> ae
1.2495366661560348
>>> loce
-0.039775813819310835
>>> scalee
2.1126121580965536
Answered By: Paul Panzer

No idea if anyone is still active on this thread. I’m sure that there is an analytical solution but I just pulled an all night obsessively coding a solution to this problem with a genetic algorithm. I’m not familiar with genetic algorithms, so I sort of just based it off intuition – accordingly, I think this code manages to sit in the intersection of both being moderately intelligent work, and profoundly dumb lmfao. I’ll put push it to GitHub if anyone might find it useful – but I’ve got to clock in to work now. Just lmk. Pic related – converging on decent distribution fit. Given mode and upper and lower 95% confidence interval bounds.

enter image description here

Answered By: Hart Traveller
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.