Jim421616
Jim421616

Reputation: 1536

Not sure what distribution to use to model my data

I have a set of astronomical data, to which I'm trying to fit a curve:

enter image description here

My fitting code is

param = stats.norm.fit(df['delta z'].dropna())   # Fit a normal distribution to the data
pdf_fitted = stats.norm.pdf(df['delta z'], *param)
x = np.linspace(*df['delta z'].agg([min, max]), 1000) # x-values
binwidth = np.diff(edges).mean()
ax.plot(x, stats.norm.pdf(x, *param)*h.sum()*binwidth, color = 'r')

which produces

enter image description here

Now, I'm clearly doing this in the wrong way, because the curve doesn't fit the data at all. All of the tutorials I've seen, such as here involve making a set of data, in which case we already know things like the mean and the skew. This question led me to estimate the parameters with

a_estimate, loc_estimate, scale_estimate = stats.skewnorm.fit(df['delta z'])
ax.plot(x, skewnorm.pdf(x, a_estimate, loc_estimate, scale_estimate), 'r-', lw=5, alpha=0.6, label='skewnorm pdf')

which produces

enter image description here

so how can I plot the fit with those parameters?

Upvotes: 2

Views: 190

Answers (1)

James Phillips
James Phillips

Reputation: 4657

In the comments you state that you don't know how to plot the curve: here is a small example fitting and plotting skewnorm.

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

data = ss. expon.rvs(size=1000)

P = ss.expon.fit(data)
rX = np.linspace(min(data), max(data), 50)
rP = ss.skewnorm.pdf(rX, *P)

plt.hist(data,bins=25, normed=True, color='slategrey')

plt.plot(rX, rP, color='darkturquoise')
plt.show()

Upvotes: 1

Related Questions