Skip to content

Commit 46a3498

Browse files
Add method to build_statespace_graph and include some general advice.
1 parent 122ab48 commit 46a3498

File tree

1 file changed

+66
-32
lines changed

1 file changed

+66
-32
lines changed

pymc_extras/statespace/core/statespace.py

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ def build_statespace_graph(
822822
mode: str | None = None,
823823
missing_fill_value: float | None = None,
824824
cov_jitter: float | None = JITTER_DEFAULT,
825+
mvn_method: Literal["cholesky", "eigh", "svd"] = "svd",
825826
save_kalman_filter_outputs_in_idata: bool = False,
826827
) -> None:
827828
"""
@@ -865,6 +866,14 @@ def build_statespace_graph(
865866
866867
- The Univariate Filter is more robust than other filters, and can tolerate a lower jitter value
867868
869+
mvn_method: str, default "svd"
870+
Method used to invert the covariance matrix when calculating the pdf of a multivariate normal
871+
(or when generating samples). One of "cholesky", "eigh", or "svd". "cholesky" is fastest, but least robust
872+
to ill-conditioned matrices, while "svd" is slow but extremely robust.
873+
874+
In general, if your model has measurement error, "cholesky" will be safe to use. Otherwise, "svd" is
875+
recommended. "eigh" can also be tried if sampling with "svd" is very slow, but it is not as robust as "svd".
876+
868877
save_kalman_filter_outputs_in_idata: bool, optional, default=False
869878
If True, Kalman Filter outputs will be saved in the model as deterministics. Useful for debugging, but
870879
should not be necessary for the majority of users.
@@ -915,6 +924,7 @@ def build_statespace_graph(
915924
logp=logp,
916925
observed=data,
917926
dims=obs_dims,
927+
method=mvn_method,
918928
)
919929

920930
self._fit_coords = pm_mod.coords.copy()
@@ -1131,10 +1141,13 @@ def _sample_conditional(
11311141
Observed data on which to condition the model. If not provided, the function will use the data that was
11321142
provided when the model was built.
11331143
1134-
mvn_method: str
1135-
Method used to compute draws from multivariate normal. One of "cholesky", "eigh", or "svd". "cholesky" is
1136-
fastest, but least robust to ill-conditioned matrices, while "svd" is slow but extremely robust. Default
1137-
is "svd".
1144+
mvn_method: str, default "svd"
1145+
Method used to invert the covariance matrix when calculating the pdf of a multivariate normal
1146+
(or when generating samples). One of "cholesky", "eigh", or "svd". "cholesky" is fastest, but least robust
1147+
to ill-conditioned matrices, while "svd" is slow but extremely robust.
1148+
1149+
In general, if your model has measurement error, "cholesky" will be safe to use. Otherwise, "svd" is
1150+
recommended. "eigh" can also be tried if sampling with "svd" is very slow, but it is not as robust as "svd".
11381151
11391152
kwargs:
11401153
Additional keyword arguments are passed to pymc.sample_posterior_predictive
@@ -1260,10 +1273,13 @@ def _sample_unconditional(
12601273
random_seed : int, RandomState or Generator, optional
12611274
Seed for the random number generator.
12621275
1263-
mvn_method: str
1264-
Method used to compute draws from multivariate normal. One of "cholesky", "eigh", or "svd". "cholesky" is
1265-
fastest, but least robust to ill-conditioned matrices, while "svd" is slow but extremely robust. Default
1266-
is "svd".
1276+
mvn_method: str, default "svd"
1277+
Method used to invert the covariance matrix when calculating the pdf of a multivariate normal
1278+
(or when generating samples). One of "cholesky", "eigh", or "svd". "cholesky" is fastest, but least robust
1279+
to ill-conditioned matrices, while "svd" is slow but extremely robust.
1280+
1281+
In general, if your model has measurement error, "cholesky" will be safe to use. Otherwise, "svd" is
1282+
recommended. "eigh" can also be tried if sampling with "svd" is very slow, but it is not as robust as "svd".
12671283
12681284
kwargs:
12691285
Additional keyword arguments are passed to pymc.sample_posterior_predictive
@@ -1366,10 +1382,13 @@ def sample_conditional_prior(
13661382
random_seed : int, RandomState or Generator, optional
13671383
Seed for the random number generator.
13681384
1369-
mvn_method: str
1370-
Method used to compute draws from multivariate normal. One of "cholesky", "eigh", or "svd". "cholesky" is
1371-
fastest, but least robust to ill-conditioned matrices, while "svd" is slow but extremely robust. Default
1372-
is "svd".
1385+
mvn_method: str, default "svd"
1386+
Method used to invert the covariance matrix when calculating the pdf of a multivariate normal
1387+
(or when generating samples). One of "cholesky", "eigh", or "svd". "cholesky" is fastest, but least robust
1388+
to ill-conditioned matrices, while "svd" is slow but extremely robust.
1389+
1390+
In general, if your model has measurement error, "cholesky" will be safe to use. Otherwise, "svd" is
1391+
recommended. "eigh" can also be tried if sampling with "svd" is very slow, but it is not as robust as "svd".
13731392
13741393
kwargs:
13751394
Additional keyword arguments are passed to pymc.sample_posterior_predictive
@@ -1406,10 +1425,13 @@ def sample_conditional_posterior(
14061425
random_seed : int, RandomState or Generator, optional
14071426
Seed for the random number generator.
14081427
1409-
mvn_method: str
1410-
Method used to compute draws from multivariate normal. One of "cholesky", "eigh", or "svd". "cholesky" is
1411-
fastest, but least robust to ill-conditioned matrices, while "svd" is slow but extremely robust. Default
1412-
is "svd".
1428+
mvn_method: str, default "svd"
1429+
Method used to invert the covariance matrix when calculating the pdf of a multivariate normal
1430+
(or when generating samples). One of "cholesky", "eigh", or "svd". "cholesky" is fastest, but least robust
1431+
to ill-conditioned matrices, while "svd" is slow but extremely robust.
1432+
1433+
In general, if your model has measurement error, "cholesky" will be safe to use. Otherwise, "svd" is
1434+
recommended. "eigh" can also be tried if sampling with "svd" is very slow, but it is not as robust as "svd".
14131435
14141436
kwargs:
14151437
Additional keyword arguments are passed to pymc.sample_posterior_predictive
@@ -1461,10 +1483,13 @@ def sample_unconditional_prior(
14611483
random_seed : int, RandomState or Generator, optional
14621484
Seed for the random number generator.
14631485
1464-
mvn_method: str
1465-
Method used to compute draws from multivariate normal. One of "cholesky", "eigh", or "svd". "cholesky" is
1466-
fastest, but least robust to ill-conditioned matrices, while "svd" is slow but extremely robust. Default
1467-
is "svd".
1486+
mvn_method: str, default "svd"
1487+
Method used to invert the covariance matrix when calculating the pdf of a multivariate normal
1488+
(or when generating samples). One of "cholesky", "eigh", or "svd". "cholesky" is fastest, but least robust
1489+
to ill-conditioned matrices, while "svd" is slow but extremely robust.
1490+
1491+
In general, if your model has measurement error, "cholesky" will be safe to use. Otherwise, "svd" is
1492+
recommended. "eigh" can also be tried if sampling with "svd" is very slow, but it is not as robust as "svd".
14681493
14691494
kwargs:
14701495
Additional keyword arguments are passed to pymc.sample_posterior_predictive
@@ -1527,10 +1552,13 @@ def sample_unconditional_posterior(
15271552
random_seed : int, RandomState or Generator, optional
15281553
Seed for the random number generator.
15291554
1530-
mvn_method: str
1531-
Method used to compute draws from multivariate normal. One of "cholesky", "eigh", or "svd". "cholesky" is
1532-
fastest, but least robust to ill-conditioned matrices, while "svd" is slow but extremely robust. Default
1533-
is "svd".
1555+
mvn_method: str, default "svd"
1556+
Method used to invert the covariance matrix when calculating the pdf of a multivariate normal
1557+
(or when generating samples). One of "cholesky", "eigh", or "svd". "cholesky" is fastest, but least robust
1558+
to ill-conditioned matrices, while "svd" is slow but extremely robust.
1559+
1560+
In general, if your model has measurement error, "cholesky" will be safe to use. Otherwise, "svd" is
1561+
recommended. "eigh" can also be tried if sampling with "svd" is very slow, but it is not as robust as "svd".
15341562
15351563
Returns
15361564
-------
@@ -2051,10 +2079,13 @@ def forecast(
20512079
verbose: bool, default=True
20522080
Whether to print diagnostic information about forecasting.
20532081
2054-
mvn_method: str
2055-
Method used to compute draws from multivariate normal. One of "cholesky", "eigh", or "svd". "cholesky" is
2056-
fastest, but least robust to ill-conditioned matrices, while "svd" is slow but extremely robust. Default
2057-
is "svd".
2082+
mvn_method: str, default "svd"
2083+
Method used to invert the covariance matrix when calculating the pdf of a multivariate normal
2084+
(or when generating samples). One of "cholesky", "eigh", or "svd". "cholesky" is fastest, but least robust
2085+
to ill-conditioned matrices, while "svd" is slow but extremely robust.
2086+
2087+
In general, if your model has measurement error, "cholesky" will be safe to use. Otherwise, "svd" is
2088+
recommended. "eigh" can also be tried if sampling with "svd" is very slow, but it is not as robust as "svd".
20582089
20592090
kwargs:
20602091
Additional keyword arguments are passed to pymc.sample_posterior_predictive
@@ -2246,10 +2277,13 @@ def impulse_response_function(
22462277
random_seed : int, RandomState or Generator, optional
22472278
Seed for the random number generator.
22482279
2249-
mvn_method: str
2250-
Method used to compute draws from multivariate normal. One of "cholesky", "eigh", or "svd". "cholesky" is
2251-
fastest, but least robust to ill-conditioned matrices, while "svd" is slow but extremely robust. Default
2252-
is "svd".
2280+
mvn_method: str, default "svd"
2281+
Method used to invert the covariance matrix when calculating the pdf of a multivariate normal
2282+
(or when generating samples). One of "cholesky", "eigh", or "svd". "cholesky" is fastest, but least robust
2283+
to ill-conditioned matrices, while "svd" is slow but extremely robust.
2284+
2285+
In general, if your model has measurement error, "cholesky" will be safe to use. Otherwise, "svd" is
2286+
recommended. "eigh" can also be tried if sampling with "svd" is very slow, but it is not as robust as "svd".
22532287
22542288
kwargs:
22552289
Additional keyword arguments are passed to pymc.sample_posterior_predictive

0 commit comments

Comments
 (0)