Table Of Contentptg21761562
Bayesian Methods
for Hackers
ptg21761562
The Addison-Wesley Data and Analytics Series
Visit informit.com/awdataseries for a complete list of available publications.
ptg21761562
T he Addison-Wesley Data and Analytics Series provides readers with practical
knowledge for solving problems and answering questions with data. Titles in this series
primarily focus on three areas:
1. Infrastructure: how to store, move, and manage data
2. Algorithms: how to mine intelligence or make predictions based on data
3. Visualizations: how to represent data and insights in a meaningful and compelling way
The series aims to tie all three of these areas together to help the reader build end-to-end
systems for fighting spam; making recommendations; building personalization;
detecting trends, patterns, or problems; and gaining insight from the data exhaust of
systems and user interactions.
Make sure to connect with us!
informit.com/socialconnect
Bayesian Methods
for Hackers
Probabilistic Programming
and Bayesian Inference
ptg21761562
Cameron Davidson-Pilon
NewYork•Boston•Indianapolis•SanFrancisco
Toronto•Montreal•London•Munich•Paris•Madrid
Capetown•Sydney•Tokyo•Singapore•MexicoCity
Manyofthedesignationsusedbymanufacturersandsellerstodistinguishtheirproductsareclaimedas
trademarks.Wherethosedesignationsappearinthisbook,andthepublisherwasawareofatrademark
claim,thedesignationshavebeenprintedwithinitialcapitallettersorinallcapitals.
Theauthorandpublisherhavetakencareinthepreparationofthisbook,butmakenoexpressedor
impliedwarrantyofanykindandassumenoresponsibilityforerrorsoromissions.Noliabilityisassumed
forincidentalorconsequentialdamagesinconnectionwithorarisingoutoftheuseoftheinformationor
programscontainedherein.
Forinformationaboutbuyingthistitleinbulkquantities,orforspecialsalesopportunities(whichmay
includeelectronicversions;customcoverdesigns;andcontentparticulartoyourbusiness,traininggoals,
marketingfocus,orbrandinginterests),pleasecontactourcorporatesalesdepartmentat
corpsales@pearsoned.comor(800)382-3419.
Forgovernmentsalesinquiries,pleasecontactgovernmentsales@pearsoned.com.
ForquestionsaboutsalesoutsidetheUnitedStates,pleasecontactinternational@pearsoned.com.
VisitusontheWeb:informit.com/aw ptg21761562
LibraryofCongressCataloging-in-PublicationData
Davidson-Pilon,Cameron.
Bayesianmethodsforhackers:probabilisticprogrammingandbayesianinference/CameronDavidson-Pilon.
pagescm
Includesbibliographicalreferencesandindex.
ISBN978-0-13-390283-9(pbk.:alk.paper)
1.Penetrationtesting(Computersecurity)–Mathematics.2.Bayesianstatisticaldecisiontheory.
3.Softcomputing.I.Title.
QA76.9.A25D3762015
006.3–dc23
2015017249
Copyright©2016CameronDavidson-Pilon
Allrightsreserved.PrintedintheUnitedStatesofAmerica.Thispublicationisprotectedbycopyright,and
permissionmustbeobtainedfromthepublisherpriortoanyprohibitedreproduction,storageinaretrieval
system,ortransmissioninanyformorbyanymeans,electronic,mechanical,photocopying,recording,or
likewise.Toobtainpermissiontousematerialfromthiswork,pleasesubmitawrittenrequesttoPearson
Education,Inc.,PermissionsDepartment,200OldTappanRoad,OldTappan,NewJersey07675,oryou
mayfaxyourrequestto(201)236-3290.
ThecodethroughoutandChapters1through6inthisbookisreleasedundertheMITLicense.
ISBN-13:978-0-13-390283-9
ISBN-10:0-13-390283-8
TextprintedintheUnitedStatesonrecycledpaperatRRDonnelleyinCrawfordsville,Indiana.
Firstprinting,October2015
(cid:118)
This book is dedicated to many important relationships: my parents,
my brothers, and my closest friends. Second to them, it is devoted
to the open-source community, whose work we consume every
day without knowing.
(cid:118)
ptg21761562
This page intentionally left blank
ptg21761562
Contents
Foreword xiii
Preface xv
Acknowledgments xvii
AbouttheAuthor xix
1 ThePhilosophyofBayesianInference 1
1.1 Introduction 1
1.1.1 TheBayesianStateofMind 1
1.1.2 BayesianInferenceinPractice 3
1.1.3 AreFrequentistMethodsIncorrect? 4
1.1.4 ANoteon“BigData” 4
1.2 OurBayesianFramework 5
1.2.1 Example:MandatoryCoin-Flip 5
1.2.2 Example:LibrarianorFarmer? 6
1.3 ProbabilityDistributions 8
1.3.1 DiscreteCase 9
ptg21761562
1.3.2 ContinuousCase 10
1.3.3 ButWhatIsλ? 12
1.4 UsingComputerstoPerformBayesianInference
forUs 12
1.4.1 Example:InferringBehaviorfrom
Text-MessageData 12
1.4.2 IntroducingOurFirstHammer:PyMC 14
1.4.3 Interpretation 18
1.4.4 WhatGoodAreSamplesfromthePosterior,
Anyway? 18
1.5 Conclusion 20
1.6 Appendix 20
1.6.1 DeterminingStatisticallyiftheTwoλsAre
IndeedDifferent? 20
1.6.2 ExtendingtoTwoSwitchpoints 22
1.7 Exercises 24
1.7.1 Answers 24
1.8 References 25
viii Contents
2 ALittleMoreonPyMC 27
2.1 Introduction 27
2.1.1 ParentandChildRelationships 27
2.1.2 PyMCVariables 28
2.1.3 IncludingObservationsintheModel 31
2.1.4 Finally... 33
2.2 ModelingApproaches 33
2.2.1 SameStory,DifferentEnding 35
2.2.2 Example:BayesianA/BTesting 38
2.2.3 ASimpleCase 38
2.2.4 AandBTogether 41
2.2.5 Example:AnAlgorithmforHuman
Deceit 45
2.2.6 TheBinomialDistribution 45
2.2.7 Example:CheatingAmongStudents 46
2.2.8 AlternativePyMCModel 50
2.2.9 MorePyMCTricks 51
2.2.10 Example:ChallengerSpaceShuttle
Disaster 52 ptg21761562
2.2.11 TheNormalDistribution 55
2.2.12 WhatHappenedtheDayofthe
ChallengerDisaster? 61
2.3 IsOurModelAppropriate? 61
2.3.1 SeparationPlots 64
2.4 Conclusion 68
2.5 Appendix 68
2.6 Exercises 69
2.6.1 Answers 69
2.7 References 69
3 OpeningtheBlackBoxofMCMC 71
3.1 TheBayesianLandscape 71
3.1.1 ExploringtheLandscapeUsing
MCMC 76
3.1.2 AlgorithmstoPerformMCMC 78
3.1.3 OtherApproximationSolutionstothe
Posterior 79
3.1.4 Example:UnsupervisedClusteringUsing
aMixtureModel 79
Contents ix
3.1.5 Don’tMixPosteriorSamples 88
3.1.6 UsingMAPtoImproveConvergence 91
3.2 DiagnosingConvergence 92
3.2.1 Autocorrelation 92
3.2.2 Thinning 95
3.2.3 pymc.Matplot.plot() 97
3.3 UsefulTipsforMCMC 98
3.3.1 IntelligentStartingValues 98
3.3.2 Priors 99
3.3.3 TheFolkTheoremofStatistical
Computing 99
3.4 Conclusion 99
3.5 Reference 99
4 TheGreatestTheoremNeverTold 101
4.1 Introduction 101
4.2 TheLawofLargeNumbers 101
4.2.1 Intuition 101
4.2.2 Example:ConvergenceofPoissonRandom
ptg21761562
Variables 102
4.2.3 HowDoWeComputeVar(Z)? 106
4.2.4 ExpectedValuesandProbabilities 106
4.2.5 WhatDoesAllThisHavetoDowithBayesian
Statistics? 107
4.3 TheDisorderofSmallNumbers 107
4.3.1 Example:AggregatedGeographicData 107
4.3.2 Example:Kaggle’sU.S.CensusReturnRate
Challenge 109
4.3.3 Example:HowtoSortReddit
Comments 111
4.3.4 Sorting! 115
4.3.5 ButThisIsTooSlowforReal-Time! 117
4.3.6 ExtensiontoStarredRatingSystems 122
4.4 Conclusion 122
4.5 Appendix 122
4.5.1 DerivationofSortingComments
Formula 122
4.6 Exercises 123
4.6.1 Answers 124
4.7 References 125