GithubHelp home page GithubHelp logo

camdavidsonpilon / probabilistic-programming-and-bayesian-methods-for-hackers Goto Github PK

View Code? Open in Web Editor NEW
26.4K 1.4K 7.8K 168.03 MB

aka "Bayesian Methods for Hackers": An introduction to Bayesian methods + probabilistic programming with a computation/understanding-first, mathematics-second point of view. All in pure Python ;)

Home Page: http://camdavidsonpilon.github.io/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/

License: MIT License

Jupyter Notebook 99.84% Python 0.14% HTML 0.01% CSS 0.01% Shell 0.01%
bayesian-methods pymc mathematical-analysis jupyter-notebook data-science statistics

probabilistic-programming-and-bayesian-methods-for-hackers's Introduction

Using Python and PyMC

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.

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.

If Bayesian inference is the destination, then mathematical analysis is a particular path 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.

Bayesian Methods for Hackers is designed as an 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.

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.

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.

Printed Version by Addison-Wesley

Bayesian Methods for Hackers is now available as a printed book! You can pick up a copy on Amazon. What are the differences between the online version and the printed version?

  • Additional Chapter on Bayesian A/B testing
  • Updated examples
  • Answers to the end of chapter questions
  • Additional explanation, and rewritten sections to aid the reader.

Contents

See the project homepage here for examples, too.

The below chapters are rendered via the nbviewer at nbviewer.jupyter.org/, and is read-only and rendered in real-time. Interactive notebooks + examples can be downloaded by cloning!

PyMC2

  • Prologue: Why we do it.

  • Chapter 1: Introduction to Bayesian Methods Introduction to the philosophy and practice of Bayesian methods and answering the question, "What is probabilistic programming?" Examples include:

    • Inferring human behaviour changes from text message rates
  • Chapter 2: A little more on PyMC We explore modeling Bayesian problems using Python's PyMC library through examples. How do we create Bayesian models? Examples include:

    • Detecting the frequency of cheating students, while avoiding liars
    • Calculating probabilities of the Challenger space-shuttle disaster
  • Chapter 3: Opening the Black Box of MCMC We discuss how MCMC operates and diagnostic tools. Examples include:

    • Bayesian clustering with mixture models
  • Chapter 4: The Greatest Theorem Never Told We explore an incredibly useful, and dangerous, theorem: The Law of Large Numbers. Examples include:

    • Exploring a Kaggle dataset and the pitfalls of naive analysis
    • How to sort Reddit comments from best to worst (not as easy as you think)
  • Chapter 5: Would you rather lose an arm or a leg? The introduction of loss functions and their (awesome) use in Bayesian methods. Examples include:

    • Solving the Price is Right's Showdown
    • Optimizing financial predictions
    • Winning solution to the Kaggle Dark World's competition
  • Chapter 6: Getting our prior-ities straight Probably the most important chapter. We draw on expert opinions to answer questions. Examples include:

    • Multi-Armed Bandits and the Bayesian Bandit solution.
    • What is the relationship between data sample size and prior?
    • Estimating financial unknowns using expert priors

    We explore useful tips to be objective in analysis as well as common pitfalls of priors.

PyMC3

  • Prologue: Why we do it.

  • Chapter 1: Introduction to Bayesian Methods Introduction to the philosophy and practice of Bayesian methods and answering the question, "What is probabilistic programming?" Examples include:

    • Inferring human behaviour changes from text message rates
  • Chapter 2: A little more on PyMC We explore modeling Bayesian problems using Python's PyMC library through examples. How do we create Bayesian models? Examples include:

    • Detecting the frequency of cheating students, while avoiding liars
    • Calculating probabilities of the Challenger space-shuttle disaster
  • Chapter 3: Opening the Black Box of MCMC We discuss how MCMC operates and diagnostic tools. Examples include:

    • Bayesian clustering with mixture models
  • Chapter 4: The Greatest Theorem Never Told We explore an incredibly useful, and dangerous, theorem: The Law of Large Numbers. Examples include:

    • Exploring a Kaggle dataset and the pitfalls of naive analysis
    • How to sort Reddit comments from best to worst (not as easy as you think)
  • Chapter 5: Would you rather lose an arm or a leg? The introduction of loss functions and their (awesome) use in Bayesian methods. Examples include:

    • Solving the Price is Right's Showdown
    • Optimizing financial predictions
    • Winning solution to the Kaggle Dark World's competition
  • Chapter 6: Getting our prior-ities straight Probably the most important chapter. We draw on expert opinions to answer questions. Examples include:

    • Multi-Armed Bandits and the Bayesian Bandit solution.
    • What is the relationship between data sample size and prior?
    • Estimating financial unknowns using expert priors

    We explore useful tips to be objective in analysis as well as common pitfalls of priors.

