---
title: "fdcoexist equation only"
author:
    - Pierre Denelle, Matthias Grenié, Cyrille Violle, François Munoz and Caroline M. Tucker
date: "`r format(Sys.time(), '%d %B %Y')`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Full equation of the model}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
  %\usepackage[utf8]{inputenc}
---

## General Population Growth Equation

This document resumes the equation used in our model and details the different processes.
Our coexistence model is developed following this equation (Beverton-Holt equation):

$$
N_{t+1, i, x} = \frac{R_{i, x} \times N_{t, i, x}}{1 + A \times \alpha_i}
$$

with $N_{t+1, i, x}$ the abundance of species $i$ in patch $x$ at time $t + 1$, $R_{i, x}$ the growth of species $i$ in patch $x$, $A$ the constant for inter-specific competition and $\alpha_i$ the competition coefficient for species $i$. We can detail both $R_{i, x}$ and $\alpha_i$:

$$
\begin{aligned}
\alpha_i = \sum_{j = 1, j \neq i}^{S} N_{t, j, x} \times \sigma_{ij}\\
R_{i, x} = k \times \exp\left(- \frac{(t_i - t_{opt(x)})^2}{2\times l^2}\right)
\end{aligned}
$$

with $\sigma_{ij}$ the functional similarity between species $i$ and $j$; we see that $\alpha_i$ gets bigger as species $i$ and $j$ are more similar and as species $j$ is more abundant. And with $t_i$ the trait of species $i$, $t_opt(x)$ the environmental value of patch $x$ and $l^2$ the width of the environmental filter across all patches.


If we replace $\alpha_i$ and $R_{i, x}$ in the first equation it gives:

$$
N_{t+1, i, x} = \frac{
    k \times \exp\left(- \frac{(t_i - t_{opt(x)})^2}{2\times l^2}\right) \times N_{t, i, x}}{
1 + A \times \displaystyle\sum_{j = 1, j \neq i}^{S} N_{t, j, x} \times \sigma_{ij}
}
$$

## Limiting similarity ($\delta_{ij}$ & $\sigma_{ij}$)

$\delta_{ij}$ is the functional dissimilarity coefficient between species $i$ and species $j$. It reflects the fact that the more two species are dissimilar the less competition they experience from each other.


### Basic Limiting Similarity

First we can compute $\delta_{ij}$ as the euclidean distance between species using multiple traits:

$$
\delta_{ij} = \sqrt{\sum_{r = 1}^{T} (t_{i, r} - t_{j, r})^2}, 
$$

with $T$ the number of traits, $t_{i, r}$ trait $r$ of species $i$ and $t_{j, r}$ trait $r$ of species $j$.


### Variable contribution of traits to limiting similarity

Different traits $r$ can contribute to limiting similarity in different
proportions $a_r$ (we have $\sum_{r = 1}^{T} a_r = 1$). If want to take the contribution of traits to competition we have to modify the dissimilarity computation by weighting them by a factor $\sqrt{a_r}$, as follow:

$$
\begin{aligned}
\delta_{ij} = \sqrt{\sum_{r = 1}^{T} \left[ \sqrt{a_r} (t_{i, r} - t_{j, r})\right]^2}\\
\delta_{ij} = \sqrt{\sum_{r = 1}^{T} a_r \times (t_{i, r} - t_{j, r})^2},
\end{aligned}
$$

with $a_r$ the contribution of trait $r$ in limiting similarity
and we have $\sum_{r = 1}^{T} a_r = 1$.


### Considering non-linear of traits on limiting similarity

In order to consider that the effect of limiting similarity is non-linear we use an exponent on distances $s$:
$$
\begin{aligned}
{\delta_{ij}}' &= {\delta_{ij}}^s\\
               &= \left(\sqrt{\sum_{r = 1}^{T} a_r \times (t_{i, r} - t_{j, r})^2}\right)^s
\end{aligned}
$$

