<a href="https://colab.research.google.com/github/john-s-butler-dit/CaseStudy_PredatorPrey/blob/master/06_Snow%20Hares%20and%20Lynxes.ipynb" target="_parent"></a>

# Predator-Prey Population Differential Equations


In this notebook we will model the interaction of Lynx and Hare populations [1].


```python
## Library
import numpy as np
import math 
import matplotlib.pyplot as plt
```

## Predators and prey
The cyclical relationship of Predators and Prey in the wild  means that when there is an abundance of prey, the predators will eat more and their population will increase. On the other hand, if there is a shortage in the number of prey, the number of predators will decrease. One of the of the most famous examples of this was observed by the fur traders  Hudson's Bay Company of Canada. The company collected data about the number of lynx and hare pelts they purchased over a 90 year period. Their data showed the cylical nature of the predator prey relationship.
This kind of behaviour has been observed in many pairs of predators and prey from mammals to fish to beyond. 

## Snow Hares and Lynxes
In a balanced eco system the relationship between snow hares (prey) and lynxes (predator) can be described mathematically using a Loketta-Volterra model [1-3]
\begin{equation}
\begin{array}{cl}
\frac{d H}{dt}=a_{Birth} H-b_{Con}LH,\\
\frac{d L}{dt}=-c_{Death}L+d_{Food} L H,\\
\end{array}
\end{equation}
this set of equations models the interaction between snow hares (H) and lynxes (L). The lynes are the predators who eat the snow hares to survive. Where there parameters 
* $a_{Birth}$ is the birth rate of the snow hares;
* $b_{Con}$ is the consumation rate of snow hares by lynxes (interaction);
* $c_{Death}$ is the death rate of lynxes if there is no food source;
* $d_{Food}$ is the rate that lynxes eat the snow hares to survive (interaction).



```python
a_Birth=0.4
b_Con=0.1
c_Death=0.09
d_Food=0.01

```

## Discrete Domain 
To numiercally approximate the solution we discretise the continuous domain using a step size, $h=1$, 
which gives
\begin{equation}
t_i=0+i h,
\end{equation}
for $i=0,...10$.
The figure below illustrate the discrete domain.


```python
h=0.001
t=np.arange(1850,1925+h/2,h)
fig = plt.figure(1,figsize=(5,4))
ax = fig.add_subplot(1,1,1)
ax.plot(t,0*t,'o:',color='k')
ax.set_xlabel('Time (yrs)')
plt.title('Discrete Time Domain')
plt.show()
```


```python
N=len(t) # Number of Steps
H=np.zeros(N) # Numerical Solution Hares
H[0]=6 # Intial Hare population
L=np.zeros(N) # Numerical Solution Lynxes
L[0]=2# Initial Lynx population


```

We numerically approximate the differential equation using Eulers method to give the difference equation
\begin{equation}
H[i+1] = H[i]+ h*\big(a_{Birth}*H[i]-b_{Con}*H[i]*L[i] \big)
\end{equation}

\begin{equation}
L[i+1] = L[i]+ h*\big(-c_{Death}*H[i]+d_{Food}*H[i]*L[i] \big)
\end{equation}
where $h$ is the stepsize and  $H[i]$ and $L[i]$ is the numerical approximation of the hare $H(t_i)$ and Lynx $L(t_i)$ at time $t_i$, for $i=0,...,N$



```python
for i in range (0,N-1):
    H[i+1]=H[i]+h*(a_Birth*H[i]-b_Con*H[i]*L[i])# INSERT EQUATION HERE 
    L[i+1]=L[i]+h*(-c_Death*L[i]+d_Food*H[i]*L[i])# INSERT EQUATION HERE 


```

## Results
The figure below plots the numerical approximation of the Hare population (green) and the Lynx poulation (red) as a function of time in years between 1850 and 1925.
The figure illustrates the oscilatory nature of the Hare and Lynx populations.
The hare population fluctuates from close to 0 to 800 ten of thousands.
The lynx population fluctuates from close to 0 to 120 ten of thousands.
The peak hare population preceeds the lynx population by about half a year.
There is a gradual increase of peak populations over time.
The model is similar to the observed data by the Hudson Bay Company.



```python
## Plotting Figure
fig = plt.figure(1,figsize=(10,4))

ax = fig.add_subplot(1,1,1)

ax.plot(t,H,':',color='green',label='Hares')
ax.plot(t,L,':',color='red',label='Lynxes')

ax.legend()
ax.set_xlabel('Time (yrs)')
ax.set_ylabel('Population (tens of thousands)')
#ax.legend(loc='best')
ax.set_title('The figure below plots the numerical approximation \n of the Hare population (green)  \nand the Lynx poulation (red) as a function of time in years between 1850 and 1925.')
plt.tight_layout()
plt.subplots_adjust(top=0.85)
plt.show()

```

## Phase Plane Plot
The phase plane plot below illustrates the population of Hares as a function of Lynxes. This removes time from out plot.
The rate of change of hares as function of the rate of lynxes is written as
\begin{equation}
\frac{d H}{dL}=\frac{a_{Birth} H-b_{Con}LH}{-c_{Death}L+d_{Food} L H}.
\end{equation}

The plot below shows a stable cyclical relationship between hares and lynxes.
It looks a bit like an orbit of a moon.


```python
## Plotting Figure
fig = plt.figure(1,figsize=(5,5))

ax = fig.add_subplot(1,1,1)

ax.plot(H,L,'-',color='black')

ax.set_ylabel('Lynx population (tens of thousands)')
ax.set_xlabel('Hare Population (tens of thousands)')
#ax.legend(loc='best')
ax.set_title('Predator Prey Relationship')
plt.tight_layout()
plt.ylim(0.9*np.min(L),1.1*np.max(L))
plt.xlim(0.9*np.min(H),1.1*np.max(H))

plt.show()
```

# References

[1] Stover, Christopher and Weisstein, Eric W. "Population Growth." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/PopulationGrowth.html

[2]  Vito Volterra.  Fluctuations in the abundance of a species considered mathematically. Nature,118:558–560,1926.

[3] Alfred J Lotka.   Analytical note on certain rhythmic  relations inorganic  systems.Proceedings of the National Academy of Sciences,6(7):410–415,1920.

0ec8f4496dd77092e8e8dadb5a851683c8c4c88e
ipynb
Jupyter Notebook
06_Snow Hares and Lynxes.ipynb
john-s-butler-dit/CaseStudy_PredatorPrey
f7d3c2a4b27bd37db5690ea81130a47dcbb602af
MIT
06_Snow Hares and Lynxes.ipynb
john-s-butler-dit/CaseStudy_PredatorPrey
f7d3c2a4b27bd37db5690ea81130a47dcbb602af
MIT
06_Snow Hares and Lynxes.ipynb
john-s-butler-dit/CaseStudy_PredatorPrey
f7d3c2a4b27bd37db5690ea81130a47dcbb602af
MIT
Qwen/Qwen-72B
1. YES
2. YES