-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/lifelib-dev/lifelib
- Loading branch information
Showing
43 changed files
with
3,864 additions
and
1 deletion.
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
lifelib/libraries/appliedlife/IntegratedLife/Assumptions/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
"""The space representing assumptions | ||
The Assumptions space represents a set of assumptions. | ||
This space is parameterized with :attr:`asmp_id`. | ||
For each value of :attr:`asmp_id`, | ||
a dynamic subspace of this space is created, | ||
representing the specific assumption set associated | ||
with the value of :attr:`asmp_id`. | ||
.. rubric:: Parameters | ||
Attributes: | ||
asmp_id: a string ID representing an assumption set | ||
.. rubric:: References | ||
Attributes: | ||
base_data: Reference to the :mod:`~appliedlife.IntegratedLife.BaseData` space | ||
Example: | ||
The sample code below demonstrates how to examine the contents of | ||
:mod:`~appliedlife.IntegratedLife.Assumptions` | ||
for a specific value of :attr:`asmp_id`. | ||
.. code-block:: python | ||
>>> import modelx as mx | ||
>>> m = mx.read_model("IntegratedLife") | ||
>>> m.Assumptions["202312"].asmp_file() | ||
WindowsPath('C:/Users/User1/appliedlife/input_tables/assumptions_202312.xlsx') | ||
>>> m.Assumptions["202312"].lapse_tables() | ||
L001 L002 L003 L004 | ||
duration | ||
0 0.03 0.03 0.01 0.05 | ||
1 0.04 0.04 0.02 0.05 | ||
2 0.05 0.05 0.03 0.05 | ||
3 0.06 0.06 0.04 0.05 | ||
4 0.07 0.07 0.05 0.05 | ||
5 0.08 0.08 0.06 0.05 | ||
6 0.09 0.09 0.07 0.05 | ||
7 0.20 0.10 0.08 0.05 | ||
8 0.15 0.11 0.09 0.05 | ||
9 0.10 0.20 0.10 0.05 | ||
10 0.10 0.15 0.10 0.05 | ||
11 0.10 0.10 0.10 0.05 | ||
12 0.10 0.10 0.10 0.05 | ||
13 0.10 0.10 0.10 0.05 | ||
14 0.10 0.10 0.10 0.05 | ||
""" | ||
|
||
from modelx.serialize.jsonvalues import * | ||
|
||
_formula = lambda asmp_id: None | ||
|
||
_bases = [] | ||
|
||
_allow_none = None | ||
|
||
_spaces = [] | ||
|
||
# --------------------------------------------------------------------------- | ||
# Cells | ||
|
||
def asmp_file(): | ||
"""The file path to an assumption file | ||
Return a `pathlib`_ Path object representing the file path | ||
of the assumption file. | ||
The file location is specified by the constant parameter, "table_dir". | ||
The file name is constructed using a prefix and :attr:`asmp_id` | ||
concatenated by an underscore, followed by ".xlsx". | ||
.. _pathlib: https://docs.python.org/3/library/pathlib.html | ||
""" | ||
|
||
dir_ = base_data.const_params().at["table_dir", "value"] | ||
prefix = base_data.const_params().at["asmp_file_prefix", "value"] | ||
|
||
return _model.path.parent / dir_ / (prefix + "_" + asmp_id + ".xlsx") | ||
|
||
|
||
def dyn_lapse_params(): | ||
"""Dynamic lapse parameters""" | ||
return pd.read_excel( | ||
asmp_file(), | ||
sheet_name="DynLapse", | ||
index_col=0) | ||
|
||
|
||
def lapse_len(): | ||
"""Duration length of the lapse table""" | ||
return len(lapse_tables()) | ||
|
||
|
||
def lapse_tables(): | ||
"""Lapse rate assumptions""" | ||
return pd.read_excel( | ||
asmp_file(), | ||
sheet_name="Lapse", | ||
index_col=0) | ||
|
||
|
||
def mort_scalar_len(): | ||
"""Duration length of the mortality scalar table""" | ||
return len(mort_scalar_tables()) | ||
|
||
|
||
def mort_scalar_tables(): | ||
"""Mortality scalar tables""" | ||
df = pd.read_excel( | ||
asmp_file(), | ||
sheet_name="Mortality", | ||
index_col=0) | ||
return df | ||
|
||
|
||
def stacked_lapse_tables(): | ||
"""Stacked lapse tables""" | ||
return lapse_tables().stack().swaplevel(0, 1).sort_index() | ||
|
||
|
||
def stacked_mort_scalar_tables(): | ||
"""Stacked mortality scalar tables""" | ||
return mort_scalar_tables().stack().swaplevel(0, 1).sort_index() | ||
|
||
|
||
# --------------------------------------------------------------------------- | ||
# References | ||
|
||
base_data = ("Interface", ("..", "BaseData"), "auto") | ||
|
||
asmp_id = "202312" |
Empty file.
153 changes: 153 additions & 0 deletions
153
lifelib/libraries/appliedlife/IntegratedLife/BaseData/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
"""Basic parameters and data | ||
This space reads parameters from a parameter file. | ||
The parameters are then be referenced in other parts of the model. | ||
There are a few types of parameters depending on the variability of their values. | ||
Constant parameters have values that are constant anywhere in the model. | ||
Run parameters can have different values for different runs. | ||
Space parameters can have different values for different product spaces. | ||
These three types of parameters are called fixed parameters, | ||
because they have fixed values in each product space. | ||
Product parameters are specific to individual product spaces. | ||
Each product space has product parameters. | ||
The values of product parameters vary by "product_id" and "plan_id" | ||
defined for the product space. | ||
Product parameters are appended to the model point table for the product space | ||
in :func:`~appliedlife.IntegratedLife.ModelPoints.model_point_table_ext`. | ||
Other spaces reference this space as ``base_data`` as a convention. | ||
In addition to the parameters, this space also reads surrender charge tables | ||
to be used in other spaces. | ||
.. rubric:: References | ||
Attributes: | ||
parameter_file: The name of the parameter file | ||
Example: | ||
The sample code below demonstrates how to examine the contents of | ||
:mod:`~appliedlife.IntegratedLife.BaseData`. | ||
.. code-block:: python | ||
>>> import modelx as mx | ||
>>> m = mx.read_model("IntegratedLife") | ||
>>> m.BaseData.const_params() | ||
value | ||
parameter | ||
model_point_dir model_point_data | ||
mp_file_prefix model_point | ||
asmp_file_prefix assumptions | ||
table_dir input_tables | ||
scen_dir economic_data | ||
scen_param_file index_parameters.xlsx | ||
scen_file_prefix risk_free | ||
mort_file mortality_tables.xlsx | ||
spec_tables product_spec_tables.xlsx | ||
>>> m.BaseData.run_params() | ||
base_date ... description | ||
run_id ... | ||
1 2023-12-31 ... New business in Jan 2024 | ||
2 2023-12-31 ... Base run end of Dec 2023 | ||
3 2023-12-31 ... Interest rate sensitivity end of Dec 2023 | ||
4 2023-12-31 ... Interest rate sensitivity end of Dec 2023 | ||
5 2022-12-31 ... Base run end of Dec 2022 | ||
[5 rows x 6 columns] | ||
>>> m.BaseData.space_params() | ||
expense_acq expense_maint currency is_lapse_dynamic | ||
space | ||
FIA 5000 500 USD True | ||
GMXB 5000 500 USD True | ||
GLWB 6000 600 USD True | ||
>>> m.BaseData.product_params("GMXB") | ||
has_gmdb has_gmab ... dyn_lapse_param_id dyn_lapse_floor | ||
product_id plan_id ... | ||
GMDB PLAN_A True False ... DL001A 0.00 | ||
PLAN_B True False ... DL001B 0.00 | ||
GMAB PLAN_A True True ... DL002A 0.03 | ||
PLAN_B True True ... DL002B 0.05 | ||
[4 rows x 16 columns] | ||
""" | ||
|
||
from modelx.serialize.jsonvalues import * | ||
|
||
_formula = None | ||
|
||
_bases = [] | ||
|
||
_allow_none = None | ||
|
||
_spaces = [] | ||
|
||
# --------------------------------------------------------------------------- | ||
# Cells | ||
|
||
def const_params(): | ||
"""Constant parameters""" | ||
return pd.read_excel(_model.path.parent / parameter_file, | ||
sheet_name="ConstParams", | ||
index_col="parameter") | ||
|
||
|
||
def param_list(): | ||
"""List of fixed parameters""" | ||
return pd.read_excel(_model.path.parent / parameter_file, | ||
sheet_name="ParamList", | ||
index_col="parameter") | ||
|
||
|
||
def product_params(space_name: str): | ||
"""Product parameters""" | ||
return pd.read_excel(_model.path.parent / parameter_file, | ||
sheet_name=space_name, | ||
index_col=[0, 1]) | ||
|
||
|
||
def run_params(): | ||
"""Run parameters""" | ||
return pd.read_excel(_model.path.parent / parameter_file, | ||
sheet_name="RunParams", | ||
index_col="run_id", | ||
dtype={"date_id": object, "asmp_id": object}) | ||
|
||
|
||
def space_params(): | ||
"""Space parameters""" | ||
return pd.read_excel(_model.path.parent / parameter_file, | ||
sheet_name="SpaceParams", | ||
index_col="space") | ||
|
||
|
||
def stacked_surr_charge_tables(): | ||
"""Stacked surrender charge tables""" | ||
return surr_charge_tables().stack().swaplevel(0, 1).sort_index() | ||
|
||
|
||
def surr_charge_len(): | ||
"""Duration length of the surrender charge table""" | ||
return len(surr_charge_tables()) | ||
|
||
|
||
def surr_charge_tables(): | ||
"""Surrender charge tables""" | ||
dir_ = _model.path.parent / const_params().at["table_dir", "value"] | ||
file = const_params().at["spec_tables", "value"] | ||
return pd.read_excel(dir_ / file, sheet_name="SurrCharge", index_col=0) | ||
|
||
|
||
# --------------------------------------------------------------------------- | ||
# References | ||
|
||
parameter_file = "model_parameters.xlsx" |
Oops, something went wrong.