Tinker, Tailor, Soldier, Sponge

View Original

Preparing for Bayesian Mathematics in Python

In which I run through setting up a Python environment on Windows...

I'm going to preface this post by saying I'm not breaking any new ground here. As is often the case, though, a beginner wanting to set-up an environment to do this will have to go to multiple websites to collect this info so I'm going to distil it into one post.

I am going to talk about Python 2.7 as currently it has the most support for libraries. This method should work equally well for Python 3 but I have not tested this.

Install Python and GUI

Make sure to add Python to command line by selecting "add to path".

You will now have a working Python environment that will run from command line. The standard install also includes a basic Python GUI that will be available through the Python folder in the Start Menu. If you wish to install a full Python IDE go ahead and install PyDev which is an add-in for the popular and free Eclipse IDE.

Install NumPy and SciPy

  1. Ensure you have the Python C++ re-distributable installed.
  2. Install NumPy and SciPy using pip: pip install numpy and pip install scipy
  3. Type import numpy followed by import scipy at a Python prompt. These should return a blank prompt if there are no errors.*

You should now have all the pre-reqs for installing Bayesian Python libraries. If you want to use the built-in Python installer though "you're gonna have a bad time." You need a Fortran compiler and Windows doesn't come with one as standard.

No compiler and no play make Windows something something...

*If you install nose (pip install nose) you can also run numpy.test() and scipy.test() to get fuller results about your install.

Installing Cygwin

The solution to your Fortran compiler woes is to set-up Cygwin. While you are at it you might as well add all the standard linux commands to your Path in Windows.

  1. Grab Cygwin and run the installer.
  2. Leave all the default packages selected.†
  3. Go to Devel and select the mingw-gcc-fortran package.‡
  4. Go to Control Panel\System and Security\System.
  5. Click on Advanced System Settings.
  6. Click on Environment Variables.
  7. Edit the path and add C:\cygwin64\bin to the end (use a semicolon to separate entries).

†You can always run the set-up again to install additional packages at a later date.

‡You might as well install the other compilers you are likely to need like GCC while you're at it.

Installing Bayesian Packages

Currently the dependency h5py needs to installed from a pre-compiled binary on windows.

  1. Install h5py for your version of Windows.

Running pip install bayespy should now work like a charm.

Avoiding pip

If you aren't fussed about ensuring the pip works on your machine you can download individual packages that have been pre-compiled for Windows.

References

[1] Python.org

[2] Probabilistic Programming and Bayesian Methods for Hackers

[3] Patil A, Fonnesbeck CJ (2010). PyMC: Bayesian Stochastic Modelling in Python. J Stat Softw 35: 1.

[4] PyMC

[5] BayesPy

[6] Downey, AB (2012). Think Bayes. Needham: Green Tea Press.