Mol evol jc.151216-amb.bayesianstats-teaser

48
Bayesian Statistics Made Simple Álvaro Martínez Barrio, PhD [email protected] linkedin.com/in/ambarrio @ambarrio Uppsala, Dec 16th 2015

Transcript of Mol evol jc.151216-amb.bayesianstats-teaser

Page 1: Mol evol jc.151216-amb.bayesianstats-teaser

BayesianStatistics

MadeSimple

ÁlvaroMartínezBarrio,PhD

[email protected]/in/ambarrio@ambarrio

!Uppsala,Dec16th2015

Page 2: Mol evol jc.151216-amb.bayesianstats-teaser

2

Think Bayes

Bayesian Statistics Made Simple

Version 1.0.3

Allen B. Downey

Green Tea PressNeedham, Massachusetts

Page 3: Mol evol jc.151216-amb.bayesianstats-teaser

Notation:Probability

3

• p(A):theprobabilitythatAoccurs!

• p(A|B):theprobabilitythatAoccurs,giventhatBhasoccurred!

• p(AandB)=p(A)p(B|A):Conjointprobability

Page 4: Mol evol jc.151216-amb.bayesianstats-teaser

Introduction:Bayes’Theorem

4

• Bydefinitionofconjointprobabilityandthatconjunctioniscommutative:p(AandB)=p(A)p(B|A)=(1)p(BandA)=p(B)p(A|B)

• p(A)p(B|A)=p(B)p(A|B)(2)• p(A|B)=p(A)p(B|A)/p(B)(3)

Page 5: Mol evol jc.151216-amb.bayesianstats-teaser

Thecookieproblem

5

Supposetherearetwobowlsofcookies.Bowl1contains30vanillacookiesand10chocolatecookies.Bowl2contains20ofeach.Nowsupposeyouchooseoneofthebowlsatrandomand,withoutlooking,selectacookieatrandom.Thecookieisvanilla.Whatisthe

probabilitythatitcamefromBowl1?

Page 6: Mol evol jc.151216-amb.bayesianstats-teaser

Thecookieproblem

6

Supposetherearetwobowlsofcookies.Bowl1contains30vanillacookiesand10chocolatecookies.Bowl2contains20ofeach.Nowsupposeyouchooseoneofthebowlsatrandomand,withoutlooking,selectacookieatrandom.Thecookieisvanilla.Whatisthe

probabilitythatitcamefromBowl1?

p(B1|V)=p(B1)p(V|B1)/p(V)

Page 7: Mol evol jc.151216-amb.bayesianstats-teaser

Thecookieproblem

7

Supposetherearetwobowlsofcookies.Bowl1contains30vanillacookiesand10chocolatecookies.Bowl2contains20ofeach.Nowsupposeyouchooseoneofthebowlsatrandomand,withoutlooking,selectacookieatrandom.Thecookieisvanilla.Whatisthe

probabilitythatitcamefromBowl1?

p(B1|V)=p(B1)p(V|B1)/p(V)

p(B1|V)=(1/2)(3/4)/5/8

Page 8: Mol evol jc.151216-amb.bayesianstats-teaser

History:Bayes’Theorem

8

ThomasBayes,

(b.1702,London-d.1761,

TunbridgeWells,Kent)

Intheearly18thcentury,themathematiciansofthetimeknewhowtofindtheprobabilitythat,say,4peopleaged50dieinagivenyearoutofasampleof60iftheprobabilityofanyoneofthemdyingwasknown.

Buttheydidnotknowhowtofindtheprobabilityofone50-yearolddyingbasedontheobservationthat4haddiedoutof60.

Page 9: Mol evol jc.151216-amb.bayesianstats-teaser

History:Bayes’Theorem

9

ThomasBayes,

(b.1702,London-d.1761,

TunbridgeWells,Kent)

Intheearly18thcentury,themathematiciansofthetimeknewhowtofindtheprobabilitythat,say,4peopleaged50dieinagivenyearoutofasampleof60iftheprobabilityofanyoneofthemdyingwasknown.

Buttheydidnotknowhowtofindtheprobabilityofone50-yearolddyingbasedontheobservationthat4haddiedoutof60.

thequestionofinverseprobability

Page 10: Mol evol jc.151216-amb.bayesianstats-teaser

The“diachronic”interpretation

10

ThomasBayes,

(b.1702,London-d.1761,

TunbridgeWells,Kent)

p(H|D)=p(H)p(D|H)/p(D)

• p(H)istheprobabilityofthehypothesisbeforeweseethedata,calledthepriorprobability,orjustprior.

• p(H|D)iswhatwewanttocompute,theprobabilityofthehypothesisa\erweseethedata,calledtheposterior.

• p(D|H)istheprobabilityofthedataunderthehypothesis,calledthelikelihood.

