{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0237d0fb",
   "metadata": {},
   "source": [
    "# Sampling variability\n",
    "We create population (known distribution), then repeatedly take random samples and computes the sample mean. Then, we compare how variability changes for different sample sizes (n). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "aa141540",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(np.float64(3.2549582990800525), np.float64(2.13814149865377))"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "rng = np.random.default_rng(7)\n",
    "population = rng.lognormal(mean=1.0, sigma=0.6, size=200_000)\n",
    "popMu = population.mean()\n",
    "popSD = population.std(ddof=0)\n",
    "popMu, popSD"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f2be2155",
   "metadata": {},
   "outputs": [],
   "source": [
    "def SamplingMeans(popu,n,R):\n",
    "    rng = np.random.default_rng(7)\n",
    "    means = np.empty(R)\n",
    "    for i in range(R):\n",
    "        sample = rng.choice(popu,size=n, replace=False)\n",
    "        means[i] = sample.mean()\n",
    "    return means"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "0e35402a",
   "metadata": {},
   "outputs": [],
   "source": [
    "meansn10 = SamplingMeans(population, n=30, R=3000)\n",
    "meansn20 = SamplingMeans(population, n=50, R=3000)\n",
    "meansn30 = SamplingMeans(population, n=100, R=3000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "255cf249",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(np.float64(0.3878374423877889),\n",
       " np.float64(0.3033596828581503),\n",
       " np.float64(0.21441262896303093))"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(meansn10, ddof=1), np.std(meansn20, ddof=1), np.std(meansn30, ddof=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "d41f1b1a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 21344 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "bins = 10 \n",
    "plt.figure()\n",
    "plt.hist(meansn10, bins=bins)\n",
    "plt.axvline(popMu, color=\"red\", alpha=0.5)\n",
    "plt.title(\"Sampling distribution of mean (n=10)\")\n",
    "plt.xlabel(\"Sample mean\"), plt.ylabel(\"count\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "3219b5e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 21376 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 19044 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 18976 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "bins = 10 \n",
    "plt.figure()\n",
    "plt.hist(meansn10, bins=bins)\n",
    "plt.axvline(popMu, color=\"red\", alpha=0.9)\n",
    "plt.title(\"Sampling distribution of mean (n=10)\")\n",
    "plt.xlabel(\"Sample mean\"), plt.ylabel(\"count\")\n",
    "plt.show()\n",
    "\n",
    "plt.figure()\n",
    "plt.hist(meansn20, bins=bins)\n",
    "plt.axvline(popMu, color=\"red\", alpha=0.5)\n",
    "plt.title(\"Sampling distribution of mean (n=20)\")\n",
    "plt.xlabel(\"Sample mean\"), plt.ylabel(\"count\")\n",
    "plt.show()\n",
    "\n",
    "plt.figure()\n",
    "plt.hist(meansn30, bins=bins)\n",
    "plt.axvline(popMu, color=\"red\", alpha=0.5)\n",
    "plt.title(\"Sampling distribution of mean (n=30)\")\n",
    "plt.xlabel(\"Sample mean\"), plt.ylabel(\"count\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "30f3f241",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 16664 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "bins = 20\n",
    "plt.figure()\n",
    "plt.hist(population, bins=bins)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "7234d672",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 21824 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "bins = 10 \n",
    "plt.figure()\n",
    "plt.hist(meansn10, bins=bins,label=\"n=10\")\n",
    "plt.hist(meansn20, bins=bins, label=\"n=50\")\n",
    "plt.hist(meansn30, bins=bins, label=\"n=100\")\n",
    "plt.axvline(popMu, color=\"red\", alpha=0.9)\n",
    "plt.title(\"Sampling distribution of mean (n=10)\")\n",
    "plt.xlabel(\"Sample mean\"), plt.ylabel(\"count\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv (3.14.2)",
   "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.14.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
