Code Search for Developers
 
 
  

stats_distributions.py from matplotlib at Krugle


Show stats_distributions.py syntax highlighted

"""
Illustrate the connections bettwen the uniform, exponential, gamma and
normal distributions by simulating waiting times from a radioactive
source using the random number generator.  Verify the numerical
results by plotting the analytical density functions from scipy.stats
"""
import numpy
import scipy.stats
from pylab import figure, show, close


# N samples from a uniform distribution on the unit interval.  Create
# a uniform distribution from scipy.stats.uniform and use the "rvs"
# method to generate N uniform random variates
N = 100000
uniform = scipy.stats.uniform()  # the frozen uniform distribution
uninse = uniform.rvs(N)          # the random variates

# in each time interval, the probability of an emission 
rate = 20.  # the emission rate in Hz
dx = 0.001  # the sampling interval in seconds
t = numpy.arange(N)*dx  # the time vector

# the probability of an emission is proportionate to the rate and the interval
emit_times = t[uninse < rate*dx]

# the difference in the emission times is the wait time
wait_times = numpy.diff(emit_times)  

# plot the distribution of waiting times and the expected exponential
# density function lambda exp( lambda wt) where lambda is the rate
# constant and wt is the wait time; compare the result of the analytic
# function with that provided by scipy.stats.exponential.pdf; note
# that the scipy.stats.expon "scale" parameter is inverse rate
# 1/lambda.  Plot all three on the same graph and make a legend.
# Decorate your graphs with an xlabel, ylabel and title
fig = figure()
ax = fig.add_subplot(111)
p, bins, patches = ax.hist(wait_times, 100, normed=True)
l1, = ax.plot(bins, rate*numpy.exp(-rate * bins), lw=2, color='red')
l2, = ax.plot(bins, scipy.stats.expon.pdf(bins, 0, 1./rate),
        lw=2, ls='--', color='green')
ax.set_xlabel('waiting time')
ax.set_ylabel('PDF')
ax.set_title('waiting time density of a %dHz Poisson emitter'%rate)
ax.legend((patches[0], l1, l2), ('simulated', 'analytic', 'scipy.stats.expon'))



# plot the distribution of waiting times for two events; the
# distribution of waiting times for N events should equal a N-th order
# gamma distribution (the exponential distribution is a 1st order
# gamma distribution.  Use scipy.stats.gamma to compare the fits.
# Hint: you can stride your emission times array to get every 2nd
# emission
wait_times2 = numpy.diff(emit_times[::2])
fig = figure()
ax = fig.add_subplot(111)
p, bins, patches = ax.hist(wait_times2, 100, normed=True)
l1, = ax.plot(bins, scipy.stats.gamma.pdf(bins, 2, 0, 1./rate),
        lw=2, ls='-', color='red')
ax.set_xlabel('2 event waiting time 2 events')
ax.set_ylabel('PDF')
ax.set_title('waiting time density of a %dHz Poisson emitter'%rate)
ax.legend((patches[0], l1), ('simulated', 'scipy.stats.gamma'))


# plot the distribution of waiting times for 10 events; again the
# distribution will be a 10th order gamma distribution so plot that
# along with the empirical density.  The central limit thm says that
# as we add N independent samples from a distribution, the resultant
# distribution should approach the normal distribution.  The mean of
# the normal should be N times the mean of the underlying and the
# variance of the normal should be 10 times the variance of the
# underlying.  HINT: Use scipy.stats.expon.stats to get the mean and
# variance of the underlying distribution.  Use scipy.stats.norm to
# get the normal distribution.  Note that the scale parameter of the
# normal is the standard deviation which is the square root of the
# variance
expon_mean, expon_var = scipy.stats.expon(0, 1./rate).stats()
mu, var = 10*expon_mean, 10*expon_var
sigma = numpy.sqrt(var)
wait_times10 = numpy.diff(emit_times[::10])
fig = figure()
ax = fig.add_subplot(111)
p, bins, patches = ax.hist(wait_times10, 100, normed=True)
l1, = ax.plot(bins, scipy.stats.gamma.pdf(bins, 10, 0, 1./rate),
        lw=2, ls='-', color='red')
l2, = ax.plot(bins, scipy.stats.norm.pdf(bins, mu, sigma),
        lw=2, ls='--', color='green')

ax.set_xlabel('waiting time 10 events')
ax.set_ylabel('PDF')
ax.set_title('10 event waiting time density of a %dHz Poisson emitter'%rate)
ax.legend((patches[0], l1, l2), ('simulated', 'scipy.stats.gamma', 'normal approx'))



show()




See more files for this project here

matplotlib

Matplotlib is a pure python plotting library with the goal of making\r\npublication quality plots using a syntax familiar to matlab users. \r\nThe library uses Numeric for handling large\r\ndata sets and supports a variety of output backends

