1

I'm trying to do a fit to a histogram but without any luck. There's a bunch of information about how to do it in a normal probability density function but not to other types of pdf's.

import pylab as py
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import exponpow

# Fit procedure
eigfit=[]
for i in range(0,len(eigenvals1)):
if eigenvals1>=4.3:
    eigfit.append(eigenvals1[i])

b = exponpow.fit(eigfit)

But I cannot find how to plot the curve for this exponential pdf. It gives me a couple of values from the fit, but in scipy docs it's not well explained how it does this fit.

Hooked
  • 84,485
  • 43
  • 192
  • 261
user2820579
  • 3,261
  • 7
  • 30
  • 45

1 Answers1

4

The fit method does a maximum likelihood estimation of the parameters.

To fit an exponpow distribution, you probably want the location parameter (that all scipy distributions have) to be 0. There are two other parameters: a scale parameter and a shape parameter. Here's an example (in an ipython session) of using exponpow.fit, with the location parameter fixed at 0:

First generate some fake data:

In [108]: np.random.seed(123)

In [109]: samples = 5 + np.random.randn(1000)

Use exponpow.fit to fit the parameters to the data. Use floc=0 to fix the location parameter to 0:

In [110]: params = exponpow.fit(samples, floc=0)

Plot a histogram of the samples, using matplotlib.pyplot.hist:

In [111]: histresult = hist(samples, density=True, facecolor='cyan', alpha=0.25)

Plot the PDF of the fitted distribution, using the method exponpow.pdf:

In [112]: x = np.linspace(0, samples.max() + 1, 100)

In [113]: pdf = exponpow.pdf(x, *params)

In [114]: plot(x, pdf, 'b-', linewidth=2)
Out[114]: [<matplotlib.lines.Line2D at 0x5b59b90>]

These are the values of the fitted parameters (shape, loc, scale):

In [115]: params
Out[115]: (3.5192555959521199, 0, 6.078044809594477)

plot

Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214