forked from ChrisBeaumont/beaumont-idl-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcmc__define.pro
301 lines (271 loc) · 7.49 KB
/
mcmc__define.pro
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
;+
; PURPOSE:
; This class defines an interface for running Markov Chain Monte
; Carlo simulations. The code is based on the opening chapters of
; "Markov Chain Monte Carlo in Practice" by Gilks et al. The code in
; this file sets up the logic for running an MCMC simulation using
; the Metropolis Hastings algorithm. To actually use these functions,
; a subclass must be defined, and the selectTrial and
; logTargetDistribution methods must be overridden. Each of these
; methods is problem-specific, and cannot be coded in advance.
;
; CATEGORY:
; statistics
;
; MODIFICATION HISTORY:
; September 2009: Written by Chris Beaumont
; October 2 2009: Modified run procedure to avoid unnecessary calls
; to logTargetDistribution
; October 3 2009: Added THIN keyword and state variable
;-
;+
; PURPOSE:
; This procedure runs the Markov Chain
;
; CATEGORY:
; statistics
;
; COMMON BLOCKS:
; mcmc_common: Holds the seed variable for calls to randomu
;
; MODIFICATION HISTORY:
; September 2009: Written by Chris Beaumont
; Oct 3 2009: Optimized so that logTargetDistribution is only ever
; evaluated once per link
;-
pro mcmc::run, verbose = verbose
compile_opt idl2
common mcmc_common, seed ;- save the random seed value
current = *self.seed
currentValue = self->logTargetDistribution(current)
nsuccess = 0 & nfail = 0
thinStep = 0
cr = string(13B)
for i = 0, self.nstep - 1, 1 do begin
if keyword_set(verbose) then print, 1. * i / self.nstep, cr, format='($, e0.2, a)'
;- pick a new trial link
trial = self->selectTrial(current, transitionRatio = transitionRatio)
newValue = self -> logTargetDistribution(trial)
;- determine acceptance probability via MH algorithm
alpha = exp(newValue - currentValue) * transitionRatio
u = randomu(seed)
if u lt alpha then begin ;- new trial accepted
current = trial
currentValue = newValue
nsuccess++
endif else nfail++ ;- new trial rejected
if (++thinStep) eq self.thin then begin
thinStep = 0
(*self.chain)[i / self.thin] = current
(*self.logf)[i / self.thin] = currentValue
endif
endfor
self.nsuccess = nsuccess & self.nfail = nfail
end
;+
; PURPOSE:
; This function generates a new trial link in the markov chain,
; given the current link. It is not implemented by default, and must
; be overridden. This function must also return the ratio of the
; transition probabilities between the old and new links (i.e.,
; q(old | new) / q(new | old) ). This ratio is needed by the
; Metropolis Hastings algorithm implemented in
; getTransitionProbability.
;
; CATEGORY:
; statistics
;
; INPUTS:
; current: The current link in the chain. This can be any kind of
; scalar (number, structure, object, etc), as long as the
; implementing class knows how to handle it.
;
; KEYWORD PARAMETERS:
; transitionRatio: This must be set to a named variable that will
; hold, on output, the transition ratio
; q(current | next) / q(next | current)
;
; OUTPUTS:
; The next link in the chain to try.
;
; MODIFICATION HISTORY:
; September 2009: Written by Chris Beaumont
;-
function mcmc::selectTrial, current, transitionRatio = transitionRatio
message, 'Method is not implemented, and must be overrided by a subclass!'
return, !values.d_nan
end
;+
; PURPOSE:
; This function calculates the (unnormalized) logarithm of the target
; distribution that the Marcov Chain aims to sample from. It is usually a
; likelihood or posterior distribution, but the function is not
; implemented in this interface. We use the logarithm of the
; function, since its values may often be very small.
;
; CATEGORY:
; statistics
;
; INPUTS:
; link: The point at which to evaluate the target distribution.
;
; OUTPUTS:
; The log of the target distribution, evaluated at link.
;
; MODIFICATION HISTORY:
; September 2009: Written by Chris Beaumont
;-
function mcmc::logTargetDistribution, link
message, 'Method is not implemented, and must be overridden by a subclass!'
return, !values.d_nan
end
;+
; PURPOSE:
; This function returns the markov chain
;
; CATEGORY:
; statistics
;
; OUTPUTS:
; The Markov Chain
;
; MODIFICATION HISTORY:
; September 2009: Written by Chris Beaumont
;-
function mcmc::getChain, logf = logf
logf = *self.logf
return, *self.chain
end
;+
; PURPOSE:
; This function returns the number of successes and failures (that
; is, acceptances and rejections of the proposal link) in the Markov
; chain.
;
; CATEGORY:
; statistics
;
; KEYWORD PARAMETERS:
; nfail: Set to a named variable to hold the number of failures
;
; OUTPUTS:
; The number of successes
;
; MODIFICATION HISTORY:
; September 2009: Written by Chris Beaumont
;-
function mcmc::getNSuccess, nfail = nfail
nfail = self.nfail
return, self.nsuccess
end
;+
; PURPOSE:
; This method returns the MCMC object's data, if any
;
; CATEGORY:
; statistics
;
; OUTPUTS:
; the data
;-
function mcmc::getData
return, *self.data
end
;+
; PURPOSE:
; This function returns the seed link
;
; CATEGORY:
; statistics
;
; OUTPUTS:
; the seed link
;-
function mcmc::getSeed
return, *self.seed
end
;+
; PURPOSE:
; This function initializes the MCMC object
;
; CATEGORY:
; statistics
;
; INPUTS:
; seed: The initial point to start the chain at. Can be any scalar,
; as long as it is correctly handeled by the other MCMC methods
; nstep: The number of links in the desired Markov Chain.
; data: Any data relevant to the process
;
; KEYWORD PARAMETERS:
; thin: Set to a number to avoid storing every step of the
; chain. This is useful for long chains which might otherwise take up
; lots of memory. If set, then only every THIN'th link will be
; saved to the chain. Should be less than the correlation length of
; the chain.
;
; OUTPUTS:
; 1 for success
;
; MODIFICATION HISTORY:
; September 2009: Written by Chris Beaumont
;-
function mcmc::init, seed, nstep, data, thin = thin
compile_opt idl2
on_error, 2
;- check inputs
if n_params() ne 3 then begin
print, 'mcmc::init calling sequence:'
print, "obj = obj_new('mcmc', seed, nstep, data, [thin = thin])"
return, -1
endif
if n_elements(thin) ne 0 && thin le 0 then $
message, 'thin keyword must be positive'
self.thin = keyword_set(thin) ? thin : 1
self.seed = ptr_new(seed)
self.data = ptr_new(data)
self.chain = ptr_new(replicate(seed, nstep / self.thin))
self.logf = ptr_new(dblarr(nstep / self.thin))
self.nstep = nstep
return, 1
end
;+
; PURPOSE:
; Free memory when we are finished
;
; CATEGORY:
; statistics
;
; MODIFICATION HISTORY:
; September 2009: Written by Chris Beaumont
;-
pro mcmc::cleanup
ptr_free, self.seed
ptr_free, self.chain
ptr_free, self.data
ptr_free, self.logf
return
end
;+
; PURPOSE:
; define the mcmc data structure
;
; CATEGORY:
; statistics
;
; MODIFICATION HISTORY:
; September 2009: Written by Chris Beaumont
; Oct 2010: Added logf field
;-
pro mcmc__define
data = {mcmc, $
seed : ptr_new(), $ ;- the first link in the chain
chain : ptr_new(), $ ;- all of the links
logf : ptr_new(), $ ;- logTargetDistribution, evaluated at chain links
data : ptr_new(), $ ;- any data needed
nsuccess : 0L, $ ;- number of accepted trial links
nfail : 0L, $ ;- number of rejected trial links
nstep : 0L, $ ;- number of links in the chain
thin : 0L $ ;- number of links to skip between storage
}
end