3
3
//! Simplistic MCMC ensemble sampler based on [emcee](https://emcee.readthedocs.io/), the MCMC hammer
4
4
//!
5
5
//! ```
6
- //! use hammer_and_sample::{sample, MinChainLen, Model, Serial};
6
+ //! use hammer_and_sample::{sample, MinChainLen, Model, Serial, Stretch };
7
7
//! use rand::{Rng, SeedableRng};
8
8
//! use rand_pcg::Pcg64;
9
9
//!
39
39
//! ([p], rng)
40
40
//! });
41
41
//!
42
- //! let (chain, _accepted) = sample(&model, walkers, MinChainLen(10 * 1000), Serial);
42
+ //! let (chain, _accepted) = sample(&model, &Stretch, walkers, MinChainLen(10 * 1000), Serial);
43
43
//!
44
44
//! // 100 iterations of 10 walkers as burn-in
45
45
//! let chain = &chain[10 * 100..];
48
48
//! }
49
49
//! ```
50
50
use std:: ops:: ControlFlow ;
51
+ use std:: ptr;
51
52
52
53
use rand:: {
53
54
distr:: { Distribution , StandardUniform , Uniform } ,
54
55
Rng ,
55
56
} ;
57
+ use rand_distr:: {
58
+ weighted:: { AliasableWeight , WeightedAliasIndex } ,
59
+ Normal ,
60
+ } ;
56
61
#[ cfg( feature = "rayon" ) ]
57
62
use rayon:: iter:: { IntoParallelRefMutIterator , ParallelExtend , ParallelIterator } ;
58
63
@@ -117,6 +122,157 @@ impl Params for Box<[f64]> {
117
122
}
118
123
}
119
124
125
+ /// TODO
126
+ pub trait Move < M >
127
+ where
128
+ M : Model ,
129
+ {
130
+ /// TODO
131
+ fn propose < ' a , O , R > ( & self , self_ : & ' a M :: Params , other : O , rng : & mut R ) -> ( M :: Params , f64 )
132
+ where
133
+ O : FnMut ( & mut R ) -> & ' a M :: Params ,
134
+ R : Rng ;
135
+ }
136
+
137
+ /// TODO
138
+ pub struct Stretch ;
139
+
140
+ impl < M > Move < M > for Stretch
141
+ where
142
+ M : Model ,
143
+ {
144
+ fn propose < ' a , O , R > ( & self , self_ : & ' a M :: Params , mut other : O , rng : & mut R ) -> ( M :: Params , f64 )
145
+ where
146
+ O : FnMut ( & mut R ) -> & ' a M :: Params ,
147
+ R : Rng ,
148
+ {
149
+ let other = other ( rng) ;
150
+
151
+ let z = ( ( M :: SCALE - 1. ) * gen_unit ( rng) + 1. ) . powi ( 2 ) / M :: SCALE ;
152
+
153
+ let new_state = M :: Params :: collect (
154
+ self_
155
+ . values ( )
156
+ . zip ( other. values ( ) )
157
+ . map ( |( self_, other) | other - z * ( other - self_) ) ,
158
+ ) ;
159
+
160
+ let factor = ( new_state. dimension ( ) - 1 ) as f64 * z. ln ( ) ;
161
+
162
+ ( new_state, factor)
163
+ }
164
+ }
165
+
166
+ /// TODO
167
+ pub struct DifferentialEvolution {
168
+ gamma : Normal < f64 > ,
169
+ }
170
+
171
+ impl DifferentialEvolution {
172
+ /// TODO
173
+ pub fn new ( sigma : Option < f64 > , gamma0 : Option < f64 > , dimension : usize ) -> Self {
174
+ let sigma = sigma. unwrap_or ( 1e-5 ) ;
175
+ let gamma0 = gamma0. unwrap_or_else ( || 2.38 / ( 2. * dimension as f64 ) . sqrt ( ) ) ;
176
+
177
+ Self {
178
+ gamma : Normal :: new ( gamma0, sigma) . unwrap ( ) ,
179
+ }
180
+ }
181
+ }
182
+
183
+ impl < M > Move < M > for DifferentialEvolution
184
+ where
185
+ M : Model ,
186
+ {
187
+ fn propose < ' a , O , R > ( & self , self_ : & ' a M :: Params , mut other : O , rng : & mut R ) -> ( M :: Params , f64 )
188
+ where
189
+ O : FnMut ( & mut R ) -> & ' a M :: Params ,
190
+ R : Rng ,
191
+ {
192
+ let first_other = other ( rng) ;
193
+ let mut second_other = other ( rng) ;
194
+
195
+ while ptr:: eq ( first_other, second_other) {
196
+ second_other = other ( rng) ;
197
+ }
198
+
199
+ let gamma = self . gamma . sample ( rng) ;
200
+
201
+ let new_state = M :: Params :: collect (
202
+ self_
203
+ . values ( )
204
+ . zip ( first_other. values ( ) )
205
+ . zip ( second_other. values ( ) )
206
+ . map ( |( ( self_, first_other) , second_other) | {
207
+ self_ + gamma * ( first_other - second_other)
208
+ } ) ,
209
+ ) ;
210
+
211
+ ( new_state, 0. )
212
+ }
213
+ }
214
+
215
+ /// TODO
216
+ pub struct Mixture < W , M > ( WeightedAliasIndex < W > , M )
217
+ where
218
+ W : AliasableWeight ;
219
+
220
+ macro_rules! impl_mixture {
221
+ ( $( $types: ident @ $weights: ident) ,+ ) => {
222
+ impl <W , $( $types ) ,+> From <( $( ( $types, W ) ) ,+ ) > for Mixture <W , ( $( $types ) ,+ ) > where W : AliasableWeight {
223
+ #[ allow( non_snake_case) ]
224
+ fn from( ( $( ( $types, $weights ) ) ,+ ) : ( $( ( $types, W ) ) ,+ ) ) -> Self {
225
+ let index = WeightedAliasIndex :: new( vec![ $( $weights ) ,+] ) . unwrap( ) ;
226
+
227
+ Self ( index, ( $( $types ) ,+ ) )
228
+ }
229
+ }
230
+
231
+ impl <W , $( $types ) ,+, M > Move <M > for Mixture <W , ( $( $types ) ,+ ) >
232
+ where
233
+ W : AliasableWeight ,
234
+ M : Model ,
235
+ $( $types: Move <M > ) ,+
236
+ {
237
+ #[ allow( non_snake_case) ]
238
+ fn propose<' a, O , R >( & self , self_: & ' a M :: Params , other: O , rng: & mut R ) -> ( M :: Params , f64 )
239
+ where
240
+ O : FnMut ( & mut R ) -> & ' a M :: Params ,
241
+ R : Rng ,
242
+ {
243
+ let Self ( index, ( $( $types ) ,+ ) ) = self ;
244
+
245
+ let chosen_index = index. sample( rng) ;
246
+
247
+ let mut index = 0 ;
248
+
249
+ $(
250
+
251
+ #[ allow( unused_assignments) ]
252
+ if index == chosen_index {
253
+ return $types. propose( self_, other, rng)
254
+ } else {
255
+ index += 1 ;
256
+ }
257
+
258
+ ) +
259
+
260
+ unreachable!( )
261
+ }
262
+ }
263
+ } ;
264
+ }
265
+
266
+ impl_mixture ! ( A @ a, B @ b) ;
267
+ impl_mixture ! ( A @ a, B @ b, C @ c) ;
268
+ impl_mixture ! ( A @ a, B @ b, C @ c, D @ d) ;
269
+ impl_mixture ! ( A @ a, B @ b, C @ c, D @ d, E @ e) ;
270
+ impl_mixture ! ( A @ a, B @ b, C @ c, D @ d, E @ e, F @ f) ;
271
+ impl_mixture ! ( A @ a, B @ b, C @ c, D @ d, E @ e, F @ f, G @ g) ;
272
+ impl_mixture ! ( A @ a, B @ b, C @ c, D @ d, E @ e, F @ f, G @ g, H @ h) ;
273
+ impl_mixture ! ( A @ a, B @ b, C @ c, D @ d, E @ e, F @ f, G @ g, H @ h, I @ i) ;
274
+ impl_mixture ! ( A @ a, B @ b, C @ c, D @ d, E @ e, F @ f, G @ g, H @ h, I @ i, J @ j) ;
275
+
120
276
/// Models are defined by the type of their parameters and their probability functions
121
277
pub trait Model : Send + Sync {
122
278
/// Type used to store the model parameters, e.g. `[f64; N]` or `Vec<f64>`
@@ -138,17 +294,19 @@ pub trait Model: Send + Sync {
138
294
/// The number of walkers must be non-zero, even and at least twice the number of parameters.
139
295
///
140
296
/// A vector of samples and the number of accepted moves are returned.
141
- pub fn sample < M , W , R , S , E > (
142
- model : & M ,
297
+ pub fn sample < MD , MV , W , R , S , E > (
298
+ model : & MD ,
299
+ move_ : & MV ,
143
300
walkers : W ,
144
301
mut schedule : S ,
145
302
execution : E ,
146
- ) -> ( Vec < M :: Params > , usize )
303
+ ) -> ( Vec < MD :: Params > , usize )
147
304
where
148
- M : Model ,
149
- W : Iterator < Item = ( M :: Params , R ) > ,
305
+ MD : Model ,
306
+ MV : Move < MD > + Send + Sync ,
307
+ W : Iterator < Item = ( MD :: Params , R ) > ,
150
308
R : Rng + Send + Sync ,
151
- S : Schedule < M :: Params > ,
309
+ S : Schedule < MD :: Params > ,
152
310
E : Execution ,
153
311
{
154
312
let mut walkers = walkers
@@ -166,10 +324,8 @@ where
166
324
167
325
let random_index = Uniform :: new ( 0 , half) . unwrap ( ) ;
168
326
169
- let update_walker = move |walker : & mut Walker < M , R > , other_walkers : & [ Walker < M , R > ] | {
170
- let other = & other_walkers[ random_index. sample ( & mut walker. rng ) ] ;
171
-
172
- walker. move_ ( model, other)
327
+ let update_walker = move |walker : & mut Walker < MD , R > , other_walkers : & [ Walker < MD , R > ] | {
328
+ walker. move_ ( model, move_, |rng| & other_walkers[ random_index. sample ( rng) ] )
173
329
} ;
174
330
175
331
while schedule. next_step ( & chain) . is_continue ( ) {
@@ -187,22 +343,22 @@ where
187
343
( chain, accepted)
188
344
}
189
345
190
- struct Walker < M , R >
346
+ struct Walker < MD , R >
191
347
where
192
- M : Model ,
348
+ MD : Model ,
193
349
{
194
- state : M :: Params ,
350
+ state : MD :: Params ,
195
351
log_prob : f64 ,
196
352
rng : R ,
197
353
accepted : usize ,
198
354
}
199
355
200
- impl < M , R > Walker < M , R >
356
+ impl < MD , R > Walker < MD , R >
201
357
where
202
- M : Model ,
358
+ MD : Model ,
203
359
R : Rng ,
204
360
{
205
- fn new ( model : & M , state : M :: Params , rng : R ) -> Self {
361
+ fn new ( model : & MD , state : MD :: Params , rng : R ) -> Self {
206
362
let log_prob = model. log_prob ( & state) ;
207
363
208
364
Self {
@@ -213,20 +369,17 @@ where
213
369
}
214
370
}
215
371
216
- fn move_ ( & mut self , model : & M , other : & Self ) -> M :: Params {
217
- let z = ( ( M :: SCALE - 1. ) * gen_unit ( & mut self . rng ) + 1. ) . powi ( 2 ) / M :: SCALE ;
218
-
219
- let mut new_state = M :: Params :: collect (
220
- self . state
221
- . values ( )
222
- . zip ( other. state . values ( ) )
223
- . map ( |( self_, other) | other - z * ( other - self_) ) ,
224
- ) ;
372
+ fn move_ < ' a , MV , O > ( & ' a mut self , model : & MD , move_ : & MV , mut other : O ) -> MD :: Params
373
+ where
374
+ MV : Move < MD > ,
375
+ O : FnMut ( & mut R ) -> & ' a Self ,
376
+ {
377
+ let ( mut new_state, factor) =
378
+ move_. propose ( & self . state , |rng| & other ( rng) . state , & mut self . rng ) ;
225
379
226
380
let new_log_prob = model. log_prob ( & new_state) ;
227
381
228
- let log_prob_diff =
229
- ( new_state. dimension ( ) - 1 ) as f64 * z. ln ( ) + new_log_prob - self . log_prob ;
382
+ let log_prob_diff = factor + new_log_prob - self . log_prob ;
230
383
231
384
if log_prob_diff > gen_unit ( & mut self . rng ) . ln ( ) {
232
385
self . state . clone_from ( & new_state) ;
@@ -380,7 +533,7 @@ where
380
533
/// Runs the inner `schedule` after calling the given `callback`
381
534
///
382
535
/// ```
383
- /// # use hammer_and_sample::{sample, MinChainLen, Model, Schedule, Serial, WithProgress};
536
+ /// # use hammer_and_sample::{sample, MinChainLen, Model, Schedule, Serial, Stretch, WithProgress};
384
537
/// # use rand::SeedableRng;
385
538
/// # use rand_pcg::Pcg64Mcg;
386
539
/// #
@@ -407,7 +560,7 @@ where
407
560
/// callback: |chain: &[_]| eprintln!("{} %", 100 * chain.len() / 100_000),
408
561
/// };
409
562
///
410
- /// let (chain, accepted) = sample(&model, walkers, schedule, Serial);
563
+ /// let (chain, accepted) = sample(&model, &Stretch, walkers, schedule, Serial);
411
564
/// ```
412
565
pub struct WithProgress < S , C > {
413
566
/// The inner schedule which determines the number of iterations
0 commit comments