Skip to content

Commit 84c2b06

Browse files
committed
Merge branch 'master', remote branch 'origin' into gradientBranch
2 parents f6f0ed6 + c597280 commit 84c2b06

File tree

19 files changed

+5555
-254
lines changed

19 files changed

+5555
-254
lines changed

docs/jss/jss_article_static.tex

Lines changed: 3746 additions & 0 deletions
Large diffs are not rendered by default.

docs/jss/pymc.bib

Lines changed: 260 additions & 57 deletions
Large diffs are not rendered by default.

docs/manual2article.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def manual2article(text):
4242

4343
# Proglang and pkg
4444
text = re.sub(r"\\pdfbookmark.*", r"", text)
45-
for pkgname in ['PyMC','NumPy','SciPy','PyTables','Matplotlib','Pylab','Pyrex']:
45+
for pkgname in ['PyMC','NumPy','SciPy','PyTables','Matplotlib','Pylab','Pyrex','WinBUGS','JAGS','Hierarchical Bayes Compiler','pyTables','pydot','IPython','nose','gfortran','gcc','Enthought Python Distribution','Gnu Compiler Collection','GCC','g77','subversion']:
4646
text = re.sub(pkgname, r'\\pkg{%s}'%pkgname,text)
4747
for proglang in ['Python','Fortran']:
4848
text = re.sub(' ' + proglang, r' \\proglang{%s}'%proglang, text)

