Skip to content

minor correction in sampling.py and starting.py #4458

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 8 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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
3 changes: 2 additions & 1 deletion pymc3/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import warnings

from collections import defaultdict
from copy import copy
from copy import copy, deepcopy
from typing import Any, Dict, Iterable, List, Optional, Set, Union, cast

import arviz
Expand Down Expand Up @@ -423,6 +423,7 @@ def sample(
p 0.609 0.047 0.528 0.699
"""
model = modelcontext(model)
start = deepcopy(start)
if start is None:
check_start_vals(model.test_point, model)
else:
Expand Down
21 changes: 21 additions & 0 deletions pymc3/tests/test_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,27 @@ def callback(trace, draw):
assert len(trace) == trace_cancel_length


def test_sample_find_MAP_does_not_modify_start():
# see https://github.com/pymc-devs/pymc3/pull/4458
with pm.Model():
pm.Lognormal("untransformed")

# make sure find_Map does not modify the start dict
start = {"untransformed": 2}
pm.find_MAP(start=start)
assert start == {"untransformed": 2}

# make sure sample does not modify the start dict
start = {"untransformed": 0.2}
pm.sample(draws=10, step=pm.Metropolis(), tune=5, start=start, chains=3)
assert start == {"untransformed": 0.2}

# make sure sample does not modify the start when passes as list of dict
start = [{"untransformed": 2}, {"untransformed": 0.2}]
pm.sample(draws=10, step=pm.Metropolis(), tune=5, start=start, chains=2)
assert start == [{"untransformed": 2}, {"untransformed": 0.2}]


def test_empty_model():
with pm.Model():
pm.Normal("a", observed=1)
Expand Down
4 changes: 3 additions & 1 deletion pymc3/tuning/starting.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

@author: johnsalvatier
"""
import copy

import numpy as np
import theano.gradient as tg

Expand Down Expand Up @@ -96,7 +98,7 @@ def find_MAP(
vars = inputvars(vars)
disc_vars = list(typefilter(vars, discrete_types))
allinmodel(vars, model)

start = copy.deepcopy(start)
if start is None:
start = model.test_point
else:
Expand Down