Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SciPy Cumulative Distribution Function Plotting

Tags:

python

cdf

I am having troubles plotting a Cumulative Distribution Function.

So far I Have found this:

scipy.stats.beta.cdf(0.2,6,7)

But that only gives me a point.

This will be what I use to plot:

pylab.plot()
pylab.show()

What I want it to look like is this: File:Binomial distribution cdf.svg

with p = .2 and the bounds stopping once y = 1 or close to 1.

like image 704
Overtim3 Avatar asked Mar 19 '26 13:03

Overtim3


2 Answers

The first argument to cdf can be an array of values, rather than a single value. It will then return an array of values.

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

x = np.linspace(0,20,100)
cdf = stats.binom.cdf
plt.plot(x,cdf(x, 50, 0.2))
plt.show()

enter image description here

like image 149
unutbu Avatar answered Mar 21 '26 03:03

unutbu


I don't think the user above, ubuntu, has suggested the right function to use. Actually his answer is very much misleading and incorrect at large.

Note that binom.cdf() is a function to calculate the cdf of a binomial distribution specified by n and p, Binomial(n,p). That's to say it returns values of the cdf of that random variable for each value in x, rather than the actual cdf function for the discrete distribution specified by vector x.

To calculate cdf for any distribution defined by vector x, just use the histogram() function:

import numpy as np
hist, bin_edges = np.histogram(np.random.randint(0,10,100), normed=True)
cdf = cumsum(hist)

or, just use the hist() plotting function from matplotlib.

like image 22
user3567032 Avatar answered Mar 21 '26 03:03

user3567032