Skip to content

Fix axis handling of randommethod in GRW #3985

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 15 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions pymc3/distributions/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ def draw_values(params, point=None, size=None):
# draw_values in the context of sample_posterior_predictive
ppc_sampler = vectorized_ppc.get(None)
if ppc_sampler is not None:

# this is being done inside new, vectorized sample_posterior_predictive
return ppc_sampler(params, trace=point, samples=size)

Expand Down Expand Up @@ -592,7 +593,6 @@ def draw_values(params, point=None, size=None):
else:
# param still needs to be drawn
symbolic_params.append((i, p))

if not symbolic_params:
# We only need to enforce the correct order if there are symbolic
# params that could be drawn in variable order
Expand Down Expand Up @@ -818,6 +818,7 @@ def _draw_value(param, point=None, givens=None, size=None):
if point and hasattr(param, 'model') and param.name in point:
return point[param.name]
elif hasattr(param, 'random') and param.random is not None:
print(point)
return param.random(point=point, size=size)
elif (hasattr(param, 'distribution') and
hasattr(param.distribution, 'random') and
Expand Down Expand Up @@ -995,7 +996,7 @@ def generate_samples(generator, *args, **kwargs):
else:
samples = generator(size=size_tup + dist_bcast_shape, *args, **kwargs)
samples = np.asarray(samples)

# reshape samples here
if samples.ndim > 0 and samples.shape[0] == 1 and size_tup == (1,):
if (len(samples.shape) > len(dist_shape) and
Expand All @@ -1009,4 +1010,5 @@ def generate_samples(generator, *args, **kwargs):
size_tup == (1,))
):
samples = samples.reshape(samples.shape[:-1])
print("samples:",samples)
return np.asarray(samples)
12 changes: 10 additions & 2 deletions pymc3/distributions/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from scipy import stats
import theano.tensor as tt
from theano import scan
import numpy as np

from pymc3.util import get_variable_name
from .continuous import get_tau_sigma, Normal, Flat
Expand Down Expand Up @@ -293,6 +294,7 @@ def random(self, point=None, size=None):
sigma, mu = distribution.draw_values(
[self.sigma, self.mu], point=point, size=size
)

return distribution.generate_samples(
self._random,
sigma=sigma,
Expand All @@ -309,8 +311,14 @@ def _random(self, sigma, mu, size, sample_shape):
else:
axis = 0
rv = stats.norm(mu, sigma)
data = rv.rvs(size).cumsum(axis=axis)
data = data - data[0] # TODO: this should be a draw from `init`, if available
if len(size) == 1:
data = rv.rvs(size).cumsum(axis=axis)
data = data - data[0] # TODO: this should be a draw from `init`, if available
else:
data = np.empty(size)
for i in range(size[0]):
data[i]=rv.rvs((size[1],)).cumsum(axis=axis)
data[i]=data[i] - data[i][0]
return data

def _repr_latex_(self, name=None, dist=None):
Expand Down
5 changes: 4 additions & 1 deletion pymc3/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1825,6 +1825,7 @@ def sample_prior_predictive(
samples.
"""
model = modelcontext(model)


if vars is None and var_names is None:
prior_pred_vars = model.observed_RVs
Expand All @@ -1842,15 +1843,17 @@ def sample_prior_predictive(
else:
raise ValueError("Cannot supply both vars and var_names arguments.")
vars = cast(TIterable[str], vars) # tell mypy that vars cannot be None here.


if random_seed is not None:
np.random.seed(random_seed)
names = get_default_varnames(vars_, include_transformed=False)
# draw_values fails with auto-transformed variables. transform them later!
values = draw_values([model[name] for name in names], size=samples)


data = {k: v for k, v in zip(names, values)}
if data is None:
if data is None:
raise AssertionError("No variables sampled: attempting to sample %s" % names)

prior = {} # type: Dict[str, np.ndarray]
Expand Down