Blog Archives

Finding 2013 in pi

My youngest daughter asked me this morning whether you can find the number 2013 in the digits of pi. I said it must be possible, then wrote the following Python code to find where 2013 first appears. from mpmath import

Tagged with: ,
Posted in Python

Basics of Sweave and Pweave

Sweave is a tool for embedding R code in a LaTeX file. Pweave is an analogous tool for Python. By putting your code in your document rather than the results of running your code somewhere else, results are automatically recomputed

Tagged with: , , ,
Posted in Python

Three new Python books

This post reviews three Python books that have come out recently: SciPy and NumPy from O’Reilly Python for Kids: A Playful Introduction to Programming from No Starch Press NumPy Cookbook from Packt SciPy and NumPy by Eli Bressert is the

Tagged with: , ,
Posted in Python

Winston Churchill, Bessie Braddock, and Python

Last night I was talking with someone about the pros and cons of various programming languages and frameworks for data analysis. One of the pros of Python is its elegance. The primary con is that it can be slow. The

Tagged with: ,
Posted in Python, Software development

Digits in powers of 2

Does the base 10 expansion of 2^n always contain the digit 7 if n is large enough? As of 1994, this was an open question (page 196 here). I don’t know whether this has since been resolved. The following Python

Tagged with: ,
Posted in Math, Python

Higher moments of normal distribution

Sometimes a little bit of Python beats a Google search. Last week I needed to look up the moments of a normal distribution. The first two moments are common knowledge, the next two are easy to find, but I wasn’t

Tagged with: ,
Posted in Python

Python for data analysis

I recommend using Python for data analysis, and I recommend Wes McKinney’s book Python for Data Analysis. I prefer Python to R for mathematical computing because mathematical computing doesn’t exist in a vacuum; there’s always other stuff to do. I

Tagged with: ,
Posted in Python

Ramanujan's factorial approximation

Ramanujan came up with an approximation for factorial that resembles Stirling’s famous approximation but is much more accurate. As with Stirling’s approximation, the relative error in Ramanujan’s approximation decreases as n gets larger. Typically these approximations are not useful for

Tagged with: , ,
Posted in Math, Python

Python book recommendations

People sometimes ask me to recommend a book for learning to program or a book on Python. If you want both in one book, i.e. to learn Python as a first programming language, take a look at Allen Downey’s new

Tagged with:
Posted in Python

Computing log gamma differences

Statistical computing often involves working with ratios of factorials. These factorials are often too big to fit in a floating point number, and so we work with logarithms. So if we need to compute log(a! / b!), we call software

Tagged with: ,
Posted in Computing, Python, Statistics

Shell != REPL

A shell is not the same as a REPL (Read Evaluate Print Loop). They look similar, but they have deep differences. Shells are designed for one-line commands, and they’re a little awkward when used as programming languages. Scripting languages are

Tagged with:
Posted in Python

Root-finding with noisy functions

Suppose you have function g(x) and you want to find x so that g(x) = 0. However, you don’t have direct access to g(x). Instead you can evaluate f(x) which is g(x) plus random noise. Typically f(x) would be an

Tagged with: , ,
Posted in Python, Software development

Calculating pi with AGM and mpmath

This post gives an algorithm based on the arithmetic-geometric mean that rapidly converges to pi. I’ll use it to illustrate multiple precision arithmetic using Python’s mpmath module. Given two non-negative numbers a and b, their arithmetic mean is (a +

Tagged with:
Posted in Math, Python

Using SciPy with IronPython

Three years ago I wrote a post about my disappointment using SciPy with IronPython. A lot has changed since then, so I thought I’d write a short follow-up post. To install NumPy and SciPy for use with IronPython, follow the

Tagged with: , ,
Posted in Python

Machine Learning in Action

A couple months ago I briefly reviewed Machine Learning for Hackers by Drew Conway and John Myles White. Today I’m looking at Machine Learning in Action by Peter Harrington and comparing the two books. Both books are about the same

Tagged with:
Posted in Python