Skip to content

Commit 2f71b49

Browse files
committed
Update time series docs
1 parent 69e967e commit 2f71b49

File tree

2 files changed

+24
-42
lines changed

2 files changed

+24
-42
lines changed

pymc/distributions/timeseries.py

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ def rng_fn(
5656
init: float,
5757
size: int,
5858
) -> np.ndarray:
59-
"""Gaussian Random Walk randon function
59+
"""Gaussian Random Walk generator.
60+
61+
The init value is drawn from the Normal distribution with the same sigma as the
62+
innovations.
6063
6164
Parameters
6265
----------
@@ -79,7 +82,7 @@ def rng_fn(
7982
-------
8083
np.ndarray
8184
"""
82-
return init + np.cumsum(rng.normal(loc=mu, scale=sigma, size=size))
85+
return np.cumsum([rng.normal(init, sigma), rng.normal(loc=mu, scale=sigma, size=size)])
8386

8487

8588
gaussianrandomwalk = GaussianRandomWalkRV()
@@ -231,55 +234,34 @@ def logp(self, value):
231234
class GaussianRandomWalk(distribution.Continuous):
232235
r"""Random Walk with Normal innovations
233236
234-
Note that this is mainly a user-friendly wrapper to enable an easier specification
235-
of GRW. You are not restricted to use only Normal innovations but can use any
236-
distribution: just use `aesara.tensor.cumsum()` to create the random walk behavior.
237+
238+
Note init is currently drawn from a normal distribution with the same sigma as the innovations
237239
238240
Parameters
239241
----------
240242
mu: tensor
241243
innovation drift, defaults to 0.0
242-
For vector valued mu, first dimension must match shape of the random walk, and
243-
the first element will be discarded (since there is no innovation in the first timestep)
244244
sigma: tensor
245-
sigma > 0, innovation standard deviation (only required if tau is not specified)
246-
For vector valued sigma, first dimension must match shape of the random walk, and
247-
the first element will be discarded (since there is no innovation in the first timestep)
248-
tau: tensor
249-
tau > 0, innovation precision (only required if sigma is not specified)
250-
For vector valued tau, first dimension must match shape of the random walk, and
251-
the first element will be discarded (since there is no innovation in the first timestep)
252-
init: distribution
253-
distribution for initial value (Defaults to Flat())
245+
sigma > 0, innovation standard deviation, defaults to 0.0
246+
init: float
247+
Mean value of initialization, defaults to 0.0
254248
"""
255249

256250
rv_op = gaussianrandomwalk
257251

258252
@classmethod
259253
def dist(
260254
cls,
261-
mu: Optional[Union[np.ndarray, float]] = None,
262-
sigma: Optional[Union[np.ndarray, float]] = None,
263-
init: float = None,
255+
mu: Optional[Union[np.ndarray, float]] = 0.0,
256+
sigma: Optional[Union[np.ndarray, float]] = 1.0,
257+
init: float = 0.0,
264258
size: int = None,
265259
*args,
266260
**kwargs
267261
) -> RandomVariable:
268262

269-
# Still working on this. The RV op is my current blocker
270263
return super().dist([mu, sigma, init], **kwargs)
271264

272-
# TODO: Add typehints
273-
# def get_moment(
274-
# self,
275-
# size: Optional[Union[np.ndarray, float]],
276-
# mu: Optional[Union[np.ndarray, float]],
277-
# sigma: Optional[Union[np.ndarray, float]],
278-
# init: Optional[Union[np.ndarray, float]],
279-
# ):
280-
# moment = mu * size + init
281-
# return moment
282-
283265
def logp(
284266
value: at.Variable,
285267
mu: at.Variable,
@@ -289,18 +271,19 @@ def logp(
289271
"""
290272
Calculate log-probability of Gaussian Random Walk distribution at specified value.
291273
292-
293-
294274
Parameters
295275
----------
296-
x: numeric
297-
Value for which log-probability is calculated.
276+
value: at.Variable,
277+
mu: at.Variable,
278+
sigma: at.Variable,
279+
init: at.Variable,
298280
299281
Returns
300282
-------
301283
TensorVariable
302284
"""
303285

286+
# TODO: Remove this and use pm.Normal.logp
304287
def normal_logp(value, mu, sigma):
305288
logp = (
306289
-0.5 * at.pow((value - mu) / sigma, 2)

pymc/tests/test_distributions_timeseries.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import numpy as np
1616
import pytest
1717

18+
from scipy import stats
19+
1820
from pymc.aesaraf import floatX
1921
from pymc.distributions.continuous import Flat, Normal
2022
from pymc.distributions.timeseries import (
@@ -46,30 +48,27 @@ def test_grw_rv_op():
4648
def test_grw_log():
4749
vals = [0, 1, 2]
4850
mu = 1
49-
sd = 1
51+
sigma = 1
5052
init = 0
5153

5254
import pymc as pm
5355

5456
from pymc.distributions.timeseries import GaussianRandomWalk
5557

5658
with pm.Model():
57-
grw = GaussianRandomWalk("grw", mu, sd, init, size=2)
59+
grw = GaussianRandomWalk("grw", mu, sigma, init, size=2)
5860

5961
logp = pm.logp(grw, vals)
6062

6163
logp_vals = logp.eval()
6264

63-
# Calculate logp from scipy
64-
from scipy import stats
65-
6665
# Calculate logp in explicit loop for testing obviousness
6766
init_val = vals[0]
68-
init_logp = stats.norm(0, 1).logpdf(init_val)
67+
init_logp = stats.norm(0, sigma).logpdf(init_val)
6968

7069
logp_reference = [init_logp]
7170
for x_minus_one_val, x_val in zip(vals, vals[1:]):
72-
logp_point = stats.norm(x_minus_one_val + mu, sd).logpdf(x_val)
71+
logp_point = stats.norm(x_minus_one_val + mu, sigma).logpdf(x_val)
7372
logp_reference.append(logp_point)
7473

7574
np.testing.assert_almost_equal(logp_vals, logp_reference)

0 commit comments

Comments
 (0)