2525from ..theanof import inputvars , make_shared_replacements , join_nonshared_inputs
2626import numpy .random as nr
2727
28+ from .metropolis import MultivariateNormalProposal
2829from .arraystep import metrop_select
2930from ..backends import smc_text as atext
3031
31- __all__ = ['SMC' , 'ATMIP_sample ' ]
32+ __all__ = ['SMC' , 'sample_smc ' ]
3233
3334EXPERIMENTAL_WARNING = "Warning: SMC is an experimental step method, and not yet" \
3435 " recommended for use in PyMC3!"
3536
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-
5537proposal_dists = {
5638 'MultivariateNormal' : MultivariateNormalProposal ,
5739 }
@@ -147,6 +129,13 @@ def __init__(self, vars=None, out_vars=None, n_chains=100, scaling=1., covarianc
147129 vars = inputvars (vars )
148130
149131 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+
150139 out_vars = model .unobserved_RVs
151140
152141 out_varnames = [out_var .name for out_var in out_vars ]
@@ -419,9 +408,9 @@ def resample(self):
419408 return outindx
420409
421410
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 ,
423412 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
425414
426415 Samples the solution space with n_chains of Metropolis chains, where each
427416 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
524513 draws = step .n_steps
525514
526515 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 }
543516
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 )
546518
547519 with model :
548520 while step .beta < 1 :
@@ -556,7 +528,7 @@ def ATMIP_sample(n_steps, step=None, start=None, homepath=None, chain=0, stage=0
556528 pm ._log .info ('Beta: %f Stage: %i' % (step .beta , step .stage ))
557529
558530 # 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 )
560532 sample_args = {
561533 'draws' : draws ,
562534 'step' : step ,
0 commit comments