Skip to content

More bugfixes for statespace #346

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 13 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
200 changes: 63 additions & 137 deletions notebooks/Making a Custom Statespace Model.ipynb

Large diffs are not rendered by default.

1,175 changes: 603 additions & 572 deletions notebooks/SARMA Example.ipynb

Large diffs are not rendered by default.

749 changes: 344 additions & 405 deletions notebooks/Structural Timeseries Modeling.ipynb

Large diffs are not rendered by default.

633 changes: 345 additions & 288 deletions notebooks/VARMAX Example.ipynb

Large diffs are not rendered by default.

33 changes: 27 additions & 6 deletions pymc_experimental/statespace/core/representation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
from typing import Optional, Type, Union

import numpy as np
Expand All @@ -10,7 +11,7 @@
)

floatX = pytensor.config.floatX
KeyLike = Union[tuple[Union[str, int]], str]
KeyLike = Union[tuple[str | int, ...], str]


class PytensorRepresentation:
Expand Down Expand Up @@ -152,6 +153,22 @@ class PytensorRepresentation:
http://www.chadfulton.com/files/fulton_statsmodels_2017_v1.pdf
"""

__slots__ = (
"k_endog",
"k_states",
"k_posdef",
"shapes",
"design",
"obs_intercept",
"obs_cov",
"transition",
"state_intercept",
"selection",
"state_cov",
"initial_state",
"initial_state_cov",
)

def __init__(
self,
k_endog: int,
Expand Down Expand Up @@ -206,16 +223,17 @@ def _validate_key(self, key: KeyLike) -> None:
if key not in self.shapes:
raise IndexError(f"{key} is an invalid state space matrix name")

def _update_shape(self, key: KeyLike, value: Union[np.ndarray, pt.TensorType]) -> None:
def _update_shape(self, key: KeyLike, value: Union[np.ndarray, pt.Variable]) -> None:
if isinstance(value, (pt.TensorConstant, pt.TensorVariable)):
shape = value.type.shape
else:
shape = value.shape

old_shape = self.shapes[key]
if not all([a == b for a, b in zip(shape[1:], old_shape[1:])]):
ndim_core = 1 if key in VECTOR_VALUED else 2
if not all([a == b for a, b in zip(shape[-ndim_core:], old_shape[-ndim_core:])]):
raise ValueError(
f"The last two dimensions of {key} must be {old_shape[1:]}, found {shape[1:]}"
f"The last two dimensions of {key} must be {old_shape[-ndim_core:]}, found {shape[-ndim_core:]}"
)

# Add time dimension dummy if none present
Expand All @@ -229,7 +247,7 @@ def _update_shape(self, key: KeyLike, value: Union[np.ndarray, pt.TensorType]) -

def _add_time_dim_to_slice(
self, name: str, slice_: Union[list[int], tuple[int]], n_dim: int
) -> tuple[int]:
) -> tuple[int | slice, ...]:
# Case 1: There is never a time dim. No changes needed.
if name in NEVER_TIME_VARYING:
return slice_
Expand Down Expand Up @@ -389,7 +407,7 @@ def __getitem__(self, key: KeyLike) -> pt.TensorVariable:
else:
raise IndexError("First index must the name of a valid state space matrix.")

def __setitem__(self, key: KeyLike, value: Union[float, int, np.ndarray]) -> None:
def __setitem__(self, key: KeyLike, value: Union[float, int, np.ndarray, pt.Variable]) -> None:
_type = type(key)

# Case 1: key is a string: we are setting an entire matrix.
Expand All @@ -416,3 +434,6 @@ def __setitem__(self, key: KeyLike, value: Union[float, int, np.ndarray]) -> Non
matrix.name = name

setattr(self, name, matrix)

def copy(self):
return copy.copy(self)
Loading
Loading