First time here? Check out the FAQ!

Ask Your Question
0

Distribution of a list of numbers

asked 13 years ago

v_2e gravatar image

updated 10 years ago

kcrisman gravatar image

Hello! I'd like to know if there is a Sage function to retrieve a distribution of some experimental data. I have a 1D list of measured values and I need to obtain a second list containing the distribution of that data, for example, to fit it with some Gaussian or Poisson curve.

Thanks.

Preview: (hide)

3 Answers

Sort by » oldest newest most voted
1

answered 13 years ago

Jason Grout gravatar image

The numpy histogram function will bin values together: http://docs.scipy.org/doc/numpy/refer...

The Sage Timeseries histogram function also bins data: http://www.sagemath.org/doc/reference...

Preview: (hide)
link
1

answered 13 years ago

Shashank gravatar image

updated 13 years ago

I use matplotlib for that purpose. Have a look at the first example on the page

http://matplotlib.sourceforge.net/exa...

Edit: To get a list use

import numpy as np
import pylab as P


mu, sigma = 200, 25
x = mu + sigma*P.randn(10000)


n, bins, patches = P.hist(x, 50, normed=1, histtype='stepfilled')
P.setp(patches, 'facecolor', 'g', 'alpha', 0.75)


y = P.normpdf( bins, mu, sigma)
l = P.plot(bins, y, 'k--', linewidth=1.5)
print bins
Preview: (hide)
link

Comments

Oh, I see. Indeed, it may be a good solution. But what do you import Numpy for in this case? Thanks!

v_2e gravatar imagev_2e ( 13 years ago )

Sorry I edited an example file from net and forgot to delete the line

Shashank gravatar imageShashank ( 13 years ago )
0

answered 13 years ago

v_2e gravatar image

I used the hist() function from Matplotlib also, but does it allow to store the distribution in the list for? The problem is that I need to do some fitting to this distribution, and not only to visualize it.

I have found a way to achieve approximately what I need using the histogram() function from scipy.stats module.

Something like this:

from scipy.stats import histogram
distribution_list = histogram(data_list, numbins=10, defaultlimits=(90,164))

It gives the values for the bins, the first bin start and the bin width. To obtain the distribution list with the actual values for further work (like fitting, plotting, adding to something, etc.) one can do something like this:

from scipy.stats import histogram
distribution_list = histogram(data_list, numbins=10, defaultlimits=(90,164))

actual_distribution = []
i = 0
for bin in distribution_list[0]:
    actual_distribution.append((distribution_list[1]+i*distribution_list[2], bin))
    i+=1

But maybe there are some other (more simple or built-in or smarter) ways to get a distribution out of a list of numbers?

Thanks.

Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 13 years ago

Seen: 1,248 times

Last updated: Oct 28 '14