{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "95067e6f",
   "metadata": {},
   "source": [
    "# TP4- Probability Distributions and Simulation\n",
    "\n",
    "## Instructions\n",
    "\n",
    "In this practical session, you will use Python (NumPy, SciPy, Matplotlib) to simulate probability distributions, compute empirical statistics, and compare them with the theoretical values. Each exercise includes simulation tasks and interpretation questions.\n",
    "\n",
    "## Exercise 1- Simulation of a Discrete Distribution and a Normal Distribution\n",
    "\n",
    "Consider the following two random variables:\n",
    "\n",
    "- A discrete random variable $X$ taking values $0, 1, 2$ with probabilities:\n",
    "\n",
    "$$\n",
    "P(X = 0) = 0.2, \\quad P(X = 1) = 0.5, \\quad P(X = 2) = 0.3\n",
    "$$\n",
    "\n",
    "- A continuous random variable $Z$ following a standard normal distribution:\n",
    "\n",
    "$$\n",
    "Z \\sim \\mathcal{N}(0, 1)\n",
    "$$\n",
    "\n",
    "1. Simulate $n = 10 000$ realizations of $X$ and $Z$ in Numpy.\n",
    "\n",
    "2. Estimate the empirical probabilities $\\hat{P}(X = k)$ for $k = 0, 1, 2$ and compare them with the theoretical probabilities.\n",
    "\n",
    "3. Estimate emprically the probability $$P(-1 \\leq Z \\leq 1)$$ and compare it with the theoretical probability $$P(-1 \\leq Z \\leq 1) = \\phi(1) - \\phi(-1),$$ where $\\phi$ is the cumulative distribution function of the standard normal distribution.\n",
    "\n",
    "### Solution\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8923708a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import scipy.stats as stats\n",
    "\n",
    "# Setup\n",
    "np.random.seed(42)\n",
    "n = 10000\n",
    "\n",
    "# 1. Simulate Discrete Variable X\n",
    "# X takes values 0, 1, 2 with given probabilities\n",
    "values_X = [0, 1, 2]\n",
    "probs_X = [0.2, 0.5, 0.3]\n",
    "X_sim = np.random.choice(values_X, size=n, p=probs_X)\n",
    "\n",
    "# Simulate Continuous Variable Z ~ N(0,1)\n",
    "# Using scipy.stats.norm.rvs instead of np.random.normal\n",
    "Z_sim = stats.norm.rvs(loc=0, scale=1, size=n)\n",
    "\n",
    "\n",
    "# 2. Analysis of Discrete Variable X\n",
    "print(\"--- Analysis of X (Discrete) ---\")\n",
    "# Count how many times each value appears\n",
    "unique, counts = np.unique(X_sim, return_counts=True)\n",
    "empirical_probs_X = counts / n\n",
    "\n",
    "# Compare empirical vs theoretical probabilities\n",
    "for val, prob_emp, prob_theo in zip(values_X, empirical_probs_X, probs_X):\n",
    "    print(f\"P(X={val}): Empirical = {prob_emp:.4f} | Theoretical = {prob_theo:.4f}\")\n",
    "\n",
    "\n",
    "# 3. Analysis of Continuous Variable Z\n",
    "print(\"\\n--- Analysis of Z (Continuous) ---\")\n",
    "# Count how many Z values fall between -1 and 1\n",
    "empirical_prob_Z = np.mean((Z_sim >= -1) & (Z_sim <= 1))\n",
    "\n",
    "# Calculate theoretical probability using CDF\n",
    "theoretical_prob_Z = stats.norm.cdf(1) - stats.norm.cdf(-1)\n",
    "\n",
    "print(f\"P(-1 <= Z <= 1):\")\n",
    "print(f\"Empirical   = {empirical_prob_Z:.4f}\")\n",
    "print(f\"Theoretical = {theoretical_prob_Z:.4f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e0bc7f4",
   "metadata": {},
   "source": [
    "---\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2105d7b8",
   "metadata": {},
   "source": [
    "## Exercise 2- Empirical Mean and Density of a Normal Distribution\n",
    "\n",
    "Let $X$ be a random variable following a normal distribution:\n",
    "\n",
    "$$\n",
    "X \\sim \\mathcal{N}(\\mu, \\sigma^2), \\quad \\mu = 2, \\quad \\sigma = 3\n",
    "$$\n",
    "\n",
    "1. Simulate $n = 5000$ realizations of $X$.\n",
    "\n",
    "2. Compute the empirical mean $\\hat{X}$ and empirical variance, then compare them to the theoretical values $\\mu = 2$ and $\\sigma^2 = 9$.\n",
    "\n",
    "3. Plot the normalized histogram of the simulated data and superimpose the theoretical density of the normal distribution $\\mathcal{N}(2, 9)$.\n",
    "\n",
    "### Solution\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0821c25b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import scipy.stats as stats\n",
    "\n",
    "# Setup\n",
    "np.random.seed(42)\n",
    "n = 5000\n",
    "mu = 2\n",
    "sigma = 3\n",
    "\n",
    "# 1. Simulate X ~ N(2, 9) using scipy\n",
    "X_sim = stats.norm.rvs(loc=mu, scale=sigma, size=n)\n",
    "\n",
    "# 2. Compute Empirical Statistics\n",
    "empirical_mean = np.mean(X_sim)\n",
    "empirical_var = np.var(X_sim)\n",
    "theoretical_var = sigma**2\n",
    "\n",
    "print(f\"Mean:     Empirical = {empirical_mean:.4f} | Theoretical = {mu}\")\n",
    "print(f\"Variance: Empirical = {empirical_var:.4f} | Theoretical = {theoretical_var}\")\n",
    "\n",
    "# 3. Plot Histogram vs Theoretical Density\n",
    "plt.figure(figsize=(10, 6))\n",
    "\n",
    "# Histogram of simulated data\n",
    "plt.hist(\n",
    "    X_sim,\n",
    "    bins=50,\n",
    "    density=True,\n",
    "    alpha=0.6,\n",
    "    color=\"skyblue\",\n",
    "    edgecolor=\"black\",\n",
    "    label=\"Empirical Histogram\",\n",
    ")\n",
    "\n",
    "# Theoretical density curve\n",
    "x_vals = np.linspace(X_sim.min(), X_sim.max(), 1000)\n",
    "pdf_vals = stats.norm.pdf(x_vals, loc=mu, scale=sigma)\n",
    "plt.plot(\n",
    "    x_vals,\n",
    "    pdf_vals,\n",
    "    \"r-\",\n",
    "    linewidth=2.5,\n",
    "    label=r\"Theoretical Density $\\mathcal{N}(2, 9)$\",\n",
    ")\n",
    "\n",
    "plt.title(\"Histogram vs Theoretical Normal Density\")\n",
    "plt.xlabel(\"Value of X\")\n",
    "plt.ylabel(\"Density\")\n",
    "plt.legend()\n",
    "plt.grid(True, alpha=0.3)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0eeaf6f4",
   "metadata": {},
   "source": [
    "---\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aac4f4dc",
   "metadata": {},
   "source": [
    "## Exercise 3- Binomial Distribution: Simulation vs. Theory\n",
    "\n",
    "Let $Y$ be a binomial random variable:\n",
    "\n",
    "$$\n",
    "Y \\sim \\mathcal{B}(n, p), \\quad n=20, \\quad p=0.4\n",
    "$$\n",
    "\n",
    "1. Recall the theoretical formulas for the expectation and variance of a binomial distribution: $$E[Y] = np, \\quad Var(Y) = np(1-p).$$\n",
    "\n",
    "2. Simulate $N = 5\\ 000$ realizations of $Y$.\n",
    "\n",
    "3. Compute the empirical mean and empirical variance, and compare them with the theoretical values.\n",
    "\n",
    "4. Estimate empirically the probabilities $\\hat{P}(Y=k)$ for $k=0, 1, ..., 20$.\n",
    "\n",
    "5. Plot on the same graph:\n",
    "\n",
    "- a bar chart of the empirical probabilities,\n",
    "- the theoretical PMF of the binomial distribution.\n",
    "\n",
    "Comment on the accuracy of the empirical approximation.\n",
    "\n",
    "### Solution\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d717025b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import scipy.stats as stats\n",
    "\n",
    "# Setup\n",
    "np.random.seed(42)\n",
    "n = 20\n",
    "p = 0.4\n",
    "N = 5000\n",
    "\n",
    "# 1. Theoretical Values\n",
    "theoretical_mean = n * p\n",
    "theoretical_var = n * p * (1 - p)\n",
    "\n",
    "print(\"--- Theoretical Values ---\")\n",
    "print(f\"E[Y] = {theoretical_mean}\")\n",
    "print(f\"Var(Y) = {theoretical_var:.2f}\")\n",
    "\n",
    "# 2. Simulate Y ~ Binomial(20, 0.4) using scipy\n",
    "Y_sim = stats.binom.rvs(n=n, p=p, size=N)\n",
    "\n",
    "# 3. Empirical Statistics\n",
    "empirical_mean = np.mean(Y_sim)\n",
    "empirical_var = np.var(Y_sim)\n",
    "\n",
    "print(\"\\n--- Empirical Statistics ---\")\n",
    "print(f\"Empirical Mean = {empirical_mean:.4f}\")\n",
    "print(f\"Empirical Var  = {empirical_var:.4f}\")\n",
    "\n",
    "# 4 & 5. Plot Empirical vs Theoretical Probabilities\n",
    "plt.figure(figsize=(10, 6))\n",
    "\n",
    "# Empirical probabilities (histogram)\n",
    "bins = np.arange(-0.5, n + 1.5, 1)\n",
    "plt.hist(\n",
    "    Y_sim,\n",
    "    bins=bins,\n",
    "    density=True,\n",
    "    rwidth=0.8,\n",
    "    color=\"skyblue\",\n",
    "    edgecolor=\"black\",\n",
    "    label=\"Empirical\",\n",
    ")\n",
    "\n",
    "# Theoretical PMF\n",
    "k_values = np.arange(0, n + 1)\n",
    "theoretical_probs = stats.binom.pmf(k_values, n, p)\n",
    "plt.plot(\n",
    "    k_values,\n",
    "    theoretical_probs,\n",
    "    \"ro-\",\n",
    "    linewidth=2,\n",
    "    markersize=6,\n",
    "    label=\"Theoretical PMF\",\n",
    ")\n",
    "\n",
    "plt.title(f\"Binomial Distribution: Simulation vs Theory (n={n}, p={p})\")\n",
    "plt.xlabel(\"Number of Successes (k)\")\n",
    "plt.ylabel(\"Probability P(Y=k)\")\n",
    "plt.xticks(np.arange(0, n + 1, 2))\n",
    "plt.legend()\n",
    "plt.grid(axis=\"y\", alpha=0.3)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b83b6ee3",
   "metadata": {},
   "source": [
    "## Exercise 4- Poisson Distribution\n",
    "\n",
    "A call center receives an average of $\\lambda = 4$ calls per minute.\n",
    "\n",
    "1. Simulate $10000$ minutes of activity ($n = 10000$).\n",
    "2. Calculate the **Empirical Mean** and **Empirical Variance**.\n",
    "3. Verify if the are approximately qual (validating the poisson property).\n",
    "4. Calculate the theoretical probability of reveiving exactly 0 calls in a minute ($P(X=0)$) and compare it to your simulation.\n",
    "\n",
    "### Solution\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f9ab34c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy.stats import poisson\n",
    "\n",
    "# 1. Setup\n",
    "lam = 4  # Average rate (lambda)\n",
    "n = 10000  # Number of simulations\n",
    "\n",
    "# 2. Simulate\n",
    "# generating n minutes of call counts\n",
    "X_sim = poisson.rvs(mu=lam, size=n)\n",
    "\n",
    "# 3. Verify Mean = Variance Property\n",
    "emp_mean = np.mean(X_sim)\n",
    "emp_var = np.var(X_sim)\n",
    "\n",
    "print(f\"Theoretical Lambda: {lam}\")\n",
    "print(f\"Empirical Mean:     {emp_mean:.4f}\")\n",
    "print(f\"Empirical Variance: {emp_var:.4f}\")\n",
    "# These two values should be very close!\n",
    "\n",
    "# 4. Probability of 0 calls (Silence)\n",
    "# Theoretical PMF: (lambda^0 * e^-lambda) / 0!\n",
    "prob_zero_theo = poisson.pmf(0, mu=lam)\n",
    "prob_zero_emp = np.mean(X_sim == 0)\n",
    "\n",
    "print(f\"\\nP(X=0) Theoretical: {prob_zero_theo:.4f}\")\n",
    "print(f\"P(X=0) Empirical:   {prob_zero_emp:.4f}\")"
   ]
  }
 ],
 "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.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
