Skip to content
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

Fix axis handling of randommethod in GRW #3985

Merged
merged 15 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from 13 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
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Pass the `tune` argument from `sample` when using `advi+adapt_diag_grad` (see issue [#3965](https://github.com/pymc-devs/pymc3/issues/3965), fixed by [#3979](https://github.com/pymc-devs/pymc3/pull/3979)).
- Add simple test case for new coords and dims feature in `pm.Model` (see [#3977](https://github.com/pymc-devs/pymc3/pull/3977)).
- Require ArviZ >= 0.9.0 (see [#3977](https://github.com/pymc-devs/pymc3/pull/3977)).
- Temporarily fixed issue [#3962](https://github.com/pymc-devs/pymc3/issues/3962) by making change in the `_random()` method of `GaussianRandomWalk` class, refer to PR [#3985].Further testing revealed a new issue which is being tracked [#4010](https://github.com/pymc-devs/pymc3/issues/4010)
Copy link
Member

Choose a reason for hiding this comment

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

I think that particular issue of sampling is fixed for good, no? It just revealed another problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, When some edge cases were tested it turned out that a deeper issue exists in the function logp. This PR is only a temporary fix which would be resolved permanently with the issue #4010 fixed

Copy link
Member

Choose a reason for hiding this comment

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

The way I understand, there are two related but separate issues: 1D sampling from the GRW did not work (#3962), and the logp for >1D GRWs is wrong (#4010). This PR fixes the first completely but doesn't address the second.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah that's my understanding too. I think the notion of temporary fix should be removed from the release notes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok then I shall make the corresponding changes in Release notes. However, I am keeping the referene to 4010


_NB: The `docs/*` folder is still removed from the tarball due to an upload size limit on PyPi._

Expand Down
1 change: 0 additions & 1 deletion docs/release-notes/pymc3-3.0.md
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@

4 changes: 2 additions & 2 deletions pymc3/distributions/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ def draw_values(params, point=None, size=None):
else:
# param still needs to be drawn
symbolic_params.append((i, p))

Rish001 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -995,7 +995,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 Down
16 changes: 13 additions & 3 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 @@ -303,14 +304,23 @@ def random(self, point=None, size=None):
)

def _random(self, sigma, mu, size, sample_shape):
"""Implement a Gaussian random walk as a cumulative sum of normals."""
"""Implement a Gaussian random walk as a cumulative sum of normals.
axis = len(size) - 1 denotes the axis along which cumulative sum would be calculated.
This might need to be corrected in future when issue #4010 is fixed.
Lines 318-322 ties the starting point of each instance of random walk to 0"
"""
if size[len(sample_shape)] == sample_shape:
axis = len(sample_shape)
else:
axis = 0
axis = len(size) - 1
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
data = np.array(data)
if len(data.shape)>1:
for i in range(data.shape[0]):
data[i] = data[i] - data[i][0]
Rish001 marked this conversation as resolved.
Show resolved Hide resolved
else:
data = data - data[0]
return data

def _repr_latex_(self, name=None, dist=None):
Expand Down