Blog Archives

Rational approximations to e

This morning Dave Richeson posted a humorous fake proof that depends on the famous approximation 22/7 for pi. It occurred to me that nearly everyone knows a decent rational approximation to pi. Some people may know more. But hardly anyone,

Tagged with: ,
Posted in Python

Python / Emacs setup

When I got a new computer a few days ago, I installed the latest version of Emacs, 24.2, and broke my Python environment. I decided to re-evaluate my environment and start over. I asked a question on the Python Google+

Tagged with: ,
Posted in Python, Software development

Narcissus prime in Python

I’ve been looking back on some of my blog posts that included Mathematica code to see whether I could rewrite them using Python. For example, I rewrote my code for finding sonnet primes in Python a few days ago. Next

Tagged with: ,
Posted in Python

Sonnet primes in Python

A while back I wrote about sonnet primes, primes of the form ababcdcdefefgg where the letters a through g represent digits and a is not zero. The name comes from the rhyme scheme of an English (Shakespearean) sonnet. In the

Tagged with: ,
Posted in Python

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