7
7
# want to change the 'm' parameters
8
8
# below.
9
9
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.
12
11
13
12
14
13
19
18
from pylab import *
20
19
from numpy import *
21
20
import model
21
+ import pymc as pm
22
22
23
23
24
24
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' )
26
29
27
30
# ====================
28
31
# = Do the inference =
29
32
# ====================
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.
32
37
DuffySampler .isample (50000 ,10000 ,100 )
33
38
34
- n = len (DuffySampler .trace ('V' )[:])
35
-
39
+ n = len (DuffySampler .trace ('V_b' )[:])
36
40
37
41
# ==========================
38
42
# = Mean and variance maps =
39
43
# ==========================
40
44
41
45
import tables
42
46
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 [:])
45
51
46
- data = ma .masked_array (covariate_raster .data [:], mask = covariate_raster .mask [:])
47
52
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 ]
49
55
50
- # This computation is O(m^2)
51
56
Msurf = zeros (data .shape )
52
57
E2surf = zeros (data .shape )
53
58
56
61
# Reset all variables to their values at frame i of the trace
57
62
DuffySampler .remember (0 ,i )
58
63
# 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
61
69
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 ))
64
72
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
66
74
67
- Msurf [where_unmasked ] += samp_i / n
75
+ Msurf [where_unmasked ] += samp_i / float ( n )
68
76
# Evaluate the observed covariance with one argument
69
- E2surf [where_unmasked ] += samp_i ** 2 / n
77
+ E2surf [where_unmasked ] += samp_i ** 2 / float ( n )
70
78
71
79
# Get the posterior variance and standard deviation
72
80
Vsurf = E2surf - Msurf ** 2
73
81
SDsurf = sqrt (Vsurf )
74
82
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 ()
76
86
77
87
# Plot mean and standard deviation surfaces
78
88
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 )
82
92
title ('Posterior predictive mean surface' )
93
+ xlabel ('Degrees longitude' )
94
+ ylabel ('Degrees latitude' )
83
95
colorbar ()
84
96
savefig ('duffymean.pdf' )
85
97
86
98
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 )
90
102
title ('Posterior predictive standard deviation surface' )
103
+ xlabel ('Degrees longitude' )
104
+ ylabel ('Degrees latitude' )
91
105
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