Nilani Algiriyage
Nilani Algiriyage

Reputation: 35626

Python fit uniform distribution

I'm trying to fit a set of data with uniform distribution. This is what I have tried based on normal distribution fitting. I'm not sure whether this implementation is correct or not? Can you please advise.

import matplotlib.pyplot as plt
from scipy.stats import uniform
mu, std = uniform.fit(data)


plt.hist(data, normed=True, alpha=0.6, color='#6495ED')


xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 100)
p = uniform.pdf(x, mu, std)
plt.plot(x, p, 'k', linewidth=2)
title = "Fit results: mu = %.2f,  std = %.2f" % (mu, std)
plt.title("Uniform Fitting")
plt.show()

Upvotes: 3

Views: 2666

Answers (2)

Michael Baudin
Michael Baudin

Reputation: 1151

I would use OpenTURNS's UniformFactory: the build method returns a distribution which has a drawPDF method.

import openturns as ot
data = [1.,2.,3.,4.,5.,6., 7., 8.]
sample = ot.Sample(data,1)
distribution = ot.UniformFactory().build(sample)
distribution.drawPDF()

This produces:

Uniform distribution

Upvotes: 0

Robert Kern
Robert Kern

Reputation: 13430

That's generally right, once you fix the name errors (I assume logods and data are meant to be the same). Note that the parameters of the uniform distribution are general location and scale parameters (specifically, the lower boundary and width, respectively) and should not be named mu and std, which are specific to the normal distribution. But that doesn't affect the correctness of the code, just the understandability.

Upvotes: 2

Related Questions