• p(D)istheprobabilityofthedataunderanyhypothesis,calledthenormalizingconstant.

Page 11: Mol evol jc.151216-amb.bayesianstats-teaser

History:Syllogism

11

4thcenturyBC

!!

• Majorpremise!!

• Minorpremise!!

• Conclusion

Arhetoricalsyllogism(a3-partdeductiveargument)usedinoratorialpractice.

Page 12: Mol evol jc.151216-amb.bayesianstats-teaser

History:Syllogism

12

4thcenturyBC

• Majorpremise:“Allhumansaremortal”!!

• Minorpremise:“AllGreeksarehuman”!

• Conclusion:“AllGreeksaremortal”

Page 13: Mol evol jc.151216-amb.bayesianstats-teaser

History:Syllogism

13

4thcenturyBC

• Majorpremise:“Allmortalsdie”!!

• Minorpremise:“Allmenaremortals”!

• Conclusion:“Allmendie”

Page 14: Mol evol jc.151216-amb.bayesianstats-teaser

History:Enthymeme

14

4thcenturyBC

!• “Socratesismortalbecausehe’s

human”

!• Majorpremise(unstated):“Allhumansaremortal.”!

• Minorpremise(stated):“Socratesishuman.”!

• Conclusion(stated):“Therefore,Socratesismortal.”

Page 15: Mol evol jc.151216-amb.bayesianstats-teaser

History:Enthymeme

15

4thcenturyBC

!• "Heisill,sincehehasacough.”

!!

• “Sinceshehasachild,shehas

givenbirth."

Page 16: Mol evol jc.151216-amb.bayesianstats-teaser

History:Enthymeme

16

4thcenturyBC

• Hestartedtoproposethatenthymemesarebasedonprobabilities(eikos),examples,tekmêria(i.e.,proofs,evidences),andsigns(sêmeia).

Page 17: Mol evol jc.151216-amb.bayesianstats-teaser

History:Enthymeme

17

4thcenturyBC

• CarolPosterarguesthatenthymemesastruncatedsyllogismswasinventedbyBritishrhetoricians(suchasRichardWhately)intheXVIIIcentury.

Poster,Carol(2003)."Theology,Canonicity,andAbbreviatedEnthymemes".RhetoricSocietyQuarterly33(1):67–103.

Page 18: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter1:Bayes’Theorem

• Mutuallyexclusive:Atmostonehypothesisinthesetcanbetrue!

• Collectivelyexhaustive:Therearenootherpossibilities;atleastoneofthehypotheseshastobetrue

Page 19: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter1:Bayes’Theorem

• Mutuallyexclusive:Atmostonehypothesisinthesetcanbetrue!

• Collectivelyexhaustive:Therearenootherpossibilities;atleastoneofthehypotheseshastobetrue

p(D)=p(B1)p(D|B1)+p(B2)p(D|B2)

Page 20: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter1:Bayes’Theorem

• Mutuallyexclusive:Atmostonehypothesisinthesetcanbetrue!

• Collectivelyexhaustive:Therearenootherpossibilities;atleastoneofthehypotheseshastobetrue

p(D)=p(B1)p(D|B1)+p(B2)p(D|B2)

p(D)=(1/2)(3/4)+(1/2)(1/2)=5/8

Page 21: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter1:Bayes’Theorem

• Mutuallyexclusive:Atmostonehypothesisinthesetcanbetrue!

• Collectivelyexhaustive:Therearenootherpossibilities;atleastoneofthehypotheseshastobetrue

p(D)=p(B1)p(D|B1)+p(B2)p(D|B2)

p(D)=(1/2)(3/4)+(1/2)(1/2)=5/8

Ifp(A|B)ishardtocompute,orhardtomeasureexperimentally,checkwhetherit

mightbeeasiertocomputetheothertermsinBayes’stheorem,p(B|A),p(A)andp(B).

Page 22: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter2:ComputationalStatistics

• Distribution:

Page 23: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter2:ComputationalStatistics

• Distribution:setofvaluesandtheircorrespondingprobabilities.

Page 24: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter2:ComputationalStatistics

• Distribution:setofvaluesandtheircorrespondingprobabilities.• Probabilitymassfunction:waytorepresentadistributionmathematically.

Page 25: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter2:ComputationalStatistics

• Distribution:setofvaluesandtheircorrespondingprobabilities.• Probabilitymassfunction:waytorepresentadistributionmathematically.

• Whentalkingaboutprobabilities,youneedtonormalise(theyshouldaddupto1)

Page 26: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter2:ComputationalStatistics

• Distribution:setofvaluesandtheircorrespondingprobabilities.• Probabilitymassfunction:waytorepresentadistributionmathematically.

• Whentalkingaboutprobabilities,youneedtonormalise(theyshouldaddupto1)