More questions about PyMC? Please post your modeling, convergence, or any other PyMC question on cross-validated, the statistics stack-exchange.

Using the book

The book can be read in three different ways, starting from most recommended to least recommended:

  1. The most recommended option is to clone the repository to download the .ipynb files to your local machine. If you have Jupyter installed, you can view the chapters in your browser plus edit and run the code provided (and try some practice questions). This is the preferred option to read this book, though it comes with some dependencies.

    • Jupyter is a requirement to view the ipynb files. It can be downloaded here. Jupyter notebooks can be run by (your-virtualenv) ~/path/to/the/book/Chapter1_Introduction $ jupyter notebook
    • For Linux users, you should not have a problem installing NumPy, SciPy, Matplotlib and PyMC. For Windows users, check out pre-compiled versions if you have difficulty.
    • 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.
  2. The second, preferred, option is to use the nbviewer.jupyter.org site, which display Jupyter notebooks in the browser (example). The contents are updated synchronously as commits are made to the book. You can use the Contents section above to link to the chapters.

  3. PDFs are the least-preferred method to read the book, as PDFs are static and non-interactive. If PDFs are desired, they can be created dynamically using the nbconvert utility.

Installation and configuration

If you would like to run the Jupyter notebooks locally, (option 1. above), you'll need to install the following:

  • Jupyter is a requirement to view the ipynb files. It can be downloaded here

  • Necessary packages are PyMC, NumPy, SciPy and Matplotlib.

  • New to Python or Jupyter, and help with the namespaces? Check out this answer.

  • In the styles/ directory are a number of files that are customized for the notebook. These are not only designed for the book, but they offer many improvements over the default settings of matplotlib and the Jupyter notebook. The in notebook style has not been finalized yet.

Development

This book has an unusual development design. The content is open-sourced, meaning anyone can be an author. Authors submit content or revisions using the GitHub interface.

How to contribute

What to contribute?

  • The current chapter list is not finalized. If you see something that is missing (MCMC, MAP, Bayesian networks, good prior choices, Potential classes etc.), feel free to start there.
  • Cleaning up Python code and making code more PyMC-esque
  • Giving better explanations
  • Spelling/grammar mistakes
  • Suggestions
  • Contributing to the Jupyter notebook styles

Commiting

  • All commits are welcome, even if they are minor ;)
  • If you are unfamiliar with Github, you can email me contributions to the email below.

Reviews

these are satirical, but real

"No, but it looks good" - John D. Cook

"I ... read this book ... I like it!" - Andrew Gelman

"This book is a godsend, and a direct refutation to that 'hmph! you don't know maths, piss off!' school of thought... The publishing model is so unusual. Not only is it open source but it relies on pull requests from anyone in order to progress the book. This is ingenious and heartening" - excited Reddit user

Contributions and Thanks

Thanks to all our contributing authors, including (in chronological order):

Authors
Cameron Davidson-Pilon Stef Gibson Vincent Ohprecio Lars Buitinck
Paul Magwene Matthias Bussonnier Jens Rantil y-p
Ethan Brown Jonathan Whitmore Mattia Rigotti Colby Lemon
Gustav W Delius Matthew Conlen Jim Radford Vannessa Sabino
Thomas Bratt Nisan Haramati Robert Grant Matthew Wampler-Doty
Yaroslav Halchenko Alex Garel Oleksandr Lysenko liori
ducky427 Pablo de Oliveira Castro sergeyfogelson Mattia Rigotti
Matt Bauman Andrew Duberstein Carsten Brandt Bob Jansen
ugurthemaster William Scott Min RK Bulwersator
elpres Augusto Hack Michael Feldmann Youki
Jens Rantil Kyle Meyer Eric Martin Inconditus
Kleptine Stuart Layton Antonino Ingargiola vsl9
Tom Christie bclow Simon Potter Garth Snyder
Daniel Beauchamp Philipp Singer gbenmartin Peadar Coyle

We would like to thank the Python community for building an amazing architecture. We would like to thank the statistics community for building an amazing architecture.

Similarly, the book is only possible because of the PyMC library. A big thanks to the core devs of PyMC: Chris Fonnesbeck, Anand Patil, David Huard and John Salvatier.

