<jupyter_start><jupyter_text>Question:How does increased life expectancy impact population and age demographics?<jupyter_code># Configure Jupyter so figures appear in the notebook
%matplotlib inline

# Configure Jupyter to display the assigned value after an assignment
%config InteractiveShell.ast_node_interactivity='last_expr_or_assign'

# import functions from the modsim.py module
from modsim import *
from pandas import read_csv
filename = 'project1data1.csv'
tables = read_csv(filename, header=0, index_col=0, decimal='M')
def plot_results( un, timeseries, title):
    """Plot the estimates and the model.
    
    census: TimeSeries of population estimates
    un: TimeSeries of population estimates
    timeseries: TimeSeries of simulation results
    title: string
    """
    plot(un, '--', label='UN DESA')
    plot(timeseries, color='gray', label='model')
    
    decorate(xlabel='Year', 
             ylabel='World population (billion)',
             title=title)
life_expectancy = tables._expectancyx1000/1000
birth_rate = tables.birthrate10000/10000
fer_rate = tables.fer_ratex1000/1000;

def plot_results(birth_rate, timeseries, title):
    """Plot the estimates and the model.
    
    census: TimeSeries of population estimates
    un: TimeSeries of population estimates
    timeseries: TimeSeries of simulation results
    title: string
    """
    plot(birth_rate, '--', label='UN DESA')
    plot(timeseries, color='gray', label='model')
    
    decorate(xlabel='Year', 
             ylabel='World population (billion)',
             title=title)
birth_rate = tables.birthrate10000/10000
empty = TimeSeries()
plot_results(birth_rate, empty, 'life expectancy')
    
decorate(xlabel='Year', 
             ylabel='birth_rate',
             title='birth_rate')
def plot_results( life_expectancy, timeseries, title):
    """Plot the estimates and the model.
    
    census: TimeSeries of population estimates
    un: TimeSeries of population estimates
    timeseries: TimeSeries of simulation results
    title: string
    """
    plot(life_expectancy, '--', label='UN DESA')
    plot(timeseries, color='gray', label='model')
    
    decorate(xlabel='Year', 
             ylabel='World population (billion)',
             title=title)
life_expectancy= tables._expectancyx1000/1000
empty = TimeSeries()
plot_results(life_expectancy, empty, 'life expectancy')
    
decorate(xlabel='Year', 
             ylabel='Life Expectancy',
             title='life_expectancy')
un = tables.UN / 1e9
empty = TimeSeries()
plot_results(un, empty, 'UN DESA')
    
decorate(xlabel='Year', 
             ylabel='World population (billion)',
             title='Estimated world population')

young = get_first_value(un) * 0.378
old= get_first_value(un) * 0.2
middle = get_first_value(un)*0.422
init = State(young=young, middle=middle, old=old)

#death_rate = 1/((life_expectancy)-old_age)
birth_rate = fer_rate/(31*2);

#children per woman in her fer age per year
system = System(mature_rate = 1/19,
                elder_rate = 1/(44-20),
                t_0 = 1965,
                t_end = 2016,
                init=init)
def update_func1(state, t, system):
    
    if(t>=1965 and t < 1992):
        old_age=44
    if (t>=1992):
        old_age=54
    death_rate = 1/((life_expectancy)-old_age)
    birth_rate2 = birth_rate[t]
    births = birth_rate2*state.middle
    system.elder_rate = 1/(old_age-19)
    maturings = system.mature_rate * state.young
    agings = system.elder_rate * state.middle
    death_rate = 1/(life_expectancy-old_age)
    
    deaths = death_rate * state.old
    young = state.young + births - maturings
    middle = state.middle + maturings - agings
    old = state.old + agings - deaths
    #print(death_rate,deaths)
    
    return State(young=young, middle=middle, old=old)
    
state = update_func1(init, system.t_0, system)
state = update_func1(state, system.t_0, system)
def run_simulation(system, update_func):
    """Simulate the system using any update function.
    
    init: initial State object
    system: System object
    update_func: function that computes the population next year
    
    returns: TimeSeries
    """
    results = TimeSeries()
    
    state = system.init
    results[system.t_0] = state.young + state.middle + state.old
    
    for t in linrange(system.t_0, system.t_end):
        state = update_func(state, t, system)
        results[t+1] = state.young + state.middle + state.old
        
    return results
results = run_simulation(system, update_func1);
plot_results(un, results, 'World population estimates')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10366514855779657, 'fraction_numerical': 0.02450661461721969, 'mean_word_length': 3.3386641580432737, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '299111', 'license': 'MIT', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1388, 'n_words': 362, 'path': 'code/Project1(fer1).ipynb', 'repo_name': 'Alexis-Xinyi/ModSimPy', 'size': '4611'}