25
25
from ..theanof import inputvars , make_shared_replacements , join_nonshared_inputs
26
26
import numpy .random as nr
27
27
28
+ from .metropolis import MultivariateNormalProposal
28
29
from .arraystep import metrop_select
29
30
from ..backends import smc_text as atext
30
31
31
- __all__ = ['SMC' , 'ATMIP_sample ' ]
32
+ __all__ = ['SMC' , 'sample_smc ' ]
32
33
33
34
EXPERIMENTAL_WARNING = "Warning: SMC is an experimental step method, and not yet" \
34
35
" recommended for use in PyMC3!"
35
36
36
-
37
- class Proposal (object ):
38
- """Proposal distributions modified from pymc3 to initially create all the
39
- Proposal steps without repeated execution of the RNG - significant speedup!
40
-
41
- Parameters
42
- ----------
43
- s : :class:`numpy.ndarray`
44
- """
45
- def __init__ (self , s ):
46
- self .s = np .atleast_1d (s )
47
-
48
-
49
- class MultivariateNormalProposal (Proposal ):
50
- def __call__ (self , num_draws = None ):
51
- return np .random .multivariate_normal (
52
- mean = np .zeros (self .s .shape [0 ]), cov = self .s , size = num_draws )
53
-
54
-
55
37
proposal_dists = {
56
38
'MultivariateNormal' : MultivariateNormalProposal ,
57
39
}
@@ -147,6 +129,13 @@ def __init__(self, vars=None, out_vars=None, n_chains=100, scaling=1., covarianc
147
129
vars = inputvars (vars )
148
130
149
131
if out_vars is None :
132
+ if not any (likelihood_name == RV .name for RV in model .unobserved_RVs ):
133
+ with model :
134
+ llk = pm .Deterministic (likelihood_name , model .logpt )
135
+ else :
136
+ raise ValueError (
137
+ 'The model likelihood name is already being used by a RV!' )
138
+
150
139
out_vars = model .unobserved_RVs
151
140
152
141
out_varnames = [out_var .name for out_var in out_vars ]
@@ -419,9 +408,9 @@ def resample(self):
419
408
return outindx
420
409
421
410
422
- def ATMIP_sample (n_steps , step = None , start = None , homepath = None , chain = 0 , stage = 0 , n_jobs = 1 ,
411
+ def sample_smc (n_steps , step = None , start = None , homepath = None , chain = 0 , stage = 0 , n_jobs = 1 ,
423
412
tune = None , progressbar = False , model = None , random_seed = - 1 , rm_flag = False ):
424
- """(C)ATMIP sampling algorithm (Cascading - (C) not always relevant)
413
+ """Sequential Monte Carlo sampling
425
414
426
415
Samples the solution space with n_chains of Metropolis chains, where each
427
416
chain has n_steps iterations. Once finished, the sampled traces are
@@ -524,25 +513,8 @@ def ATMIP_sample(n_steps, step=None, start=None, homepath=None, chain=0, stage=0
524
513
draws = step .n_steps
525
514
526
515
stage_handler .clean_directory (stage , None , rm_flag )
527
- with model :
528
- chains = stage_handler .recover_existing_results (stage , draws , step , n_jobs )
529
- if chains is not None :
530
- rest = len (chains ) % n_jobs
531
- if rest > 0 :
532
- pm ._log .info ('Fixing %i chains ...' % rest )
533
- chains , rest_chains = chains [:- rest ], chains [- rest :]
534
- # process traces that are not a multiple of n_jobs
535
- sample_args = {
536
- 'draws' : draws ,
537
- 'step' : step ,
538
- 'stage_path' : stage_handler .stage_path (stage ),
539
- 'progressbar' : progressbar ,
540
- 'model' : model ,
541
- 'n_jobs' : rest ,
542
- 'chains' : rest_chains }
543
516
544
- _iter_parallel_chains (** sample_args )
545
- pm ._log .info ('Back to normal!' )
517
+ chains = stage_handler .recover_existing_results (stage , draws , step , n_jobs )
546
518
547
519
with model :
548
520
while step .beta < 1 :
@@ -556,7 +528,7 @@ def ATMIP_sample(n_steps, step=None, start=None, homepath=None, chain=0, stage=0
556
528
pm ._log .info ('Beta: %f Stage: %i' % (step .beta , step .stage ))
557
529
558
530
# Metropolis sampling intermediate stages
559
- chains = stage_handler .clean_directory (stage , chains , rm_flag )
531
+ chains = stage_handler .clean_directory (step . stage , chains , rm_flag )
560
532
sample_args = {
561
533
'draws' : draws ,
562
534
'step' : step ,
0 commit comments