Project homepage: http://sourceforge.net/projects/matplotlib
Programming language(s): C,C++,Python
License: other

  data/
    key_stats/
      CROX_key_stats.html
      GE_key_stats.html
      GOOG_key_stats.html
      INTC_key_stats.html
      MSFT_key_stats.html
      WMT_key_stats.html
      YHOO_key_stats.html
    HISTORY.gz
    ge.csv
    hsales.dat
    monthly_sunspots.dat
    moonlanding.jpg
    nm560.dat
    synapse_data.dat
    synapse_times.dat
  extras/
    fft_demo.py
    spec_interp.py
    steinman_interp.py
    weave_examples.py
  faces/
    data_test/
      face10tn.pcx
      face11tn.pcx
      face12tn.pcx
      face13tn.pcx
      face14tn.pcx
      face15tn.pcx
      face16tn.pcx
      face17tn.pcx
      face18tn.pcx
      face19tn.pcx
      face1tn.pcx
      face20tn.pcx
      face21tn.pcx
      face22tn.pcx
      face23tn.pcx
      face24tn.pcx
      face25tn.pcx
      face26tn.pcx
      face27tn.pcx
      face28tn.pcx
      face29tn.pcx
      face2tn.pcx
      face30tn.pcx
      face3tn.pcx
      face4tn.pcx
      face5tn.pcx
      face6tn.pcx
      face7tn.pcx
      face8tn.pcx
      face9tn.pcx
    data_train/
      face10n.pcx
      face11n.pcx
      face12n.pcx
      face13n.pcx
      face14n.pcx
      face15n.pcx
      face16n.pcx
      face17n.pcx
      face18n.pcx
      face19n.pcx
      face1n.pcx
      face20n.pcx
      face21n.pcx
      face22n.pcx
      face23n.pcx
      face24n.pcx
      face25n.pcx
      face26n.pcx
      face27n.pcx
      face28n.pcx
      face29n.pcx
      face2n.pcx
      face30n.pcx
      face31n.pcx
      face32n.pcx
      face33n.pcx
      face34n.pcx
      face35n.pcx
      face36n.pcx
      face37n.pcx
      face38n.pcx
      face39n.pcx
      face3n.pcx
      face40n.pcx
      face41n.pcx
      face42n.pcx
      face43n.pcx
      face44n.pcx
      face45n.pcx
      face46n.pcx
      face47n.pcx
      face48n.pcx
      face49n.pcx
      face4n.pcx
      face50n.pcx
      face51n.pcx
      face52n.pcx
      face53n.pcx
      face54n.pcx
      face55n.pcx
      face56n.pcx
      face57n.pcx
      face58n.pcx
      face59n.pcx
      face5n.pcx
      face60n.pcx
      face61n.pcx
      face62n.pcx
      face63n.pcx
      face64n.pcx
      face65n.pcx
      face6n.pcx
      face7n.pcx
      face8n.pcx
      face9n.pcx
    faces.py
    fmatch.py
    imatch.py
    imatch2.py
    imtools.py
    test_imatch.py
  logistic/
    sethna_ori/
    __init__.py
    exercise01.py
    exercise02.py
    maplib.py
    maplib.pyc
  numpy_wrap/
    f2py/
    pyrex/
    swig/
  schrodinger/
    Schrodinger_FDTD.pdf
    schrod_fdtd.py
  skel/
    faces/
    fortran_wrap/
    distributions_skel.py
    erathostenes_skel.py
    fft_imdenoise_skel.py
    fit_synapse_skel.py
    fitting_skel.py
    montecarlo_pi_skel.py
    polyroots1d_skel.py
    qsort_skel.py
    quad_newton_skel.py
    recarray_demo_skel.py
    regress_demo_skel.py
    scrape_key_stats_skel.py
    shoot_skel.py
    spline_demo_skel.py
    stats_descriptives_skel.py
    stats_distributions_skel.py
    trapezoid_skel.py
    wallis_pi_skel.py
    wordfreqs_skel.py
  visual/
    bounce.py
    shoot.py
    shoot_t.py
    toroid_drag.py
  BeautifulSoup.py
  __init__.py
  bessel.py
  distributions.py
  erathostenes.py
  erathostenes_fperez.py
  erathostenes_list.py
  erathostenes_set.py
  fft_imdenoise.py
  fit_synapse.py
  fitting.py
  getbibtex.py
  lsys.py
  montecarlo_pi.py
  numpy-blitz_1000.png
  numpy-blitz_300.png
  numpy-blitz_500.png
  numpy_slicing.py
  polyroots1d.py
  qsort.py
  quad_newton.py
  recarray_demo.py
  regress.py
  regress_demo.py
  scrape_key_stats.py
  spline_demo.py
  stats_descriptives.py
  stats_distributions.py
  test.ipy
  trapezoid.py
  wallis_pi.py
  weave_blitz.py
  weave_blitz0.py
  weave_blitz_comp.png
  weave_examples_simple.py
  weave_exercises.py
  wordfreqs.py