{
 "cells": [
  {"cell_type":"markdown","metadata":{},"source":["# Sets, conditioning, and Bayes\n","Use exact counts and simulation to make conditional probability visible."]},
  {"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["from itertools import product\n","omega = list(product([0, 1], repeat=3))\n","A = {w for w in omega if sum(w) >= 2}\n","B = {w for w in omega if w[0] == 1}\n","print('P(A)=', len(A)/len(omega))\n","print('P(A|B)=', len(A & B)/len(B))"]},
  {"cell_type":"markdown","metadata":{},"source":["## A diagnostic test\n","Predict the positive predictive value before running the cell. Then vary prevalence across several orders of magnitude."]},
  {"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["def ppv(prevalence, sensitivity=.95, specificity=.90):\n","    return sensitivity*prevalence/(sensitivity*prevalence + (1-specificity)*(1-prevalence))\n","for p in [.001, .01, .05, .10, .50]:\n","    print(f'prevalence={p:5.3f}  P(case|+)={ppv(p):.3f}')"]},
  {"cell_type":"markdown","metadata":{},"source":["**Challenge.** Find the prevalence at which a positive result first has predictive value above 50%. Derive the answer algebraically and confirm numerically."]}
 ],
 "metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3"}},
 "nbformat":4,"nbformat_minor":5
}