We use $\max({\delta_{ij}}') - {\delta_{ij}}'$ in above-mentioned equations to consider limiting similarity (closer species experience higher inter-specific competition) thus we can use a similarity term:

$$
\begin{aligned}
\sigma_{ij} = \max({\delta_{ij}}') - {\delta_{ij}}',\quad \sigma_{ii} = 0 ~\forall~i \in [1, S] \\
\end{aligned}
$$

## Adding intra-specific competition

The equation above only considers inter-specific competition when $j \neq i$ in the sum. We can however add intra-specific competition when $j = i$. Each site has a species-specific carrying capacity $K$ as the number of individuals approaches this carrying capacity the intra-specific competition increases:

$$
\alpha_{ii} = B \times N_{t,i,x}
$$

Thus the equation becomes:

$$
N_{t+1, i, x} = \frac{
k \times \exp\left(- \displaystyle\frac{(t_i - e_x)^2}{2\times l^2}\right) \times N_{t, i, x}}{
1 + A \displaystyle\sum_{j = 1,~j \neq i}^{S} N_{t, j, x} \times \sigma_{ij} +
B \times N_{t,i,x}
}
$$

with $B$ the coefficient scaling intra-specific competition. 


## Variable contribution of traits to environmental filtering

Because several traits contribute to the growth term depending on their contribution we can rewrite the growth term as:

$$
R_{i, x} = \sum_{r = 1}^{T} b_r \times k \times \exp\left(- \frac{(t_{i, r} - t_{opt, x})^2}{2\times l^2} \right)
$$

with $r$ the trait number, $0 \leq b_r \leq 1$ the contribution of this trait to growth (and $\sum_{r = 1}^{T} b_r = 1$), $t_{i, r}$ the trait number $r$ of species $i$.

So the equation becomes:

$$
N_{t+1, i, x} = \frac{
    \sum_{r = 1}^{T} b_r \times k \times \exp\left(-
    \frac{(t_{i, r} - t_{opt, x})^2}{2\times l^2} \right)
    \times N_{t, i, x} }{
        1 + A \displaystyle\sum_{j = 1,~j \neq i}^{S} N_{t, j, x} \times \sigma_{ij} +
        B \times N_{t,i,x}
}
$$

## Adding Hierarchical Competition

We consider hierarchical competition by considering its influence on
growth rate. To do so, we break down the growth rate term $R_{ix}$ into
two components:

$$
R_{ix} = R_{i, x, env} + R_{i, x, hierarch}
$$
with $R_ix$ the total growth rate as numerator of the global equation;
$R_{i, x, env}$ the growth rate based on the environmental fitting of the species
as developed above; and $R_{i, x, hierarch}$ the effect of hierarchical
competition on growth rate that we develop beneath.

### Basic Hierarchical Competition

We want hierarchical competition to depends on the species present in the community and the distance of species such that smallest species get their growth reduced by tallest species in proportion of their distances and abundances. So the hierarchical competition term $R_{i, x, hierarch}$ of a species $i$ in patch $x$ at time $t$ can be:

$$
R_{t, i, x, hierarch} = H \sum_{
                \substack{j = 1\\
                          t_j \ge t_i}}^S
           (t_j - t_i) N_{t,j,x}
$$
with $H$ the scale for hierarchical competition $t_j$ the trait of species $j$, $t_i$ the trait of species $i$ and $N_{t,j,x}$ the abundance of species $j$ at time $t$ in patch $x$. To account for the fact that hierarchical competition has a negative influence on growth we add a minus sign in front of the equation which becomes:

$$
R_{t, i, x, hierarch} = - H \sum_{
                \substack{j = 1\\
                          t_j \ge t_i}}^S
           (t_j - t_i) N_{t,j,x}
$$


### Variable contribution of traits to hierarchical competition

If we want to consider multiple traits with different contributions we can generalize the equation using the same principle as for limiting similarity:

$$
R_{t, i, x, hierarch} = - H \sum_{r = 1}^T c_r \sum_{
                \substack{j = 1\\
                          t_{j, r} \ge t_{i, r}}}^S
           (t_{j, r} - t_{i, r}) N_{t,j,x}
$$
with $c_r$ the hierarchical weight of trait $r$ among the $T$ traits, with $\sum c_r = 1$.


### Considering non-linear effect of hierarchical competiton

Finally we can consider that hierarchical competition is not linear with the trait difference but instead that taller species have a saturating influence on smaller species (a tree of 10m or 50m will have almost similar influence for light interception for grasses 0.5m high). To account for that we can scale the distances using a hierarchical competition exponent $h$:

$$
R_{t, i, x, hierarch} = - H \sum_{r = 1}^T c_r \sum_{
                \substack{j = 1\\
                          t_{j, r} \ge t_{i, r}}}^S
            N_{t,j,x} (t_{j, r} - t_{i, r})^h
$$


### Updated final equation

The general equation with hierarchical competition added becomes:

$$
N_{t+1, i, x} = \frac{
    \left[
        k \displaystyle\sum_{r = 1}^{T} b_r \times \exp\left(-
        \frac{(t_{i, r} - t_{opt, x})^2}{2\times l^2} \right)
        - H \sum_{r = 1}^T c_r \sum_{
                \substack{j = 1\\
                          t_{j, r} \ge t_{i, r}}}^S
            N_{t,j,x} (t_{j, r} - t_{i, r})^h
            \right]
    \times N_{t, i, x} }{
        1 + A \displaystyle\sum_{j = 1,~j \neq i}^{S} N_{t, j, x} \times \sigma_{ij} +
        B \times N_{t,i,x}
}
$$

## Considering Species Dispersal Across Patches

In our model we consider dispersal between patches as if all patches were in a meta-community. At each time step a proportion $d$ of inviduals for each species move across patches. $d$ percent of individuals leave each patch and move evenly across patches.

The dispersion is thus:
$$
\begin{aligned}
    N_{t, i, x, \text{leaving}} &= d \times N_{t, i, x} \\
    N_{t, i, x, \text{sedentaries}} &= N_{t, i, x} - N_{t, i, x, \text{leaving}} = N_{t, i, x} - d \times N_{t, i, x} \\
    N_{t, i, x, \text{sedentaries}} &= (1 - d) N_{t, i, x}\\
\end{aligned}
$$

For then for each species all immigrants are split evenly across patches:

$$
    N_{t, i, \text{migrants}} = \sum_{y = 1}^{P} d \times N_{t, i, y}
$$
with $P$ the total number of patches.

Finally the number of inviduals in the patch after dispersal becomes:

$$
\begin{aligned}
N_{t, i, x} = N_{t, i, x, \text{sedentaries}} + \frac{N_{t, i, \text{migrants}}}{P} \\
N_{t, i, x} = (1 - d) N_{t, i, x} + d \frac{\displaystyle\sum_{y = 1}^{P} N_{t, i, y}}{P}\\
N_{t, i, x} = (1 - \frac{P - 1}{P}d) N_{t, i, x} +
    d \frac{\displaystyle\sum_{y = 1, y \neq x}^{P} N_{t, i, y}}{P} \\
N_{t, i, x} = N_{t, i, x} \underbrace{- d N_{t,i,x} + d \frac{\displaystyle\sum_{y = 1}^{P} N_{t, i, k}}{P}}_{D_{t,i,x}} \\
D_{t, i, x} = - d N_{t,i,x} + d \frac{\displaystyle\sum_{y = 1}^{P} N_{t, i, y}}{P}
\end{aligned}
$$

## Final equation

### Single trait contributing to everything

If a single trait contributes to everything than the equation simplifies as there is no need to to account for the difference in contribution to the different processes the final equation (and as such $a_r = b_r = c_r = 1$)

$$
N_{t+1, i, x} = \frac{
    \left[
        k \exp\left(-
        \frac{(t_i - t_{opt, x})^2}{2\times l^2} \right)
        - H \displaystyle\sum_{
                \substack{j = 1\\
                          t_j \ge t_i}}^S
            N_{t,j,x} (t_j - t_i)^h
            \right]
    \times N_{t, i, x} }{
        1 + A \displaystyle\sum_{j = 1,~j \neq i}^{S} N_{t, j, x} \times \sigma_{ij} +
        B \times N_{t,i,x}
} + D_{t, i, x}
$$
If we fully develop the limiting similarity term it becomes:

$$
N_{t+1, i, x} = \frac{
    \left[
        k \exp\left(-
        \frac{(t_{i, r} - t_{opt, x})^2}{2\times l^2} \right)
        - H \displaystyle\sum_{
                \substack{j = 1\\
                          t_{j, r} \ge t_{i, r}}}^S
            N_{t,j,x} (t_{j, r} - t_{i, r})^h
            \right]
    \times N_{t, i, x} }{
        1 + A \displaystyle\sum_{j = 1,~j \neq i}^{S} N_{t, j, x} \times \left[ \max_{\forall~i,j \in [1, S]^2}[(t_{i} - t_{j})^s] - \left(t_{i} - t_{j}\right)^s \right] +
        B \times N_{t,i,x}
} + D_{t, i, x}
$$


### Multiple traits co-contributing to some processes

To account for the various contributions of multiple processes to the different processes the final equation is:

$$
N_{t+1, i, x} = \frac{
    \left[
        k \displaystyle\sum_{r = 1}^{T} b_r \times \exp\left(-
        \frac{(t_{i, r} - t_{opt, x})^2}{2\times l^2} \right)
        - H \sum_{r = 1}^T c_r \displaystyle\sum_{
                \substack{j = 1\\
                          t_{j, r} \ge t_{i, r}}}^S
            N_{t,j,x} (t_{j, r} - t_{i, r})^h
            \right]
    \times N_{t, i, x} }{
        1 + A \displaystyle\sum_{j = 1,~j \neq i}^{S} N_{t, j, x} \times \sigma_{ij} +
        B \times N_{t,i,x}
} + D_{t, i, x}
$$

with $b_r$ and $c_r$, the contribution respectively of trait to growth and hierarchical competition with $T$ the number of traits.

If we develop fully the similarity term $\sigma_{ij}$ it gives:

$$
N_{t+1, i, x} = \frac{
    \left[
        k \displaystyle\sum_{r = 1}^{T} b_r \times \exp\left(-
        \frac{(t_{i, r} - t_{opt, x})^2}{2\times l^2} \right)
        - H \sum_{r = 1}^T c_r \displaystyle\sum_{
                \substack{j = 1\\
                          t_{j, r} \ge t_{i, r}}}^S
            N_{t,j,x} (t_{j, r} - t_{i, r})^h
            \right]
    \times N_{t, i, x} }{
        1 + A \displaystyle\sum_{j = 1,~j \neq i}^{S} N_{t, j, x} \times 
        \left[\max_{\forall~i,j \in [1, S]^2} \left[
            \left(\sqrt{\sum_{r = 1}^{T} a_r \times (t_{i, r} - t_{j, r})^2}\right)^s
            \right] -
            \left(\sqrt{\sum_{r = 1}^{T} a_r \times (t_{i, r} - t_{j, r})^2}\right)^s
        \right] + 
        B \times N_{t,i,x}
} + D_{t, i, x}
$$
