1
1
import logging
2
- from typing import Any , Callable , Dict , List , Optional , Sequence , Tuple , Union
2
+ from typing import Any , Callable , Optional , Sequence , Union
3
3
4
4
import numpy as np
5
5
import pandas as pd
@@ -219,8 +219,8 @@ def __init__(
219
219
measurement_error : bool = False ,
220
220
):
221
221
self ._fit_mode : Optional [str ] = None
222
- self ._fit_coords : Optional [dict [str , Sequence [str , ... ]]] = None
223
- self ._fit_dims : Optional [dict [str , Sequence [str , ... ]]] = None
222
+ self ._fit_coords : Optional [dict [str , Sequence [str ]]] = None
223
+ self ._fit_dims : Optional [dict [str , Sequence [str ]]] = None
224
224
self ._fit_data : Optional [pt .TensorVariable ] = None
225
225
226
226
self ._needs_exog_data = False
@@ -237,7 +237,7 @@ def __init__(
237
237
self .ssm = PytensorRepresentation (k_endog , k_states , k_posdef )
238
238
239
239
# This will be populated with PyMC random matrices after calling _insert_random_variables
240
- self .subbed_ssm : Optional [list [pt .TensorVariable , ... ]] = None
240
+ self .subbed_ssm : Optional [list [pt .TensorVariable ]] = None
241
241
242
242
if filter_type .lower () not in FILTER_FACTORY .keys ():
243
243
raise NotImplementedError (
@@ -296,7 +296,7 @@ def _print_data_requirements(self) -> None:
296
296
f"{ out } "
297
297
)
298
298
299
- def _unpack_statespace_with_placeholders (self ) -> Tuple :
299
+ def _unpack_statespace_with_placeholders (self ) -> tuple [ pt . TensorVariable , ...] :
300
300
"""
301
301
Helper function to quickly obtain all statespace matrices in the standard order. Matrices returned by this
302
302
method will include pytensor placeholders.
@@ -314,7 +314,7 @@ def _unpack_statespace_with_placeholders(self) -> Tuple:
314
314
315
315
return a0 , P0 , c , d , T , Z , R , H , Q
316
316
317
- def unpack_statespace (self ) -> list [pt .TensorVariable , ... ]:
317
+ def unpack_statespace (self ) -> list [pt .TensorVariable ]:
318
318
"""
319
319
Helper function to quickly obtain all statespace matrices in the standard order.
320
320
"""
@@ -329,7 +329,7 @@ def unpack_statespace(self) -> list[pt.TensorVariable, ...]:
329
329
return self .subbed_ssm
330
330
331
331
@property
332
- def param_names (self ) -> List [str ]:
332
+ def param_names (self ) -> list [str ]:
333
333
"""
334
334
Names of model parameters
335
335
@@ -339,7 +339,7 @@ def param_names(self) -> List[str]:
339
339
raise NotImplementedError ("The param_names property has not been implemented!" )
340
340
341
341
@property
342
- def data_names (self ) -> List [str ]:
342
+ def data_names (self ) -> list [str ]:
343
343
"""
344
344
Names of data variables expected by the model.
345
345
@@ -349,7 +349,7 @@ def data_names(self) -> List[str]:
349
349
raise NotImplementedError ("The data_names property has not been implemented!" )
350
350
351
351
@property
352
- def param_info (self ) -> Dict [str , Dict [str , Any ]]:
352
+ def param_info (self ) -> dict [str , dict [str , Any ]]:
353
353
"""
354
354
Information about parameters needed to declare priors
355
355
@@ -377,7 +377,7 @@ def data_info(self) -> dict[str, dict[str, Any]]:
377
377
raise NotImplementedError ("The data_info property has not been implemented!" )
378
378
379
379
@property
380
- def state_names (self ) -> List [str ]:
380
+ def state_names (self ) -> list [str ]:
381
381
"""
382
382
A k_states length list of strings, associated with the model's hidden states
383
383
@@ -386,22 +386,22 @@ def state_names(self) -> List[str]:
386
386
raise NotImplementedError ("The state_names property has not been implemented!" )
387
387
388
388
@property
389
- def observed_states (self ) -> List [str ]:
389
+ def observed_states (self ) -> list [str ]:
390
390
"""
391
391
A k_endog length list of strings, associated with the model's observed states
392
392
"""
393
393
raise NotImplementedError ("The observed_states property has not been implemented!" )
394
394
395
395
@property
396
- def shock_names (self ) -> List [str ]:
396
+ def shock_names (self ) -> list [str ]:
397
397
"""
398
398
A k_posdef length list of strings, associated with the model's shock processes
399
399
400
400
"""
401
401
raise NotImplementedError ("The shock_names property has not been implemented!" )
402
402
403
403
@property
404
- def default_priors (self ) -> Dict [str , Callable ]:
404
+ def default_priors (self ) -> dict [str , Callable ]:
405
405
"""
406
406
Dictionary of parameter names and callable functions to construct default priors for the model
407
407
@@ -411,7 +411,7 @@ def default_priors(self) -> Dict[str, Callable]:
411
411
raise NotImplementedError ("The default_priors property has not been implemented!" )
412
412
413
413
@property
414
- def coords (self ) -> Dict [str , Sequence [str ]]:
414
+ def coords (self ) -> dict [str , Sequence [str ]]:
415
415
"""
416
416
PyMC model coordinates
417
417
@@ -422,7 +422,7 @@ def coords(self) -> Dict[str, Sequence[str]]:
422
422
raise NotImplementedError ("The coords property has not been implemented!" )
423
423
424
424
@property
425
- def param_dims (self ) -> Dict [str , Sequence [str ]]:
425
+ def param_dims (self ) -> dict [str , Sequence [str ]]:
426
426
"""
427
427
Dictionary of named dimensions for each model parameter
428
428
@@ -483,7 +483,9 @@ def make_and_register_variable(self, name, shape, dtype=floatX) -> Variable:
483
483
self ._name_to_variable [name ] = placeholder
484
484
return placeholder
485
485
486
- def make_and_register_data (self , name , shape , dtype = floatX ) -> Variable :
486
+ def make_and_register_data (
487
+ self , name : str , shape : Union [int , tuple [int ]], dtype : str = floatX
488
+ ) -> Variable :
487
489
r"""
488
490
Helper function to create a pytensor symbolic variable and register it in the _name_to_data dictionary
489
491
@@ -577,7 +579,9 @@ def make_symbolic_graph(self) -> None:
577
579
"""
578
580
raise NotImplementedError ("The make_symbolic_statespace method has not been implemented!" )
579
581
580
- def _get_matrix_shape_and_dims (self , name : str ) -> Tuple [Tuple , Tuple ]:
582
+ def _get_matrix_shape_and_dims (
583
+ self , name : str
584
+ ) -> tuple [Optional [tuple [int ]], Optional [tuple [str ]]]:
581
585
"""
582
586
Get the shape and dimensions of a matrix associated with the specified name.
583
587
@@ -614,7 +618,11 @@ def _get_matrix_shape_and_dims(self, name: str) -> Tuple[Tuple, Tuple]:
614
618
615
619
return shape , dims
616
620
617
- def _get_output_shape_and_dims (self , idata : InferenceData , filter_output : str ) -> Tuple :
621
+ def _get_output_shape_and_dims (
622
+ self , idata : InferenceData , filter_output : str
623
+ ) -> tuple [
624
+ Optional [tuple [int ]], Optional [tuple [int ]], Optional [tuple [str ]], Optional [tuple [str ]]
625
+ ]:
618
626
"""
619
627
Get the shapes and dimensions of the output variables from the provided InferenceData.
620
628
@@ -756,15 +764,15 @@ def _insert_data_variables(self):
756
764
replacement_dict = {data : pymc_model [name ] for name , data in self ._name_to_data .items ()}
757
765
self .subbed_ssm = graph_replace (self .subbed_ssm , replace = replacement_dict , strict = True )
758
766
759
- def _register_matrices_with_pymc_model (self ) -> List [pt .TensorVariable ]:
767
+ def _register_matrices_with_pymc_model (self ) -> list [pt .TensorVariable ]:
760
768
"""
761
769
Add all statespace matrices to the PyMC model currently on the context stack as pm.Deterministic nodes, and
762
770
adds named dimensions if they are found.
763
771
764
772
Returns
765
773
-------
766
774
registered_matrices: list of pt.TensorVariable
767
- List of statespace matrices, wrapped in pm.Deterministic
775
+ list of statespace matrices, wrapped in pm.Deterministic
768
776
"""
769
777
770
778
pm_mod = modelcontext (None )
@@ -788,9 +796,7 @@ def _register_matrices_with_pymc_model(self) -> List[pt.TensorVariable]:
788
796
return registered_matrices
789
797
790
798
@staticmethod
791
- def _register_kalman_filter_outputs_with_pymc_model (
792
- outputs : tuple [pt .TensorVariable , ...]
793
- ) -> None :
799
+ def _register_kalman_filter_outputs_with_pymc_model (outputs : tuple [pt .TensorVariable ]) -> None :
794
800
mod = modelcontext (None )
795
801
states , covs = outputs [:4 ], outputs [4 :]
796
802
@@ -1014,7 +1020,7 @@ def _build_dummy_graph(self) -> None:
1014
1020
1015
1021
Returns
1016
1022
-------
1017
- List [pm.Flat]
1023
+ list [pm.Flat]
1018
1024
A list of pm.Flat variables representing all parameters estimated by the model.
1019
1025
"""
1020
1026
for name in self .param_names :
@@ -1026,7 +1032,7 @@ def _build_dummy_graph(self) -> None:
1026
1032
1027
1033
def _kalman_filter_outputs_from_dummy_graph (
1028
1034
self ,
1029
- ) -> tuple [list [pt .TensorVariable , ... ], tuple [pt .TensorVariable , pt .TensorVariable ]]:
1035
+ ) -> tuple [list [pt .TensorVariable ], list [ tuple [pt .TensorVariable , pt .TensorVariable ] ]]:
1030
1036
"""
1031
1037
Builds a Kalman filter graph using "dummy" pm.Flat distributions for the model variables and sorts the returns
1032
1038
into (mean, covariance) pairs for each of filtered, predicted, and smoothed output.
@@ -1379,9 +1385,9 @@ def sample_unconditional_prior(
1379
1385
1380
1386
def sample_unconditional_posterior (
1381
1387
self ,
1382
- idata ,
1383
- steps = None ,
1384
- use_data_time_dim = False ,
1388
+ idata : InferenceData ,
1389
+ steps : Optional [ int ] = None ,
1390
+ use_data_time_dim : bool = False ,
1385
1391
random_seed : Optional [RandomState ] = None ,
1386
1392
** kwargs ,
1387
1393
) -> InferenceData :
@@ -1775,7 +1781,8 @@ def _sort_obs_inputs_by_time_varying(self, d, Z):
1775
1781
1776
1782
return seqs , non_seqs
1777
1783
1778
- def _sort_obs_scan_args (self , args ):
1784
+ @staticmethod
1785
+ def _sort_obs_scan_args (args ):
1779
1786
args = list (args )
1780
1787
1781
1788
# If a matrix is time-varying, pytensor will put a [t] on the name
0 commit comments