One final thanks. This book was generated by Jupyter Notebook, a wonderful tool for developing in Python. We thank the IPython/Jupyter community for developing the Notebook interface. All Jupyter notebook files are available for download on the GitHub repository.

Contact

Contact the main author, Cam Davidson-Pilon at [email protected] or @cmrndp

Imgur

probabilistic-programming-and-bayesian-methods-for-hackers's People

Contributors

bigsnarfdude avatar brennon avatar camdavidsonpilon avatar cebe avatar eli-b avatar elpres avatar gbenmartin avatar gustavdelius avatar haraldschilly avatar hsm207 avatar larsmans avatar lnauta avatar manuelweiss avatar matrig avatar matthew-mcateer avatar mdiephuis avatar miemiekurisu avatar mksenzov avatar mmargenot avatar noelevans avatar pablooliveira avatar pieteradejong avatar pushmatrix avatar radford avatar sash-ko avatar shayan-b avatar thomasaarholt avatar twiecki avatar williamscott avatar zaxtax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

probabilistic-programming-and-bayesian-methods-for-hackers's Issues

Edits to Poisson example in chapter 1

From Alex Nelson (direct communication)

In chapter 1 when you describe the Poisson distribution, I have some suggestions.

First, your description of lambda could be improved. You should describe it as the "intensity" of the distribution, which can be seen if one derives it from the Binomial distribution Bin(n,p) taking n to infinity, provided we fix n*p = lambda.

Second, why not leave the aforementioned derivation as an exercise? (Or -- what I do in my notes -- is ask it as a question, then insist the reader make the attempt, and solve it in an example.)

Repo too large

Currently the repo is +25mb. Thats rediculous. Most of it is the data files associated with chapters. Either

  • reduce the size by cutting data,
  • zip them then extract when needed.
  • other?

requirements.txt

To start, I just want to say that this book is excellent.

It might be helpful if this project came with a requirements.txt file (for use with pip). This would make it trivial to install the small number of dependencies, and ensure the book works for people who might have older versions installed.

Typo in Chapter 2

I was reading this fabulous book and noticed in Chapter 2 this definition where it appears that you used the ambient “temperature” array instead of the formal variable. I am not a Python expert so perhaps this is correct but it is odd looking.

Thanks for the book
--david

def p( temp = temperature, alpha = alpha, beta = beta):
return 1.0/( 1. + np.exp( beta*temperature + alpha) )

PEP8 fixes

The code examples I've read do not adhere to the PEP8 coding standards. Are you open to cosmetic fixes for this issue?

Likelyhood computation in chapter 3

In chapter 3, in "The Bayesian landscape" we create observed data this way:

### create the observed data

N = 1 #sample size of data we observe
p_1_true = 1
p_2_true = 3

data = np.concatenate( [ stats.poisson.rvs( p_1_true, size = (N,1)), \
                            stats.poisson.rvs( p_2_true, size = (N,1))],  axis=1 )

As I understand, stats.poisson.rvs takes a sample in poisson distribution.

Then data is used to compute likelyhood. So we compute likelyhood for two numbers that we do not know and that are not our observed data. Why is it so ?

Why don't we define data as data = [p_1_true, p_2_true] ?

protip: Matrix distributions

Since matrix distributions, like Wishart etc. involve on the order of N^2 variables, it is really useful to give it a good starting value. Unfortunately, setting value = M, where M is usually the emprical scatter matrix, in the initialization call for Stochastic often fails. This is (likely) due to numerical imprecision in M not being truly symmetric. It is useful then to use M = np.round(M, 8 ), which Fortran will like.

Python AUC code

pinging @benhamner to share his AUC code for Chapter 7, to use in the Don't Overfit example.

Does that ping work?

Chapter 3, Clustering example, creating model.

I have a quick question about the
initialization of the model class in the Chapter 3, clustering example.
Why are observations not included in the
list of parameters in mc.Model?

#and to combine it with the observations:
observations = mc.Normal( "obs", center_i, tau_i, value = data, observed = True )

#below we create a model class
model = mc.Model( [p, assignment, taus, centers ] )

Chapter 1 explanation issue

Hello,

in chapter one it is stated: "and our confidence is proportional to the width of the curve." - actually, I think the confidence is inversely proportional to the width.

Line 63 in Chapter 1 contains an unfinished sentence

Problem area is italicized:

