%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.11%22%0A%23%20dependencies%20%3D%20%5B%22marimo%3E%3D0.23.9%22%2C%20%22ipython%22%5D%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Bayesian%20Statistics%0A%0A%20%20%20%20%3E%20Marcos%20Duarte%2C%20Renato%20Naville%20Watanabe%3Cbr%3E%0A%20%20%20%20%3E%20%5BLaboratory%20of%20Biomechanics%20and%20Motor%20Control%5D(https%3A%2F%2Fbmclab.pesquisa.ufabc.edu.br%2F)%3Cbr%3E%0A%20%20%20%20%3E%20Federal%20University%20of%20ABC%2C%20Brazil%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20How%20to%20use%20this%20guide%0A%0A%20%20%20%20A%20positive%20result%20sounds%20decisive%20until%20you%20ask%20how%20common%20the%20condition%20was%20before%20testing.%20This%20guide%20keeps%20the%20arithmetic%20visible%3A%20count%20the%20two%20routes%20to%20a%20positive%20result%2C%20apply%20Bayes%E2%80%99%20theorem%2C%20then%20ask%20whether%20another%20test%20really%20contributes%20independent%20evidence.%20The%20disease%20example%20is%20a%20hypothetical%20probability%20model.%0A%0A%20%20%20%20Read%20the%20examples%20in%20order%2C%20predict%20each%20result%2C%20and%20then%20run%20the%20cell.%20Try%20the%20distributed%20Exercises%20before%20opening%20their%20worked%20solutions.%20Finish%20with%20the%20checkpoint%20questions%20and%20independent%20Problems%3B%20their%20panels%20contain%20final%20answers%20only.%0A%0A%20%20%20%20**Challenge%200.**%20Imagine%20100%2C000%20people%20and%20a%20condition%20present%20in%200.1%25%20of%20them.%20A%20test%20detects%2099%25%20of%20cases%20but%20flags%201%25%20of%20non-cases.%20Will%20most%20positive%20results%20be%20true%20cases%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Python%20setup%0A%0A%20%20%20%20Only%20Python%20arithmetic%20and%20Marimo%20are%20required%20for%20the%20calculations.%20The%20linked%20diagnostic%20app%20is%20optional%20and%20requires%20an%20Internet%20connection%3B%20the%20probability%20examples%20here%20remain%20self-contained.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Definition%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20From%20%5BWikipedia%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FBayesian_statistics)%3A%0A%0A%20%20%20%20%3E%20Bayesian%20statistics%20is%20a%20theory%20in%20the%20field%20of%20statistics%20based%20on%20the%20Bayesian%20interpretation%20of%20probability%20where%20probability%20expresses%20a%20degree%20of%20belief%20in%20an%20event.%20The%20degree%20of%20belief%20may%20be%20based%20on%20prior%20knowledge%20about%20the%20event%2C%20such%20as%20the%20results%20of%20previous%20experiments%2C%20or%20on%20personal%20beliefs%20about%20the%20event.%3Cbr%3E%0A%20%20%20%20This%20differs%20from%20a%20number%20of%20other%20interpretations%20of%20probability%2C%20such%20as%20the%20frequentist%20interpretation%20that%20views%20probability%20as%20the%20limit%20of%20the%20relative%20frequency%20of%20an%20event%20after%20a%20large%20number%20of%20trials.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Bayes'%20theorem%0A%0A%20%20%20%20Given%20two%20events%20%24A%24%20and%20%24B%24%2C%20the%20conditional%20probability%20of%20%24A%24%20given%20that%20%24B%24%20is%20true%20is%20expressed%20by%20the%20Bayes'%20theorem%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20P(A%20%5Cmid%20B)%20%3D%20%5Cfrac%7BP(B%20%5Cmid%20A)P(A)%7D%7BP(B)%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20where%20%24P(B)%20%5Cneq%200%24.%0A%0A%20%20%20%20%24P(A)%24%20is%20the%20prior%20probability%20of%20%24A%24%20which%20expresses%20one's%20beliefs%20about%20%24A%24%20before%20evidence%20is%20taken%20into%20account%3B%3Cbr%3E%0A%20%20%20%20%24P(B)%24%20is%20the%20probability%20of%20the%20evidence%20%24B%24%3B%3Cbr%3E%0A%20%20%20%20%24P(B%E2%88%A3A)%24%20is%20the%20likelihood%20function%2C%20which%20can%20be%20interpreted%20as%20the%20probability%20of%20the%20evidence%20%24B%24%20given%20that%20%24A%24%20is%20true.%20The%20likelihood%20quantifies%20the%20extent%20to%20which%20the%20evidence%20%24B%24%20supports%20the%20proposition%20%24A%24%3B%3Cbr%3E%0A%20%20%20%20%24P(A%E2%88%A3B)%24%20is%20the%20posterior%20probability%2C%20the%20probability%20of%20the%20proposition%20%24A%24%20after%20taking%20the%20evidence%20%24B%24%20into%20account.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%201.**%20Are%20%24P(A%5Cmid%20B)%24%20and%20%24P(B%5Cmid%20A)%24%20interchangeable%3F%20Use%20the%20screening%20example%20to%20name%20each.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%201%0A%20%20%20%20No.%20%24P(B%5Cmid%20A)%24%20is%20sensitivity%3A%20the%20chance%20of%20a%20positive%20result%20among%20cases.%20%24P(A%5Cmid%20B)%24%20is%20the%20proportion%20of%20positives%20who%20are%20cases.%20Bayes%E2%80%99%20denominator%20counts%20**all**%20ways%20of%20obtaining%20a%20positive%20result%2C%20so%20prevalence%20matters.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Example%3A%20Probability%20of%20having%20a%20disease%20after%20a%20positive%20result%0A%0A%20%20%20%20This%20example%20is%20taken%20from%20%5Bhttps%3A%2F%2Fyoutu.be%2FR13BD8qKeTg%5D(https%3A%2F%2Fyoutu.be%2FR13BD8qKeTg).%0A%0A%20%20%20%20Suppose%20you%20test%20positive%20for%20a%20rare%20disease%2C%20with%200.1%25%20of%20frequency%20of%20occurrence%20in%20the%20population.%3Cbr%3E%0A%20%20%20%20You%20investigate%20more%20about%20the%20test%20for%20the%20disease%20and%20you%20found%20out%20that%20the%20employed%20test%20correctly%20identifies%20as%20positive%2099%25%20of%20the%20people%20who%20have%20the%20disease%20(true%20positives)%20and%20incorrectly%20identifies%20as%20positive%201%25%20of%20the%20people%20who%20don't%20have%20the%20disease%20(false%20positives).%0A%0A%20%20%20%20How%20certain%20you%20actually%20have%20the%20disease%3F%0A%0A%20%20%20%20Let's%20use%20Bayes'%20theorem%20to%20answer%20this%20problem%3A%0A%0A%20%20%20%20The%20two%20events%20are%3A%20having%20the%20disease%20(%24A%24)%20and%20the%20positive%20result%20(%24B%24).%3Cbr%3E%0A%20%20%20%20The%20question%20is%3A%20which%20is%20the%20probability%2C%20%24P(A%E2%88%A3B)%24%2C%20of%20actually%20having%20the%20disease%20given%20a%20positive%20result%20of%20the%20test%20for%20the%20disease.%3Cbr%3E%0A%20%20%20%20%24P(A)%24%20is%20the%20prior%20probability%2C%20the%20probability%20of%20having%20the%20disease%20before%20taking%20the%20test.%3Cbr%3E%0A%20%20%20%20%24P(B%E2%88%A3A)%24%20is%20the%20likelihood%20function%2C%20the%20probability%20of%20testing%20positive%20if%20you%20have%20the%20disease.%3Cbr%3E%0A%20%20%20%20%24P(B)%24%20is%20the%20probability%20of%20testing%20positive%20for%20the%20disease.%0A%0A%20%20%20%20With%20no%20more%20information%20about%20you%20and%20the%20disease%2C%20let's%20assume%20that%20%24P(A)%24%20is%20equal%20to%20the%20frequency%20of%20occurrence%20of%20the%20disease%20in%20the%20population%2C%20%240.001%24.%0A%20%20%20%20%24P(B%E2%88%A3A)%24%20is%20known%2C%20%240.99%24.%0A%20%20%20%20Concerning%20%24P(B)%24%2C%20we%20know%20there%20are%20two%20ways%20of%20testing%20positive%20for%20the%20disease%3A%20someone%20has%20the%20disease%20and%20the%20test%20result%20is%20positive%20and%20someone%20doesn't%20have%20the%20disease%20but%20the%20the%20test%20result%20is%20positive.%20The%20probability%20for%20the%20first%20case%20is%20%240.001%20%5Ctimes%200.99%24%20and%20for%20the%20second%20case%20is%20%240.999%20%5Ctimes%200.01%24.%20The%20total%20probability%20of%20testing%20positive%20is%20the%20sum%20of%20these%20two%20probabilities.%3Cbr%3E%0A%20%20%20%20Them%20for%20%24P(A%E2%88%A3B)%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20P(A%5Cmid%20B)%20%3D%20%5Cfrac%7BP(B%5Cmid%20A)P(A)%7D%7BP(B)%7D%20%3D%20%5Cfrac%7B0.99%20%5Ctimes%200.001%7D%7B0.001%20%5Ctimes%200.99%20%2B%200.999%20%5Ctimes%200.01%7D%20%3D%200.09%0A%20%20%20%20%24%24%0A%0A%20%20%20%20So%2C%20there%20is%20a%209%25%20chance%20that%20you%20actually%20have%20the%20disease.%20Given%20the%20sensitivity%20and%20specificity%20of%20the%20test%2C%20both%2099%25%2C%20and%20the%20fact%20that%20you%20tested%20positive%2C%20only%209%25%20of%20chance%20seems%20surprising.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20FO%20%3D%200.001%0A%20%20%20%20_TP%20%3D%200.99%0A%20%20%20%20_FP%20%3D%200.01%0A%20%20%20%20_PBA%20%3D%200.99%0A%20%20%20%20_PA%20%3D%20FO%0A%20%20%20%20_PB%20%3D%20_PA%20*%20_TP%20%2B%20(1%20-%20_PA)%20*%20_FP%0A%20%20%20%20_PAB%20%3D%20_PBA%20*%20_PA%20%2F%20_PB%0A%20%20%20%20print('P(A%7CB)%20%3D%20%7B%3A.5f%7D'.format(_PAB))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%202.**%20In%20100%2C000%20people%2C%20calculate%20true%20and%20false%20positives%20under%20the%20example%20assumptions.%20Recover%20the%20posterior%20without%20a%20formula.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%202%0A%20%20%20%20There%20are%20100%20cases%20and%2099%2C900%20non-cases.%20Expected%20true%20positives%3A%20%240.99(100)%3D99%24%3B%20false%20positives%3A%20%240.01(99%7B%2C%7D900)%3D999%24.%20Thus%2099%20of%201%2C098%20positives%20are%20cases%3A%20%2499%2F1098%3D0.09016%24%2C%20about%209.02%25.%20These%20are%20expected%20counts%2C%20not%20observations.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20In%20this%20hypothetical%20model%2C%20the%20posterior%20after%20one%20positive%20result%20is%20about%209.02%25.%20Suppose%20a%20second%20test%20has%20the%20same%20sensitivity%20and%20false-positive%20rate.%20Multiplying%20their%20likelihoods%20requires%20**conditional%20independence%20given%20disease%20status**%2C%20both%20among%20cases%20and%20among%20non-cases.%20Repeating%20the%20same%20test%20may%20share%20errors%20and%20need%20not%20satisfy%20this%20assumption.%3Cbr%3E%0A%20%20%20%20Suppose%20you%20test%20positive%20again%2C%20now%20what%20is%20the%20probability%20that%20you%20actually%20have%20the%20disease%3F%0A%0A%20%20%20%20Now%20what%20is%20different%20is%20that%20we%20have%20more%20information%20about%20you%20and%20the%20disease%2C%20i.e.%2C%20about%20the%20prior%20probability%2C%20P(A).%20Given%20that%20your%20first%20test%20was%20positive%2C%20we%20will%20use%20P(A)%20%3D%200.09016%20and%20repeat%20the%20calculation.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20_TP%20%3D%200.99%0A%20%20%20%20_FP%20%3D%200.01%0A%20%20%20%20_PBA%20%3D%200.99%0A%20%20%20%20_PA%20%3D%20(0.99%20*%200.001)%20%2F%20(0.99%20*%200.001%20%2B%200.01%20*%200.999)%0A%20%20%20%20_PB%20%3D%20_PA%20*%20_TP%20%2B%20(1%20-%20_PA)%20*%20_FP%0A%20%20%20%20_PAB%20%3D%20_PBA%20*%20_PA%20%2F%20_PB%0A%20%20%20%20print('P(A%7CB)%20%3D%20%7B%3A.5f%7D'.format(_PAB))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20With%20two%20positive%20tests%2C%20there%20is%20a%2091%25%20probability%20that%20you%20actually%20have%20the%20disease.%0A%0A%20%20%20%20Note%20that%20even%20after%20two%20positive%20tests%2C%20this%20probability%20is%20still%20below%20the%20sensitivity%20and%20specificity%20of%20the%20test%20(99%25).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%203.**%20Suppose%20the%20second%20test%20merely%20copies%20the%20first%20result.%20Does%20a%20second%20positive%20change%20the%20posterior%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%203%0A%20%20%20%20No.%20Once%20the%20first%20positive%20is%20known%2C%20a%20copied%20result%20is%20certain%20in%20both%20groups.%20Its%20likelihood%20ratio%20is%20one%2C%20so%20the%20posterior%20remains%20about%209.02%25.%20Treating%20the%20copy%20as%20independent%20would%20count%20the%20same%20evidence%20twice.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Bayesian%20Clinical%20Diagnostic%20Model%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20from%20IPython.display%20import%20IFrame%0A%20%20%20%20IFrame('https%3A%2F%2Fkennis-research.shinyapps.io%2FBayes-App%2F'%2C%20width%3D'100%25'%2C%20height%3D500)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Checkpoint%20questions%0A%0A%20%20%20%201.%20What%20is%20the%20prior%3F%0A%20%20%20%202.%20Why%20is%20the%20denominator%20not%20just%20sensitivity%3F%0A%20%20%20%203.%20Which%20independence%20assumption%20permits%20sequential%20updating%20with%20the%20same%20likelihoods%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Checkpoint%20answers%0A%20%20%20%201.%20Probability%20before%20the%20new%20evidence.%0A%20%20%20%202.%20It%20includes%20true%20and%20false%20positives.%0A%20%20%20%203.%20Conditional%20independence%20of%20test%20results%20within%20each%20disease-status%20group.%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20%23%23%20Problems%0A%0A%20%20%20%201.%20Prevalence%201%25%2C%20sensitivity%2090%25%2C%20specificity%2095%25%3A%20find%20the%20probability%20of%20the%20condition%20after%20one%20positive%20result.%0A%20%20%20%202.%20In%20the%20original%200.1%25-prevalence%20example%2C%20give%20the%20posterior%20after%20two%20conditionally%20independent%20positives.%0A%20%20%20%203.%20Can%20a%20higher%20prior%20change%20the%20posterior%20when%20the%20test%20itself%20is%20unchanged%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Final%20answers%0A%20%20%20%201.%2015.38%25.%0A%20%20%20%202.%2090.75%25.%0A%20%20%20%203.%20Yes%3B%20a%20higher%20prior%20gives%20a%20higher%20posterior%20for%20the%20same%20positive%20likelihood%20ratio.%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20%23%23%20Go%20deeper%0A%0A%20%20%20%20%5BMIT%20probability%20and%20statistics%5D(https%3A%2F%2Focw.mit.edu%2Fcourses%2F18-05-introduction-to-probability-and-statistics-spring-2022%2F)%20%E2%80%94%20the%20Bayesian%20inference%20readings%20extend%20this%20discrete%20example.%0A%0A%20%20%20%20%23%23%23%20Video%20lectures%20on%20the%20Internet%0A%0A%20%20%20%20%5BBayes%E2%80%99%20theorem%2C%203Blue1Brown%5D(https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DHZGCoVF3YvM)%20%E2%80%94%20a%20geometric%20explanation%20of%20how%20prior%20information%20changes%20the%20meaning%20of%20evidence.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20References%0A%0A%20%20%20%20-%20Cameron%20Davidson-Pilon%20(2015)%20%5BBayesian%20Methods%20for%20Hackers%3A%20Probabilistic%20Programming%20and%20Bayesian%20Inference%5D(https%3A%2F%2Fcamdavidsonpilon.github.io%2FProbabilistic-Programming-and-Bayesian-Methods-for-Hackers%2F).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
903c5582508120617901061f60bf5ba0