docs/tutorial.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ \section{Variables' values and log-probabilities}
267267

268268
\section{Fitting the model with MCMC}
269269

270-
PyMC provides several objects that fit probability models (linked collections of variables) like ours. The primary such object, \code{MCMC}, fits models with the Markov chain Monte Carlo algorithm \cite{Gamerman:1997tb}. To create an \code{MCMC} object to handle our model, import \module{DisasterModel.py} and use it as an argument for \code{MCMC}:
270+
PyMC provides several objects that fit probability models (linked collections of variables) like ours. The primary such object, \code{MCMC}, fits models with a Markov chain Monte Carlo algorithm \cite{Gamerman:1997tb}. To create an \code{MCMC} object to handle our model, import \module{DisasterModel.py} and use it as an argument for \code{MCMC}:
271271
\begin{verbatim}
272272
>>> from pymc.examples import DisasterModel
273273
>>> from pymc import MCMC

pymc/Model.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,11 @@ def remember(self, chain=-1, trace_index = None):
604604

605605
for variable in self._variables_to_tally:
606606
if isinstance(variable, Stochastic):
607-
variable.value = self.trace(variable.__name__, chain=chain)[trace_index]
607+
try:
608+
variable.value = self.trace(variable.__name__, chain=chain)[trace_index]
609+
except:
610+
cls, inst, tb = sys.exc_info()
611+
warnings.warn('Unable to remember value of variable %s. Original error: \n\n%s: %s'%(variable,cls.__name__,inst.message))
608612

609613
def trace(self, name, chain=-1):
610614
"""Return the trace of a tallyable object stored in the database.

pymc/distributions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ def gamma_expval(alpha, beta):
11461146
11471147
Expected value of gamma distribution.
11481148
"""
1149-
return asarray(alpha) / beta
1149+
return 1. * asarray(alpha) / beta
11501150

11511151
def gamma_like(x, alpha, beta):
11521152
R"""
@@ -1410,7 +1410,7 @@ def inverse_gamma_expval(alpha, beta):
14101410
14111411
Expected value of inverse gamma distribution.
14121412
"""
1413-
return 1. / (asarray(beta) * (alpha-1.))
1413+
return 1. * asarray(beta) / (alpha-1.)
14141414

14151415
def inverse_gamma_like(x, alpha, beta):
14161416
R"""

pymc/examples/gp/more_examples/Duffy/duffy-jittered.csv

Lines changed: 211 additions & 0 deletions
Large diffs are not rendered by default.

pymc/examples/gp/more_examples/Duffy/getdata.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

pymc/examples/gp/more_examples/Duffy/mcmc.py

Lines changed: 41 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
# want to change the 'm' parameters
88
# below.
99

10-
# The MCMC takes several hours on my machine. To make it run faster, thin the
11-
# dataset in getdata.py
10+
# The MCMC takes several hours on my machine. To make it run faster, thin the dataset.
1211

1312

1413

@@ -19,35 +18,41 @@
1918
from pylab import *
2019
from numpy import *
2120
import model
21+
import pymc as pm
2222

2323

2424
data = csv2rec('duffy-jittered.csv')
25-
DuffySampler = MCMC(model.make_model(data), db='hdf5', dbcomplevel=1, dbcomplib='zlib')
25+
# Convert from record array to dictionary
26+
data = dict([(k,data[k]) for k in data.dtype.names])
27+
# Create model. Use the HDF5 database backend, because the trace will take up a lot of memory.
28+
DuffySampler = pm.MCMC(model.make_model(**data), db='hdf5', dbcomplevel=1, dbcomplib='zlib', dbname='duffydb.hdf5')
2629

2730
# ====================
2831
# = Do the inference =
2932
# ====================
30-
# Use the HDF5 database backend, because the trace will take up a lot of memory.
31-
DuffySampler.use_step_method(GPEvaluationGibbs, walker_v, V, d)
33+
# Use GPEvaluationGibbs step methods.
34+
DuffySampler.use_step_method(pm.gp.GPEvaluationGibbs, DuffySampler.sp_sub_b, DuffySampler.V_b, DuffySampler.tilde_fb)
35+
DuffySampler.use_step_method(pm.gp.GPEvaluationGibbs, DuffySampler.sp_sub_s, DuffySampler.V_s, DuffySampler.tilde_fs)
36+
# Run the MCMC.
3237
DuffySampler.isample(50000,10000,100)
3338

34-
n = len(DuffySampler.trace('V')[:])
35-
39+
n = len(DuffySampler.trace('V_b')[:])
3640

3741
# ==========================
3842
# = Mean and variance maps =
3943
# ==========================
4044

4145
import tables
4246
covariate_raster = tables.openFile('africa.hdf5')
43-
xplot = covariate_raster.lon[:]*pi/180.
44-
yplot = covariate_raster.lat[:]*pi/180.
47+
xplot = covariate_raster.root.lon[:]*pi/180.
48+
yplot = covariate_raster.root.lat[:]*pi/180.
49+
50+
data = ma.masked_array(covariate_raster.root.data[:], mask = covariate_raster.root.mask[:])
4551

46-
data = ma.masked_array(covariate_raster.data[:], mask = covariate_raster.mask[:])
4752
where_unmasked = np.where(True-data.mask)
48-
dpred = dstack(meshgrid(xplot,yplot))[where_unmasked]
53+
dpred = dstack(meshgrid(xplot,yplot))[::-1][where_unmasked]
54+
africa = covariate_raster.root.data[:][where_unmasked]
4955

50-
# This computation is O(m^2)
5156
Msurf = zeros(data.shape)
5257
E2surf = zeros(data.shape)
5358

@@ -56,64 +61,46 @@
5661
# Reset all variables to their values at frame i of the trace
5762
DuffySampler.remember(0,i)
5863
# Evaluate the observed mean
59-
Msurf_b, Vsurf_b = point_eval(DuffySampler.sp_sub_b.M_obs.value, DuffySampler.sp_sub_b.C_obs.value, dpred)
60-
Msurf_0, Vsurf_0 = point_eval(DuffySampler.sp_sub_0.M_obs.value, DuffySampler.sp_sub_0.C_obs.value, dpred)
64+
store_africa_val(DuffySampler.sp_sub_b.M_obs.value, dpred, africa)
65+
Msurf_b, Vsurf_b = pm.gp.point_eval(DuffySampler.sp_sub_b.M_obs.value, DuffySampler.sp_sub_b.C_obs.value, dpred)
66+
Msurf_s, Vsurf_s = pm.gp.point_eval(DuffySampler.sp_sub_s.M_obs.value, DuffySampler.sp_sub_s.C_obs.value, dpred)
67+
Vsurf_b += DuffySampler.V_b.value
68+
Vsurf_s += DuffySampler.V_s.value
6169

62-
freq_b = pm.invlogit(pm.rnormal(Msurf_b, 1/Vsurf_b))
63-
freq_0 = pm.invlogit(pm.rnormal(Msurf_0, 1/Vsurf_0))
70+
freq_b = pm.invlogit(Msurf_b +pm.rnormal(0,1)*np.sqrt(Vsurf_b))
71+
freq_s = pm.invlogit(Msurf_s +pm.rnormal(0,1)*np.sqrt(Vsurf_s))
6472

65-
samp_i = (freq_b*freq_0+(1-freq_b)*DuffySampler.p1.value)**2
73+
samp_i = (freq_b*freq_s+(1-freq_b)*DuffySampler.p1.value)**2
6674

67-
Msurf[where_unmasked] += samp_i/n
75+
Msurf[where_unmasked] += samp_i/float(n)
6876
# Evaluate the observed covariance with one argument
69-
E2surf[where_unmasked] += samp_i**2/n
77+
E2surf[where_unmasked] += samp_i**2/float(n)
7078

7179
# Get the posterior variance and standard deviation
7280
Vsurf = E2surf - Msurf**2
7381
SDsurf = sqrt(Vsurf)
7482

75-
83+
Msurf = ma.masked_array(Msurf, mask=covariate_raster.root.mask[:])
84+
SDsurf = ma.masked_array(SDsurf, mask=covariate_raster.root.mask[:])
85+
covariate_raster.close()
7686

7787
# Plot mean and standard deviation surfaces
7888
close('all')
79-
imshow(Msurf, extent=[x.min(),x.max(),y.min(),y.max()],interpolation='nearest')
80-
plot(x,y,'r.',markersize=4)
81-
axis([x.min(),x.max(),y.min(),y.max()])
89+
imshow(Msurf[::-1,:], extent=np.array([xplot.min(),xplot.max(),yplot.min(),yplot.max()])*180./pi,interpolation='nearest')
90+
plot(DuffySampler.lon,DuffySampler.lat,'r.',markersize=4)
91+
axis(np.array([xplot.min(),xplot.max(),yplot.min(),yplot.max()])*180./pi)
8292
title('Posterior predictive mean surface')
93+
xlabel('Degrees longitude')
94+
ylabel('Degrees latitude')
8395
colorbar()
8496
savefig('duffymean.pdf')
8597

8698
figure()
87-
imshow(SDsurf, extent=[x.min(),x.max(),y.min(),y.max()],interpolation='nearest')
88-
plot(x,y,'r.',markersize=4)
89-
axis([x.min(),x.max(),y.min(),y.max()])
99+
imshow(SDsurf[::-1,:], extent=np.array([xplot.min(),xplot.max(),yplot.min(),yplot.max()])*180./pi,interpolation='nearest')
100+
plot(DuffySampler.lon,DuffySampler.lat,'r.',markersize=4)
101+
axis(np.array([xplot.min(),xplot.max(),yplot.min(),yplot.max()])*180./pi)
90102
title('Posterior predictive standard deviation surface')
103+
xlabel('Degrees longitude')
104+
ylabel('Degrees latitude')
91105
colorbar()
92-
savefig('duffyvar.pdf')
93-
94-
95-
# # ====================
96-
# # = Realization maps =
97-
# # ====================
98-
#
99-
# # Use thinner input arrays, this computation is O(m^6)!!
100-
# m = 101
101-
# xplot = linspace(x.min(),x.max(),m)
102-
# yplot = linspace(y.min(),y.max(),m)
103-
# dpred = dstack(meshgrid(yplot,xplot))
104-
#
105-
# indices = random_integers(n,size=2)
106-
# for j,i in enumerate(indices):
107-
# # Reset all variables to their values at frame i of the trace
108-
# DuffySampler.remember(0,i)
109-
# # Evaluate the Gaussian process realisation
110-
# R = DuffySampler.walker_v.f.value(dpred)
111-
#
112-
# # Plot the realization
113-
# figure()
114-
# imshow(R,extent=[x.min(),x.max(),y.min(),y.max()],interpolation='nearest')
115-
# plot(x,y,'r.',markersize=4)
116-
# axis([x.min(),x.max(),y.min(),y.max()])
117-
# title('Realization from the posterior predictive distribution')
118-
# colorbar()
119-
# savefig('elevdraw%i.pdf'%j)
106+
savefig('duffyvar.pdf')

0 commit comments

Comments
 (0)