1#cython: boundscheck=False 2#cython: wraparound=False 3#cython: cdivision=False 4""" 5State Space Model Smoother declarations 6 7Author: Chad Fulton 8License: Simplified-BSD 9""" 10 11cdef int SMOOTHER_STATE # Durbin and Koopman (2012), Chapter 4.4.2 12cdef int SMOOTHER_STATE_COV # Durbin and Koopman (2012), Chapter 4.4.3 13cdef int SMOOTHER_DISTURBANCE # Durbin and Koopman (2012), Chapter 4.5 14cdef int SMOOTHER_DISTURBANCE_COV # Durbin and Koopman (2012), Chapter 4.5 15cdef int SMOOTHER_STATE_AUTOCOV # Durbin and Koopman (2012), Chapter 4.7 16cdef int SMOOTHER_ALL 17 18cdef int SMOOTH_CONVENTIONAL 19cdef int SMOOTH_CLASSICAL 20cdef int SMOOTH_ALTERNATIVE 21cdef int SMOOTH_UNIVARIATE 22 23# Typical imports 24cimport numpy as np 25 26from statsmodels.tsa.statespace._representation cimport ( 27 sStatespace, dStatespace, cStatespace, zStatespace 28) 29from statsmodels.tsa.statespace._kalman_filter cimport ( 30 sKalmanFilter, dKalmanFilter, cKalmanFilter, zKalmanFilter 31) 32 33# Single precision 34cdef class sKalmanSmoother(object): 35 # Statespace object 36 cdef readonly sStatespace model 37 # Kalman filter 38 cdef readonly sKalmanFilter kfilter 39 40 cdef readonly int t 41 cdef readonly int smoother_output 42 cdef readonly int smooth_method 43 cdef readonly int _smooth_method 44 cdef readonly int filter_method 45 46 cdef readonly np.float32_t [::1,:] scaled_smoothed_estimator 47 cdef readonly np.float32_t [::1,:,:] scaled_smoothed_estimator_cov 48 cdef readonly np.float32_t [::1,:] smoothing_error 49 cdef readonly np.float32_t [::1,:] smoothed_state 50 cdef readonly np.float32_t [::1,:,:] smoothed_state_cov 51 cdef readonly np.float32_t [::1,:] smoothed_measurement_disturbance 52 cdef readonly np.float32_t [::1,:] smoothed_state_disturbance 53 cdef readonly np.float32_t [::1,:,:] smoothed_measurement_disturbance_cov 54 cdef readonly np.float32_t [::1,:,:] smoothed_state_disturbance_cov 55 56 cdef readonly np.float32_t [::1,:,:] smoothed_state_autocov, innovations_transition 57 cdef readonly np.float32_t [::1,:] tmp_autocov 58 59 cdef readonly np.float32_t [::1,:] scaled_smoothed_diffuse_estimator 60 cdef readonly np.float32_t [::1,:,:] scaled_smoothed_diffuse1_estimator_cov 61 cdef readonly np.float32_t [::1,:,:] scaled_smoothed_diffuse2_estimator_cov 62 63 cdef readonly np.float32_t [:] selected_design 64 cdef readonly np.float32_t [:] selected_obs_cov 65 66 cdef readonly np.float32_t [::1,:] tmpL, tmpL2, tmp0, tmp00, tmp000 67 68 # Statespace 69 # cdef np.float32_t * _design 70 # cdef np.float32_t * _obs_cov 71 # cdef np.float32_t * _transition 72 # cdef np.float32_t * _selection 73 # cdef np.float32_t * _state_cov 74 75 # Kalman filter 76 # cdef np.float32_t * _predicted_state 77 # cdef np.float32_t * _predicted_state_cov 78 # cdef np.float32_t * _kalman_gain 79 80 # cdef np.float32_t * _tmp1 81 # cdef np.float32_t * _tmp2 82 # cdef np.float32_t * _tmp3 83 # cdef np.float32_t * _tmp4 84 85 # Kalman smoother 86 cdef np.float32_t * _input_scaled_smoothed_estimator 87 cdef np.float32_t * _input_scaled_smoothed_estimator_cov 88 89 cdef np.float32_t * _scaled_smoothed_estimator 90 cdef np.float32_t * _scaled_smoothed_estimator_cov 91 cdef np.float32_t * _smoothing_error 92 cdef np.float32_t * _smoothed_state 93 cdef np.float32_t * _smoothed_state_cov 94 cdef np.float32_t * _smoothed_measurement_disturbance 95 cdef np.float32_t * _smoothed_state_disturbance 96 cdef np.float32_t * _smoothed_measurement_disturbance_cov 97 cdef np.float32_t * _smoothed_state_disturbance_cov 98 99 cdef np.float32_t * _innovations_transition 100 cdef np.float32_t * _smoothed_state_autocov 101 cdef np.float32_t * _tmp_autocov 102 103 cdef np.float32_t * _input_scaled_smoothed_diffuse_estimator 104 cdef np.float32_t * _input_scaled_smoothed_diffuse1_estimator_cov 105 cdef np.float32_t * _input_scaled_smoothed_diffuse2_estimator_cov 106 cdef np.float32_t * _scaled_smoothed_diffuse_estimator 107 cdef np.float32_t * _scaled_smoothed_diffuse1_estimator_cov 108 cdef np.float32_t * _scaled_smoothed_diffuse2_estimator_cov 109 110 # Temporary 111 cdef np.float32_t * _tmpL 112 cdef np.float32_t * _tmpL2 113 cdef np.float32_t * _tmp0 114 cdef np.float32_t * _tmp00 115 cdef np.float32_t * _tmp000 116 117 # Functions 118 cdef int (*smooth_estimators_measurement)( 119 sKalmanSmoother, sKalmanFilter, sStatespace 120 ) except * 121 cdef int (*smooth_estimators_time)( 122 sKalmanSmoother, sKalmanFilter, sStatespace 123 ) 124 cdef int (*smooth_state)( 125 sKalmanSmoother, sKalmanFilter, sStatespace 126 ) 127 cdef int (*smooth_disturbances)( 128 sKalmanSmoother, sKalmanFilter, sStatespace 129 ) 130 131 # cdef readonly int k_endog, k_states, k_posdef, k_endog2, k_states2, k_posdef2, k_endogstates, k_statesposdef 132 133 cdef allocate_arrays(self) 134 cdef int check_filter_method_changed(self) 135 cdef int reset_filter_method(self, int force_reset=*) 136 cpdef set_smoother_output(self, int smoother_output, int force_reset=*) 137 cpdef set_smooth_method(self, int smooth_method) 138 cpdef reset(self, int force_reset=*) 139 cpdef seek(self, unsigned int t) 140 cdef void initialize_statespace_object_pointers(self) except * 141 cdef void initialize_filter_object_pointers(self) 142 cdef void initialize_smoother_object_pointers(self) except * 143 cdef void initialize_function_pointers(self) except * 144 cdef void _initialize_temp_pointers(self) except * 145 146# Double precision 147cdef class dKalmanSmoother(object): 148 # Statespace object 149 cdef readonly dStatespace model 150 # Kalman filter 151 cdef readonly dKalmanFilter kfilter 152 153 cdef readonly int t 154 cdef readonly int smoother_output 155 cdef readonly int smooth_method 156 cdef readonly int _smooth_method 157 cdef readonly int filter_method 158 159 cdef readonly np.float64_t [::1,:] scaled_smoothed_estimator 160 cdef readonly np.float64_t [::1,:,:] scaled_smoothed_estimator_cov 161 cdef readonly np.float64_t [::1,:] smoothing_error 162 cdef readonly np.float64_t [::1,:] smoothed_state 163 cdef readonly np.float64_t [::1,:,:] smoothed_state_cov 164 cdef readonly np.float64_t [::1,:] smoothed_measurement_disturbance 165 cdef readonly np.float64_t [::1,:] smoothed_state_disturbance 166 cdef readonly np.float64_t [::1,:,:] smoothed_measurement_disturbance_cov 167 cdef readonly np.float64_t [::1,:,:] smoothed_state_disturbance_cov 168 169 cdef readonly np.float64_t [::1,:,:] smoothed_state_autocov, innovations_transition 170 cdef readonly np.float64_t [::1,:] tmp_autocov 171 172 cdef readonly np.float64_t [::1,:] scaled_smoothed_diffuse_estimator 173 cdef readonly np.float64_t [::1,:,:] scaled_smoothed_diffuse1_estimator_cov 174 cdef readonly np.float64_t [::1,:,:] scaled_smoothed_diffuse2_estimator_cov 175 176 cdef readonly np.float64_t [:] selected_design 177 cdef readonly np.float64_t [:] selected_obs_cov 178 179 cdef readonly np.float64_t [::1,:] tmpL, tmpL2, tmp0, tmp00, tmp000 180 181 # Statespace 182 # cdef np.float64_t * _design 183 # cdef np.float64_t * _obs_cov 184 # cdef np.float64_t * _transition 185 # cdef np.float64_t * _selection 186 # cdef np.float64_t * _state_cov 187 188 # Kalman filter 189 # cdef np.float64_t * _predicted_state 190 # cdef np.float64_t * _predicted_state_cov 191 # cdef np.float64_t * _kalman_gain 192 193 # cdef np.float64_t * _tmp1 194 # cdef np.float64_t * _tmp2 195 # cdef np.float64_t * _tmp3 196 # cdef np.float64_t * _tmp4 197 198 # Kalman smoother 199 cdef np.float64_t * _input_scaled_smoothed_estimator 200 cdef np.float64_t * _input_scaled_smoothed_estimator_cov 201 202 cdef np.float64_t * _scaled_smoothed_estimator 203 cdef np.float64_t * _scaled_smoothed_estimator_cov 204 cdef np.float64_t * _smoothing_error 205 cdef np.float64_t * _smoothed_state 206 cdef np.float64_t * _smoothed_state_cov 207 cdef np.float64_t * _smoothed_measurement_disturbance 208 cdef np.float64_t * _smoothed_state_disturbance 209 cdef np.float64_t * _smoothed_measurement_disturbance_cov 210 cdef np.float64_t * _smoothed_state_disturbance_cov 211 212 cdef np.float64_t * _innovations_transition 213 cdef np.float64_t * _smoothed_state_autocov 214 cdef np.float64_t * _tmp_autocov 215 216 cdef np.float64_t * _input_scaled_smoothed_diffuse_estimator 217 cdef np.float64_t * _input_scaled_smoothed_diffuse1_estimator_cov 218 cdef np.float64_t * _input_scaled_smoothed_diffuse2_estimator_cov 219 cdef np.float64_t * _scaled_smoothed_diffuse_estimator 220 cdef np.float64_t * _scaled_smoothed_diffuse1_estimator_cov 221 cdef np.float64_t * _scaled_smoothed_diffuse2_estimator_cov 222 223 # Temporary 224 cdef np.float64_t * _tmpL 225 cdef np.float64_t * _tmpL2 226 cdef np.float64_t * _tmp0 227 cdef np.float64_t * _tmp00 228 cdef np.float64_t * _tmp000 229 230 # Functions 231 cdef int (*smooth_estimators_measurement)( 232 dKalmanSmoother, dKalmanFilter, dStatespace 233 ) except * 234 cdef int (*smooth_estimators_time)( 235 dKalmanSmoother, dKalmanFilter, dStatespace 236 ) 237 cdef int (*smooth_state)( 238 dKalmanSmoother, dKalmanFilter, dStatespace 239 ) 240 cdef int (*smooth_disturbances)( 241 dKalmanSmoother, dKalmanFilter, dStatespace 242 ) 243 244 # cdef readonly int k_endog, k_states, k_posdef, k_endog2, k_states2, k_posdef2, k_endogstates, k_statesposdef 245 246 cdef allocate_arrays(self) 247 cdef int check_filter_method_changed(self) 248 cdef int reset_filter_method(self, int force_reset=*) 249 cpdef set_smoother_output(self, int smoother_output, int force_reset=*) 250 cpdef set_smooth_method(self, int smooth_method) 251 cpdef reset(self, int force_reset=*) 252 cpdef seek(self, unsigned int t) 253 cdef void initialize_statespace_object_pointers(self) except * 254 cdef void initialize_filter_object_pointers(self) 255 cdef void initialize_smoother_object_pointers(self) except * 256 cdef void initialize_function_pointers(self) except * 257 cdef void _initialize_temp_pointers(self) except * 258 259# Single precision complex 260cdef class cKalmanSmoother(object): 261 # Statespace object 262 cdef readonly cStatespace model 263 # Kalman filter 264 cdef readonly cKalmanFilter kfilter 265 266 cdef readonly int t 267 cdef readonly int smoother_output 268 cdef readonly int smooth_method 269 cdef readonly int _smooth_method 270 cdef readonly int filter_method 271 272 cdef readonly np.complex64_t [::1,:] scaled_smoothed_estimator 273 cdef readonly np.complex64_t [::1,:,:] scaled_smoothed_estimator_cov 274 cdef readonly np.complex64_t [::1,:] smoothing_error 275 cdef readonly np.complex64_t [::1,:] smoothed_state 276 cdef readonly np.complex64_t [::1,:,:] smoothed_state_cov 277 cdef readonly np.complex64_t [::1,:] smoothed_measurement_disturbance 278 cdef readonly np.complex64_t [::1,:] smoothed_state_disturbance 279 cdef readonly np.complex64_t [::1,:,:] smoothed_measurement_disturbance_cov 280 cdef readonly np.complex64_t [::1,:,:] smoothed_state_disturbance_cov 281 282 cdef readonly np.complex64_t [::1,:,:] smoothed_state_autocov, innovations_transition 283 cdef readonly np.complex64_t [::1,:] tmp_autocov 284 285 cdef readonly np.complex64_t [::1,:] scaled_smoothed_diffuse_estimator 286 cdef readonly np.complex64_t [::1,:,:] scaled_smoothed_diffuse1_estimator_cov 287 cdef readonly np.complex64_t [::1,:,:] scaled_smoothed_diffuse2_estimator_cov 288 289 cdef readonly np.complex64_t [:] selected_design 290 cdef readonly np.complex64_t [:] selected_obs_cov 291 292 cdef readonly np.complex64_t [::1,:] tmpL, tmpL2, tmp0, tmp00, tmp000 293 294 # Statespace 295 # cdef np.complex64_t * _design 296 # cdef np.complex64_t * _obs_cov 297 # cdef np.complex64_t * _transition 298 # cdef np.complex64_t * _selection 299 # cdef np.complex64_t * _state_cov 300 301 # Kalman filter 302 # cdef np.complex64_t * _predicted_state 303 # cdef np.complex64_t * _predicted_state_cov 304 # cdef np.complex64_t * _kalman_gain 305 306 # cdef np.complex64_t * _tmp1 307 # cdef np.complex64_t * _tmp2 308 # cdef np.complex64_t * _tmp3 309 # cdef np.complex64_t * _tmp4 310 311 # Kalman smoother 312 cdef np.complex64_t * _input_scaled_smoothed_estimator 313 cdef np.complex64_t * _input_scaled_smoothed_estimator_cov 314 315 cdef np.complex64_t * _scaled_smoothed_estimator 316 cdef np.complex64_t * _scaled_smoothed_estimator_cov 317 cdef np.complex64_t * _smoothing_error 318 cdef np.complex64_t * _smoothed_state 319 cdef np.complex64_t * _smoothed_state_cov 320 cdef np.complex64_t * _smoothed_measurement_disturbance 321 cdef np.complex64_t * _smoothed_state_disturbance 322 cdef np.complex64_t * _smoothed_measurement_disturbance_cov 323 cdef np.complex64_t * _smoothed_state_disturbance_cov 324 325 cdef np.complex64_t * _innovations_transition 326 cdef np.complex64_t * _smoothed_state_autocov 327 cdef np.complex64_t * _tmp_autocov 328 329 cdef np.complex64_t * _input_scaled_smoothed_diffuse_estimator 330 cdef np.complex64_t * _input_scaled_smoothed_diffuse1_estimator_cov 331 cdef np.complex64_t * _input_scaled_smoothed_diffuse2_estimator_cov 332 cdef np.complex64_t * _scaled_smoothed_diffuse_estimator 333 cdef np.complex64_t * _scaled_smoothed_diffuse1_estimator_cov 334 cdef np.complex64_t * _scaled_smoothed_diffuse2_estimator_cov 335 336 # Temporary 337 cdef np.complex64_t * _tmpL 338 cdef np.complex64_t * _tmpL2 339 cdef np.complex64_t * _tmp0 340 cdef np.complex64_t * _tmp00 341 cdef np.complex64_t * _tmp000 342 343 # Functions 344 cdef int (*smooth_estimators_measurement)( 345 cKalmanSmoother, cKalmanFilter, cStatespace 346 ) except * 347 cdef int (*smooth_estimators_time)( 348 cKalmanSmoother, cKalmanFilter, cStatespace 349 ) 350 cdef int (*smooth_state)( 351 cKalmanSmoother, cKalmanFilter, cStatespace 352 ) 353 cdef int (*smooth_disturbances)( 354 cKalmanSmoother, cKalmanFilter, cStatespace 355 ) 356 357 # cdef readonly int k_endog, k_states, k_posdef, k_endog2, k_states2, k_posdef2, k_endogstates, k_statesposdef 358 359 cdef allocate_arrays(self) 360 cdef int check_filter_method_changed(self) 361 cdef int reset_filter_method(self, int force_reset=*) 362 cpdef set_smoother_output(self, int smoother_output, int force_reset=*) 363 cpdef set_smooth_method(self, int smooth_method) 364 cpdef reset(self, int force_reset=*) 365 cpdef seek(self, unsigned int t) 366 cdef void initialize_statespace_object_pointers(self) except * 367 cdef void initialize_filter_object_pointers(self) 368 cdef void initialize_smoother_object_pointers(self) except * 369 cdef void initialize_function_pointers(self) except * 370 cdef void _initialize_temp_pointers(self) except * 371 372# Double precision complex 373cdef class zKalmanSmoother(object): 374 # Statespace object 375 cdef readonly zStatespace model 376 # Kalman filter 377 cdef readonly zKalmanFilter kfilter 378 379 cdef readonly int t 380 cdef readonly int smoother_output 381 cdef readonly int smooth_method 382 cdef readonly int _smooth_method 383 cdef readonly int filter_method 384 385 cdef readonly np.complex128_t [::1,:] scaled_smoothed_estimator 386 cdef readonly np.complex128_t [::1,:,:] scaled_smoothed_estimator_cov 387 cdef readonly np.complex128_t [::1,:] smoothing_error 388 cdef readonly np.complex128_t [::1,:] smoothed_state 389 cdef readonly np.complex128_t [::1,:,:] smoothed_state_cov 390 cdef readonly np.complex128_t [::1,:] smoothed_measurement_disturbance 391 cdef readonly np.complex128_t [::1,:] smoothed_state_disturbance 392 cdef readonly np.complex128_t [::1,:,:] smoothed_measurement_disturbance_cov 393 cdef readonly np.complex128_t [::1,:,:] smoothed_state_disturbance_cov 394 395 cdef readonly np.complex128_t [::1,:,:] smoothed_state_autocov, innovations_transition 396 cdef readonly np.complex128_t [::1,:] tmp_autocov 397 398 cdef readonly np.complex128_t [::1,:] scaled_smoothed_diffuse_estimator 399 cdef readonly np.complex128_t [::1,:,:] scaled_smoothed_diffuse1_estimator_cov 400 cdef readonly np.complex128_t [::1,:,:] scaled_smoothed_diffuse2_estimator_cov 401 402 cdef readonly np.complex128_t [:] selected_design 403 cdef readonly np.complex128_t [:] selected_obs_cov 404 405 cdef readonly np.complex128_t [::1,:] tmpL, tmpL2, tmp0, tmp00, tmp000 406 407 # Statespace 408 # cdef np.complex128_t * _design 409 # cdef np.complex128_t * _obs_cov 410 # cdef np.complex128_t * _transition 411 # cdef np.complex128_t * _selection 412 # cdef np.complex128_t * _state_cov 413 414 # Kalman filter 415 # cdef np.complex128_t * _predicted_state 416 # cdef np.complex128_t * _predicted_state_cov 417 # cdef np.complex128_t * _kalman_gain 418 419 # cdef np.complex128_t * _tmp1 420 # cdef np.complex128_t * _tmp2 421 # cdef np.complex128_t * _tmp3 422 # cdef np.complex128_t * _tmp4 423 424 # Kalman smoother 425 cdef np.complex128_t * _input_scaled_smoothed_estimator 426 cdef np.complex128_t * _input_scaled_smoothed_estimator_cov 427 428 cdef np.complex128_t * _scaled_smoothed_estimator 429 cdef np.complex128_t * _scaled_smoothed_estimator_cov 430 cdef np.complex128_t * _smoothing_error 431 cdef np.complex128_t * _smoothed_state 432 cdef np.complex128_t * _smoothed_state_cov 433 cdef np.complex128_t * _smoothed_measurement_disturbance 434 cdef np.complex128_t * _smoothed_state_disturbance 435 cdef np.complex128_t * _smoothed_measurement_disturbance_cov 436 cdef np.complex128_t * _smoothed_state_disturbance_cov 437 438 cdef np.complex128_t * _innovations_transition 439 cdef np.complex128_t * _smoothed_state_autocov 440 cdef np.complex128_t * _tmp_autocov 441 442 cdef np.complex128_t * _input_scaled_smoothed_diffuse_estimator 443 cdef np.complex128_t * _input_scaled_smoothed_diffuse1_estimator_cov 444 cdef np.complex128_t * _input_scaled_smoothed_diffuse2_estimator_cov 445 cdef np.complex128_t * _scaled_smoothed_diffuse_estimator 446 cdef np.complex128_t * _scaled_smoothed_diffuse1_estimator_cov 447 cdef np.complex128_t * _scaled_smoothed_diffuse2_estimator_cov 448 449 # Temporary 450 cdef np.complex128_t * _tmpL 451 cdef np.complex128_t * _tmpL2 452 cdef np.complex128_t * _tmp0 453 cdef np.complex128_t * _tmp00 454 cdef np.complex128_t * _tmp000 455 456 # Functions 457 cdef int (*smooth_estimators_measurement)( 458 zKalmanSmoother, zKalmanFilter, zStatespace 459 ) except * 460 cdef int (*smooth_estimators_time)( 461 zKalmanSmoother, zKalmanFilter, zStatespace 462 ) 463 cdef int (*smooth_state)( 464 zKalmanSmoother, zKalmanFilter, zStatespace 465 ) 466 cdef int (*smooth_disturbances)( 467 zKalmanSmoother, zKalmanFilter, zStatespace 468 ) 469 470 # cdef readonly int k_endog, k_states, k_posdef, k_endog2, k_states2, k_posdef2, k_endogstates, k_statesposdef 471 472 cdef allocate_arrays(self) 473 cdef int check_filter_method_changed(self) 474 cdef int reset_filter_method(self, int force_reset=*) 475 cpdef set_smoother_output(self, int smoother_output, int force_reset=*) 476 cpdef set_smooth_method(self, int smooth_method) 477 cpdef reset(self, int force_reset=*) 478 cpdef seek(self, unsigned int t) 479 cdef void initialize_statespace_object_pointers(self) except * 480 cdef void initialize_filter_object_pointers(self) 481 cdef void initialize_smoother_object_pointers(self) except * 482 cdef void initialize_function_pointers(self) except * 483 cdef void _initialize_temp_pointers(self) except * 484