Skip to content
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
2 changes: 1 addition & 1 deletion pymc3/distributions/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def logp(self, x):
boundary = Normal.dist(0., tau=tau_e).logp

innov_like = Normal.dist(k * x_im1, tau=tau_e).logp(x_i)
return boundary(x[0]) + tt.sum(innov_like) + boundary(x[-1])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, this looks like a bug to me.

return boundary(x[0]) + tt.sum(innov_like)

def _repr_latex_(self, name=None, dist=None):
if dist is None:
Expand Down
11 changes: 10 additions & 1 deletion pymc3/tests/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
NegativeBinomial, Geometric, Exponential, ExGaussian, Normal,
Flat, LKJCorr, Wald, ChiSquared, HalfNormal, DiscreteUniform,
Bound, Uniform, Triangular, Binomial, SkewNormal, DiscreteWeibull, Gumbel,
Interpolated, ZeroInflatedBinomial)
Interpolated, ZeroInflatedBinomial, AR1)
from ..distributions import continuous
from pymc3.theanof import floatX
from numpy import array, inf, log, exp
Expand Down Expand Up @@ -284,6 +284,10 @@ def mvt_logpdf(value, nu, Sigma, mu=0):
return log_pdf


def AR1_logpdf(value, k, tau_e):
return (sp.norm(loc=0,scale=1/np.sqrt(tau_e)).logpdf(value[0]) +

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

pep8 wants spaces around math operators.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

But yeah, this test is a bit odd as it basically repcliates the likelihood. statsmodels is an idea but we don't want that as a dependency, but could be optional. @junpenglao?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I dont think we should use statsmodels in the test. I am thinking more rewriting our AR1 logp similar to the (exact) unconditional maximum likelihood as in statsmodels. We should take this opportunity to extend our AR1 model into AR(n).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

  • Sorry about the test. I didn't know the best way to write one without introducing additional dependencies.
  • As it written now, the AR(1) likelihood calculation is conditional on $x_0 = 0$, which seems a bit arbitrary to me. It seems more natural to conditional on the initial observation.
  • You could use the unconditional distribution to calculate the likelihood of the initial observation (as in stats models), but this only makes sense if the process is stationary ($ |k| < 1 $). Maybe something like init in GaussianRandomWalk?
  • I am trying to write a more general AR RV, but I'm getting a bit tripped up by creating the matrix of lagged observations in theano.

sp.norm(loc=k*value[:-1],scale=1/np.sqrt(tau_e)).logpdf(value[1:]).sum())

class Simplex(object):
def __init__(self, n):
self.vals = list(simplex_values(n))
Expand Down Expand Up @@ -676,6 +680,11 @@ def test_mvt(self, n):
{'nu': Rplus, 'Sigma': PdMatrix(n), 'mu': Vector(R, n)},
mvt_logpdf)

@pytest.mark.parametrize('n',[2,3,4])
def test_AR1(self, n):
self.pymc3_matches_scipy(AR1, Vector(R, n), {'k': Unit, 'tau_e': Rplus}, AR1_logpdf)


@pytest.mark.parametrize('n', [2, 3])
def test_wishart(self, n):
# This check compares the autodiff gradient to the numdiff gradient.
Expand Down