"Your code either has a bug in it or not, but we do not know for certain which is true. Though we have a belief about the presence or absence of a bug."

Anyone know how this line is supposed to end?

Chapter 4: Notable Failures of the Law of Large Numbers

While the Law of Large Numbers is a profoundly important result, I like many statisticians have come to doubt its universal applicability.

This Law holds for any distribution, minus some pathological examples that only mathematicians have fun with.

I take issue with this assertion. Two important failures off the top of my head:

  1. Fat-tailed distributions - These distributions frequently fail a critical assumption underlying the law of large numbers - namely, that

    $$\sum_{n=1}^\infty \frac{Var(X_n)}{n^2} < \infty$$

    One example that fails this is the Pareto distribution with $\alpha \in (1,2]$ (which has a divergent variance). This happens _all the time_ - here's a typical example from Terry Tao's blog (2009). Likewise, fat-tailed distributions that do satisfy the (weak) law of large numbers only converge to the predicted asymptote rather slowly, rendering the principle ineffective (see Weron et al, International Journal of Modern Physics C (2001))

    1. Flicker Noise (aka 1/f Noise) - Flicker noise is another ubiquitous phenomenon that fails the law of large numbers. Example: A perfect hour-glass has an average clock-drift of 0 seconds. However, clock drift is governed by flicker noise, which fails to satisfy the assumptions behind the (weak) law of large numbers. As a consequence, no matter how many hour-glasses you have, you can't be more accurate than an atomic clock. This example is from Bill Press (of Numerical Recipes fame), who wrote something of a classic on this subject: Flicker Noises in Astronomy and Elsewhere (1978)

Of course, as you point out in your chapter, one can hold the Law of Large Numbers at arms length with Bayesian analysis.

ipynb -> tex converter

Hi,

I love your notebooks on nbconvert.

We are working on the brand new nbconvert to convert IPython noteboko files, and especially latex here , pinging @jdfreder that he is the most involved in the latex converter. I thought this might interest both you to convert to latex, and us to have a nice example to get feedback and refine the settings.

Also, if/when you wish I can also put you on the home page of nbconvert.

Estimate the reading time

Before I start reading a paperbook or a PDF I usually look at how many pages it has. Then I try to estimate how much time I will spent to get through it.
I do not see an easy way to perform such an assessment with any HTML/GitHub book.
So please tell me how many pages this book would have taken if it was printed on paper. What is your best guess for the average reading time?

Issue with halo/sky drawing plot in chapter 5.

Brought to me attention by Gabriel Kronberger:

I'm almost sure that your draw_sky routine has a bug. This is apparent in one-halo sky (7) because the nearest galaxies are not elongated tangentially but instead point into the direction of the halo. Comparing your picture with my outputs some galaxies are shown correctly while others are elongated in the wrong direction. This indicates that there might by a problem with the calculation of the angle using arctan / atan2. I checked your code but can't point the finger at the bug, because I'm not familiar with the ellipse command, does it handle negative angles correctly?

If it helps I used linear transformations for the graphical output in C#

        m.Translate(x, y]);
        m.Rotate(45);
        m.Scale(1f + Math.Max(0, e2[i]), 1.0f - Math.Min(e2[i], 0));
        m.Rotate(-45);
        m.Scale(1f + Math.Max(0, e1[i]), 1.0f - Math.Min(e1[i], 0));
        graphics.Transform = m;
        graphics.FillEllipse(Brushes.Black, -2f, -2f, 4f, 4f);

A main issue is the strange reference system for e1 and e2.
One thing you can try is to check skys that have one very pronounced halo (e.g. 3). For comparison I attached my visualization of training sky number 3.

Training_Sky3

Bandit example stopped working

In Chapter 6 the interactive bandits example does not work anymore.
The buttons are displayed, but the bar charts and pdf plots are not shown.

Just looking over the code the problem could be in these lines:

<div id="paired-bar-chart" style="width: 600px; margin: auto;"> </div>
<div id="beta-graphs" style="width: 600px; margin-left:125px; "> </div>

The same is true for the solo BanditsD3.html file.

Error in Price is Right's Showdown

There is a mistake in the rules: if both players go over, it is a double overbid, and no parties win. Thus the loss function will need to be modified a bit.

Instantiating MCMC in PyMC examples

Having a peek through Chapter 3, I notice that you instantiate your MCMC objects in a two-step process:

model = Model([var1, var2, var3])
sampler = MCMC(Model)

