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…
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…
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…
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…
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…
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…
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…
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…
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…
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…
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…
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…
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…
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 +…
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…
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…