
P1: JYS
c04 JWBK378-Fletcher May 23, 2009 4:21 Printer: Yet to come
4
Basic Mathematical Tools
There are some basic mathematical tools and algorithms that are used constantly in computa-
tional quantitative analysis. Reviewing the implementation of these in Python gives us a good
workout in Python programming and the implementations provide us with needed tools to
construct more advanced programs in later chapters.
4.1 RANDOM NUMBER GENERATION
A module for pseudo-random number generators is provided in the Python libraries. It uses the
Mersenne Twister
1
algorithm as the core generator, one of the most extensively tested random
number generation schemes of all time. The following is a program demonstrating how the
module can be used. The program prints, firstly, 100 samples from a Gaussian distribution
with mean µ = 0 and standard deviation σ = 1, and then 100 samples from a lognormal
distribution with the same µ and σ .
import random, sys, getopt
def
print gauss():
g = random.Random(1234)
print [g.gauss(mu = 0, sigma = 1) for i in range(100)]
def
print lognormal variate():
g = random.Random(1234)
print [g.lognormvariate(mu = 0, sigma = 1) for i in range(100)]
def
usage():
print "usage: %s" % sys.argv[0]
print "Try ‘python %s -h’ for more information." % sys.argv[0]
def
help():
print "usage: %s" % sys.argv[0]
print "-h (--help) : print this help message and exit"
print "-v (--version) : print the version number ...