{ "metadata": { "name": "Prologue" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Probabilistic Programming and \n", "=====\n", "\n", "Bayesian Methods for Hackers \n", "=======" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "###Probabilistic Programming & Bayesian Methods for Hackers \n", "#### *Using Python and PyMC*\n", "\n", "\n", "\n", "\n", "The Bayesian method is the natural approach to inference, yet it is hidden from readers behind chapters of slow, mathematical analysis. The typical text on Bayesian inference involves two to three chapters on probability theory, then enters what Bayesian inference is. Unfortunately, due to mathematical intractability of most Bayesian models, the reader is only shown simple, artificial examples. This can leave the user with a *so-what* feeling about Bayesian inference. In fact, this was the author's own prior opinion.\n", "\n", "\n", "
\n", "\n", "After some recent success of Bayesian methods in machine-learning competitions, I decided to investigate the subject again. Even with my mathematical background, it took me three straight-days of reading examples and trying to put the pieces together to understand the methods. There was simply not enough literature bridging theory to practice. The problem with my misunderstanding was the disconnect between Bayesian mathematics and probabilistic programming. That being said, I suffered then so the reader would not have to now. This book attempts to bridge the gap.\n", "\n", "If Bayesian inference is the destination, then mathematical analysis is a particular path to towards it. On the other hand, computing power is cheap enough that we can afford to take an alternate route via probabilistic programming. The latter path is much more useful, as it denies the necessity of mathematical intervention at each step, that is, we remove often-intractable mathematical analysis as a prerequisite to Bayesian inference. Simply put, this latter computational path proceeds via small intermediate jumps from beginning to end, where as the first path proceeds by enormous leaps, often landing far away from our target. Furthermore, without a strong mathematical background, the analysis required by the first path cannot even take place.\n", "\n", "*Bayesian Methods for Hackers* is designed as a introduction to Bayesian inference from a computational/understanding-first, and mathematics-second, point of view. Of course as an introductory book, we can only leave it at that: an introductory book. For the mathematically trained, they may cure the curiosity this text generates with other texts designed with mathematical analysis in mind. For the enthusiast with less mathematical-background, or one who is not interested in the mathematics but simply the practice of Bayesian methods, this text should be sufficient and entertaining.\n", "\n", "\n", "The choice of PyMC as the probabilistic programming language is two-fold. As of this writing, there is currently no central resource for examples and explanations in the PyMC universe. The official documentation assumes prior knowledge of Bayesian inference and probabilistic programming. We hope this book encourages users at every level to look at PyMC. Secondly, with recent core developments and popularity of the scientific stack in Python, PyMC is likely to become a core component soon enough.\n", "\n", "PyMC does have dependencies to run, namely NumPy and (optionally) SciPy. To not limit the user, the examples in this book will rely only on PyMC, NumPy, SciPy and Matplotlib only.\n", "\n", "\n", "Contents\n", "------\n", "\n", "(The below chapters are rendered via the *nbviewer* at\n", "[nbviewer.ipython.org/](http://nbviewer.ipython.org/), and is read-only and rendered in real-time.\n", "Interactive notebooks + examples can be downloaded by cloning! )\n", "\n", "\n", "* [**Prologue:**](http://nbviewer.ipython.org/urls/raw.github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/master/Prologue/Prologue.ipynb) Why we do it.\n", "\n", "* [**Chapter 1: Introduction to Bayesian Methods**](http://nbviewer.ipython.org/urls/raw.github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/master/Chapter1_Introduction/Chapter1_Introduction.ipynb)\n", " Introduction to the philosophy and practice of Bayesian methods and answering the question \"What is probabilistic programming?\" Examples include:\n", " - Inferring human behaviour changes from text message rates.\n", " \n", "* [**Chapter 2: A little more on PyMC**](http://nbviewer.ipython.org/urls/raw.github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/master/Chapter2_MorePyMC/MorePyMC.ipynb)\n", " We explore modeling Bayesian problems using Python's PyMC library through examples. How do we create Bayesian models? Examples include:\n", " - Detecting the frequency of cheating students, while avoiding liars.\n", " - Calculating probabilities of the Challenger space-shuttle disaster.\n", " \n", "* [**Chapter 3: Opening the Black Box of MCMC**](http://nbviewer.ipython.org/urls/raw.github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/master/Chapter3_MCMC/IntroMCMC.ipynb)\n", " We discuss how MCMC operates and diagnostic tools. Examples include:\n", " - Bayesian clustering with mixture models\n", " \n", "* [**Chapter 4: The Greatest Theorem Never Told**](http://nbviewer.ipython.org/urls/raw.github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/master/Chapter4_TheGreatestTheoremNeverTold/LawOfLargeNumbers.ipynb)\n", " We explore an incredibly useful, and dangerous, theorem: The Law of Large Numbers. Examples include:\n", " - Exploring a Kaggle dataset and the pitfalls of naive analysis\n", " - How to sort Reddit comments from best to worst (not as easy as you think)\n", " \n", "* [**Chapter 5: Would you rather loss an arm or a leg?**](http://nbviewer.ipython.org/urls/raw.github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/master/Chapter5_LossFunctions/LossFunctions.ipynb)\n", " The introduction of Loss functions and their (awesome) use in Bayesian methods. Examples include:\n", " - Solving the Price is Right's Showdown\n", " - Optimizing financial predictions\n", " - Winning solution to the Kaggle Dark World's competition.\n", " \n", "* [**Chapter 6: Getting our *prior*-ities straight**](http://nbviewer.ipython.org/urls/raw.github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/master/Chapter6_Priorities/Priors.ipynb)\n", " Probably the most important chapter. We draw on expert opinions to answer questions. Examples include:\n", " - Multi-Armed Bandits and the Bayesian Bandit solution.\n", " - what is the relationship between data sample size and prior?\n", " - estimating financial unknowns using expert priors.\n", " \n", " We explore useful tips to be objective in analysis, and common pitfalls of priors. \n", " \n", "* **Chapter X1: Bayesian Markov Models**\n", " \n", "* **Chapter X2: Bayesian methods in Machine Learning** \n", " We explore how to resolve the overfitting problem plus popular ML methods. Also included are probablistic explainations of Ridge Regression and LASSO Regression.\n", " - Bayesian spam filtering plus *how to defeat Bayesian spam filtering*\n", " - Tim Saliman's winning solution to Kaggle's *Don't Overfit* problem \n", " \n", "* **Chapter X3: More PyMC Hackery**\n", " We explore the gritty details of PyMC. Examples include:\n", " - Analysis on real-time GitHub repo stars and forks.\n", "\n", "* **Chapter X4: Troubleshooting and debugging**\n", "\n", " \n", "**More questions about PyMC?**\n", "Please post your modeling, convergence, or any other PyMC question on [cross-validated](http://stats.stackexchange.com/), the statistics stack-exchange.\n", " \n", " \n", "Using the book\n", "-------\n", "\n", "The book can be read in three different ways, starting from most recommended to least recommended: \n", "\n", "1. The most recommended option is to clone the repository to download the .ipynb files to your local machine. If you have IPython installed, you can view the \n", "chapters in your browser *plus* edit and run the code provided (and try some practice questions). This is the preferred option to read\n", "this book, though it comes with some dependencies. \n", " - IPython 0.13 is a requirement to view the ipynb files. It can be downloaded [here](http://ipython.org/)\n", " - For Linux users, you should not have a problem installing Numpy, Scipy, Matplotlib and PyMC. For Windows users, check out [pre-compiled versions](http://www.lfd.uci.edu/~gohlke/pythonlibs/) if you have difficulty. \n", " - In the styles/ directory are a number of files (.matplotlirc) that used to make things pretty. These are not only designed for the book, but they offer many improvements over the default settings of matplotlib and the IPython notebook.\n", " - while technically not required, it may help to run the IPython notebook with `--pylab inline` if you encounter runtime errors.\n", "2. The second, preferred, option is to use the nbviewer.ipython.org site, which display IPython notebooks in the browser ([example](http://nbviewer.ipython.org/urls/raw.github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/master/Chapter1_Introduction/Chapter1_Introduction.ipynb)).\n", "The contents are updated synchronously as commits are made to the book. You can use the Contents section above to link to the chapters.\n", " \n", "3. **PDF versions are coming.** PDFs are the least-prefered method to read the book, as pdf's are static and non-interactive. If PDFs are desired, they can be created dynamically using Chrome's builtin print-to-pdf feature.\n", " \n", "\n", "Installation and configuration\n", "------\n", "If you would like to run the IPython notebooks locally, (option 1. above), you'll need to install the following:\n", "\n", "- IPython 0.13 is a requirement to view the ipynb files. It can be downloaded [here](http://ipython.org/ipython-doc/dev/install/index.html)\n", "- For Linux users, you should not have a problem installing Numpy, Scipy and PyMC. For Windows users, check out [pre-compiled versions](http://www.lfd.uci.edu/~gohlke/pythonlibs/) if you have difficulty. \n", " - also recommended, for data-mining exercises, are [PRAW](https://github.com/praw-dev/praw) and [requests](https://github.com/kennethreitz/requests). \n", "- In the styles/ directory are a number of files that are customized for the notebook. \n", "These are not only designed for the book, but they offer many improvements over the \n", "default settings of matplotlib and the IPython notebook. The in notebook style has not been finalized yet.\n", "\n", "\n", "Contributions and Thanks\n", "-----\n", "\n", "\n", "Thanks to all our contributing authors, including (in chronological order):\n", "\n", "- [Cameron Davidson-Pilon](http://www.camdp.com)\n", "- [Stef Gibson](http://stefgibson.com)\n", "- [Vincent Ohprecio](http://bigsnarf.wordpress.com/)\n", "- [Lars Buitinck](https://github.com/larsman)\n", "- [Paul Magwene](http://github.com/pmagwene) \n", "- [Matthias Bussonnier](https://github.com/Carreau)\n", "- [Jens Rantil](https://github.com/JensRantil)\n", "- [y-p](https://github.com/y-p)\n", "- [Ethan Brown](http://www.etano.net/)\n", "- [Jonathan Whitmore](http://jonathanwhitmore.com/)\n", "- [Mattia Rigotti](https://github.com/matrig)\n", "- [Colby Lemon](https://github.com/colibius)\n", "- [Gustav W Delius](https://github.com/gustavdelius)\n", "- [Matthew Conlen](http://www.mathisonian.com/) \n", "- [Jim Radford](https://github.com/radford)\n", "- [Vannessa Sabino](http://baniverso.com/)\n", "- [Thomas Bratt](https://github.com/thomasbratt)\n", "- [Nisan Haramati](https://github.com/nisanharamati)\n", "- [Thomas Bratt](https://github.com/thomasbratt)\n", "- [Robert Grant](https://github.com/bgrant)\n", "- [Yaroslav Halchenko](https://github.com/yarikoptic)\n", "- [Alex Garel](https://github.com/alexgarel)\n", "\n", "\n", "We would like to thank the Python community for building an amazing architecture. We would like to thank the \n", "statistics community for building an amazing architecture. \n", "\n", "Similarly, the book is only possible because of the [PyMC](http://github.com/pymc-devs/pymc) library. A big thanks to the core devs of PyMC: Chris Fonnesbeck, Anand Patil, David Huard and John Salvatier.\n", "\n", "One final thanks. This book was generated by IPython Notebook, a wonderful tool for developing in Python. We thank the IPython \n", "community for developing the Notebook interface. All IPython notebook files are available for download on the GitHub repository. \n", "\n", "\n", "\n", "####Contact\n", "Contact the main author, Cam Davidson-Pilon at cam.davidson.pilon@gmail.com or [@cmrndp](https://twitter.com/cmrn_dp)\n", "\n", "\n", "![Imgur](http://i.imgur.com/Zb79QZb.png)\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.core.display import HTML\n", "def css_styling():\n", " styles = open(\"../styles/custom.css\", \"r\").read()\n", " return HTML(styles)\n", "css_styling()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "" ], "output_type": "pyout", "prompt_number": 1, "text": [ "" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }