Forecasting¶
forecast()¶
Forecasting module for pyStMoMo.
ForStMoMo
dataclass
¶
Result of :func:~pystmomo.forecast.forecast.
Attributes:
| Name | Type | Description |
|---|---|---|
fit |
FitStMoMo
|
The fitted model that was projected. |
h |
int
|
Forecast horizon (number of future years). |
years_f |
ndarray
|
Future year labels, shape (h,). |
cohorts_f |
ndarray
|
All future cohorts that were forecast (those beyond max(fit.cohorts)), shape (n_new_cohorts,). Empty array when the model has no cohort term. |
kt_f |
ndarray
|
Central forecast of period indexes, shape (N, h). |
kt_f_lower |
ndarray
|
Lower confidence band for kt, shape (N, h). |
kt_f_upper |
ndarray
|
Upper confidence band for kt, shape (N, h). |
gc_f |
ndarray | None
|
Central forecast of new cohort indexes, shape (n_new_cohorts,). None when the model has no cohort term. |
gc_f_lower |
ndarray | None
|
Lower band for gc, shape (n_new_cohorts,). None if no cohort. |
gc_f_upper |
ndarray | None
|
Upper band for gc, shape (n_new_cohorts,). None if no cohort. |
rates |
ndarray
|
Central forecast mortality rates, shape (n_ages, h). |
level |
float
|
Confidence level used for intervals. |
kt_model |
object
|
The fitted MRWD or IndependentArima used to project kt. |
gc_model |
object | None
|
The fitted model used to project gc, or None. |
Source code in src/pystmomo/forecast/forecast_result.py
IndependentArima
¶
One ARIMA model per row of kt (or per scalar gc).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
order
|
tuple[int, int, int]
|
ARIMA order (p, d, q). |
required |
include_constant
|
bool
|
Whether to include a constant/trend term. |
required |
models
|
list
|
List of fitted statsmodels ARIMA results, one per series. |
required |
last_values
|
ndarray
|
Shape (N,) — last observed values for each series. |
required |
Source code in src/pystmomo/forecast/arima_fc.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |
fit(kt, order=(1, 1, 0), include_constant=True)
classmethod
¶
Fit independent ARIMA models to each row of kt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kt
|
ndarray
|
Shape (N, n_years) or (n_years,) for a single series. |
required |
order
|
tuple[int, int, int]
|
ARIMA (p, d, q). |
(1, 1, 0)
|
include_constant
|
bool
|
Include constant/trend term. |
True
|
Returns:
| Type | Description |
|---|---|
IndependentArima
|
|
Source code in src/pystmomo/forecast/arima_fc.py
forecast(h, level=0.95)
¶
Point forecast and confidence intervals for h steps ahead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h
|
int
|
Forecast horizon. |
required |
level
|
float
|
Confidence level (e.g. 0.95). |
0.95
|
Returns:
| Type | Description |
|---|---|
(mean, lower, upper)
|
Each shape (N, h). |
Source code in src/pystmomo/forecast/arima_fc.py
simulate(h, nsim, rng)
¶
Simulate future trajectories from fitted ARIMA models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h
|
int
|
Forecast horizon. |
required |
nsim
|
int
|
Number of simulations. |
required |
rng
|
Generator
|
NumPy random generator (used to set seed for statsmodels). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Shape (N, h, nsim). |
Source code in src/pystmomo/forecast/arima_fc.py
MultivariateRandomWalkDrift
¶
Multivariate Random Walk with Drift fitted to a matrix of period indexes.
The model is: Δκ_t = drift + ε_t, ε_t ~ MVN(0, Σ)
Forecast mean at step s: last + s * drift Forecast variance at step s: Var(s) = s * Σ + (s²/T) * Σ where T = number of differences (n_years - 1).
The second term accounts for estimation uncertainty in the drift.
Attributes:
| Name | Type | Description |
|---|---|---|
drift |
Shape (N,) — mean of first differences. |
|
sigma |
Shape (N, N) — sample covariance of first differences. |
|
last |
Shape (N,) — last observed κ_t values. |
|
n_years |
Number of fitted years (T+1); T = n_years - 1 differences are used. |
Source code in src/pystmomo/forecast/mrwd.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
fit(kt)
classmethod
¶
Fit MRWD to observed period indexes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kt
|
ndarray
|
Period indexes, shape (N, n_years). |
required |
Returns:
| Type | Description |
|---|---|
MultivariateRandomWalkDrift
|
|
Source code in src/pystmomo/forecast/mrwd.py
forecast(h, level=0.95)
¶
Point forecast and confidence interval for h steps ahead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h
|
int
|
Forecast horizon. |
required |
level
|
float
|
Confidence level (e.g. 0.95). |
0.95
|
Returns:
| Type | Description |
|---|---|
(mean, lower, upper)
|
Each shape (N, h). |
Source code in src/pystmomo/forecast/mrwd.py
simulate(h, nsim, rng)
¶
Simulate future trajectories using Cholesky-based MVN innovations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h
|
int
|
Forecast horizon. |
required |
nsim
|
int
|
Number of simulations. |
required |
rng
|
Generator
|
NumPy random generator. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Shape (N, h, nsim). |
Source code in src/pystmomo/forecast/mrwd.py
ForStMoMo¶
Result of :func:~pystmomo.forecast.forecast.
Attributes:
| Name | Type | Description |
|---|---|---|
fit |
FitStMoMo
|
The fitted model that was projected. |
h |
int
|
Forecast horizon (number of future years). |
years_f |
ndarray
|
Future year labels, shape (h,). |
cohorts_f |
ndarray
|
All future cohorts that were forecast (those beyond max(fit.cohorts)), shape (n_new_cohorts,). Empty array when the model has no cohort term. |
kt_f |
ndarray
|
Central forecast of period indexes, shape (N, h). |
kt_f_lower |
ndarray
|
Lower confidence band for kt, shape (N, h). |
kt_f_upper |
ndarray
|
Upper confidence band for kt, shape (N, h). |
gc_f |
ndarray | None
|
Central forecast of new cohort indexes, shape (n_new_cohorts,). None when the model has no cohort term. |
gc_f_lower |
ndarray | None
|
Lower band for gc, shape (n_new_cohorts,). None if no cohort. |
gc_f_upper |
ndarray | None
|
Upper band for gc, shape (n_new_cohorts,). None if no cohort. |
rates |
ndarray
|
Central forecast mortality rates, shape (n_ages, h). |
level |
float
|
Confidence level used for intervals. |
kt_model |
object
|
The fitted MRWD or IndependentArima used to project kt. |
gc_model |
object | None
|
The fitted model used to project gc, or None. |
Source code in src/pystmomo/forecast/forecast_result.py
MRWD¶
Multivariate Random Walk with Drift fitted to a matrix of period indexes.
The model is: Δκ_t = drift + ε_t, ε_t ~ MVN(0, Σ)
Forecast mean at step s: last + s * drift Forecast variance at step s: Var(s) = s * Σ + (s²/T) * Σ where T = number of differences (n_years - 1).
The second term accounts for estimation uncertainty in the drift.
Attributes:
| Name | Type | Description |
|---|---|---|
drift |
Shape (N,) — mean of first differences. |
|
sigma |
Shape (N, N) — sample covariance of first differences. |
|
last |
Shape (N,) — last observed κ_t values. |
|
n_years |
Number of fitted years (T+1); T = n_years - 1 differences are used. |
Source code in src/pystmomo/forecast/mrwd.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
fit(kt)
classmethod
¶
Fit MRWD to observed period indexes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kt
|
ndarray
|
Period indexes, shape (N, n_years). |
required |
Returns:
| Type | Description |
|---|---|
MultivariateRandomWalkDrift
|
|
Source code in src/pystmomo/forecast/mrwd.py
forecast(h, level=0.95)
¶
Point forecast and confidence interval for h steps ahead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h
|
int
|
Forecast horizon. |
required |
level
|
float
|
Confidence level (e.g. 0.95). |
0.95
|
Returns:
| Type | Description |
|---|---|
(mean, lower, upper)
|
Each shape (N, h). |
Source code in src/pystmomo/forecast/mrwd.py
simulate(h, nsim, rng)
¶
Simulate future trajectories using Cholesky-based MVN innovations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h
|
int
|
Forecast horizon. |
required |
nsim
|
int
|
Number of simulations. |
required |
rng
|
Generator
|
NumPy random generator. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Shape (N, h, nsim). |
Source code in src/pystmomo/forecast/mrwd.py
Independent ARIMA¶
One ARIMA model per row of kt (or per scalar gc).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
order
|
tuple[int, int, int]
|
ARIMA order (p, d, q). |
required |
include_constant
|
bool
|
Whether to include a constant/trend term. |
required |
models
|
list
|
List of fitted statsmodels ARIMA results, one per series. |
required |
last_values
|
ndarray
|
Shape (N,) — last observed values for each series. |
required |
Source code in src/pystmomo/forecast/arima_fc.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |
fit(kt, order=(1, 1, 0), include_constant=True)
classmethod
¶
Fit independent ARIMA models to each row of kt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kt
|
ndarray
|
Shape (N, n_years) or (n_years,) for a single series. |
required |
order
|
tuple[int, int, int]
|
ARIMA (p, d, q). |
(1, 1, 0)
|
include_constant
|
bool
|
Include constant/trend term. |
True
|
Returns:
| Type | Description |
|---|---|
IndependentArima
|
|
Source code in src/pystmomo/forecast/arima_fc.py
forecast(h, level=0.95)
¶
Point forecast and confidence intervals for h steps ahead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h
|
int
|
Forecast horizon. |
required |
level
|
float
|
Confidence level (e.g. 0.95). |
0.95
|
Returns:
| Type | Description |
|---|---|
(mean, lower, upper)
|
Each shape (N, h). |
Source code in src/pystmomo/forecast/arima_fc.py
simulate(h, nsim, rng)
¶
Simulate future trajectories from fitted ARIMA models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h
|
int
|
Forecast horizon. |
required |
nsim
|
int
|
Number of simulations. |
required |
rng
|
Generator
|
NumPy random generator (used to set seed for statsmodels). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Shape (N, h, nsim). |