Random walks in Python

I’ve been finishing up the semester and talking about random walks and Brownian motion. In order to add some images to my course notes at https://www.softcover.io/read/bf34ea25/math_for_finance, I made some quick Python calculations:

Simple symmetric random walk: The laziest thing I could think of was to use the binomial function from numpy. It returns 0 or 1, so I simply translated it (x->2x-1) so that I can get 1 or -1 instead.

from math import sqrt
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

def RandomWalk(N,d):
walk = np.cumsum(2*np.random.binomial(1,.5,N)-1)
return walk

It’s easy then to plot this:



Of course, rather than looking at one random walk, it’s more fun to look at a bunch. Here’s 180 simple symmetric random walks:


I plotted this using the following code:

endpoints = []
for k in range(180):
particularWalk = RandomWalk(100,1)

This also peeled off the endpoints of the walks (at step 100), so that I could make a histogram of the positions at time 100:


The idea is to show the distribution of S_100, position at time 100. We know that as n goes to infinity we can say that the limiting distribution for S_n is the normal distribution; 100 is quite far from infinity but even there we start getting some idea of the distribution.

I also generated some asymmetrical simple random walks — still one step of length 1 each time unit, but now one direction is more probable than the other:


This above is a random walk with P(X=1) = 0.6 and P(X=-1)=0.4. The expected value is plotted with the black line on top.

And last, I showed what horrible things can happen if you scale time (taking steps in the random walk more quickly) without scaling time:


The variance goes crazy (not that I showed that) and the character of the walks seems to change. Yes, that’s touchy-feely talk, but I want people to have a feeling for the shape of Brownian motion. I’ll put some of that up later!

Anonymous friend’s review of Pat Donohue at Vieux Carre

I have always found it curious that music could raise such passions.  An extra syncopation here, a couple bass notes there, and the next thing you know you’ve got the moralists wringing their hands or worse yet, cossacks kicking in your doors.  We tend to lump all music which predates our teens to be the same: Bach to Sibelius is classical and Buddy Holly to Ringo, well that’s classic rock.  And yet, at their respective times, whether it was Mozart pushing the boundries of propriety or Chopin’s stirring melodies, music did make institutions tremble.

Which brings me to last night’s performance at St. Paul’s Vieux Carré.  Pat Donohue and the Prairie All Stars performed with special guests Butch Thompson on the piano and Dan Newton on accordion.  Pat Donohue is famous for picking his way around the guitar (and for his numerous Prairie Home Companion appearances, if the name has not yet rung a bell).  Pick he did.  Sure, chords got their due, but fingerpicking is a bit of rarity these days on the guitar outside of the classical guitar genres.  In typical blues fashion, the musical center stage was passed back and forth between Mr. Thompson, Mr. Newton and the rest of the band.  It was a virtuosic performance, at times restrained, methodical, notes tinkling like raindrops.  At other times guitar, bass, drums, piano and accordion boisterously threw their weight around.  It was music to both listen and dance to.

Throughout the night, I heard quintessentially American music consisting of bits of blues, jazz, ragtime, honky-tonk and even a dusting of zydeco.  One tune early on set the stage.  With a musical approach reminiscent of Glenn Gould playing the Goldberg Variations, Pat Donohue posed the question, “How long, how long, has that evening train gone?”  On some level, it doesn’t matter.  The train has gone and if you’re asking the question, you’ve missed it, bud, and you missed it good.  Start walking or start waiting.  On the other hand, there is always more to the story. The blues can be about a relentless, even masochistic, pursuit of truth.  That means dwelling on and trying to understand uncomfortable questions that certain demographics would rather not be asked at all.  Perhaps this is one of the reasons why uniquely American musical genres, many of which sprang from African American culture, were initially considered subversive.  Once one gets past the seductive rhythms, the blues can sing to the human condition.  We never really find out just how long that evening train has gone because the song is a meditation.  The question is real, but the answers, so far, remain rhetorical.  It is music which I suspect Eugené Delacroix would have enjoyed.

Just as our enjoyment of food depends on how it is presented, so too it is with music.  The Vieux Carré is in the same location as the former Artist’s Quarter.  The interior is much improved with beautifully rendered vintage photographs adorning the walls and there isn’t a bad seat in the house.  Some might say that having a “jazz club” in an intimate, windowless, lower level is cliché, but it is a formula that works. And what could go better with this sensuous, decadent, music than sensuous, decadent nosh and drinks?  The menu fittingly tends towards umami and, along with a well-curated drink selection (wines, beers and cocktails) is well executed.
Once again, anonymous friend is not me, but attended the Theoroi get-together with me at Pat Donohue’s Vieux Carre performance last week! Anonymous friend makes me look lazy with all this great writing 🙂