• Thisdistribution,whichcontainsthepriorsforeachhypothesis,iscalled(waitforit)thepriordistribution.

• Toupdatethedistributionbasedonnewdata(avanillacookie!),wemultiplyeachpriorbythecorrespondinglikelihood.

• Thedistributionisnolongernormalized,youneedtorenormalize• Theresultisadistributionthatcontainstheposteriorprobabilityforeachhypothesis,whichiscalled(waitagain!)theposteriordistribution.

Page 27: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter2:ComputationalStatistics

• Distribution:setofvaluesandtheircorrespondingprobabilities.• Probabilitymassfunction:waytorepresentadistributionmathematically.

• Whentalkingaboutprobabilities,youneedtonormalise(theyshouldaddupto1)

• Thisdistribution,whichcontainsthepriorsforeachhypothesis,iscalled(waitforit)thepriordistribution.

• Toupdatethedistributionbasedonnewdata(avanillacookie!),wemultiplyeachpriorbythecorrespondinglikelihood.

• Thedistributionisnolongernormalized,youneedtorenormalize• Theresultisadistributionthatcontainstheposteriorprobabilityforeachhypothesis,whichiscalled(waitagain!)theposteriordistribution.

Terminologyanddesignpatternsofpythonprogramsthatyoucanuseduringtherest

ofthecourse

Page 28: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter3:Estimation

SupposeIhaveaboxofdicethatcontainsa4-sideddie,a6-sideddie,an8-sideddie,a12-sideddie,anda20-sideddie.IfyouhaveeverplayedDungeons&Dragons,youknowwhatIamtalkingabout.SupposeIselectadiefromtheboxatrandom,rollit,andgeta6.WhatistheprobabilitythatIrolledeachdie?

Page 29: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter3:Estimation

SupposeIhaveaboxofdicethatcontainsa4-sideddie,a6-sideddie,an8-sideddie,a12-sideddie,anda20-sideddie.IfyouhaveeverplayedDungeons&Dragons,youknowwhatIamtalkingabout.SupposeIselectadiefromtheboxatrandom,rollit,andgeta6.WhatistheprobabilitythatIrolledeachdie?

Letmesuggestathree-stepstrategyforapproachingaproblemlikethis:1.Choosearepresentationforthehypotheses. 2.Choosearepresentationforthedata. 3.Writethelikelihoodfunction.

Page 30: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter3:Estimation

Mosteller’sFiftyChallengingProblemsinProbabilitywithSolutions

“Arailroadnumbersitslocomotivesinorder1..N.Onedayyouseealocomotivewiththenumber60.Estimatehowmanylocomotives

therailroadhas.”

Page 31: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter3:Estimation

PartIofStatisticalInference.

“Arailroadnumbersitslocomotivesinorder1..N.Onedayyouseealocomotivewiththenumber60.Estimatehowmanylocomotives

therailroadhas.”

Page 32: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter3:Estimation

PartIofStatisticalInference.

“Arailroadnumbersitslocomotivesinorder1..N.Onedayyouseealocomotivewiththenumber60.Estimatehowmanylocomotives

therailroadhas.”

Page 33: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter3:Estimation

PartIofStatisticalInference.

“Arailroadnumbersitslocomotivesinorder1..N.Onedayyouseealocomotivewiththenumber60.Estimatehowmanylocomotives

therailroadhas.”

Page 34: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter3:Estimation

PartIofStatisticalInference.

“Arailroadnumbersitslocomotivesinorder1..N.Onedayyouseealocomotivewiththenumber60.Estimatehowmanylocomotives

therailroadhas.”

Therearetwowaystoproceed:!

•Getmoredata. •Getmorebackgroundinformation.

Page 35: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter3:Estimation

PartIofStatisticalInference.

“Arailroadnumbersitslocomotivesinorder1..N.Onedayyouseealocomotivewiththenumber60.Estimatehowmanylocomotives

therailroadhas.”

Therearetwowaystoproceed:!

•Getmoredata. •Getmorebackgroundinformation.

Page 36: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter3:Estimation

PartIofStatisticalInference.

“Arailroadnumbersitslocomotivesinorder1..N.Onedayyouseealocomotivewiththenumber60.Estimatehowmanylocomotives

therailroadhas.”

Therearetwowaystoproceed:!

•Getmoredata. •Getmorebackgroundinformation.

Page 37: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter3:Estimation

PartIofStatisticalInference.

“Arailroadnumbersitslocomotivesinorder1..N.Onedayyouseealocomotivewiththenumber60.Estimatehowmanylocomotives

therailroadhas.”

Therearetwowaystoproceed:!

•Getmoredata. •Getmorebackgroundinformation.

Page 38: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter3:Estimation

PartIofStatisticalInference.

