Rabbit population growth

In [7]:
rabbit_pair_count = [1,1]
In [6]:
next_month = rabbit_pair_count[-1] + rabbit_pair_count[-2]
rabbit_pair_count.append(next_month)
print(rabbit_pair_count)
[1, 1, 2, 3, 5, 8]
In [8]:
num_times_to_run = 4
for i in range(num_times_to_run):
    next_month = rabbit_pair_count[-1] + rabbit_pair_count[-2]
    rabbit_pair_count.append(next_month)
print(rabbit_pair_count)
[1, 1, 2, 3, 5, 8]
In [9]:
k = 3
num_times_to_run = 4
rabbit_pair_count = [1,1]
for i in range(num_times_to_run):
    next_month = rabbit_pair_count[-1] + rabbit_pair_count[-2]*k
    rabbit_pair_count.append(next_month)
print(rabbit_pair_count)
[1, 1, 4, 7, 19, 40]
In [12]:
k = 3
num_times_to_run = 4
m = 4
rabbit_pair_count = [1,1]
for i in range(num_times_to_run):
    if m <= len(rabbit_pair_count):
        next_month = rabbit_pair_count[-1] + rabbit_pair_count[-2]*k - \
          rabbit_pair_count[-m]
    else:
        next_month = rabbit_pair_count[-1] + rabbit_pair_count[-2]*k
        
    rabbit_pair_count.append(next_month)
print(rabbit_pair_count)
[1, 1, 4, 7, 18, 38]
In [28]:
def rabbit_population(n_months):
    """
    Summary line: Generates the rabbit population for a set number of months
    
    Extended description: This example was generated in class
      It doesn't take into account mortality
      We assume each pair of rabbits has a pair of offspring 2 months after they are born
      
    Parameters:
    n_months (int): Number of months to generate data for
    
    Return:
    list: list of numbers of rabbit pairs for each month
    """
    assert n_months > 0, "Please specify months > 0"
    if n_months == 1:
        return [1]
    rabbit_pair_count = [1,1]
    for i in range(n_months-len(rabbit_pair_count)):
        next_month = rabbit_pair_count[-1] + rabbit_pair_count[-2]
        rabbit_pair_count.append(next_month)
    return rabbit_pair_count
    
rabbit_counts_5 = rabbit_population(5)
print(rabbit_counts_5)
[1, 1, 2, 3, 5]
In [29]:
rabbit_population(-2)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-29-a1011639f483> in <module>
----> 1 rabbit_population(-2)

<ipython-input-28-948b7bdadb49> in rabbit_population(n_months)
     13     list: list of numbers of rabbit pairs for each month
     14     """
---> 15     assert n_months > 0, "Please specify months > 0"
     16     if n_months == 1:
     17         return [1]

AssertionError: Please specify months > 0
In [30]:
help(rabbit_population)
Help on function rabbit_population in module __main__:

rabbit_population(n_months)
    Summary line: Generates the rabbit population for a set number of months
    
    Extended description: This example was generated in class
      It doesn't take into account mortality
      We assume each pair of rabbits has a pair of offspring 2 months after they are born
      
    Parameters:
    n_months (int): Number of months to generate data for
    
    Return:
    list: list of numbers of rabbit pairs for each month

In [32]:
help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

In [51]:
def rabbit_population2(n_months,m,k=1):
    """
    Summary line: Generates the rabbit population for a set number of months
    
    Extended description: This example was generated in class
      It allows for rabbit mortality
      We assume each pair of rabbits has a pair of offspring 2 months after they are born 
        unless otherwise specified
      
    Parameters:
    n_months (int): Number of months to generate data for
    m (int): number of months the rabbits live
    k (int): number of pairs of rabbits produced by each pair in a month
    
    Return:
    list: list of numbers of rabbit pairs for each month
    """
    assert n_months > 0, "Please specify months > 0"
    assert k > 0, "k must be > 0"
    if n_months == 1:
        return [1]
    rabbit_pair_count = [1,1]
    for i in range(n_months-len(rabbit_pair_count)):
        next_month = rabbit_pair_count[-1] + rabbit_pair_count[-2]*k - \
          rabbit_pair_count[-m]
        rabbit_pair_count.append(next_month)
        if next_month == 0:
            break
    return rabbit_pair_count

pop_m2_k6 = rabbit_population2(100,2,3)
pop_m2_k4 = rabbit_population2(100,2,4)
In [45]:
rabbit_population2(100,2,0)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-45-325254c70060> in <module>
----> 1 rabbit_population2(100,2,0)

<ipython-input-44-47b8417be5a7> in rabbit_population2(n_months, m, k)
     17     """
     18     assert n_months > 0, "Please specify months > 0"
---> 19     assert k > 0, "k must be > 0"
     20     if n_months == 1:
     21         return [1]

AssertionError: k must be > 0

Plotting

In [46]:
import matplotlib.pyplot as plt
In [57]:
months = list(range(1,len(pop_m2_k4)+1))
plt.plot(months,pop_m2_k4,pop_m2_k6)
plt.ylabel('Number of Rabbit Pairs')
Out[57]:
Text(0, 0.5, 'Number of Rabbit Pairs')
In [ ]:
help(plt.plot)
In [58]:
import pandas as pd
In [59]:
rabbit_df = pd.DataFrame(
 {'generation':months,
   'rabbit_count1':pop_m2_k4,
   'rabbit_count2':pop_m2_k6     
 }
)
In [ ]:
print(rabbit_df[0:5])
In [61]:
rabbit_df2 = pd.DataFrame(
 {
   'rabbit_count1':pop_m2_k4,
   'rabbit_count2':pop_m2_k6     
 }
)
In [62]:
rabbit_df2.index = months
In [ ]:
print(rabbit_df2[0:5])
In [64]:
rabbit_df2.columns
Out[64]:
Index(['rabbit_count1', 'rabbit_count2'], dtype='object')
In [65]:
rabbit_df2.loc[10:15]
Out[65]:
rabbit_count1 rabbit_count2
10 1159 341
11 2683 683
12 6160 1365
13 14209 2731
14 32689 5461
15 75316 10923
In [66]:
rabbit_df2.iloc[10:15]
Out[66]:
rabbit_count1 rabbit_count2
11 2683 683
12 6160 1365
13 14209 2731
14 32689 5461
15 75316 10923
In [68]:
rabbit_df2.rabbit_count1.loc[10:15]
Out[68]:
10     1159
11     2683
12     6160
13    14209
14    32689
15    75316
Name: rabbit_count1, dtype: object
In [69]:
rabbit_df2['rabbit_count1'].loc[10:15]
Out[69]:
10     1159
11     2683
12     6160
13    14209
14    32689
15    75316
Name: rabbit_count1, dtype: object
In [71]:
rabbit_df2.loc[ rabbit_df2['rabbit_count1'] < 50 ]
Out[71]:
rabbit_count1 rabbit_count2
1 1 1
2 1 1
3 4 3
4 7 5
5 19 11
6 40 21
In [72]:
import plotnine as p9
In [77]:
p9.ggplot(data = rabbit_df2,
         mapping = p9.aes(x = 'rabbit_df2.index',
                         y = 'rabbit_count1')) + \
p9.geom_point()
Out[77]:
<ggplot: (-9223372029310859092)>
In [82]:
for i in range(10000000000000): 
    print(i)
0
1
2
3
4
In [83]:
type(range(5))
Out[83]:
range
In [86]:
a=[0,1,2,3]
type(a)
Out[86]:
list
In [87]:
N = 10**12
for i in range(N):
    if i>=10:
        break
    print(i)
0
1
2
3
4
5
6
7
8
9
In [ ]: