[Python] I tried to reproduce the battle between the lion and the gazelle with a super cool mathematical formula called the Lotka-Volterra equation.

Lotka-Volterra equation

Well, an equation with a name like this second grader thought. ** Transcendental cool. ** ** This time I would like to play with this equation with numerical calculation (python). The content of the equation itself looks like this.

\frac{dx}{dt}=\alpha x-\beta xy \\
\frac{dy}{dt}=\gamma x-\delta xy

It's a pretty good formula, but it's a formula that expresses the relationship between predation and prey **. Rest assured that it's not that difficult because it's a tinsel! !! This time, I will read it in the African setting of ** Lion vs Gazelle **.

Understanding the formula

There are many letters, but I will explain them one by one.

Definition of variables and constants

variable

$ x $: Number of predators (number of gazelles), $ y $: Number of predators (number of lions)

constant

$ \ alpha $: Predator-prey birth rate, $ \ beta $: Predator-prey rate, $ \ gamma $: Predator-prey birth rate, $ \ delta $: Predator-prey mortality rate

(Academically, there are some parts that are accurate, but we emphasize clarity)

Some people may have a ??? in their brain, but please wait for a while as it will be explained in the next section.

Meaning for expressions

This is the most interesting place! !! Let's take a look at the original formula while looking at the meanings of the letters defined above.

\frac{dx}{dt}=\alpha x-\beta xy~~~~(1) \\
\frac{dy}{dt}=\gamma xy-\delta y~~~~(2)

First, let's think about (1).

\frac{dx}{dt}=\alpha x-\beta xy~~~~(1)

The left side means the rate of change over time of $ x $, that is, ** how much the gazelle increases **. So what can be thought of as an increase or decrease in gazelle? Yes, ** childbirth ** and ** death **, right? Birth corresponds to $ \ alpha x $ in the first term, and death corresponds to $ \ beta xy . The birth of a gazelle, that is, the number of gazelle born ** can be expressed by the product of the number of gazelle and the birth rate ** ( = \ alpha x $). Will increase). On the other hand, the death of gazelle and the number of decrease of gazelle ** can be expressed by the product of the number of gazelle, the number of lions and the prey rate ** (= $ \ beta xy $). I think this is a little difficult to understand, but I think it will be easier to understand if you imagine that if there are many gazelles, it will be easier for lions to target, and if there are many lions, many gazelles will be preyed on. Therefore, the total increase / decrease in gazelle is $ \ alpha x- \ beta xy $, which is the sum of the number of births and the number of deaths.

Let's think about the same thing in equation (2).

\frac{dy}{dt}=\gamma xy-\delta y~~~~(2)

The left side is the time change rate of $ y , so it corresponds to ** how many lions will increase **. The increase or decrease of lions can also be divided into childbirth and death, but there are some factors that are slightly different from gazelle. ** The number of lions born ** is represented by the product of the number of lions, the number of gazelles and the birth rate ** ( = \ gamma xy ). ** Why is the number of gazelle related? ?? The reader who thought ** is sharp. If you were a lion, wouldn't you feel that the more gazelle you have, the more prosperous your offspring? That's the tension. Now, ** the number of lion deaths ** is represented by ** the product of the number of lions and the mortality rate ** ( = \ delta y $). Lions cannot be eaten from gazelle, so the number of deaths is determined regardless of the number of gazelle. Therefore, the total increase and decrease of lions is $ \ gamma xy- \ delta y $, which is the sum of the number of born and the number of dead.

Try to calculate numerically

I hope you have deepened your understanding of gazelle and lion in the previous section and understood the formula. However, since differential equations cannot be handled directly by a computer, this time we will use the ** difference method ** for calculation. Suppose $ x, y $ at time $ n $ is represented as $ x_ {n}, y_ {n} $. This is

\frac{dx}{dt}=\alpha x-\beta xy~~~~(1) \\
\frac{dy}{dt}=\gamma xy-\delta y~~~~(2)

When you make a difference, it looks like this,

\frac{x_{n+1}-x_{n}}{\Delta t}=\alpha x_{n}-\beta x_{n}y_{n}~~~~(1) \\
\frac{y_{n+1}-y_{n}}{\Delta t}=\gamma x_{n}y_{n}-\delta y_{n}~~~~(2)

Solving the unknown values $ x_ {n + 1}, y_ {n + 1} $ at the next time $ n + 1 $

x_{n+1}=(\alpha -\beta y_{n})x_{n}\Delta t + x_{n}~~~~(1) \\
y_{n+1}=(\gamma x_{n} -\delta)y_{n}\Delta t + x_{n}~~~~(2)

Will be. Since the value on the right side consists of known values (constant or value at time n), the left side can be obtained simply by performing substitution calculation.

Source code

This time I wrote it in python. You can change the value of the constant as you like.

Lotka-Volterra.py


import numpy as np
import matplotlib.pyplot as plt

#coefficient
a, b, c, d = 0.3, 0.1, 0.3, 1.3

