278 lines
9.3 KiB
Plaintext
278 lines
9.3 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import numpy as np\n",
|
|
"import matplotlib.pyplot as plt\n",
|
|
"import matplotlib.gridspec as gridspec\n",
|
|
"import pandas as pd\n",
|
|
"import numpy.random as rnd\n",
|
|
"from scipy.integrate import odeint,quad\n",
|
|
"from scipy.stats import kde,beta\n",
|
|
"import seaborn as sns\n",
|
|
"%matplotlib inline\n",
|
|
"from importlib import reload\n",
|
|
"pi=np.pi\n",
|
|
"\n",
|
|
"\n",
|
|
"from numpy import linalg as LA\n",
|
|
"from scipy.linalg import expm\n",
|
|
"from scipy.optimize import brentq\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import stablecompoper\n",
|
|
"sns.set()\n",
|
|
"plt.rc('text', usetex=False)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## The monotype linear birth and death process in a periodic environment\n",
|
|
"\n",
|
|
"This process $(X(t),t\\ge 0)$ with values in $\\mathbb{N}$ is described by its\n",
|
|
"time varying generator\n",
|
|
"\\begin{equation}\n",
|
|
" L_t f(x) = x\\left[\\lambda(t)(f(x+1)-f(x)) + \\mu(t) (f(x-1)-f(x))\\right]\\,,\n",
|
|
"\\end{equation}\n",
|
|
"where $\\lambda,\\mu$ are non negative $T$-periodic functions.\n",
|
|
"\n",
|
|
"Let $Z(t)$ be the point measure on $S$ describing the set of states\n",
|
|
"(i.e. phases) of the individuals born before $t$ and still alive at\n",
|
|
"time $t$ : if $Z(t) = \\sum_i \\delta_{s_i}$ then $<Z(t), f>=\n",
|
|
"\\sum_i f(s_i)$. We have the convergence in $L^1$, when we start with $X(s)=1$ one individual of phase $s$,\n",
|
|
"\\begin{equation}\n",
|
|
" \\lim_{n\\to +\\infty} e^{-\\alpha(nT-s)} <Z(nT),f> = h(s) \\int_S f(t)\\, d\\pi(t)\\,,\n",
|
|
"\\end{equation}\n",
|
|
"where the reproductive value of phase $s$ is the periodic function for $T=1$\n",
|
|
"\\begin{equation}\n",
|
|
" h(s) = e^{\\alpha s -\\varphi(s)}\\,,\n",
|
|
"\\end{equation}\n",
|
|
"and the measure $\\pi$ is the stable composition law\n",
|
|
"\\begin{equation}\n",
|
|
" \\boxed{\\pi(dt) = \\frac1{e^{A(T)} -1} \\lambda(t) e^{A(t)}\\, 1_{t\\in(0,T)}\\, dt\\,.}\n",
|
|
"\\end{equation}\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"The process is one dimensional, and the death rate is constant\n",
|
|
"$\\mu(t)=\\mu_0$ and the birth rate is\n",
|
|
"\\begin{equation}\n",
|
|
" \\lambda(t) = \\lambda_0 (1 + c \\cos(2\\pi t/T))\\,.\n",
|
|
"\\end{equation}\n",
|
|
"The stable composition law is thus\n",
|
|
"\\begin{equation}\n",
|
|
"\\pi(dt) = \\frac1{e^{A(T)} -1} \\lambda(t) e^{A(t)}\\, 1_{t\\in(0,T)}\\,\n",
|
|
"dt\\,,\n",
|
|
"\\end{equation}\n",
|
|
"with\n",
|
|
"\\begin{equation}\n",
|
|
" A(t) = \\lambda_0 (t + \\frac{ c T}{2 \\pi} \\sin(2\\pi t/T) )\n",
|
|
"\\end{equation}\n",
|
|
"We perform a simulation of the linear birth and death process for $N$\n",
|
|
"periods, and we keep the phase, the birth dates modulo $T$, of the\n",
|
|
"living individuals at time $N T$. We wait until the first non extinct\n",
|
|
"population, and then we plot its histogram against the true density\n",
|
|
"$\\pi$ and against the birth rate $\\lambda(t)$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {
|
|
"scrolled": true
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"application/vnd.jupyter.widget-view+json": {
|
|
"model_id": "8554deb58f8549b4a98f2704c1e40090",
|
|
"version_major": 2,
|
|
"version_minor": 0
|
|
},
|
|
"text/plain": [
|
|
"interactive(children=(FloatSlider(value=0.8, continuous_update=False, description='lzero', max=4.0), FloatSlid…"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"<function stablecompoper.nsestimdenszchi(lzero, muzero, T, N, coeff=1.0, estimnoyau=False, image=False)>"
|
|
]
|
|
},
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"#reload(stablecompoper)\n",
|
|
"#from ipywidgets import interact, interactive, fixed, interact_manual\n",
|
|
"#import ipywidgets as widgets\n",
|
|
"#interact(stablecompoper.nsestimdenszchi,\n",
|
|
"# lzero=widgets.FloatSlider(min=0.0, max=4.0, step=0.1, value=0.8, continuous_update=False),\n",
|
|
"# muzero=widgets.FloatSlider(min=0.0, max=2.0, step=0.1, value=0.1, continuous_update=False),\n",
|
|
"# T=widgets.IntSlider(min=1, max=10, step=1, value=2, continuous_update=False),\n",
|
|
"#N=widgets.IntSlider(min=1, max=20, step=1, value=8, continuous_update=False),\n",
|
|
"# coeff=widgets.FloatSlider(min=0.0, max=2.0, step=0.1, value=0.5, continuous_update=False),\n",
|
|
"# )\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"#reload(stablecompoper)\n",
|
|
"#from IPython.display import display\n",
|
|
"#w=interactive(stablecompoper.nsestimdenszchi,\n",
|
|
"# lzero=widgets.FloatSlider(min=0.0, max=4.0, step=0.1, value=0.8, continuous_update=False),\n",
|
|
"# muzero=widgets.FloatSlider(min=0.0, max=2.0, step=0.1, value=0.1, continuous_update=False),\n",
|
|
"# T=widgets.IntSlider(min=1, max=10, step=1, value=2, continuous_update=False),\n",
|
|
"#N=widgets.IntSlider(min=1, max=20, step=1, value=8, continuous_update=False),\n",
|
|
"# coeff=widgets.FloatSlider(min=0.0, max=2.0, step=0.1, value=0.5, continuous_update=False),\n",
|
|
"# )\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {
|
|
"scrolled": true
|
|
},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"application/vnd.jupyter.widget-view+json": {
|
|
"model_id": "c12f3784d53a4c7994ab875ce1163f87",
|
|
"version_major": 2,
|
|
"version_minor": 0
|
|
},
|
|
"text/plain": [
|
|
"interactive(children=(FloatSlider(value=0.8, continuous_update=False, description='lzero', max=4.0), FloatSlid…"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
}
|
|
],
|
|
"source": [
|
|
"#display(w)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### Remarque\n",
|
|
"Il ne faut pas partir avec un nombre de périodes $N$ trop grand, ni une période $T$ trop grande, sinon la taille de l'échantillon est beaucoup trop grande. Pour avoir une bonne estimation de la densité par l'histogramme, il suffit d'avoir une taile d'échantillon au dessus de 2000.En considerant le cas constant, on voit qu'il faut à peu près prendre $e^{N T (\\lambda_0 -\\mu_0)}\\simeq 2000$ ce qui donne $NT \\simeq 7.6/(\\lambda_0 -\\mu_0) \\simeq 12$\n",
|
|
"\n",
|
|
"Pour obtenir le cas où les taux de naissance et de mort sont constants, \n",
|
|
"i l suffit de prendre c=0."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"application/vnd.jupyter.widget-view+json": {
|
|
"model_id": "83cf7c407ce24ab6bf5e6f836c282f6e",
|
|
"version_major": 2,
|
|
"version_minor": 0
|
|
},
|
|
"text/plain": [
|
|
"GridspecLayout(children=(FloatSlider(value=0.8, continuous_update=False, description='$\\\\lambda_0$', layout=La…"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
},
|
|
{
|
|
"data": {
|
|
"application/vnd.jupyter.widget-view+json": {
|
|
"model_id": "dc265442f70b47cfa273d0add5e7d254",
|
|
"version_major": 2,
|
|
"version_minor": 0
|
|
},
|
|
"text/plain": [
|
|
"Output()"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"output_type": "display_data"
|
|
}
|
|
],
|
|
"source": [
|
|
"from ipywidgets import GridspecLayout,Layout,Button, AppLayout,TwoByTwoLayout,interactive_output\n",
|
|
"import ipywidgets as widgets\n",
|
|
"def create_expanded_button(description, button_style):\n",
|
|
" return Button(description=description, button_style=button_style, layout=Layout(height='auto', width='auto'))\n",
|
|
"grid = GridspecLayout(3, 3)\n",
|
|
"blzero=widgets.FloatSlider(min=0.0, max=4.0, step=0.1, value=0.8, continuous_update=False,description=r'$\\lambda_0$')\n",
|
|
"bmuzero=widgets.FloatSlider(min=0.0, max=2.0, step=0.1, value=0.1, continuous_update=False,description=r'$\\mu_0$')\n",
|
|
"bT=widgets.IntSlider(min=1, max=10, step=1, value=2, continuous_update=False,description='T')\n",
|
|
"bN=widgets.IntSlider(min=1, max=20, step=1, value=8, continuous_update=False,description='N')\n",
|
|
"bcoeff=widgets.FloatSlider(min=0.0, max=2.0, step=0.1, value=0.5, continuous_update=False,description='c')\n",
|
|
"\n",
|
|
"grid[0,0]=blzero\n",
|
|
"grid[0,1]=bmuzero\n",
|
|
"grid[0,2]=bcoeff\n",
|
|
"grid[1,0]=bT\n",
|
|
"grid[1,1]=bN\n",
|
|
"w=interactive_output(stablecompoper.nsestimdenszchi,{'lzero':blzero,'muzero':bmuzero,'T':bT,'N':bN,'coeff':bcoeff})\n",
|
|
"display(grid,w)\n",
|
|
"#grid\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.8.2"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|