There is really no need to explicitly create a Model object at all -- MCMC will do this for you if you pass it the variables:

sampler = MCMC([var1, var2, var3])

In fact, typically I just pass a call to vars (or locals) which MCMC will sift through to look for PyMC objects in the local namespace:

sampler = MCMC(vars())

This is particularly useful if you encapsulate your model in a function that returns vars or locals.

In any case, its just one fewer step in the PyMC modeling process to have to think about.

Overly complicated text message example

From Ghassen HAMROUNI, direct communication

I have a remark concerning the first chapter:
The example "Inferring behaviour from text-message data" is certainly interesting. But I think it is over-complicated. In fact the reader has just learned about the Poisson process. And he is expecting a simple
application where he can infer the value of the constant λ. But instead you introduce an inhomogeneous Poisson process where the λ(t) is a time-dependent function !

NBviewer experiment.

Hi,

Want to do a nbviewer experiment with css/mathjax, hope you don't mind if we try to do it with your notebooks.

The goal would be to allow notebooks to declare a prefered css in metadata so I shamelessly extracted the css from your notebooks and hosted it on nbviewer ( I still need to add you as the author on top, and would like to know in which is the liscence). I just changed width to max-width for mobile device. So you should be able to remove your hack of reading from your custom-css an modify the metadata and still have your nice css.

What are your thinking on that ? What do you think of the nbviewer prefix in metadata (we'll remove the leading underscore once we think this can be released) ?

Anyway, I let you look at the new version of nbviewer released a few hours ago, and give me your thought.

Chapter 2's cheating example's two alternative implementations show different posteriors

From Chapter 2, on cheating detection: "I'll demonstrate the most explicit way, and later show a simplified version. Both versions arrive at the same inference."

The first model shows posterior with no weight at 0 and very little weight above 0.6. The alternative model has a posterior with considerable weight at 0 and no weight above 0.5. Are the two models supposed to be equivalent or not?

Matplotlib styles

Hi,

The plots in the notebooks rely on a custom matplotlibrc for visual style,
Users who do share the same matplotlubrc can't reproduce the graphs,
in terms of visual style, as that information is not part of the notebook.

To make it worse, the default style is horrible:
def

fyi.

add %pylab ontop of each ipynb

Currently the --pylab flag is used ( and necessary ) when running the book. Adding a %pylab tag in the first cell alleviates this.

Running notebook on own, Environment issues

When running the notebook on my own I'm having some issues. I've set my environment up as follows:

clone project
cd into project
mkvirtualenv probabilistic-programming
pip install -r requirements.txt
cd Chapter1_Introduction
ipython notebook

From there my browser opens up and I'm able to read, however when I try to modify any code I see an error. I've tried modifying the the graph in section 'Example: Bug, or just sweet, unintended feature?' and get this error:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-3563e911edb3> in <module>()
----> 1 figsize(12.5, 4)
      2 p = np.linspace(0, 1, 50)
      3 plt.plot(p, 2*p/(1+p), color="#348ABD", lw=3)
      4 #plt.fill_between(p, 2*p/(1+p), alpha=.5, facecolor=["#A60628"])
      5 plt.scatter(0.2, 2*(0.2)/1.2, s=140, c="#348ABD")

NameError: name 'figsize' is not defined

Please let me know what I need to do to modify my environment and be able to run the notebooks interactively. Thanks!

Minor grammar issue

In Chapter 2 towards the end of describing Deterministic variables, I think the following sentence is not quite right:

During the learning phase, it the variables value that is repeatedly passed in, not the actual variable.

When reading, it seemed like it should be:

During the learning phase, it is the variable's value that is repeatedly passed in, not the actual variable.

Bayesian Methods for Hackers

Noticed this commit a3d172b and was wondering if you have any plans to rename everything to just Bayesian Methods for Hackers.

The only benefit I see is the name fitting inline with the header.

No pressure. Just curious.

Histogram Densities Mislabeled As Probabilities in Chapter 1

In chapter 1, when computing histogram results for the traces of MCMC on the example there, the y-axis is labeled probabilities.

This is not how histograms really work in numpy, however. From the documentation:

numpy.histogram(a, bins=10, range=None, normed=False, weights=None, density=None)[source]
Compute the histogram of a set of data.
...
density : bool, optional
If False, the result will contain the number of samples in each bin. If True, the result is the value of the probability density function at the bin, normalized such that the integral over the range is 1. Note that the sum of the histogram values will not be equal to 1 unless bins of unity width are chosen; it is not a probability mass function. Overrides the normed keyword if given.

http://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html

It is correct to write probability density, not probability.

Interpreting probability density is not as straight-forward, however.

In the ideal, limiting case for a Poisson distribution, MCMC should yield a posterior distribution approximating a Dirac delta. Higher variance distributions will be broader and look more uniform. The example in the first chapter has low variance indeed compared to Poisson statistics that tend to arise in nature.

PyMC 2.0 not supported

Some simple things appear to not work with PyMC 2.0. Should probably list PyMC 2.2 explicitly as the supported version. An example of something you can't do with PyMC 2.0 is:

p = pymc.Poisson('test', 5)
s = p + 1

This will complain about not being able to add a Poisson and an int:

TypeError: unsupported operand type(s) for +: 'Poisson' and 'int'

notebook doesn't look right on windows

I installed python x,y and upgraded all required packages to the newest versions and I'm using the ipython notebook server to view the notebooks. This is how it looks for me:

http://i.imgur.com/8mQuCjx.jpg

You notice that the font looks weird, e.g. the dot in the letter i is shifted. Also the indented text box is not light gray as it should be.

Typos chapter 1

postierior instead of posterior in second code snippet.
abscent instead of absent.
positor instead of posterior.

Spelling error?

In the 3rd sentence of the 4th paragraph of the "Bayesian State of Mind" section, "Simpley" should be "Simply", right?

Enhancing the notebooks with dynamic UIs

Note: the dynamic notebooks live Here

Hi cameron,

I released Exhibitionist about 2 weeks ago. It's a python library for building dynamic HTML/UI views on top of live
python objects in interactive python work. That's practically synonymous with ipnb today.

Since then, I've been looking for a way to showcase the library and what's
possible with it. I'd like to take your notebooks and use Exhibitionist to
integrate interactive UI's (dynamic plots, etc') so that readers can interact
in realtime with the concepts.

