1import matplotlib.pyplot as plt
2import numpy as np
3import scipy.stats as stats
4import seaborn as sns
5from src.code.simulation.galton_watson import GaltonWatson
6from src.code.simulation.utils import plot_zn_distribution, test_loi_exponentielle
7from src.config.config import seed
8from src.utils.utils import init_notebook
1init_notebook(seed)

Simulation Galton-Watson#

Loi de Poisson#

λ = 1#

Soit \(L\) la loi de reproduction.

Nous avons \(L \sim {\mathrm {Poisson}}(1)\).

1poisson_1 = stats.poisson(1)
1gp1 = GaltonWatson(poisson_1)
2gp1
Processus Galton-Watson
- loi de reproduction L : poisson
- espérance E[L] = 1.0
- époque n = 0
- nombre de survivants Z_n = 1
1nb_survivants = gp1.simule(20)
1print(f"Il reste {nb_survivants} survivants au bout de {gp1.n} époques.")
Il reste 0 survivants au bout de 10 époques.
1plt.figure(figsize=(15, 10))
2gp1.plot_historique_descendants()
3plt.savefig("assets/img/number-of-children.png")
../_images/427bdebe6d63eda51de60007f429fc08869d464fc0200ebab7851e3e74baa42d.png
1# noinspection JupyterPackage
2plt.figure(figsize=(15, 10))
3gp1.plot_zn_sur_n()
4plt.savefig("assets/img/zn-over-n.png")
../_images/2d362b0aa95f328b4de318f65635f9053857c10cdc10bf341b81145fe6d9b373.png

Arbre de Galton-Watson#

1plt.figure(figsize=(12, 10))
2gp1.plot_arbre()
3plt.savefig("assets/img/galton-watson-tree.png", transparent=True)
4plt.savefig("assets/img/galton-watson-tree.svg", transparent=True)
../_images/fba2fbf4c7b0ec1fcb3edc1e62718e073931d221b9217fcb560559d9118ca85d.png
1gp1.plot_arbre(circular=True)
../_images/7a899b942b171602401d508baf9eb27070ba892d20fd1fabf14593ae62904936.png

λ = 2#

1poisson_2 = stats.poisson(2)
1gp2 = GaltonWatson(poisson_2)
2gp2
Processus Galton-Watson
- loi de reproduction L : poisson
- espérance E[L] = 2.0
- époque n = 0
- nombre de survivants Z_n = 1
1nb_survivants = gp2.simule(20)
1print(f"Il reste {nb_survivants} survivants au bout de {gp2.n} époques.")
Il reste 680149 survivants au bout de 20 époques.
1gp2.plot_historique_descendants(logscale=True, affiche_moyenne=True)
../_images/0dcbcac8d40515d50dbb3095961249e6fac5eaabdcd953365a73e772d9f81858.png
1gp2.plot_zn_sur_n(logscale=True)
../_images/174d85114e78b54ef56358af8a821adc3b587aec8e4e52b913cd3b5cb6c586cb.png

Essais \(Z_n / n\)#

1nb_simulations = 10_000
2nb_epoques = 100
1simulations = gp1.lance_simulations(nb_simulations, nb_epoques)
2simulations = np.array(simulations)
1plt.figure(figsize=(25, 10))
2plot_zn_distribution(simulations, nb_epoques)
<Figure size 2500x1000 with 0 Axes>
../_images/4560caba18dfc54062896b55aa8ae7821e9f2abba90453e8861d1b86675a70c1.png
1np.sum(simulations > 0)
np.int64(191)
1zn_sup_zero = simulations[simulations > 0]
1plt.figure(figsize=(10, 10))
2plt.title("Distribution des $Z_n$,\n$n = 100$, et $Z_n > 0$")
3plt.hist(zn_sup_zero)
4plt.savefig("assets/img/zn-sup-zero-distribution.png")
../_images/024df831e70d61b4bd21d2a384fe377808977a2fc9316fccacbd12a02f9a17c7.png
1lambda_estime = 1.0 / np.mean(zn_sup_zero / nb_epoques)
2print(f"{lambda_estime = }")
lambda_estime = np.float64(1.902959051509415)
1loi_expo1 = stats.expon(scale=1 / lambda_estime)
1echantillon_expo = loi_expo1.rvs(size=len(zn_sup_zero))
 1plt.figure(figsize=(10, 10))
 2plt.title("Distribution de $Z_{n} / n$, $Z_n > 0$ avec $n = 100$")
 3sns.histplot(zn_sup_zero / nb_epoques, stat="probability", label="$Z_n / n$")
 4sns.histplot(
 5    echantillon_expo,
 6    stat="probability",
 7    label=f"Échantillon de loi exponentielle ($\\lambda = {lambda_estime: 0.2}$)",
 8)
 9
