{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import numpy.random as rand\n",
    "from ipywidgets import interact"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Siete stati coinvolti in uno screening di persone asintomatiche al covid-19 con l'intento di capire quanti sono comunque positivi. I medici sospettano che siano positivi fra 1/100 e 1/1000 persone. Il test di positività non è perfetto (come tutti i test medici) e ha un bias positivo: 99.5% dei positivi risultano positivi, 0.5% dei positivi risultano negativi, 98% dei negativi risultano negativi e 2% dei negativi risultano positivi.*\n",
    "\n",
    "Useremo la seguente notazione:\n",
    "- N: negativi\n",
    "- n: negativi al test\n",
    "- P: positivi\n",
    "- p: positivi al test\n",
    "\n",
    "Trasformiamo le frequenze in istanze, considereremo una popolazione di 10000 di abitanti, per ipotesi:\n",
    "\n",
    "- P=10000/100=100; stima pessimista\n",
    "- P,p=100*0.995=99.5\n",
    "- N=1M-P=9900\n",
    "- N,p=9900*0.02=198\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Il gioco delle tre porte**\n",
    "\n",
    "Probabilità a priori\n",
    "\n",
    "\\begin{matrix}\n",
    "A & B & C \\\\\n",
    "\\frac 1 3 & \\frac 1 3 & \\frac 1 3\n",
    "\\end{matrix}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "a. Conduttore con strategia: apre C ogni volta che C è vuota\n",
    "\n",
    "\\begin{matrix}\n",
    "A & B & C &\\\\\n",
    "1 & 1 & 0 & c \\\\\n",
    "0 & 0 & 1 & b\n",
    "\\end{matrix}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "b. Conduttore senza strategia, apre C o B indifferentemente se sono vuote\n",
    "\n",
    "\\begin{matrix}\n",
    "& A & B & C \\\\\n",
    "c & 1/2 & 1 & 0 \\\\\n",
    "b & 1/2 & 0 & 1 \\\\\n",
    "\\end{matrix}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Bayes**\n",
    "\n",
    "$P(A|c)P(c)=P(A,c)=P(c|A) P(A)$\n",
    "\n",
    "$P(A|c)= \\frac {P(c|A)*P(A)}{P(c)} = \\frac 1 3 $\n",
    "\n",
    "\n",
    "$P(B|c)P(c)=P(c|B) P(B) $\n",
    "\n",
    "$P(B|c) = \\frac{P(c|B) P(B)} {P(c)} = \\frac 2 3 $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**applichiamo bayes al conduttore con strategia:**\n",
    "\n",
    "$P(A)=P(B)=P(C)=\\frac 1 3; P(c|A)=1; P(b|A)=0 $\n",
    "\n",
    "$P(A|c)= \\frac {P(c|A)*P(A)}{P(c)} = \\frac 1 2 $\n",
    "\n",
    "$P(B|c) = \\frac{P(c|B) P(B)} {P(c)} = \\frac 1 2 $\n",
    "\n",
    "$P(A|b) = \\frac {P(b|A)*P(A)}{P(b)} = 0 $\n",
    "\n",
    "$P(C|b) = \\frac{P(b|C) P(C)} {P(b)} = 1 $\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def descrivi(scelta, premi):\n",
    "    porte=['a','b','c']\n",
    "    porte[scelta]=porte[scelta].upper()\n",
    "    print(''.join(['{:>5}'.format(x) for x in porte]))\n",
    "    print(''.join(['{:>5}'.format(x) for x in premi]))\n",
    "\n",
    "# @interact(scelta=[()])\n",
    "def gioca(porta=' ', cambio=' ', verb=True):\n",
    "    '''gioca il gioco delle tre porte, decidendo in anticipo se cambi(True) o resti(False)'''\n",
    "    if verb:\n",
    "        mostra=print\n",
    "    else:\n",
    "        mostra=lambda x:None\n",
    "    niente='😦'\n",
    "    premio='😃'\n",
    "    #premi=3*[niente]\n",
    "    #premi[rand.randint(0,3)]=premio\n",
    "    pos_premio=rand.randint(0,3)\n",
    "    premi=[premio if x==pos_premio else niente for x in range(3)]\n",
    "    porte=['a','b','c']\n",
    "    while porta not in porte:\n",
    "        print('scegli una porta:')\n",
    "        print(porte)\n",
    "        porta=input()\n",
    "    porta=porte.index(porta)\n",
    "    apri=porta\n",
    "    while apri==porta or premi[apri]==premio:\n",
    "        apri=rand.randint(0,3)\n",
    "    mostra('apro la porta {}'.format(porte[apri]))\n",
    "    while cambio not in 'ct':\n",
    "        cambio=input('cambi o tieni? [c,t]')\n",
    "    if cambio=='c':\n",
    "        for i in range(3):\n",
    "            if i != apri and i !=porta:\n",
    "                porta=i\n",
    "                mostra('hai cambiato porta, prendendo '+porte[porta])\n",
    "    mostra('   '.join(['[{}:{}]'.format(x,y) for x,y in zip(porte,premi)]))\n",
    "    if premi[porta]==premio:\n",
    "        mostra('hai vinto')\n",
    "        return 1\n",
    "    else:\n",
    "        mostra('hai perso')\n",
    "        return 0\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "gioca(porta='a', cambio='c')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "esiti_c=[gioca('a','c', verb=False) for i in range(1000)]\n",
    "#print(esiti_c, sum(esiti_c))\n",
    "print(sum(esiti_c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "esiti_t=[gioca('a','t',verb=False) for i in range(1000)]\n",
    "#print(esiti_t, sum(esiti_t))\n",
    "print(sum(esiti_t))"
   ]
  },
  {
   "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
}