I've already implemented the first view, allowing the user to visualize the
exponential distribution while varying λ by using a slider. Here's a snapshot:
xb_pp

I'll be working on this in the coming weeks, how would you feel about
having this live in a fork under @Exhibitionist. Would that be ok?

Chapter 3 - Bayesian Landscape with more than one sample

In the section on Bayesian Landscape, the text advises the user to observe how the mountains change with varying sample size.

Changing the sample size from 1 to 100, I can clearly see that the mountain narrows, but it does not converge to the true parameter. (It seems to converge to 3,1 instead of 1,3.)

I am pretty sure the only parameter I changed was the Sample size (N). Could this be an error with the plotting code?

Thanks!

2013-07-09-111247_721x711_scrot

fix Firefox issue

The custom css styling for the book, in styles/custom.css do not play well with Firefox. I'd like to fix this asap.

Explain why we plot M*L in chapter 3

In the first part of chapter 3, it is really mysterious to me why we choose to plot M*L to illustrate how the prior and posterior are related. Could you explain why it is meaningful ?

Suppressing matplotlib.text, .legend

In the first plot in chapter 1, we see <matplotlib.text.Text at ....>
This can be suppressed by putting a semicolon at the end of
plt.title( "Are there bugs in my code?");
Similarly for legend and other snippets.

MCMC = Markov Chain Monte Carlo; not Monte Carlo Markov Chains

I believe it should be Markov Chain Monte Carlo not Monte Carlo Markov Chains.

Chapter 3 - Opening the black box of MCMC
"The machinery being employed is called Monte Carlo Markov Chains "

In Chapter 1
"The machinery being employed is called Monte Carlo Markov Chains"

Issue with clustering example in chapter 3

There is a subtlety in the clustering example in chapter 3 that should probably be pointed out to the reader, as it is quite instructive. There is a symmetry in the model. Exchanging cluster 0 and cluster 1 and at the same time exchanging p and 1-p leads to an equivalent configuration with the same probability. The prior clearly has that symmetry. The true posterior will have the same symmetry. The distribution obtained by MCMC is very far from symmetric. Markov chain has been trapped in a metastable state. That is good for us, because that distribution is easier to interpret. The true posterior distribution with its bimodal marginals would probably confuse the reader.

We can either point out the above to the reader, or we can change the example to a model that does not have the symmetry. For example one could introduce another positive hyperparameter and set the mean of the second cluster to be different from the mean of the first cluster by this parameter. An exponential prior could be chosen for that new parameter. I implemented that and the convergence of the MCMC is better.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.