8000 Fixing some issues when we have a vector-valued time-varying parameter in the state-space class + started working on multivariate time series in the structural class. by sangrey · Pull Request #490 · pymc-devs/pymc-extras · GitHub
[go: up one dir, main page]

Skip to content

Fixing some issues when we have a vector-valued time-varying parameter in the state-space class + started working on multivariate time series in the structural class. #490

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
Prev Previous commit
Fixed a few other minor syntax errors.
  • Loading branch information
sangrey committed May 30, 2025
commit 93b244c553dae38e8894e9c549c9c807a1331144
42 changes: 19 additions & 23 deletions pymc_extras/statespace/models/structural.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
ALL_STATE_DIM,
AR_PARAM_DIM,
LONG_MATRIX_NAMES,
OBS_STATE_AUX_DIM,
OBS_STATE_DIM,
POSITION_DERIVATIVE_NAMES,
TIME_DIM,
)
Expand Down Expand Up @@ -353,7 +355,7 @@ def __init__(
k_endog,
k_states,
k_posdef,
endog_names=None:
endog_names=None,
state_names=None,
data_names=None,
shock_names=None,
Expand Down Expand Up @@ -601,7 +603,6 @@ def _make_combined_name(self):
return name

def __add__(self, other):

if self.endog_names != other.endog_names:
raise ValueError("The endog names must be the same.")

Expand Down Expand Up @@ -859,7 +860,7 @@ def __init__(
measurement_error=False,
combine_hidden_states=False,
obs_state_idxs=np.array([1.0] + [0.0] * (k_states - 1)),
**kwargs
**kwargs,
)

@property
Expand All @@ -870,7 +871,6 @@ def observed_states(self):
def k_endog(self):
return len(self.endog_names)


def populate_component_properties(self):
name_slice = POSITION_DERIVATIVE_NAMES[: self.k_states]
self.param_names = ["initial_trend"]
Expand Down Expand Up @@ -901,7 +901,9 @@ def make_symbolic_graph(self) -> None:
R = R[:, self.innovations_order]
self.ssm["selection", :, :] = R

self.ssm["design", :, :] = np.tile(np.array([1.0] + [0.0] * (self.k_states - 1)), (self.k_endog, 1))
self.ssm["design", :, :] = np.tile(
np.array([1.0] + [0.0] * (self.k_states - 1)), (self.k_endog, 1)
)

if self.k_posdef > 0:
sigma_trend = self.make_and_register_variable("sigma_trend", shape=(self.k_posdef,))
Expand Down Expand Up @@ -949,36 +951,36 @@ class MeasurementError(Component):
"""

def __init__(self, endog_names, time_dim, name: str = "MeasurementError", **kwargs):

k_states = 0
k_posdef = 0
self._time_dim = time_dim

super().__init__(
name,
endog_names=endog_names,
k_endog=len(endog_names),
k_states,
k_posdef,
endog_names=endog_names,
k_endog=len(endog_names),
measurement_error=True,
combine_hidden_states=False,
**kwargs,
)


def populate_component_properties(self):
self.param_info = {
f"{self.name}_covariance": {
"shape": (self.k_endog, self.k_endog),
"constraints": "Positive semi-definite",
"dims": ( OBS_STATE_DIM, OBS_STATE_AUX_DIM,),
"dims": (
OBS_STATE_DIM,
OBS_STATE_AUX_DIM,
),
}
}
self.param_names = list(self.param_info.keys())
self.param_dims = {name: val["dims"] for name, val in self.param_info.items()}

def make_symbolic_graph(self) -> None:

sigma_shape = (self.k_endog, self.k_endog)
obs_covariance = self.make_and_register_variable(
f"{self.name}_covariance", shape=sigma_shape
Expand Down Expand Up @@ -1043,7 +1045,7 @@ class AutoregressiveComponent(Component):

"""

def __init__( self, endog_names, order: int = 1, name: str = "AutoRegressive"):
def __init__(self, endog_names, order: int = 1, name: str = "AutoRegressive"):
order = order_to_mask(order)
ar_lags = np.flatnonzero(order).ravel().astype(int) + 1
k_states = len(order)
Expand Down Expand Up @@ -1351,9 +1353,7 @@ class FrequencySeasonality(Component):
to isolate and identify a "Monday" effect, for instance.
"""

def __init__(
self, endog_names, season_length, n=None, name=None, innovations=True, **kwargs
):
def __init__(self, endog_names, season_length, n=None, name=None, innovations=True, **kwargs):
if n is None:
n = int(season_length // 2)
if name is None:
Expand Down Expand Up @@ -1382,7 +1382,7 @@ def __init__(
measurement_error=False,
combine_hidden_states=True,
obs_state_idxs=obs_state_idx,
**kwargs
**kwargs,
)

def make_symbolic_graph(self) -> None:
Expand Down Expand Up @@ -1718,11 +1718,9 @@ def populate_component_properties(self) -> None:
"dims": ("exog_state",),
}

class ObsIntercept(Component):


class ObsIntercept(Component):
def __init__(self, endog_names, time_dim, name: str = "y"):

self._time_dim = time_dim
super().__init__(
name=name,
Expand All @@ -1735,9 +1733,8 @@ def __init__(self, endog_names, time_dim, name: str = "y"):
)

def populate_component_properties(self):

self.param_info = {
f"mu": {
"mu": {
"shape": (
# TODO This should not be necessary.
self._time_dim,
Expand All @@ -1754,9 +1751,8 @@ def populate_component_properties(self):
self.param_names = list(self.param_info.keys())

def make_symbolic_graph(self) -> None:

mu = self.make_and_register_variable(
f"mu",
"mu",
shape=(
self._time_dim,
self.k_endog,
Expand Down
Loading
0