-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathLotkaVolterraModel.py
144 lines (114 loc) · 3.65 KB
/
LotkaVolterraModel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
Module containing simulation code of the Lotka Volterra model.
Adapted from:
https://github.com/scipy/scipy-cookbook/blob/master/ipython/LotkaVolterraTutorial.ipynb
"""
import numpy as np
def dX_dt(X, a=1.0, b=0.1, c=1.5, d=0.75):
"""
Computes the growth rate of fox and rabbit populations based on system state (X) and parameters (a,b,c,d)
Parameters
----------
X : array or tuple
[prey_count, predator_count]
a : float, optional
natural growth rate of the prey (rabbit)
b : float, optional
natural dying rate of the prey
c : float, optional
natural growth rate of the predator (fox)
d : float, optional
natural dying rate of the predator
Returns
-------
numpy array
[change of prey_count, change of predator_count]
Examples
-------
>>> dX_dt(np.ones(2),1,0.1,1.5,.75)
array([ 0.9 , -1.425])
>>> dX_dt(np.zeros(2),1,0.1,1.5,.75) # zero is a fixpoint
array([0., 0.])
"""
if np.size(X)>2:
raise ValueError("X has only two dimensions!")
return np.array([a * X[0] - b * X[0] * X[1], -c * X[1] + d * b * X[0] * X[1]])
def d2X_dt2(X, a=1.0, b=0.1, c=1.5, d=0.75):
"""
Compute the Jacobian matrix evaluated at X.
Parameters
----------
X : array
Vector state of populations (rabbits, foxes)
a : float, optional
natural growth rate of the prey (rabbit)
b : float, optional
natural dying rate of the prey
c : float, optional
natural growth rate of the predator (fox)
d : float, optional
natural dying rate of the predator
Returns
-------
numpy array, shape (2, 2)
Array containing Jacobian of dX_dt
Example
-------
Jacobian at [0, 0] for 0 rabbits and 0 foxes for default parameters
>>> d2X_dt2(np.zeros(2))
array([[ 1. , -0. ],
[ 0. , -1.5]])
>>> d2X_dt2(np.zeros(2),a=2,b=.3,c=1.2,d=0.4)
array([[ 2. , -0. ],
[ 0. , -1.2]])
"""
return np.array([[a - b * X[1], -b * X[0]], [b * d * X[1], -c + b * d * X[0]]])
def population_equilibrium(a=1.0, b=0.1, c=1.5, d=0.75):
"""
Returns equilibrium points of dX_dt, i.e. where growth rate is equal to zero.
Parameters
----------
a : float, optional
natural growth rate of the prey (rabbit)
b : float, optional
natural dying rate of the prey
c : float, optional
natural growth rate of the predator (fox)
d : float, optional
natural dying rate of the predator
Returns
-------
list
List of points with dX_dt = 0.
Examples
-------
>>> population_equilibrium()
(array([0., 0.]), array([20., 10.]))
>>> population_equilibrium(1,0.1,1.5,.2)
(array([0., 0.]), array([75., 10.]))
"""
return np.zeros(2), np.array([c / (d * b), a / b])
def check_equilibrium(a=1.0, b=0.1, c=1.5, d=0.75):
"""
Checks if population_equilibrium returns fixpoints of dX_dt.
Parameters
----------
a : float, optional
natural growth rate of the prey (rabbit)
b : float, optional
natural dying rate of the prey
c : float, optional
natural growth rate of the predator (fox)
d : float, optional
natural dying rate of the predator
"""
equilibria = population_equilibrium(a=a, b=b, c=c, d=d)
for eq in equilibria:
if np.all(dX_dt(eq, a=a, b=b, c=c, d=d) == np.zeros(2)):
print(eq, " is a fix point!")
else:
print(eq, " is not a fix point!")
if __name__ == "__main__":
import doctest
print("Starting doctests") # not required!
doctest.testmod()