mfe.timeseries¶
mfe.timeseries ¶
mfe.timeseries — Time series models.
vectorar VAR(P) estimation with 4 VCV options grangercause Granger causality LR/LM/Wald tests impulse_response IRF with delta-method standard errors beveridge_nelson Beveridge-Nelson trend/cycle decomposition for I(1) series
VARResult
dataclass
¶
VARResult(params: list[FloatArray], const: FloatArray | None, errors: FloatArray, sigma: FloatArray, r_squared: FloatArray, vcv: FloatArray, param_vec: FloatArray, lags: list[int], n_obs: int, n_vars: int, log_likelihood: float)
VAR(P) estimation result.
GCResult
dataclass
¶
Granger causality test result.
IRFResult
dataclass
¶
Impulse response function result.
BNResult
dataclass
¶
BNResult(trend: FloatArray, cycle: FloatArray, original: FloatArray, drift: float, ar_params: FloatArray, ar_order: int, method: str)
Beveridge-Nelson decomposition result.
vectorar ¶
vectorar(y: FloatArray, lags: int | list[int] = 1, include_const: bool = True, het: bool = True, uncorr: bool = False) -> VARResult
Estimate a VAR(P) or irregular VAR.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
FloatArray
|
|
required |
lags
|
int | list[int]
|
|
1
|
include_const
|
bool
|
|
True
|
het
|
bool
|
|
True
|
uncorr
|
bool
|
|
False
|
Returns:
| Type | Description |
|---|---|
VARResult with params as list of (K,K) matrices, one per lag.
|
|
Source code in src/mfe/timeseries/var.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
grangercause ¶
grangercause(y: FloatArray, lags: int | list[int] = 1, include_const: bool = True, het: bool = True, uncorr: bool = False, method: Literal['lr', 'lm', 'wald'] = 'lr') -> GCResult
Granger causality testing in a VAR.
stat[i,j] tests H0: lags of y_j do not Granger-cause y_i.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
same as vectorar
|
|
required |
lags
|
same as vectorar
|
|
required |
include_const
|
same as vectorar
|
|
required |
het
|
same as vectorar
|
|
required |
uncorr
|
same as vectorar
|
|
required |
method
|
'lr' | 'lm' | 'wald'
|
"lr" — likelihood ratio (Chi2, robust if het=True) "lm" — score/LM test "wald" — Wald test using VCV from vectorar |
'lr'
|
Returns:
| Type | Description |
|---|---|
GCResult with (K,K) matrices of statistics and p-values.
|
|
Source code in src/mfe/timeseries/var.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | |
impulse_response ¶
impulse_response(y: FloatArray, lags: int | list[int] = 1, horizon: int = 12, decomp: Literal['unit', 'cholesky', 'spectral'] = 'cholesky', include_const: bool = True, het: bool = True, uncorr: bool = False) -> IRFResult
Impulse response functions for a VAR(P) with delta-method standard errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
FloatArray
|
|
required |
lags
|
int | list[int]
|
|
1
|
horizon
|
int
|
|
12
|
decomp
|
Literal['unit', 'cholesky', 'spectral']
|
"unit" — unit shocks (unscaled), i.e. P0 = I_K "cholesky" — Cholesky of Sigma (lower triangular), recursive identification "spectral" — symmetric square root of Sigma (spectral decomposition) |
'cholesky'
|
het
|
VCV options for standard error computation
|
|
True
|
uncorr
|
VCV options for standard error computation
|
|
True
|
Returns:
| Type | Description |
|---|---|
IRFResult
|
.responses : (K, K, H+1) — responses[response_var, shock_var, h] .std_errors : (K, K, H+1) delta-method std errors |
Source code in src/mfe/timeseries/var.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 | |
beveridge_nelson ¶
Beveridge-Nelson Decomposition.
Beveridge, S. & Nelson, C.R. (1981): "A New Approach to Decomposition of Economic Time Series into Permanent and Transitory Components with Particular Attention to Measurement of the Business Cycle", Journal of Monetary Economics, 7(2), 151-174.
The BN decomposition splits an I(1) series y_t into: y_t = tau_t + c_t
where: tau_t = permanent (trend) component — a random walk with drift c_t = transitory (cycle) component — a zero-mean stationary process
The trend is defined as the long-run forecast: tau_t = lim_{h→∞} E[y_{t+h} - h*mu | I_t]
where mu = drift of y_t = E[Delta y_t].
The cycle is: c_t = y_t - tau_t = -sum_{j=1}^{∞} E[Delta y_{t+j} - mu | I_t]
Computation
Given a forecasting model for Delta y_t (typically an AR or ARMA), the BN decomposition can be computed exactly without truncating infinite sums.
Two approaches are implemented:
-
State-space (exact): Cast the ARMA model into companion form and compute the long-run forecast analytically using the matrix (I - A)^{-1}. This matches the algorithm of Morley (2002) and the MFE MATLAB implementation.
-
Direct AR: Fit an AR(p) to Delta y by OLS and compute the BN trend via the standard formula c_t = -sum_{k=1}^{p} pi_k * Delta y_{t-k+1} (Stock & Watson 1988; Cogley 2001). Faster and simpler.
The MFE MATLAB beveridgenelson.m uses approach 2 (AR on first differences). We implement both and default to approach 2.
References
Morley, J.C. (2002): "A State–Space Approach to Calculating the Beveridge–Nelson Decomposition", Economics Letters, 75(1), 123-127.
Newbold, P. (1990): "Precise and Efficient Computation of the Beveridge–Nelson Decomposition of Economic Time Series", Journal of Monetary Economics.
BNResult
dataclass
¶
BNResult(trend: FloatArray, cycle: FloatArray, original: FloatArray, drift: float, ar_params: FloatArray, ar_order: int, method: str)
Beveridge-Nelson decomposition result.
beveridge_nelson ¶
beveridge_nelson(y: FloatArray, ar_order: int | None = None, method: str = 'ar', ic: str = 'aic') -> BNResult
Beveridge-Nelson decomposition of an I(1) time series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
FloatArray
|
|
required |
ar_order
|
AR order p for the model of Delta y.
|
|
None
|
method
|
str
|
|
'ar'
|
ic
|
str
|
|
'aic'
|
Returns:
| Type | Description |
|---|---|
BNResult
|
.trend — permanent component tau_t (same length as y) .cycle — transitory component c_t = y_t - tau_t .drift — estimated drift of Delta y |
Notes
The BN trend is NOT smooth — it inherits all the innovation variance of the series. If you want a smooth trend, use HP or BK filter instead. The BN cycle is zero-mean, stationary, and reflects the business-cycle component as defined by forecasts.
Source code in src/mfe/timeseries/beveridge_nelson.py
var ¶
Vector Autoregression: estimation, Granger causality, impulse response functions.
vectorar — VAR(P) estimation with 4 VCV options grangercause — Granger causality LR / LM / Wald tests impulse_response — IRF with delta-method std errors, Cholesky or spectral decomp
Design gap vs. statsmodels.tsa.VAR: statsmodels VAR: OLS only, homoskedastic VCV, no heteroskedastic sandwich statsmodels IRF: standard errors only under homoskedastic assumption No Granger causality test with robust VCV in statsmodels
This module fills those gaps exactly, matching the MFE MATLAB vectorar.m outputs.
VARResult
dataclass
¶
VARResult(params: list[FloatArray], const: FloatArray | None, errors: FloatArray, sigma: FloatArray, r_squared: FloatArray, vcv: FloatArray, param_vec: FloatArray, lags: list[int], n_obs: int, n_vars: int, log_likelihood: float)
VAR(P) estimation result.
GCResult
dataclass
¶
Granger causality test result.
IRFResult
dataclass
¶
Impulse response function result.
vectorar ¶
vectorar(y: FloatArray, lags: int | list[int] = 1, include_const: bool = True, het: bool = True, uncorr: bool = False) -> VARResult
Estimate a VAR(P) or irregular VAR.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
FloatArray
|
|
required |
lags
|
int | list[int]
|
|
1
|
include_const
|
bool
|
|
True
|
het
|
bool
|
|
True
|
uncorr
|
bool
|
|
False
|
Returns:
| Type | Description |
|---|---|
VARResult with params as list of (K,K) matrices, one per lag.
|
|
Source code in src/mfe/timeseries/var.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
grangercause ¶
grangercause(y: FloatArray, lags: int | list[int] = 1, include_const: bool = True, het: bool = True, uncorr: bool = False, method: Literal['lr', 'lm', 'wald'] = 'lr') -> GCResult
Granger causality testing in a VAR.
stat[i,j] tests H0: lags of y_j do not Granger-cause y_i.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
same as vectorar
|
|
required |
lags
|
same as vectorar
|
|
required |
include_const
|
same as vectorar
|
|
required |
het
|
same as vectorar
|
|
required |
uncorr
|
same as vectorar
|
|
required |
method
|
'lr' | 'lm' | 'wald'
|
"lr" — likelihood ratio (Chi2, robust if het=True) "lm" — score/LM test "wald" — Wald test using VCV from vectorar |
'lr'
|
Returns:
| Type | Description |
|---|---|
GCResult with (K,K) matrices of statistics and p-values.
|
|
Source code in src/mfe/timeseries/var.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | |
impulse_response ¶
impulse_response(y: FloatArray, lags: int | list[int] = 1, horizon: int = 12, decomp: Literal['unit', 'cholesky', 'spectral'] = 'cholesky', include_const: bool = True, het: bool = True, uncorr: bool = False) -> IRFResult
Impulse response functions for a VAR(P) with delta-method standard errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
FloatArray
|
|
required |
lags
|
int | list[int]
|
|
1
|
horizon
|
int
|
|
12
|
decomp
|
Literal['unit', 'cholesky', 'spectral']
|
"unit" — unit shocks (unscaled), i.e. P0 = I_K "cholesky" — Cholesky of Sigma (lower triangular), recursive identification "spectral" — symmetric square root of Sigma (spectral decomposition) |
'cholesky'
|
het
|
VCV options for standard error computation
|
|
True
|
uncorr
|
VCV options for standard error computation
|
|
True
|
Returns:
| Type | Description |
|---|---|
IRFResult
|
.responses : (K, K, H+1) — responses[response_var, shock_var, h] .std_errors : (K, K, H+1) delta-method std errors |
Source code in src/mfe/timeseries/var.py
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 | |