31
31
32
32
__all__ = ['SMC' , 'sample_smc' ]
33
33
34
- EXPERIMENTAL_WARNING = "Warning: SMC is an experimental step method, and not yet" \
35
- " recommended for use in PyMC3!"
34
+ EXPERIMENTAL_WARNING = ( "Warning: SMC is an experimental step method, and not yet "
35
+ " recommended for use in PyMC3!")
36
36
37
- proposal_dists = {
38
- 'MultivariateNormal' : MultivariateNormalProposal ,
39
- }
37
+ proposal_dists = {'MultivariateNormal' : MultivariateNormalProposal }
40
38
41
39
42
40
def choose_proposal (proposal_name , scale = 1. ):
@@ -117,7 +115,9 @@ def __init__(self, vars=None, out_vars=None, n_chains=100, scaling=1., covarianc
117
115
likelihood_name = 'like' , proposal_name = 'MultivariateNormal' , tune = True ,
118
116
tune_interval = 100 , coef_variation = 1. , check_bound = True , model = None ,
119
117
random_seed = - 1 ):
118
+
120
119
warnings .warn (EXPERIMENTAL_WARNING )
120
+
121
121
if random_seed != - 1 :
122
122
nr .seed (random_seed )
123
123
@@ -183,8 +183,19 @@ def __init__(self, vars=None, out_vars=None, n_chains=100, scaling=1., covarianc
183
183
# create initial population
184
184
self .population = []
185
185
self .array_population = np .zeros (n_chains )
186
- for _ in range (self .n_chains ):
187
- self .population .append (pm .Point ({v .name : v .random () for v in vars }, model = model ))
186
+
187
+ init_rnd = {}
188
+ for v in vars :
189
+ if pm .util .is_transformed_name (v .name ):
190
+ trans = v .distribution .transform_used .forward
191
+ rnd = trans (v .distribution .dist .random (size = self .n_chains ))
192
+ init_rnd [v .name ] = rnd .eval ()
193
+ else :
194
+ init_rnd [v .name ] = v .random (size = self .n_chains )
195
+
196
+ for i in range (self .n_chains ):
197
+ self .population .append (pm .Point ({v .name : init_rnd [v .name ][i ] for v in vars },
198
+ model = model ))
188
199
189
200
self .chain_previous_lpoint = copy .deepcopy (self .population )
190
201
@@ -332,15 +343,13 @@ def select_end_points(self, mtrace):
332
343
333
344
# collect end points of each chain and put into array
334
345
for var , slc , shp , _ in self .ordering .vmap :
335
- slc_population = mtrace .get_values (varname = var , burn = n_steps - 1 , combine = True )
346
+ slc_population = mtrace .get_values (varname = var , burn = n_steps - 1 , combine = True )
336
347
if len (shp ) == 0 :
337
348
array_population [:, slc ] = np .atleast_2d (slc_population ).T
338
349
else :
339
350
array_population [:, slc ] = slc_population
340
351
# get likelihoods
341
- likelihoods = mtrace .get_values (varname = self .likelihood_name ,
342
- burn = n_steps - 1 ,
343
- combine = True )
352
+ likelihoods = mtrace .get_values (varname = self .likelihood_name , burn = n_steps - 1 , combine = True )
344
353
345
354
# map end array_endpoints to dict points
346
355
population = [self .bij .rmap (row ) for row in array_population ]
@@ -363,7 +372,7 @@ def get_chain_previous_lpoint(self, mtrace):
363
372
array_population = np .zeros ((self .n_chains , self .lordering .size ))
364
373
n_steps = len (mtrace )
365
374
for _ , slc , shp , _ , var in self .lordering .vmap :
366
- slc_population = mtrace .get_values (varname = var , burn = n_steps - 1 , combine = True )
375
+ slc_population = mtrace .get_values (varname = var , burn = n_steps - 1 , combine = True )
367
376
if len (shp ) == 0 :
368
377
array_population [:, slc ] = np .atleast_2d (slc_population ).T
369
378
else :
@@ -414,7 +423,8 @@ def resample(self):
414
423
415
424
416
425
def sample_smc (n_steps , n_chains = 100 , step = None , start = None , homepath = None , stage = 0 , n_jobs = 1 ,
417
- tune_interval = 10 , tune = None , progressbar = False , model = None , random_seed = - 1 , rm_flag = True ):
426
+ tune_interval = 10 , tune = None , progressbar = False , model = None , random_seed = - 1 ,
427
+ rm_flag = True ):
418
428
"""Sequential Monte Carlo sampling
419
429
420
430
Samples the solution space with n_chains of Metropolis chains, where each
@@ -504,11 +514,11 @@ def sample_smc(n_steps, n_chains=100, step=None, start=None, homepath=None, stag
504
514
step .population = start
505
515
506
516
if not any (step .likelihood_name in var .name for var in model .deterministics ):
507
- raise TypeError ('Model (deterministic) variables need to contain a variable %s '
508
- 'as defined in `step`.' % step .likelihood_name )
517
+ raise TypeError ('Model (deterministic) variables need to contain a variable {} as defined '
518
+ 'in `step`.' . format ( step .likelihood_name ) )
509
519
510
520
step .n_steps = int (n_steps )
511
-
521
+
512
522
stage_handler = atext .TextStage (homepath )
513
523
514
524
if progressbar and n_jobs > 1 :
@@ -539,21 +549,19 @@ def sample_smc(n_steps, n_chains=100, step=None, start=None, homepath=None, stag
539
549
540
550
# Metropolis sampling intermediate stages
541
551
chains = stage_handler .clean_directory (step .stage , chains , rm_flag )
542
- sample_args = {
543
- 'draws' : draws ,
544
- 'step' : step ,
545
- 'stage_path' : stage_handler .stage_path (step .stage ),
546
- 'progressbar' : progressbar ,
547
- 'model' : model ,
548
- 'n_jobs' : n_jobs ,
549
- 'chains' : chains }
552
+ sample_args = {'draws' : draws ,
553
+ 'step' : step ,
554
+ 'stage_path' : stage_handler .stage_path (step .stage ),
555
+ 'progressbar' : progressbar ,
556
+ 'model' : model ,
557
+ 'n_jobs' : n_jobs ,
558
+ 'chains' : chains }
550
559
551
560
_iter_parallel_chains (** sample_args )
552
561
553
562
mtrace = stage_handler .load_multitrace (step .stage )
554
563
555
- step .population , step .array_population , step .likelihoods = step .select_end_points (
556
- mtrace )
564
+ step .population , step .array_population , step .likelihoods = step .select_end_points (mtrace )
557
565
step .beta , step .old_beta , step .weights , sj = step .calc_beta ()
558
566
step .sjs *= sj
559
567
@@ -602,8 +610,8 @@ def sample_smc(n_steps, n_chains=100, step=None, start=None, homepath=None, stag
602
610
model = model )
603
611
604
612
605
- def _sample (draws , step = None , start = None , trace = None , chain = 0 , tune = None ,
606
- progressbar = True , model = None , random_seed = - 1 ):
613
+ def _sample (draws , step = None , start = None , trace = None , chain = 0 , tune = None , progressbar = True ,
614
+ model = None , random_seed = - 1 ):
607
615
608
616
sampling = _iter_sample (draws , step , start , trace , chain ,
609
617
tune , model , random_seed )
0 commit comments