#initial value
init_x = 10
init_y = 2

#Time progress
dt = 0.01
n = 5000

#Array initialization
x = np.zeros(n)
x[0] = init_x
y = np.zeros(n)
y[0] = init_y

for i in range(1, n):
  x[i] = (a - b * y[i - 1]) * x[i - 1] * dt + x[i - 1]
  y[i] = (c * x[i - 1] - d) * y[i - 1] * dt + y[i - 1]
  print(i, x[i], y[i])

t = np.arange(0, n * dt, dt)

plt.plot(t, x, label="Prey")
plt.plot(t, y, label="Predator")
plt.legend()
plt.show()

result

The result should look like the figure below. Figure_1.png

Consideration

It's interesting that the result vibrates. If you consider it in chronological order,

  1. The number of lions (yellow) increases and the number of gazelle (blue) decreases.
  2. The increase of lions stops and begins to decrease, and the gazelle increases accordingly.
  3. The number of gazelle increases, but after a while, the number of lions increases and the number of gazelle decreases.
  4. Repeat this

It has become.

1. The number of lions (yellow) increases and the number of gazelle (blue) decreases.

Well, that's right. If the number of lions increases, the gazelle will be eaten and will decrease.

2. The increase of lions stops and begins to decrease, and the gazelle increases accordingly.

As a result of too many lions ** running out of food and starting to starve ** (I feel something like a suggestion ...). And as a result of the decrease in lions, Gazelle regains momentum and begins to increase.

3. The number of gazelle increases, but after a while, the number of lions increases and the number of gazelle decreases.

As the number of gazelle increases, it becomes ** heaven ** for Lion. And the gazelle will decrease

4. Repeat this

Yes, this world goes around forever in this cycle. It makes me feel strange.

Summary

This time, I used a cool equation just named ** Lotka-Volterra equation ** to perform numerical calculations on the lion and gazelle ecosystems. I would be happy if anyone finds it interesting! I hope you like it! !!

Recommended Posts

[Python] I tried to reproduce the battle between the lion and the gazelle with a super cool mathematical formula called the Lotka-Volterra equation.
I tried to enumerate the differences between java and python
I also tried to imitate the function monad and State monad with a generator in Python
I tried to make a periodical process with Selenium and Python
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
[ES Lab] I tried to develop a WEB application with Python and Flask ②
I tried to touch the CSV file with Python
I tried to draw a route map with Python
I tried to solve the soma cube with python
I tried to automatically generate a password with Python3
I tried to solve the problem with Python Vol.1
I tried to automate the article update of Livedoor blog with Python and selenium.
I tried to compare the processing speed with dplyr of R and pandas of Python
I tried to find the entropy of the image with python
A super introduction to Django by Python beginners! Part 6 I tried to implement the login function
I tried to simulate how the infection spreads with Python
I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python
I made a server with Python socket and ssl and tried to access it from a browser
I tried to make GUI tic-tac-toe with Python and Tkinter
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
I tried to divide the file into folders with Python
I tried to predict the number of domestically infected people of the new corona with a mathematical model
A story that didn't work when I tried to log in with the Python requests module
[5th] I tried to make a certain authenticator-like tool with python
I tried to solve the ant book beginner's edition with python
[2nd] I tried to make a certain authenticator-like tool with python
[3rd] I tried to make a certain authenticator-like tool with python
[Python] A memo that I tried to get started with asyncio
I tried to create a list of prime numbers with python
I tried to make a 2channel post notification application with Python
I wanted to solve the ABC164 A ~ D problem with Python
I tried to create Bulls and Cows with a shell program
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
I tried to easily detect facial landmarks with python and dlib
[1st] I tried to make a certain authenticator-like tool with python
I tried to improve the efficiency of daily work with Python
A super introduction to Django by Python beginners! Part 3 I tried using the template file inheritance function
I ran GhostScript with python, split the PDF into pages, and converted it to a JPEG image.
A super introduction to Django by Python beginners! Part 2 I tried using the convenient functions of the template
I tried to get and analyze the statistical data of the new corona with Python: Data of Johns Hopkins University
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
Python: I tried to make a flat / flat_map just right with a generator
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
[Introduction to Python] What is the difference between a list and a tuple?
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
I tried to express sadness and joy with the stable marriage problem.
I tried to get the authentication code of Qiita API with Python.
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I tried to discriminate a 6-digit number with a number discrimination application made with python
I tried to learn the angle from sin and cos with chainer
I tried with the top 100 PyPI packages> I tried to graph the packages installed on Python
I tried to verify and analyze the acceleration of Python by Cython
I tried to streamline the standard role of new employees with Python
[Outlook] I tried to automatically create a daily report email with Python
I tried to get the movie information of TMDb API with Python
I tried to build a Mac Python development environment with pythonz + direnv
I tried to create a sample to access Salesforce using Python and Bottle
I tried to control the network bandwidth and delay with the tc command
I tried a functional language with Python