Skip to content

Fix _check_start_shape and BinaryMetropolis.astep #4698

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 3 commits into from
May 15, 2021
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions pymc3/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,11 @@ def sample(
def _check_start_shape(model, start):
if not isinstance(start, dict):
raise TypeError("start argument must be a dict or an array-like of dicts")

# Filter "non-input" variables
initial_point = model.initial_point
start = {k: v for k, v in start.items() if k in initial_point}

e = ""
for var in model.basic_RVs:
var_shape = model.fastfn(var.shape)(start)
Expand Down
4 changes: 3 additions & 1 deletion pymc3/step_methods/metropolis.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ def __init__(self, vars, scaling=1.0, tune=True, tune_interval=100, model=None):

def astep(self, q0: RaveledVars, logp) -> Tuple[RaveledVars, List[Dict[str, Any]]]:

logp_q0 = logp(q0)
point_map_info = q0.point_map_info
q0 = q0.data

Expand All @@ -340,8 +341,9 @@ def astep(self, q0: RaveledVars, logp) -> Tuple[RaveledVars, List[Dict[str, Any]
# Locations where switches occur, according to p_jump
switch_locs = rand_array < p_jump
q[switch_locs] = True - q[switch_locs]
logp_q = logp(RaveledVars(q, point_map_info))

accept = logp(q) - logp(q0)
accept = logp_q - logp_q0
q_new, accepted = metrop_select(accept, q, q0)
self.accepted += accepted

Expand Down
5 changes: 2 additions & 3 deletions pymc3/tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def build_disaster_model(masked=False):


@pytest.mark.xfail(
reason="_check_start_shape fails with start dictionary"
reason="Arviz summary fails"
# condition=(aesara.config.floatX == "float32"), reason="Fails on float32"
)
class TestDisasterModel(SeededTest):
Expand Down Expand Up @@ -222,7 +222,6 @@ def test_disaster_model_missing(self):
az.summary(tr)


@pytest.mark.xfail(reason="_check_start_shape fails with start dictionary")
class TestLatentOccupancy(SeededTest):
"""
From the PyMC example list
Expand Down Expand Up @@ -278,7 +277,7 @@ def test_run(self):
"theta": np.array(5, dtype="f"),
}
step_one = pm.Metropolis([model["theta_interval__"], model["psi_logodds__"]])
step_two = pm.BinaryMetropolis([model.z])
step_two = pm.BinaryMetropolis([model.rvs_to_values[model["z"]]])
Copy link
Member Author

@ricardoV94 ricardoV94 May 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to pass the value_var explicitly, or it works as well with the rv_var (i.e., model["z"])?

The test passes but I didn't have the patience to check if it affected sampling.

The ArrayStep docs are ambiguous in this regard:

    Blocked step method that is generalized to accept vectors of variables.

    Parameters
    ----------
    vars: list
        List of variables for sampler.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value variables are given to the step methods. See here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant when specifying manually the steps as in this test

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, step methods require value variables as their vars argument/field, so it looks like your manual setup for that step method in the test is correct.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that was exactly my hunch.

Should we add some logic where we check if a variable manually assigned to a stepper contains a value_var and if so use that instead?

That would make manual assignments less cumbersome and error prone. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only need to state what kind of variables they should be in a docstring. Aside from that, we could perhaps check that the vars are actually value variables in the step method's model.

pm.sample(50, step=[step_one, step_two], start=start, chains=1)


Expand Down