{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy.stats import poisson\n",
    "from numpy import random as rnd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Strategia per la determinazione della probabilità di un evento** \n",
    "\n",
    "esempio: dado a sei facce"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "[(k,poisson.pmf(k, 200/6)) for k in range(50)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# lanciamo un dado perfetto 200 volte\n",
    "esiti=[k for k in range(200//3)]\n",
    "pp=[poisson.pmf(k, 200/6) for k in esiti]\n",
    "pp2=[poisson.pmf(k, 200/5.7) for k in esiti]\n",
    "plt.plot(esiti, pp, label='1/6')\n",
    "plt.plot(esiti, pp2, label='1/5.7')\n",
    "plt.grid()\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**invertire il problema **\n",
    "\n",
    "$$ P(p|N) P(N) = P(N|p) P(p) $$\n",
    "\n",
    "$$ P(p|N) = \\frac {P(N|p) P(p)} {P(N)} $$\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "#possiamo ottenere il lancio di 200 dadi in diversi modi\n",
    "lanci=rnd.randint(1,7,200)\n",
    "esito=sum(lanci==6)\n",
    "print(esito)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "rnd.randint(1,7,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "esito=rnd.poisson(200/6)\n",
    "print(esito)\n",
    "print(esito/200*6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "esiti=np.arange(10,60)\n",
    "pcond=[poisson.pmf(k,esito) for k in esiti]\n",
    "plt.plot(esiti/200*6, pcond)\n",
    "plt.xlabel('probabilità*6')\n",
    "plt.grid()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$ P(N) $?\n",
    "\n",
    "$$ P(N)= \\sum_p P(N,p) P(p) $$\n",
    "\n",
    "oppure\n",
    "\n",
    "$$ P(N) = \\int_p P(N,p) P(p) dp $$\n",
    "\n",
    "e $P(p)$?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "#esempi\n",
    "esiti6=esiti*6/200\n",
    "ap0=np.ones(len(esiti))\n",
    "ap1=1/(1+4*(esiti6-1)**2)\n",
    "ap2=(1-esiti6/6)**2\n",
    "fig, axs=plt.subplots(ncols=2, figsize=(7,4))\n",
    "axs[0].plot(esiti6,ap0)\n",
    "axs[0].plot(esiti6,ap1)\n",
    "axs[0].plot(esiti6,ap2)\n",
    "axs[0].set_ylim(0,1.05)\n",
    "axs[1].plot(esiti6, pcond*ap0, label='che ne so?')\n",
    "axs[1].plot(esiti6, pcond*ap1, label='fiducioso')\n",
    "axs[1].plot(esiti6, pcond*ap2, label='sul serio?')\n",
    "axs[1].legend()\n",
    "axs[1].set_xlabel('p*6')\n",
    "axs[1].grid()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
