-
Notifications
You must be signed in to change notification settings - Fork 0
/
Examples.py
133 lines (97 loc) · 3.42 KB
/
Examples.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
Example inference instances for Assignment 04.
Created 2015-03-13 by Tom Loredo
"""
import numpy as np
import scipy
import matplotlib as mpl
from matplotlib.pyplot import *
from scipy import *
from scipy import stats
from poisson_binomial_cauchy import PoissonRateInference, BinomialInference, \
CauchyLocationInference
try:
import myplot
from myplot import close_all
except ImportError:
pass
ion() # for interactive use in a terminal session
def g_mean(params):
"""
Return the function whose expectation gives the posterior mean, i.e.,
just return the values of the params.
"""
params = asarray(params)
return params
have_laplace = False
#-------------------------------------------------------------------------------
# 1st case: Poisson, const prior, (n,T) = (16, 2)
r_u = 20. # upper limit for PDF calculation and plotting
# Create a PRI instance and plot the PDF.
prior_l, prior_u = 0., 1e5
flat_pdf = 1./(prior_u - prior_l)
n, T = 16, 2
pri = PoissonRateInference(T, n, flat_pdf, r_u)
pri.plot(alpha=.5)
xlabel(r'Rate (s$^{-1}$)')
ylabel('PDF (s)')
title('Poisson case')
if have_laplace:
# Laplace approx for the marg. like. and the mean:
ampl, locn, sig, ml = pri.laplace()
laplace_mean = pri.laplace(g_mean)
post_mean_l = laplace_mean[3]/ml
# Use results to plot a Gaussian PDF here.
# Print using string formatting:
print 'Poisson case:'
print 'Marg. like.: {:.4e} (quad), {:.4e} (Laplace)'.format(pri.mlike, ml)
print 'Posterior mean: {:4.2f} (quad), {:4.2f} (Laplace)'.format(pri.post_mean, post_mean_l)
print
#-------------------------------------------------------------------------------
# 2nd case: Binomial, const prior, (n, n_trials) = (8, 12)
# Define the data.
n, n_trials = 8, 12
bi = BinomialInference(n, n_trials)
bfig = figure() # separate figure for binomial case
bi.plot(alpha=.5)
xlabel(r'$\alpha$')
ylabel('Posterior PDF')
title('Binomial case')
if have_laplace:
# Laplace approx for the marg. like. and the mean:
laplace_ml = bi.laplace()
laplace_mean = bi.laplace(g_mean)
post_mean_l = laplace_mean[3]/laplace_ml[3]
# Use results to plot a Gaussian PDF here.
# Print using string formatting:
print 'Beta case:'
print 'Marg. like.: {:10.4e} (quad), {:10.4e} (Laplace)'.format(bi.mlike, laplace_ml[3])
print 'Posterior mean: {:4.2f} (quad), {:4.2f} (Laplace)'.format(bi.post_mean, post_mean_l)
print
#-------------------------------------------------------------------------------
# 3rd case: Cauchy, const prior
x0, d = 5., 3.
data = stats.cauchy(x0, d).rvs(5)
flat_pdf = .001 # e.g., for prior range 1e3
cli = CauchyLocationInference(d, data, flat_pdf, (-15., 25.))
cfig = figure()
cli.plot(alpha=.5)
xlim(-10, 15.)
xlabel('$x_0$')
ylabel('Posterior PDF')
title('Cauchy case; CDF method')
samps = []
for i in range(10000):
samps.append(cli.samp_cdf())
samps = array(samps)
hist(samps, 50, normed=True, color='g', alpha=.5)
if have_laplace:
# Laplace approx for the marg. like. and the mean:
ampl, locn, sig, ml = cli.laplace()
laplace_mean = cli.laplace(g_mean)
post_mean_l = laplace_mean[3]/ml
# Use results to plot a Gaussian PDF here.
# Print using string formatting:
print 'Poisson case:'
print 'Marg. like.: {:.4e} (quad), {:.4e} (Laplace)'.format(cli.mlike, ml)
print 'Posterior mean: {:4.2f} (quad), {:4.2f} (Laplace)'.format(cli.post_mean, post_mean_l)