Skip to content

Commit

Permalink
add life table
Browse files Browse the repository at this point in the history
  • Loading branch information
longbui committed Nov 2, 2023
1 parent 7777859 commit c051386
Showing 1 changed file with 145 additions and 0 deletions.
145 changes: 145 additions & 0 deletions notebooks/user/vbui/demo.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import warnings\n",
"import pandas as pd\n",
"import plotly.graph_objects as go\n",
"import plotly.express as px\n",
"\n",
"from autumn.core.inputs.tb_camau import queries\n",
"from autumn.core.inputs import get_death_rates_by_agegroup, get_life_expectancy_by_agegroup, get_crude_birth_rate\n",
"import pathlib"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"warnings.filterwarnings(\"ignore\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"age_string_map = {\n",
" 0: \"0-4\",\n",
" 5: \"5-14\",\n",
" 15: \"15-34\",\n",
" 35: \"35-49\",\n",
" 50: \"50+\",\n",
"}\n",
"age_breakpoints = [0,5,15,35,50]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def get_death_rates_by_year(year):\n",
" death_rates_from_db =get_death_rates_by_agegroup(age_breakpoints, \"VNM\")\n",
" df = pd.DataFrame(death_rates_from_db[0], index=death_rates_from_db[1])\n",
" if year not in death_rates_from_db[1]:\n",
" print(\"No data for this year!\")\n",
" else:\n",
" return(df.loc[year]) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"death_series = get_death_rates_by_year(1952.5)\n",
"death_series"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"init_pop = 100000"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame({\n",
" 'age': death_series.index,\n",
" 'qx': death_series.values\n",
"})\n",
"\n",
"# Initialize the 'lx' column with an initial population of 100,000 for age 0\n",
"df['lx'] = 100000\n",
"\n",
"# Calculate population 'lx' for each age group\n",
"for i in range(1, len(df)):\n",
" df.loc[i, 'lx'] = df.loc[i - 1, 'lx'] - (df.loc[i - 1, 'qx'] * df.loc[i - 1, 'lx'])\n",
"\n",
"#calculate number of death\n",
"df[\"dx\"] = 0\n",
"for x in range(5):\n",
" if x >=4:\n",
" df[\"dx\"][x] = df[\"lx\"][x]\n",
" else:\n",
" df['dx'] = df['lx'] * df['qx']\n",
"\n",
"#μx: is the force of mortality, i.e., represents the instantaneous rate at which people are dying.\n",
"df[\"μx\"] = 0\n",
"df[\"μx\"][0] = 2.0692602739726 * df[\"dx\"][0] /(2*df[\"lx\"][0])\n",
"for x in range(1,5):\n",
" df[\"μx\"][x] = (df[\"dx\"][x-1]+df[\"dx\"][x])/(2*df[\"lx\"][x])\n",
"#Tx: is the total years of life to be lived by those aged exactly x (not the random variable Tx) until they all die\n",
"df[\"Tx\"] = 0\n",
"for x in range(5):\n",
" df[\"Tx\"][x] = 0\n",
" for y in range(x,5):\n",
" df[\"Tx\"][x] += df[\"lx\"][y]\n",
"df[\"e0x\"] = df[\"Tx\"]/df[\"lx\"] - 0.5\n",
"\n",
"df[\"Lx\"] = 0.000\n",
"for x in range(5):\n",
" df[\"Lx\"][x] = df[\"Tx\"][x]-df[\"lx\"][x]\n",
"df"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "autumn310",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit c051386

Please sign in to comment.