Skip to content

Fix docstring and apply black formatter to mlda #4162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 22 additions & 29 deletions pymc3/step_methods/mlda.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,21 @@ class MLDA(ArrayStepShared):
... datum = 1
...
... with pm.Model() as coarse_model:
... x = Normal("x", mu=0, sigma=10)
... y = Normal("y", mu=x, sigma=1, observed=datum - 0.1)
... x = pm.Normal("x", mu=0, sigma=10)
... y = pm.Normal("y", mu=x, sigma=1, observed=datum - 0.1)
...
... with pm.Model():
... x = Normal("x", mu=0, sigma=10)
... y = Normal("y", mu=x, sigma=1, observed=datum)
... step_method = pm.MLDA(coarse_models=[coarse_model]
... x = pm.Normal("x", mu=0, sigma=10)
... y = pm.Normal("y", mu=x, sigma=1, observed=datum)
... step_method = pm.MLDA(coarse_models=[coarse_model],
... subsampling_rates=5)
... trace = pm.sample(ndraws=500, chains=2,
... trace = pm.sample(500, chains=2,
... tune=100, step=step_method,
... random_seed=123)
...
... pm.summary(trace)
mean sd hpd_3% hpd_97%
x 1.011 0.975 -0.925 2.824
... pm.summary(trace, kind="stats")
mean sd hdi_3% hdi_97%
x 0.99 0.987 -0.734 2.992

References
----------
Expand Down Expand Up @@ -161,7 +161,7 @@ def __init__(
mode: Optional = None,
subsampling_rates: List[int] = 5,
base_blocked: bool = False,
**kwargs
**kwargs,
) -> None:

warnings.warn(
Expand All @@ -174,9 +174,7 @@ def __init__(
# assign internal state
self.coarse_models = coarse_models
if not isinstance(coarse_models, list):
raise ValueError(
"MLDA step method cannot use coarse_models if it is not a list"
)
raise ValueError("MLDA step method cannot use coarse_models if it is not a list")
if len(self.coarse_models) == 0:
raise ValueError(
"MLDA step method was given an empty "
Expand Down Expand Up @@ -233,9 +231,7 @@ def __init__(
if self.num_levels == 2:
with self.next_model:
# make sure the correct variables are selected from next_model
vars_next = [
var for var in self.next_model.vars if var.name in self.var_names
]
vars_next = [var for var in self.next_model.vars if var.name in self.var_names]
# MetropolisMLDA sampler in base level (level=0), targeting self.next_model
self.next_step_method = pm.MetropolisMLDA(
vars=vars_next,
Expand All @@ -253,9 +249,7 @@ def __init__(
next_subsampling_rates = self.subsampling_rates[:-1]
with self.next_model:
# make sure the correct variables are selected from next_model
vars_next = [
var for var in self.next_model.vars if var.name in self.var_names
]
vars_next = [var for var in self.next_model.vars if var.name in self.var_names]
# MLDA sampler in some intermediate level, targeting self.next_model
self.next_step_method = pm.MLDA(
vars=vars_next,
Expand Down Expand Up @@ -335,9 +329,7 @@ def astep(self, q0):
self.base_scaling_stats = {"base_scaling": np.array(scaling_list)}
elif not isinstance(self.next_step_method, MLDA):
# next method is any block sampler
self.base_scaling_stats = {
"base_scaling": np.array(self.next_step_method.scaling)
}
self.base_scaling_stats = {"base_scaling": np.array(self.next_step_method.scaling)}
else:
# next method is MLDA - propagate dict from lower levels
self.base_scaling_stats = self.next_step_method.base_scaling_stats
Expand Down Expand Up @@ -366,19 +358,20 @@ class RecursiveDAProposal(Proposal):
each of which is used to propose samples to the chain above.
"""

def __init__(self,
next_step_method: Union[MLDA, Metropolis, CompoundStep],
next_model: Model,
tune: bool,
subsampling_rate: int) -> None:
def __init__(
self,
next_step_method: Union[MLDA, Metropolis, CompoundStep],
next_model: Model,
tune: bool,
subsampling_rate: int,
) -> None:

self.next_step_method = next_step_method
self.next_model = next_model
self.tune = tune
self.subsampling_rate = subsampling_rate

def __call__(self,
q0_dict: dict) -> dict:
def __call__(self, q0_dict: dict) -> dict:
"""Returns proposed sample given the current sample
in dictionary form (q0_dict).
"""
Expand Down