{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# IntroStat Week 2 \n", "\n", "Welcome to the second lecture in IntroStat\n", "\n", "During the lectures we will present both slides and notebooks. \n", "\n", "This is the notebook used in the lecture in week 2.\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Week 2:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 1: Simulation of a stochastic variable (rolling a dice)" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2]\n" ] } ], "source": [ "result = np.random.choice(range(1,7), size=1)\n", "print(result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 2: Simulate n rolls with a fair dice" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[6 1 2 6 5 2 4 3 3 5 5 6 5 1 5 2 1 1 1 1 2 1 3 3 1 1 1 2 5 3]\n" ] } ], "source": [ "# Number of simulated realizations (sample size)\n", "n = 30\n", "\n", "# roll dice:\n", "xFair = np.random.choice([1,2,3,4,5,6], size=n, replace=True)\n", "print(xFair)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAjMAAAHFCAYAAAAHcXhbAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/TGe4hAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAtk0lEQVR4nO3de1SVdaL/8c9WcHsDUpBbIjJKXvJamGGNoo1MlJ7ULna6YWWjI1pETg06JnaB0pVjJ02PXdTymK6mLE9XsYTskI1ipjlmukKhlBAVNt62Cs/vj1nuXwQqIO5nf/X9Wmuv5f4+l/3ZDxqfnqvDsixLAAAAhmpidwAAAIDzQZkBAABGo8wAAACjUWYAAIDRKDMAAMBolBkAAGA0ygwAADAaZQYAABiNMgMAAIxGmQF80OLFi+VwOLRx48Zapw8bNkwdO3asNtaxY0eNGTOmXp+Tl5enjIwMlZWVNSzoJWjFihW68sor1aJFCzkcDm3evPm81nf6Z13ba/LkyXVez+7du+VwOLR48eLzygOYyM/uAAAax8qVKxUYGFivZfLy8jRjxgyNGTNGl1122YUJdhHZv3+/7r33Xt144416+eWX5XQ6dcUVVzTKuhctWqSuXbtWG4uMjKzz8hEREfrqq6/UqVOnRskDmIQyA1wk+vbta3eEejt58qQcDof8/Mz4T9EPP/ygkydP6p577tGgQYMadd09evRQXFxcg5d3Op269tprzznf0aNH1bJlywZ/DuCLOMwEXCR+e5ipqqpKzzzzjLp06aIWLVrosssuU69evfTiiy9KkjIyMvSXv/xFkhQTE+M5tJGTk+NZfubMmerataucTqdCQ0N133336aeffqr2uZZlKTMzU9HR0WrevLni4uKUnZ2thIQEJSQkeObLycmRw+HQm2++qccee0yXX365nE6ndu3apf3792vChAnq3r27WrdurdDQUA0ZMkTr1q2r9lmnD6XMmjVLzz//vDp27KgWLVooISHBUzT++te/KjIyUkFBQRo5cqRKSkrqtP1WrVql+Ph4tWzZUgEBARo6dKi++uorz/QxY8bo+uuvlySNHj1aDoej2vf77Ta56aabFBwcrMLCQs/40aNHdeWVV6pbt246cuRInXLt2rVL999/v2JjY9WyZUtdfvnlGj58uLZu3Vrrtvn1YaaMjAw5HA5t2rRJt912m9q0acOeG1yUzPjfIeASVVlZqVOnTtUYr8vD7mfOnKmMjAz97W9/08CBA3Xy5El9//33nvNjxo4dq4MHD+qll17Su+++q4iICElS9+7dJUl//vOftXDhQk2cOFHDhg3T7t27NW3aNOXk5GjTpk0KCQmRJE2dOlVZWVn605/+pFGjRqmoqEhjx47VyZMnaz0Ek56ervj4eC1YsEBNmjRRaGio9u/fL0maPn26wsPDdfjwYa1cuVIJCQn67LPPapSGefPmqVevXpo3b57Kysr02GOPafjw4erfv7/8/f31+uuva8+ePZo8ebLGjh2rVatWnXVbLVu2THfffbcSExP11ltvye12a+bMmZ7Pv/766zVt2jRdc801SklJUWZmpgYPHnzGw3qnS1ufPn10xx13aN26dfL399eECRNUUFCgr7/+Wq1ataq2TG0/az8/P+3du1fBwcF67rnn1K5dOx08eFBLlixR//799c0336hLly5n/W6SNGrUKN15550aP358nUsUYBQLgM9ZtGiRJemsr+jo6GrLREdHW8nJyZ73w4YNs/r06XPWz5k1a5YlySooKKg2vn37dkuSNWHChGrjX3/9tSXJmjJlimVZlnXw4EHL6XRao0ePrjbfV199ZUmyBg0a5Blbu3atJckaOHDgOb//qVOnrJMnT1o33HCDNXLkSM94QUGBJcnq3bu3VVlZ6RmfM2eOJcn6j//4j2rrSU1NtSRZ5eXlZ/ysyspKKzIy0urZs2e1dVZUVFihoaHWgAEDanyHt99++5zfwbIs68svv7T8/Pys1NRU6/XXX7ckWa+++mq1ec72sz558mSt2+bEiRNWbGys9eijj9bYNosWLfKMTZ8+3ZJkPfnkk3XKC5iKPTOAD3vjjTfUrVu3GuOPPvqoioqKzrrsNddcow8//FATJkzQLbfcovj4+DqfILx27VpJqnF11DXXXKNu3brps88+07PPPqv169fL7XbrjjvuqDbftddeW+Nqq9NuvfXWWscXLFighQsX6l//+pfcbrdn/LcnxUrSTTfdpCZN/v9R8tPb6Oabb6423+nxwsJC9ejRo9bP3bFjh/bu3avU1NRq62zdurVuvfVW/fd//3eDzzO57rrr9Oyzz+qJJ56Q0+nUPffcowcffLDWeWv7Wfv5+enUqVOaOXOmli5dql27dunkyZOe6du3b69TjjNtc+BiQZkBfFi3bt1qPSk0KCjonGUmPT1drVq10tKlS7VgwQI1bdpUAwcO1PPPP3/OE00PHDggSZ5DT78WGRmpPXv2VJsvLCysxny1jZ1pnbNnz9Zjjz2m8ePH6+mnn1ZISIiaNm2qadOm1foLu23bttXeN2vW7Kzjx48frzXLr7/Dmb5rVVWVDh061OCTZu+++25NmzZNbrfbc45Sbc70s05LS9O8efP0xBNPaNCgQWrTpo2aNGmisWPH6tixY3XKUNt3Ay4mlBngIuXn56e0tDSlpaWprKxMa9as0ZQpU/THP/5RRUVFZ/3lHBwcLEnat2+f2rdvX23a3r17PefLnJ7vl19+qbGO4uLiWvfOOByOGmNLly5VQkKC5s+fX228oqLi7F+yEfz6u/7W3r171aRJE7Vp06ZB666srNTdd9+tNm3ayOl06sEHH9T//d//eUpWXSxdulT33XefMjMzq42XlpbW+XL62rY5cDHhaibgEnDZZZfptttuU0pKig4ePKjdu3dL+vflvJJq/B/+kCFDJP37F+mvbdiwQdu3b9cNN9wgSerfv7+cTqdWrFhRbb7169d79t7UhcPh8GQ5bcuWLdWuJrpQunTpossvv1zLli2rdmL1kSNH9M4773iucGqI6dOna926dfqf//kfrVixQt9+++1Z987UprZt8+GHH+rnn39uUCbgYsSeGeAiNXz4cM+9S9q1a6c9e/Zozpw5io6OVmxsrCSpZ8+ekqQXX3xRycnJ8vf3V5cuXdSlSxf96U9/0ksvvaQmTZooKSnJczVTVFSUHn30UUn/PqyTlpamrKwstWnTRiNHjtRPP/2kGTNmKCIioto5KGczbNgwPf3005o+fboGDRqkHTt26KmnnlJMTEytV3M1piZNmmjmzJm6++67NWzYMI0bN05ut1uzZs1SWVmZnnvuuQatNzs7W1lZWZo2bZqn/GVlZWny5MlKSEjQyJEj67SeYcOGafHixeratat69eql/Px8zZo1q8YeM+BSRpkBLlKDBw/WO++8o1dffVUul0vh4eEaOnSopk2bJn9/f0lSQkKC0tPTtWTJEr3yyiuqqqrS2rVrPYd8OnXqpNdee03z5s1TUFCQbrzxRmVlZXkOzUjSs88+q1atWmnBggWeu9jOnz9fU6dOrfNhkKlTp+ro0aN67bXXNHPmTHXv3l0LFizQypUrPfe9uZDuuusutWrVSllZWRo9erSaNm2qa6+9VmvXrtWAAQPqvb59+/bpnnvuUUJCgp588knPeFpamnJzc/XAAw+ob9++ZzxJ+tdefPFF+fv7KysrS4cPH9ZVV12ld999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i2KykpsSRZubm5dkexVZs2baxXX33V7hheV1FRYcXGxlrZ2dnWoEGDrEceecTuSF41ffp0q3fv3nbHsNUTTzxhXX/99XbH8CmPPPKI1alTJ6uqqsprn8memTo4ceKE8vPzlZiYWG08MTFReXl5NqWCLygvL5cktW3b1uYk9qisrNTy5ct15MgRxcfH2x3H61JSUnTzzTfrD3/4g91RbLNz505FRkYqJiZGd955p3788Ue7I3nVqlWrFBcXp9tvv12hoaHq27evXnnlFbtj2ebEiRNaunSpHnjggfN+uHN9UGbqoLS0VJWVlTWe1B0WFlbjid64dFiWpbS0NF1//fXq0aOH3XG8auvWrWrdurWcTqfGjx+vlStXqnv37nbH8qrly5dr06ZNysrKsjuKbfr376833nhDn376qV555RUVFxdrwIABOnDggN3RvObHH3/U/PnzFRsbq08//VTjx4/Xww8/rDfeeMPuaLZ47733VFZWpjFjxnj1c414nIGv+G3LtCzLq80TvmXixInasmWLvvzyS7ujeF2XLl20efNmlZWV6Z133lFycrJyc3MvmUJTVFSkRx55RKtXr1bz5s3tjmObpKQkz5979uyp+Ph4derUSUuWLFFaWpqNybynqqpKcXFxyszMlCT17dtX27Zt0/z583XffffZnM77XnvtNSUlJSkyMtKrn8uemToICQlR06ZNa+yFKSkpqbG3BpeGSZMmadWqVVq7dq3at29vdxyva9asmTp37qy4uDhlZWWpd+/eevHFF+2O5TX5+fkqKSnR1VdfLT8/P/n5+Sk3N1f/9V//JT8/P1VWVtod0RatWrVSz549tXPnTrujeE1ERESNEt+tW7dL6uKQ0/bs2aM1a9Zo7NixXv9sykwdNGvWTFdffbXnDO3TsrOzNWDAAJtSwQ6WZWnixIl699139fnnnysmJsbuSD7Bsiy53W67Y3jNDTfcoK1bt2rz5s2eV1xcnO6++25t3rxZTZs2tTuiLdxut7Zv366IiAi7o3jNddddV+P2DD/88IOio6NtSmSfRYsWKTQ0VDfffLPXP5vDTHWUlpame++9V3FxcYqPj9fChQtVWFio8ePH2x3Naw4fPqxdu3Z53hcUFGjz5s1q27atOnToYGMy70lJSdGyZcv0/vvvKyAgwLO3LigoSC1atLA5nXdMmTJFSUlJioqKUkVFhZYvX66cnBx98skndkfzmoCAgBrnSbVq1UrBwcGX1PlTkydP1vDhw9WhQweVlJTomWeekcvlUnJyst3RvObRRx/VgAEDlJmZqTvuuEP//Oc/tXDhQi1cuNDuaF5VVVWlRYsWKTk5WX5+NlQLr103dRGYN2+eFR0dbTVr1sy66qqrLrnLcdeuXWtJqvFKTk62O5rX1Pb9JVmLFi2yO5rXPPDAA55/B+3atbNuuOEGa/Xq1XbHst2leGn26NGjrYiICMvf39+KjIy0Ro0aZW3bts3uWF73v//7v1aPHj0sp9Npde3a1Vq4cKHdkbzu008/tSRZO3bssOXzHZZlWd6vUAAAAI2Dc2YAAIDRKDMAAMBolBkAAGA0ygwAADAaZQYAABiNMgMAAIxGmQEAAEajzAAAAKNRZgAAgNEoMwAAwGiUGQBG2b9/v8LDw5WZmekZ+/rrr9WsWTOtXr3axmQA7MKzmQAY56OPPtKIESOUl5enrl27qm/fvrr55ps1Z84cu6MBsAFlBoCRUlJStGbNGvXr10/ffvutNmzYoObNm9sdC4ANKDMAjHTs2DH16NFDRUVF2rhxo3r16mV3JAA24ZwZAEb68ccftXfvXlVVVWnPnj12xwFgI/bMADDOiRMndM0116hPnz7q2rWrZs+era1btyosLMzuaABsQJkBYJy//OUv+sc//qFvv/1WrVu31uDBgxUQEKAPPvjA7mgAbMBhJgBGycnJ0Zw5c/Tmm28qMDBQTZo00Ztvvqkvv/xS8+fPtzseABuwZwYAABiNPTMAAMBolBkAAGA0ygwAADAaZQYAABiNMgMAAIxGmQEAAEajzAAAAKNRZgAAgNEoMwAAwGiUGQAAYDTKDAAAMBplBgAAGO3/AR4bNcrhI67qAAAAAElFTkSuQmCC", "text/plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Plot histogram:\n", "plt.hist(xFair, bins=np.arange(0.05,7,.1),edgecolor='black')\n", "plt.xlabel('x')\n", "plt.ylabel('Counts')\n", "plt.ylim([0,n])\n", "plt.title('Histogram of xFair')\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Count the number of each outcome using the bincount function\n", "counts = np.bincount(xFair)\n", "print(counts)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Plot the true/theoretical pdf \n", "plt.bar(range(1,7), [1/6]*6, color='red', label='True pdf', width=0.1)\n", "\n", "# add the empirical pdf (normalised histogram) to the plot\n", "plt.bar(range(1,7), counts[1:7]/n, width=0.05, label='Histogram / \"Empirical pdf\"')\n", "\n", "# add legend to the plot\n", "plt.legend()\n", "\n", "# set limits of y-axis \n", "plt.ylim([0,1])\n", "\n", "# show the plot\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 3: Simmulate n rolls with an unfair dice" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Number of simulated realizations (sample size)\n", "n = 30\n", "\n", "# roll dice:\n", "probs = [1/7, 1/7, 1/7, 1/7, 1/7, 2/7]\n", "xUnFair = np.random.choice([1,2,3,4,5,6], size=n, replace=True, p=probs)\n", "print(xUnFair)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Count the number of each outcome using the bincount function\n", "counts = np.bincount(xUnFair)\n", "print(counts)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Plot the true/theoretical pdf \n", "plt.bar(range(1,7), probs, color='red', label='True pdf', width=0.1)\n", "\n", "# add the empirical pdf to the plot\n", "plt.bar(range(1,7), counts[1:7]/n, width=0.05, label='Empirical pdf')\n", "\n", "\n", "plt.legend()\n", "plt.ylim([0,1])\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 4: ECDF" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Take the data from the unfair dice again\n", "print(xUnFair)\n", "print(counts)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Plot empirical pdf and cdf\n", "\n", "fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8,4))\n", "\n", "ax1.bar(range(1,7), counts[1:7]/n, label='True pdf', width=0.1)\n", "ax1.set_ylim([0,1])\n", "ax1.set_title('Empirical pdf')\n", "\n", "ax2.ecdf(xUnFair)\n", "ax2.set_ylim([0,1])\n", "ax2.set_title('Empirical cdf')\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 5: The binomial distribution" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# import scipy.stats for theoretical distributions (and much more)\n", "import scipy.stats as stats" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0]\n" ] } ], "source": [ "# Probability of \"success\"\n", "p = 0.1\n", "\n", "# Number of repititions\n", "nRepeat = 30\n", "\n", "# Simulate Bernoulli experiment 'nRepeat' times\n", "tmp = np.random.choice([0,1], p=[1-p, p], size=nRepeat, replace=True)\n", "print(tmp)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "# Calculate number of successes\n", "print(np.sum(tmp))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Or: use the binomial distribution simulation function\n", "stats.binom.rvs(n=nRepeat, p=p) # \"rvs\" is random variates" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "have a look at documentation for binomial distribution in scipy.stats.binom:\n", "https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.binom.html\n", "\n", "- .rvs
\n", "- .pmf (\"pdf\" but for discrete distributions)
\n", "- .cdf
\n", "- .ppf
\n", "
\n", "and more.." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 6: Number of 6's with fair dice" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 'nRepeat' independent draws from the set [1,2,3,4,5,6]\n", "nRepeat = 30\n", "xFair = np.random.choice([1,2,3,4,5,6], size=nRepeat, replace=True)\n", "print(xFair)\n", "\n", "# Count number of 6's:\n", "print(np.sum(xFair == 6))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Or: use the binomial distribution simulation function\n", "stats.binom.rvs(n=nRepeat, p=1/6)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.bar(np.arange(0, nRepeat+1, 1), stats.binom.pmf(k=np.arange(0,nRepeat+1,1), n=nRepeat, p=1/6))\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Callcenter example" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# we can calculate the probability using the pdf (\"pmf\") of the binomial:\n", "print(stats.binom.pmf(k=6, n=6, p=0.70))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# lets plot entire pdf (\"pmf\"):\n", "plt.bar(np.arange(0,7,1),stats.binom.pmf(k=np.arange(0,7,1), n=6, p=0.70))\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Harddrive example\n", "Python (scipy.stats) uses different argument names than the book for the many parameters of the Hypergeometric distribution.\n", "\n", "In the book we use `n` draws with `a` number of successes from a `N` size population. `(n,a,N)`. Whereas the corresponding Python (scipy.stats) arguments (parameters) would be `(N,n,M)` - so `N` for number of draws, `n` number of successes from a `M` size population.\n", "\n", "Additionally, `k` correponds to `x` from the book.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# we can calculate the probability using the pdf (\"pmf\") of the hypergoemetrical distribution:\n", "print(1 - stats.hypergeom.pmf(k=0, M=10, n=2, N=3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Hospital admission example" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# we can calculate the probability using the pdf (\"pmf\") of the poisson distribution:\n", "print(stats.poisson.pmf(k=0,mu=0.3)+stats.poisson.pmf(k=1,mu=0.3)+stats.poisson.pmf(k=2,mu=0.3))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# or we can use the cumulative distribution function (cdf):\n", "print(stats.poisson.cdf(k=2,mu=0.3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 7: Sample mean of n rolls with a fair dice" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Number of simulated realizations (sample size)\n", "n = 30\n", "\n", "# n independent draws from the set (1,2,3,4,5,6) \n", "# with equal probability of each outcome\n", "xFair = np.random.choice(range(1, 7), size=n, replace=True)\n", "print(xFair)\n", "\n", "# compute the sample mean:\n", "print(xFair.mean())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now change n to 1000 and try again (do not print xFair) - what happens?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 8: Sample variance of n rolls with a fair dice " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Number of simulated realizations (sample size)\n", "n = 30\n", "\n", "# n independent draws from the set (1,2,3,4,5,6) \n", "# with equal probability of each outcome\n", "xFair = np.random.choice(range(1, 7), size=n, replace=True)\n", "print(xFair)\n", "\n", "# compute the sample mean:\n", "print(xFair.var(ddof=1))" ] } ], "metadata": { "kernelspec": { "display_name": "pernille", "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.11.9" } }, "nbformat": 4, "nbformat_minor": 2 }