Referência da API
Nesta seção você encontra a documentação detalhada das funções e classes gerada automaticamente a partir do código fonte (Docstrings em Inglês).
Módulo Actuarial
pylifecontingencies.actuarial
Single-life actuarial present values.
All functions accept an ActuarialTable as first argument and return floats, matching the R lifecontingencies interface exactly.
Formulas use commutation columns from ActuarialTable for integer k=1 cases. For k>1 (m-thly payments) the UDD approximation is applied.
AExn(at, x, n, k=1)
Endowment insurance A_{x:n|} = A^1_{x:n|} + _n E_x.
Pays 1 at end of year of death (if within n years) or at age x+n.
Source code in src/pylifecontingencies/actuarial.py
129 130 131 132 133 134 135 136 137 138 139 140 | |
Axn(at, x, n=None, k=1)
Actuarial present value of a term (or whole-life) insurance.
Pays 1 at end of year of death if death occurs within n years. If n is None or inf, a whole-life insurance is returned.
Parameters
k : int For k>1: A^(k) = (i / i^(k)) * A under UDD.
Source code in src/pylifecontingencies/actuarial.py
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 | |
DAxn(at, x, n, k=1)
Decreasing term insurance (DA)^1_{x:n|}.
Pays (n-k) if death occurs in year k+1 (k = 0, …, n-1). Identity: (DA)^1 + (IA)^1 = (n+1) A^1_{x:n|}.
Source code in src/pylifecontingencies/actuarial.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
Exn(at, x, n)
n-year pure endowment n E_x = v^n * _n p_x = D{x+n} / D_x.
Parameters
at : ActuarialTable x : int Attained age. n : int Term in years.
Source code in src/pylifecontingencies/actuarial.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
IAxn(at, x, n=None, k=1)
Increasing insurance (IA)_{x:n|}.
Pays (k+1) if death occurs in year k+1 (k = 0, 1, …, n-1). For whole-life (n=None): (IA)x = R_x / D_x. For term: (IA)^1{x:n|} = (R_x - R_{x+n} - n M_{x+n}) / D_x.
k parameter: k>1 uses UDD scaling as for Axn.
Source code in src/pylifecontingencies/actuarial.py
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 177 178 179 180 181 182 | |
axn(at, x, n=None, k=1)
Actuarial present value of a life annuity-due.
Parameters
at : ActuarialTable
x : int
Attained age.
n : float or None
Term in years. None (default) = whole-life.
k : int
Payment frequency per year (k=1 annual-due, k=2 semi-annual, …).
For k>1 the UDD approximation is used.
Source code in src/pylifecontingencies/actuarial.py
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 | |
exn(at, x, n=None)
Curtate future lifetime expectation e_x (or temporary e_{x:n|}).
e_x = (l_{x+1} + l_{x+2} + ... + l_omega) / l_x — no discounting. e_{x:n|} = sum_{k=1}^{n} _k p_x.
Note: N_{x+1}/D_x gives the interest-discounted annuity-immediate, NOT e_x.
Source code in src/pylifecontingencies/actuarial.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | |
Tábua Atuarial
pylifecontingencies.actuarialtable
ActuarialTable
Life table combined with an interest rate.
Provides commutation columns (Dx, Nx, Cx, Mx, Rx) and delegates to the module-level actuarial functions (axn, Axn, …) via convenience methods.
Parameters
lifetable : LifeTable interest : float or InterestRate Annual effective interest rate (float) or InterestRate instance. name : str Optional label.
Source code in src/pylifecontingencies/actuarialtable.py
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 177 178 179 180 181 | |
simulate_pv(x, n=None, benefit='term', n_sim=10000, random_state=None, k=1, m=0)
Monte Carlo present-value distribution for a static life-contingent benefit.
Source code in src/pylifecontingencies/actuarialtable.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | |
Modelos Dinâmicos
pylifecontingencies.dynamic
CBD
Cairns-Blake-Dowd (CBD) M5 two-factor mortality model.
Usage
::
cbd = CBD().fit(rates)
forecast = cbd.forecast(horizon=30, ci=0.95)
Source code in src/pylifecontingencies/dynamic/cbd.py
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 177 178 179 180 181 182 183 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 | |
fit(rates)
Fit the CBD M5 model.
Parameters
rates : MortalityRates Log mortality rate surface. The model internally works with q_x values derived from m_x via UDD.
Source code in src/pylifecontingencies/dynamic/cbd.py
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 | |
forecast(horizon=30, n_bootstrap=0, ci=0.95, seed=None)
Forecast κ^1_t and κ^2_t as a bivariate random walk with drift.
Drift and covariance are estimated from the historical differences Δκ_t = κ_t - κ_{t-1}.
Source code in src/pylifecontingencies/dynamic/cbd.py
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 177 178 179 180 181 182 183 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 | |
CBDForecast
Forecast object returned by CBD.forecast().
Source code in src/pylifecontingencies/dynamic/cbd.py
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 | |
mx(year)
Convert q_x to m_x via UDD approximation.
Source code in src/pylifecontingencies/dynamic/cbd.py
70 71 72 73 | |
qx(year)
Predicted q_x for a given year.
Source code in src/pylifecontingencies/dynamic/cbd.py
65 66 67 68 | |
DynamicActuarialTable
Actuarial table built on top of a ProjectedLifeTable or DynamicLifeTable.
Wraps one or more ActuarialTable instances (one per scenario) and
exposes the same EPV API. Returns float for single-path tables
and StochasticResult for stochastic/PI tables.
Parameters
source : ProjectedLifeTable, DynamicLifeTable, or LifeTable list interest : float or InterestRate Annual effective interest rate.
Examples
From a ProjectedLifeTable::
plt = ProjectedLifeTable.from_mx(df, birth_year=1985)
dat = DynamicActuarialTable(plt, i=0.03)
dat.axn(x=40) # → float
With prediction intervals::
plt = ProjectedLifeTable.from_mx(
df, lower=df_lo, upper=df_hi, birth_year=1985
)
dat = DynamicActuarialTable(plt, i=0.03)
r = dat.axn(x=40) # → StochasticResult (3 values: lo, central, hi)
Stochastic (N scenarios)::
plt = ProjectedLifeTable.from_scenarios(paths, birth_year=1985)
dat = DynamicActuarialTable(plt, i=0.03)
r = dat.axn(x=40)
r.mean, r.std, r.ci(0.95) # → StochasticResult
Source code in src/pylifecontingencies/dynamic/dynamic_actuarialtable.py
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 177 178 179 180 181 182 183 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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | |
AExn(x, n, k=1)
Endowment insurance A_{x:n|}.
Source code in src/pylifecontingencies/dynamic/dynamic_actuarialtable.py
171 172 173 174 | |
Axn(x, n=None, k=1)
Term (or whole-life) insurance A^1_{x:n|}.
Source code in src/pylifecontingencies/dynamic/dynamic_actuarialtable.py
165 166 167 168 169 | |
DAxn(x, n, k=1)
Decreasing term insurance (DA)^1_{x:n|}.
Source code in src/pylifecontingencies/dynamic/dynamic_actuarialtable.py
182 183 184 185 | |
Exn(x, n)
n-year pure endowment _n E_x.
Source code in src/pylifecontingencies/dynamic/dynamic_actuarialtable.py
138 139 140 141 | |
IAxn(x, n=None, k=1)
Increasing insurance (IA)_{x:n|}.
Source code in src/pylifecontingencies/dynamic/dynamic_actuarialtable.py
176 177 178 179 180 | |
axn(x, n=None, k=1)
Actuarial present value of a life annuity-due ä_{x:n|}.
Parameters
x : int Attained age. n : float or None Term (None = whole-life). k : int Payment frequency per year.
Source code in src/pylifecontingencies/dynamic/dynamic_actuarialtable.py
147 148 149 150 151 152 153 154 155 156 157 158 159 | |
exn(x, n=None)
Curtate future lifetime expectation e_x (or temporary e_{x:n|}).
Source code in src/pylifecontingencies/dynamic/dynamic_actuarialtable.py
191 192 193 194 195 | |
gross_premium(x, n=None, k=1, benefit='term', expense_ratio=0.0, per_policy=0.0)
Gross premium including expense loading.
Source code in src/pylifecontingencies/dynamic/dynamic_actuarialtable.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
net_premium(x, n=None, k=1, benefit='term')
Net (benefit) premium via the equivalence principle.
Source code in src/pylifecontingencies/dynamic/dynamic_actuarialtable.py
201 202 203 204 205 206 207 208 209 210 211 212 213 | |
prospective_reserve(x, n, t, k=1, benefit='term', premium=None)
Prospective net reserve _t V at duration t.
Source code in src/pylifecontingencies/dynamic/dynamic_actuarialtable.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
reserve_recursion(x, n, k=1, benefit='term')
Full reserve vector [_0 V, _1 V, ..., _n V].
Returns a list of floats (single-path) or a list of StochasticResult (stochastic), one element per duration t = 0, ..., n.
Source code in src/pylifecontingencies/dynamic/dynamic_actuarialtable.py
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 | |
DynamicLifeTable
A life table (or collection of life tables) built from an external mortality-rate forecast.
Parameters
tables : list[LifeTable]
One LifeTable per scenario. Single-path tables have len(tables) == 1.
is_stochastic : bool
True when multiple scenarios are stored.
See Also
DynamicActuarialTable : wraps this with an interest rate and computes EPVs.
Source code in src/pylifecontingencies/dynamic/dynamic_lifetable.py
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 177 178 179 180 181 182 183 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 286 287 288 289 290 291 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 | |
lifetable
property
The single LifeTable — raises if stochastic.
from_forecast_log_mx(df_log_mx, *, birth_year=None, period_year=None, fractional=FractionalAge.UDD, name='')
classmethod
Build a single-path DynamicLifeTable from log central death rates log(m_x).
This is the natural output of Lee-Carter and similar log-linear models.
Source code in src/pylifecontingencies/dynamic/dynamic_lifetable.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
from_forecast_mx(df_mx, *, birth_year=None, period_year=None, fractional=FractionalAge.UDD, name='')
classmethod
Build a single-path DynamicLifeTable from central death rates m_x.
Parameters
df_mx : pd.DataFrame Ages as index (int), calendar years as columns (int), values are central death rates m_{x,t}. birth_year : int or None Extract cohort diagonal (age x → year birth_year + x). period_year : int or None Extract a period column (all ages from the same year). fractional : FractionalAge Fractional-age assumption for the resulting LifeTable. name : str Optional label for the LifeTable.
Examples
::
dlt = DynamicLifeTable.from_forecast_mx(df_mx, birth_year=1985)
dat = DynamicActuarialTable(dlt, i=0.03)
dat.axn(x=40)
Source code in src/pylifecontingencies/dynamic/dynamic_lifetable.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
from_forecast_qx(df_qx, *, birth_year=None, period_year=None, fractional=FractionalAge.UDD, name='')
classmethod
Build a single-path DynamicLifeTable from mortality rates q_x.
Parameters
df_qx : pd.DataFrame Ages as index (int), calendar years as columns (int), values are annual mortality rates q_{x,t}.
Source code in src/pylifecontingencies/dynamic/dynamic_lifetable.py
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 | |
from_scenarios(scenarios, *, birth_year=None, period_year=None, input_type='mx', fractional=FractionalAge.UDD)
classmethod
Build a stochastic DynamicLifeTable from multiple forecast paths.
Parameters
scenarios : sequence of pd.DataFrame Each DataFrame is a (ages × years) forecast surface. All must share the same index (ages) and columns (years). input_type : {'mx', 'qx', 'log_mx'} What the DataFrame values represent.
Examples
::
# 500 bootstrap paths
dlt = DynamicLifeTable.from_scenarios(list_of_dfs, birth_year=1985)
dat = DynamicActuarialTable(dlt, i=0.03)
result = dat.axn(x=40) # StochasticResult
print(result.mean, result.std, result.ci(0.95))
Source code in src/pylifecontingencies/dynamic/dynamic_lifetable.py
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
from_scenarios_array(arr, *, ages, years, birth_year=None, period_year=None, input_type='mx', fractional=FractionalAge.UDD)
classmethod
Build a stochastic DynamicLifeTable from a 3-D numpy array.
Parameters
arr : np.ndarray, shape (n_samples, n_ages, n_years) Mortality-rate cube (central rates, raw rates, or log rates). ages : array-like of int Age labels for axis 1. years : array-like of int Year labels for axis 2. input_type : {'mx', 'qx', 'log_mx'}
Examples
::
# arr shape: (500, 80, 50) — 500 scenarios, ages 20-99, years 2025-2074
dlt = DynamicLifeTable.from_scenarios_array(
arr, ages=range(20, 100), years=range(2025, 2075), birth_year=1985
)
Source code in src/pylifecontingencies/dynamic/dynamic_lifetable.py
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 | |
LeeCarter
Lee-Carter mortality model.
Usage
::
lc = LeeCarter().fit(rates) # rates: MortalityRates
forecast = lc.forecast(horizon=30, n_bootstrap=500, ci=0.95)
Source code in src/pylifecontingencies/dynamic/leecarter.py
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 177 178 179 180 181 182 183 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 | |
fit(rates)
Fit the Lee-Carter model by SVD.
Parameters
rates : MortalityRates Log mortality rates surface (ages × years).
Source code in src/pylifecontingencies/dynamic/leecarter.py
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 | |
fitted_log_mx()
Fitted log(m_{x,t}) = α_x + β_x κ_t.
Source code in src/pylifecontingencies/dynamic/leecarter.py
151 152 153 154 155 | |
forecast(horizon=30, arima_order=(0, 1, 0), n_bootstrap=0, ci=0.95, seed=None)
Forecast kt forward using ARIMA and return a LeeCarterForecast.
Parameters
horizon : int Number of years ahead. arima_order : (p, d, q) ARIMA order for kt. Default (0,1,0) = random walk with drift. n_bootstrap : int Number of bootstrap samples for prediction intervals. 0 = no PI. ci : float Confidence level for prediction intervals (e.g. 0.95). seed : int or None Random seed for bootstrap reproducibility.
Source code in src/pylifecontingencies/dynamic/leecarter.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 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 | |
LeeCarterForecast
Forecast object returned by LeeCarter.forecast().
Source code in src/pylifecontingencies/dynamic/leecarter.py
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 | |
log_mx(year)
Predicted log(m_x) for a given forecast year.
Source code in src/pylifecontingencies/dynamic/leecarter.py
51 52 53 54 55 56 57 58 59 | |
qx(year)
Approximate q_x from m_x via UDD: q_x = m_x / (1 + 0.5 m_x).
Source code in src/pylifecontingencies/dynamic/leecarter.py
64 65 66 67 | |
to_dataframe()
Return a long-format DataFrame (year, age, log_mx_central, log_mx_lower, log_mx_upper).
Source code in src/pylifecontingencies/dynamic/leecarter.py
69 70 71 72 73 74 75 76 77 78 | |
MortalityRates
Mortality rate surface: log(m_{x,t}) indexed by (age, year).
The underlying data is a DataFrame with ages as the index and calendar years as columns.
Parameters
data : pd.DataFrame log(m_{x,t}) with integer ages as index and integer years as columns. label : str Optional description (e.g. "USA Female 1950-2020").
Source code in src/pylifecontingencies/dynamic/rates.py
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 | |
log_mx
property
log(mx) matrix, shape (n_ages, n_years).
mx
property
Central death rates m_x, shape (n_ages, n_years).
from_dataframe(df, label='')
classmethod
Construct from a DataFrame already shaped (ages × years) of log(mx).
Index should be integer ages; columns should be integer years.
Source code in src/pylifecontingencies/dynamic/rates.py
38 39 40 41 42 43 44 45 46 47 48 49 | |
from_hmd_file(path, *, sex='Female', label='')
classmethod
Load from an HMD-format Mx (central death rates) flat text file.
HMD files have columns: Year, Age, Female, Male, Total. Download from https://www.mortality.org (requires free account).
Source code in src/pylifecontingencies/dynamic/rates.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
from_mx_dataframe(df, label='')
classmethod
Construct from a (ages × years) DataFrame of raw m_x values (not log).
Source code in src/pylifecontingencies/dynamic/rates.py
51 52 53 54 55 56 57 58 59 | |
from_qx_dataframe(df, label='')
classmethod
Construct from a (ages × years) DataFrame of q_x values.
Source code in src/pylifecontingencies/dynamic/rates.py
61 62 63 64 65 66 67 68 69 | |
subset(ages=None, years=None)
Return a sub-surface restricted to the given ages and/or years.
Source code in src/pylifecontingencies/dynamic/rates.py
118 119 120 121 122 123 124 125 126 127 128 129 | |
to_dataframe()
Return the underlying DataFrame (ages × years).
Source code in src/pylifecontingencies/dynamic/rates.py
135 136 137 | |
ProjectedLifeTable
Model-agnostic life table built from any mortality-rate forecast.
Stores one or more LifeTable objects internally:
- Single-path: one table (central forecast).
- With prediction intervals: three tables (lower, central, upper).
- Stochastic: N tables (one per scenario).
Parameters
forecast : object with .qx(year), .ages, .years_calib, .years_forecast
A model forecast object (LeeCarterForecast, CBDForecast, or compatible).
For raw DataFrame input, use the from_mx / from_qx class methods instead.
birth_year : int or None
Birth year for cohort extraction.
period_year : int or None
Calendar year for period extraction.
ages : list[int] or None
Ages to include. Defaults to all available ages.
fractional : FractionalAge
Fractional-age assumption for the resulting LifeTable(s).
extrapolation : str
"clamp", "constant_slope", or "none".
Source code in src/pylifecontingencies/dynamic/projected_table.py
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 177 178 179 180 181 182 183 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 286 287 288 289 290 291 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 429 430 431 432 433 434 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 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 | |
has_prediction_interval
property
True when lower/central/upper prediction interval tables are stored.
is_stochastic
property
True when multiple tables are stored (PI or scenarios).
lifetable
property
The central (or only) LifeTable.
For single-path: the sole table.
For PI: the central table (index 1 of [lower, central, upper]).
For scenarios: raises ValueError (use .tables instead).
lower
property
Lower prediction interval LifeTable, or None.
n_scenarios
property
Number of scenarios / tables stored.
tables
property
All underlying LifeTable objects.
upper
property
Upper prediction interval LifeTable, or None.
from_log_mx(df_log_mx, *, lower=None, upper=None, birth_year=None, period_year=None, ages=None, fractional=FractionalAge.UDD, extrapolation='clamp')
classmethod
Build from a DataFrame of log(m_{x,t}).
The natural output of Lee-Carter and similar log-linear models.
Source code in src/pylifecontingencies/dynamic/projected_table.py
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
from_mx(df_mx, *, lower=None, upper=None, birth_year=None, period_year=None, ages=None, fractional=FractionalAge.UDD, extrapolation='clamp')
classmethod
Build from a DataFrame of central death rates m_{x,t}.
Parameters
df_mx : pd.DataFrame Ages as index (int), calendar years as columns (int), values are central death rates m_{x,t}. lower : pd.DataFrame or None Lower prediction interval (same shape as df_mx). upper : pd.DataFrame or None Upper prediction interval (same shape as df_mx). birth_year, period_year, ages, fractional, extrapolation : See class docstring.
Examples
::
# Central only
plt = ProjectedLifeTable.from_mx(df_mx, birth_year=1985)
# With 95% prediction intervals
plt = ProjectedLifeTable.from_mx(
df_central, lower=df_lo, upper=df_hi, birth_year=1985
)
Source code in src/pylifecontingencies/dynamic/projected_table.py
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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
from_qx(df_qx, *, lower=None, upper=None, birth_year=None, period_year=None, ages=None, fractional=FractionalAge.UDD, extrapolation='clamp')
classmethod
Build from a DataFrame of mortality rates q_{x,t}.
Parameters
df_qx : pd.DataFrame Ages as index (int), calendar years as columns (int), values are annual mortality rates q_{x,t}. lower, upper : pd.DataFrame or None Prediction interval bounds.
Source code in src/pylifecontingencies/dynamic/projected_table.py
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 | |
from_scenarios(scenarios, *, birth_year=None, period_year=None, input_type='mx', ages=None, fractional=FractionalAge.UDD, extrapolation='clamp')
classmethod
Build from multiple forecast paths (stochastic analysis).
Parameters
scenarios : sequence of pd.DataFrame Each DataFrame is an (ages × years) forecast surface. input_type : {'mx', 'qx', 'log_mx'} What the DataFrame values represent.
Examples
::
# 500 bootstrap paths
plt = ProjectedLifeTable.from_scenarios(
list_of_dfs, birth_year=1985, input_type="mx"
)
dat = DynamicActuarialTable(plt, i=0.03)
result = dat.axn(x=40) # StochasticResult
Source code in src/pylifecontingencies/dynamic/projected_table.py
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
from_scenarios_array(arr, *, ages, years, birth_year=None, period_year=None, input_type='mx', fractional=FractionalAge.UDD, extrapolation='clamp')
classmethod
Build from a 3-D numpy array (n_samples × n_ages × n_years).
Parameters
arr : np.ndarray, shape (n_samples, n_ages, n_years) ages : array-like of int years : array-like of int
Source code in src/pylifecontingencies/dynamic/projected_table.py
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 | |
to_life_table(name='')
Return the central LifeTable (backward compatibility).
Identical to .lifetable for single-path and PI tables.
Source code in src/pylifecontingencies/dynamic/projected_table.py
458 459 460 461 462 463 464 465 466 467 | |
StochasticResult
dataclass
Distribution of a scalar actuarial quantity across N scenarios.
Returned by DynamicActuarialTable methods when the table was built from multiple forecast paths.
Attributes
samples : np.ndarray
1-D array of length N (one value per scenario).
label : str
Optional label (e.g. "axn(x=40, n=25)").
Source code in src/pylifecontingencies/dynamic/stochastic.py
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 177 178 179 180 181 182 183 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 | |
n
property
Number of scenarios.
__float__()
Coerce to float via the mean — allows use in scalar contexts.
Source code in src/pylifecontingencies/dynamic/stochastic.py
210 211 212 | |
ci(level=0.95)
Symmetric (1-level)/2 credible interval: (lower, upper).
Source code in src/pylifecontingencies/dynamic/stochastic.py
70 71 72 73 | |
hist(bins=50, ax=None, ci=True, **kwargs)
Histogram of the PV distribution.
Parameters
bins : int
Number of histogram bins (default 50).
ax : matplotlib Axes or None
Axes to draw on; a new figure is created when None.
ci : bool
If True, draw vertical lines for the mean and 95 % CI.
**kwargs
Forwarded to ax.hist().
Returns
matplotlib.axes.Axes
Source code in src/pylifecontingencies/dynamic/stochastic.py
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 | |
plot(ax=None, **kwargs)
Empirical CDF of the PV distribution.
Draws the step-wise ECDF and a dashed vertical line at the mean.
Parameters
ax : matplotlib Axes or None
Axes to draw on; a new figure is created when None.
**kwargs
Forwarded to ax.plot().
Returns
matplotlib.axes.Axes
Source code in src/pylifecontingencies/dynamic/stochastic.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
quantile(q)
Return the q-th quantile (0 ≤ q ≤ 1).
Source code in src/pylifecontingencies/dynamic/stochastic.py
66 67 68 | |
summary()
Return a dict with key percentiles — useful for quick inspection.
Source code in src/pylifecontingencies/dynamic/stochastic.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
to_dataframe()
Return samples as a one-column pandas.DataFrame with column 'pv'.
Source code in src/pylifecontingencies/dynamic/stochastic.py
110 111 112 113 | |
tvar(alpha)
Tail Value at Risk (Expected Shortfall) at confidence level alpha.
TVaR_α = E[X | X > VaR_α]
Returns var(alpha) when no sample exceeds it (degenerate tail).
Source code in src/pylifecontingencies/dynamic/stochastic.py
98 99 100 101 102 103 104 105 106 107 108 | |
var(alpha)
Value at Risk at confidence level alpha.
Equal to the alpha-quantile of the PV distribution.
Source code in src/pylifecontingencies/dynamic/stochastic.py
90 91 92 93 94 95 96 | |