# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/OnlyBelter/HKBU/blob/main/GLV_model/GLV_multiple_species.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="CYkRzUg2J5RU"
# ### 4. the Generalized Lotka-Volterra model (广义LV模型)
# - ref: https://stefanoallesina.github.io/Sao_Paulo_School/intro.html
#
#
#

# + [markdown] id="2BMwLT7pnUzd"
#
# #### 4.1 A single population
#
# $$\frac{d_{N}}{d_t} = N (r + a N)$$
#
# Same as the Verhulst Equation.

# + [markdown] id="FKj7qzxAnVFN"
# #### 4.2 Multi-species
#
# $$\frac{d_{x(t)}}{d_t} = D(x(t))(r + Ax(t))$$
#
# where $x(t)$ is a (column) vector with length $n$ containing the densities of all populations $1,…,n$ at time $t$, 
#
# $r$ is a vector of “intrinsic growth rates” (or death rates, when negative), measuring the growth (decline) of population $i$ when grown alone at low density,
#
# and $A$ is a $n\times n$ matrix of interaction coefficients.
#
# We use $D(x)$ to denote the diagonal matrix with $x$ on the diagonal.

# + [markdown] id="gD8lsTC8LCgO"
# #### 4.3 Multi-species dynamics
#

# + [markdown] id="2aGgRZrSnlFr"
# ##### 4.3.1 Existence of an equilibrium
# In analogy with the single species, we can look for stationary points (equilibria). If the matrix $A$ is not singular, then we can look for a solution of $r+Ax$ that has positive components (called a feasible equilibrium). If such point exists, it is unique and is the solution of  
# $Ax^{*}=−r, x^{*}=−A^{−1}r$.
#
# > **Q**: How to deal with this problem if $A$ is a big matrix (may $500 \times 500$) and is singular? 
#
# Suppose that the GLV has no feasible equilibrium. Then all trajectories (if bounded; some could grow to infinity) reach the boundary of  
# $\mathbb R^n_{0+}$. Practically, this means that **to ensure coexistence of all species, it is necessary to have an equilibrium in the interior $\mathbb R^n_+$**.

# + [markdown] id="pRoiDGfERhMp"
# ##### 4.3.2 Stability of an equilibrium
#
# Suppose that a feasible equilibrium $x^{*}$ exists. Then we can ask whether it is attractive, i.e. if trajectories started at initial condition  
# $x(0)$ will eventually reach $x^{*}$. This problem is in general difficult to solve (but see below);
#
# > **Q**: Why is it difficult to solve? What's the general method to solve this kind of problem?
#
# as an alternative, we can test for local asymptotic stability, i.e., ask whether the system will return to the equilibrium if perturbed infinitesimally away from it. In general, whenever we describe an **ecological community** as a system of nonlinear, autonomous ODEs:
#
# $$\frac{d x_i (t)}{d t} = f_i (x(t)) \;,$$
#
# we define an equilibrium $x^{*}$ as a vector of densities such that:
#
# $$\left. \frac{d x_i}{d t} \right|_{{x}^*} = f_i({x}^*) = 0 \quad \forall i$$
#
# > **Q**: How to understand the concept of "Dynamic equilibrium" and how to describe it by mathematical way?
#
# A given system might have a multitude of equilibria. When the system is resting at an equilibrium point, it will remain there unless it is perturbed away from it. **Local stability analysis** is a method to probe whether a system that is perturbed infinitesimally away from an equilibrium will eventually return to it, or rather move away from it.
#
# Suppose that the system is resting at an equilibrium $x^{*}$, and that it is slightly perturbed away from it. $\Delta x(0)=x(0)−x^{*}$ is the state of the system immediately after the perturbation. We Taylor-expand around $x^{*}$:
#
# $$f(\Delta x(0)) = f(x^{*})+ \left. J \right|_{x^{*}} \Delta x(0) + \ldots$$
#
# Where $J$ is the Jacobian matrix of the system, whose elements are defined as: $J_{ij} = \frac{\partial f_i(x)}{\partial x_j}$ 
#
# Each element of this matrix is therefore a function, whose value depends on  
# $x$. When we evaluate the Jacobian matrix at an equilibrium point $x^{*}$, we obtain the so-called “community matrix” $M$:
#
# $$M = \left. {J} \right|_{ {x}^{*}}$$
#
# Note that, although **each system has a unique Jacobian matrix**, there are as many community matrices as there are equilibria. The community matrix details the effect of increasing the density of one species on any other species around the equilibrium point.
#
# > Why does each system has a unique Jacobiam matrix? Does it should change along with time?
#
# We can therefore write the differential equation:
#
# $$\frac{d \Delta x(t)}{dt} \approx M \Delta x(t)$$
# with solution:
# $$\Delta x(t) = \Delta x(0) e^{Mt} = \Delta x(0) Q e^{\Lambda t} Q^{-1}$$
#
# Where $Q$ is the matrix containing the (unit) eigenvectors of $M$, and $\Lambda$ is a diagonal matrix containing the eigenvalues of $M$. 
#
# > What's the meaning of the eigenvectors and eigenvalues of $M$ here?
#
# As such, the eigenvalues of $M$ determine the stability of the equilibrium $x^{*}$: **if all the eigenvalues have negative real part, then the system will eventually return to the equilibrium after sufficiently small perturbations**; conversely, if any of the eigenvalues have positive real part, the system will move away from the equilibrium whenever perturbed. Therefore, depending on the sign of the “rightmost” eigenvalue of $M$, $\lambda_1$, we can determine the stability of $x^{*}$:
#
#
# $$\text{Re}(\lambda_1) \begin{cases}
#     < 0 \to {x}^{*} \quad \text{is stable}\\
#     > 0 \to {x}^{*} \quad \text{is unstable}
#   \end{cases}
# $$
#
#
# Local asymptotic stability means that the equilibrium is stable with respect to infinitesimal perturbations (“local”), and that returning to the equilibrium could take a long time (“asymptotic”). Ecologists have also studied stronger forms of stability (e.g., “global stability”, in which all trajectories started at positive densities lead to the equilibrium).
#
# For the GLV model, the Jacobian is easy to compute:
#
# $$J_{ij} = \frac{\partial f_i}{\partial x_j} = a_{ij} x_i$$
#
# and
#
# $$J_{ii} = \frac{\partial f_i}{\partial x_i} = r_i + \sum_j a_{ij} x_j + a_{ii} x_i$$
#
# > What's the meaning of $a$ and $r$?
#
# At equilibrium $r_i + \sum_j a_{ij} x_j = 0$, 
#
# $$M = \left. {J} \right|_{ {x}^{*}} = D(x^{*})A$$
#

# + id="V99d_2-akEGo"


StarcoderdataJupyterScriptsDedupFiltered