<jupyter_start><jupyter_text>ModSim project 1<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
from math import exp
table = read_csv('data.csv',index_col=0);

plot(table.Population,':', label='Bee Population')
decorate(xlabel='Year', 
             ylabel='Bee population (Million)',
             title='Bee population over time')<jupyter_output><empty_output><jupyter_text>Will bees go exticnt in the US? Since 1947 the number of Honey Bees colonies have been decreasing. One of the posible reasons for this sudden decline could be the increased use of insecticides in agrigulture. Honey Bees are a main natural pollinator that farmers acknowledge as good, but are inadvertly the subjects of the toxic insecticides. To explore this question we will develop a quadratic model that predicts the number of Honey Bee colonies in the United States. Specifically, we will add one new feature to the model1. Insecticide Usage: The current model doesn't account for the change in insectide usage. We will use this feature to more accurately predict the number of bee colonies. We will begin our model with the data from 1943, but begin modeling in 1947 (when the number of colonies began to decrease).<jupyter_code>population = table.Population<jupyter_output><empty_output><jupyter_text>We are going to create a system that will hold the parameters of our model.<jupyter_code>t_0 = 1947;
t_end = 2200;
p_0 = population[1947];
system = System(t_0=t_0, t_end=t_end, p_0=p_0, alpha = -0.014);<jupyter_output><empty_output><jupyter_text>We created an update function so we compute the population for the following year given the current population state.<jupyter_code>def update_func(pop, t, system):
    """Compute the population next year.
    
    pop: current population
    t: current year
    system: system object containing parameters of the model
    
    returns: population next year
    """
    net_growth = system.alpha  * pop
    return pop + net_growth<jupyter_output><empty_output><jupyter_text>Following the update function, we created a run_simulation to establish a time series of the population.<jupyter_code>def run_simulation(system, update_func):
    """Simulate the system using any update function.
    
    system: System object
    update_func: function that computes the population next year
    
    returns: TimeSeries
    """
    results = TimeSeries()
    results[system.t_0] = system.p_0
    
    for t in linrange(system.t_0, system.t_end):
        results[t+1] = update_func(results[t], t, system)
        
    return results<jupyter_output><empty_output><jupyter_text>We will now plot the imported data along side the model created by the data<jupyter_code>def plot_results(population, 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(timeseries, color='gray', label='model')
    
    plot(population,':', label='Bee Population')
    decorate(xlabel='Year', 
             ylabel='Bee population (Million)',
             title='Bee population over time')
results = run_simulation(system, update_func)
plot_results(population, results, 'Quadratic model')<jupyter_output><empty_output><jupyter_text>The model does not fit the data perfectly because we did not account for the change in pesticide usage over the years. We will import and plot the data for the expenditures on pesticides in the United States.<jupyter_code>table2 = read_csv('pestdata.csv',index_col=0);
plot(table2.Dollars,':', label='Dollars spend on Pesticides')
decorate(xlabel='Year', 
             ylabel='Dollars (Billion)',
             title='Dollars spend on Pesticides over time')<jupyter_output><empty_output><jupyter_text>Based on the correlation between the pesticide usage and decline in Honey Bee numbers, we will create another update function that will take into account the change in pesticide usage. We will use if statements to establish the boundaries for the death rate.<jupyter_code>def update_func2(pop, t, system):
    """Compute the population next year.
    
    pop: current population
    t: current year
    system: system object containing parameters of the model
    
    returns: population next year
    """
    if t<1972:
        net_growth = system.alpha  * pop
        return pop + net_growth
    if 1972<=t <1981:
        net_growth = system.alpha+0.004  * pop
        return pop + net_growth
    if 1981 <=t < 1997:
        net_growth = system.alpha-0.026 * pop
        return pop + net_growth
    if 1997<= t <2125:
        net_growth = system.alpha- 0.005 *pop
        return pop + net_growth
    
results = run_simulation(system, update_func2)
plot_results(population, results, 'Quadratic model')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07338017174082748, 'fraction_numerical': 0.01678376268540203, 'mean_word_length': 4.1715438950555, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 33, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '587613', 'license': 'MIT', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1422, 'n_words': 547, 'path': 'code/Project1.4.ipynb', 'repo_name': 'akriezis/ModSimPy', 'size': '5124'}