Menu

[r4657]: / trunk / py4science / examples / distributions.py  Maximize  Restore  History

Download this file

100 lines (84 with data), 4.2 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
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 as npy
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 frozed 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
dt = 0.001 # the sampling interval in seconds
t = npy.arange(N)*dt # the time vector
# the probability of an emission is proportionate to the rate and the interval
emit_times = t[uninse < rate*dt]
# the difference in the emission times is the wait time
wait_times = npy.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*npy.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 = npy.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 indenpendent 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 = npy.sqrt(var)
wait_times10 = npy.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()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.