10plt.legend()
11
12plt.savefig("assets/img/zn-sup-zero-distribution-with-exponential.png")
../_images/b0cc53c9643de5efa75236d7ca1267aec89cfc45fa4fc5364f5f91ef5cabc487.png
1test_loi_exponentielle(zn_sup_zero / nb_epoques)
(np.float64(0.531818713902256), np.float64(0.057586680541798524))

Loi uniforme sur {0, 1, 2}#

Soit \(L\) la loi de reproduction.

Nous avons \(L \sim {\mathrm {Uniforme}}(0, 2)\).

1uniforme2 = stats.randint(0, 3)
1gu2 = GaltonWatson(uniforme2)
2gu2
Processus Galton-Watson
- loi de reproduction L : randint
- espérance E[L] = 1.0
- époque n = 0
- nombre de survivants Z_n = 1
1nb_survivants = gu2.simule(100)
1print(f"Il reste {nb_survivants} survivants au bout de {gu2.n} époques.")
Il reste 23 survivants au bout de 100 époques.
1gu2.plot_historique_descendants()
../_images/dbfbff319d4ec7ab38467cf05072f6ade82bb20bd7bcc057f4e410456fa9bc01.png
1gu2.plot_zn_sur_n()
../_images/19cc426462db0666db88373a6c119fb81eacae7a3c716f5790a06b04d5eda291.png

Arbre de Galton-Watson#

1gu2.plot_arbre()
../_images/3a3f074bd027491038216d269178c657c8acbb45cf01791028829fcaa8e75eda.png

Essais \(Z_n / n\)#

1nb_simulations = 10_000
2nb_epoques = 100
3
4simulations = gu2.lance_simulations(nb_simulations, nb_epoques)
1simulations = np.array(simulations)
1plot_zn_distribution(simulations, nb_epoques)
../_images/8d268454f5ba119354fb324087b8f066cf2cbd4ac84424d585f46cf1212eca2e.png
1np.sum(simulations > 0)
np.int64(295)
1zn_sup_zero = simulations[simulations > 0]
1plt.figure(figsize=(10, 10))
2plt.title("Distribution des $Z_n$ avec $Z_n > 0$")
3plt.hist(zn_sup_zero)
4
5plt.savefig("assets/img/zn-sup-zero-distribution-uniform.png")
../_images/8d38067f12a8f1446003d57e8903ae9d61066deb96c9f3ada9b3fd0d184dcac5.png
1lambda_estime = 1.0 / np.mean(zn_sup_zero / nb_epoques)
2print(f"{lambda_estime = }")
lambda_estime = np.float64(2.6538323137819364)
1loi_expo1 = stats.expon(scale=1 / lambda_estime)
1echantillon_expo = loi_expo1.rvs(size=len(zn_sup_zero))
 1plt.figure(figsize=(10, 10))
 2plt.title("Distribution de $Z_{n} / n$, $Z_n > 0$ avec $n = 100$")
 3sns.histplot(zn_sup_zero / nb_epoques, stat="probability", label="$Z_n / n$")
 4sns.histplot(
 5    echantillon_expo,
 6    stat="probability",
 7    label=f"Échantillon de loi exponentielle ($\\lambda = {lambda_estime: 0.2}$)",
 8)
 9
10plt.legend()
11plt.savefig("assets/img/zn-sup-zero-distribution-uniform-with-exponential.png")
../_images/82a584f8c9c8ae3f15fc22090f23536734b66bc0b91c8cbf27baff9d4e682017.png
1p_value, _ = test_loi_exponentielle(zn_sup_zero / nb_epoques)

Expérimentations#

1for _ in range(500):
2    gp1.reset()
3    gp1.simule(20)
4    gp1.plot_historique_descendants()
../_images/13b08d09dd46e6b3022a13717c33296fb7bc2611b724eee75867e3a075129ca3.png
1for _ in range(500):
2    gp1.reset()
3    gp1.simule(20)
4    gp1.plot_zn_sur_n()
../_images/67db4047751e389a8e525553f61aac72e7a1cf5c7d0c6f9c02d11080b747a5ea.png