“Arailroadnumbersitslocomotivesinorder1..N.Onedayyouseealocomotivewiththenumber60.Estimatehowmanylocomotives

therailroadhas.”

Therearetwowaystoproceed:!

•Getmoredata. •Getmorebackgroundinformation.

Page 39: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter3:Estimation

• Credibleinterval:Forintervalsweusuallyreporttwovaluescomputedsothatthereisa90%chancethattheunknownvaluefallsbetweenthem(oranyotherprobability).

• Thewidthofthisintervalsuggestshowuncertainweareabouttheconclusionbasedinourunknownvalue.

• Therearetwoapproachestochoosingpriordistributions:• i)informative:bestrepresentsbackgroundinformation• ii)uninformative:intendedtobeasunrestrictedaspossible

Page 40: Mol evol jc.151216-amb.bayesianstats-teaser

Chapter3:Estimation

• Credibleinterval:Forintervalsweusuallyreporttwovaluescomputedsothatthereisa90%chancethattheunknownvaluefallsbetweenthem(oranyotherprobability).

• Thewidthofthisintervalsuggestshowuncertainweareabouttheconclusionbasedinourunknownvalue.

• Therearetwoapproachestochoosingpriordistributions:• i)informative:bestrepresentsbackgroundinformation• ii)uninformative:intendedtobeasunrestrictedaspossible

Inrealworldyouhavetwowaystoproceed:!

Ifyouhavealotofdata,thechoiceofthepriordoesn’tmatterverymuch;informativeanduninformativepriorsyieldalmostthesameresults.

!Ifyoudon’thavemuchdata,usingrelevantbackgroundinformationmakesabig

difference.

Page 41: Mol evol jc.151216-amb.bayesianstats-teaser

DifferencesbetweenBayesiansandNon-Bayesians

AccordingtoJeffGill(CenterforAppliedStatistics,WashU)

Page 42: Mol evol jc.151216-amb.bayesianstats-teaser

DifferencesbetweenBayesiansandNon-Bayesians

AccordingtoJeffGill(CenterforAppliedStatistics,WashU)

ACCP 37th Annual Meeting, Philadelphia, PA [2]

Differences Between Bayesians and Non-BayesiansAccording to my friend Jeff Gill

Typical Bayesian Typical Non-BayesianTypicalBayesian

Page 43: Mol evol jc.151216-amb.bayesianstats-teaser

DifferencesbetweenBayesiansandNon-Bayesians

ACCP 37th Annual Meeting, Philadelphia, PA [2]

Differences Between Bayesians and Non-BayesiansAccording to my friend Jeff Gill

Typical Bayesian Typical Non-BayesianTypicalBayesian

ACCP 37th Annual Meeting, Philadelphia, PA [2]

Differences Between Bayesians and Non-BayesiansAccording to my friend Jeff Gill

Typical Bayesian Typical Non-BayesianTypicalNon-Bayesian

AccordingtoJeffGill(CenterforAppliedStatistics,WashU)

Page 44: Mol evol jc.151216-amb.bayesianstats-teaser

Conclusions

• Importanceofmodelling• Followadiscreteapproach:correctfirst,andexpandlater

Page 45: Mol evol jc.151216-amb.bayesianstats-teaser

GeneralApproach

1.Startwithsimplemodelsandimplementtheminclear,readableanddemonstrablycorrectcode.Focusshouldbeongoodmodellingdecisions,notoptimisation

Page 46: Mol evol jc.151216-amb.bayesianstats-teaser

GeneralApproach

1.Startwithsimplemodelsandimplementtheminclear,readableanddemonstrablycorrectcode.Focusshouldbeongoodmodellingdecisions,notoptimisation2.Identifythebiggestsourcesoferror.Perhapsincreasethenumberofvaluesinadiscreteapproximation,increasethenumberofiterationsinaMCsimulation,oradddetailstothemodel

Page 47: Mol evol jc.151216-amb.bayesianstats-teaser

GeneralApproach

1.Startwithsimplemodelsandimplementtheminclear,readableanddemonstrablycorrectcode.Focusshouldbeongoodmodellingdecisions,notoptimisation2.Identifythebiggestsourcesoferror.Perhapsincreasethenumberofvaluesinadiscreteapproximation,increasethenumberofiterationsinaMCsimulation,oradddetailstothemodel3.Isperformancegood?Ifnot,tryoptimisingthen

Page 48: Mol evol jc.151216-amb.bayesianstats-teaser

REFERENCES

• PyContutorials(byAllenDowney)https://sites.google.com/site/simplebayes/!• “ProbablyOverthinkingIt”(byAllenDowney)http://allendowney.blogspot.se/!

• MarkA.Beaumont&BruceRannala(2004)NatureRevGenetics

MonumenttomembersoftheBayesandCottonfamilies,includingThomasBayesandhisfatherJoshua,inBunhillFieldsburialground