%matplotlib inline

from modsim import *

"""
Explanation: Modeling and Simulation in Python
Rabbit example
Copyright 2017 Allen Downey
License: Creative Commons Attribution 4.0 International
End of explanation
"""


system = System(t0 = 0, 
                t_end = 10,
                adult_pop0 = 10,
                birth_rate = 0.9,
                death_rate = 0.5)

system

"""
Explanation: Rabbit Redux
This notebook starts with a version of the rabbit population growth model and walks through some steps for extending it.
In the original model, we treat all rabbits as adults; that is, we assume that a rabbit is able to breed in the season after it is born.  In this notebook, we extend the model to include both juvenile and adult rabbits.
As an example, let's assume that rabbits take 3 seasons to mature.  We could model that process explicitly by counting the number of rabbits that are 1, 2, or 3 seasons old.  As an alternative, we can model just two stages, juvenile and adult.  In the simpler model, the maturation rate is 1/3 of the juveniles per season.
To implement this model, make these changes in the System object:


Before you make any changes, run all cells and confirm your understand them.


Then, add a second initial populations: juvenile_pop0, with value 0.


Add an additional variable, mature_rate, with the value 0.33.
End of explanation
"""


def run_simulation(system):
    """Runs a proportional growth model.
    
    Adds TimeSeries to `system` as `results`.
    
    system: System object with t0, t_end, p0,
            birth_rate and death_rate
    """
    adults = TimeSeries()
    adults[system.t0] = system.adult_pop0
    
    for t in linrange(system.t0, system.t_end):
        births = system.birth_rate * adults[t]
        deaths = system.death_rate * adults[t]
        
        adults[t+1] = adults[t] + births - deaths
        
    system.adults = adults

"""
Explanation: Now update run_simulation with the following changes:


Add a second TimeSeries, named juveniles, to keep track of the juvenile population, and initialize it with juvenile_pop0.


Inside the for loop, compute the number of juveniles that mature during each time step.


Also inside the for loop, add a line that stores the number of juveniles in the new TimeSeries.  For simplicity, let's assume that only adult rabbits die.


During each time step, subtract the number of maturations from the juvenile population and add it to the adult population.


After the for loop, store the juveniles TimeSeries as a variable in System.
End of explanation
"""


run_simulation(system)
system.adults

"""
Explanation: Test your changes in run_simulation:
End of explanation
"""


def plot_results(system, title=None):
    """Plot the estimates and the model.
    
    system: System object with `results`
    """
    newfig()
    plot(system.adults, 'bo-', label='adults')
    decorate(xlabel='Season', 
             ylabel='Rabbit population',
             title=title)

"""
Explanation: Next, update plot_results to plot both the adult and juvenile TimeSeries.
End of explanation
"""


plot_results(system, title='Proportional growth model')

"""
Explanation: And test your updated version of plot_results.
End of explanation
"""


