#!/usr/bin/python -Wall # ================================================================ # John Kerl # kerl at math dot arizona dot edu # 2008-05-12 # ================================================================ from __future__ import division # 1/2 = 0.5, not 0. import random from math import * import copy import sys import re # ---------------------------------------------------------------- def usage(): print >> sys.stderr, "Usage: %s [options]" % (sys.argv[0]) print >> sys.stderr, "Options:" print >> sys.stderr, " T=[number]" print >> sys.stderr, " nt=[number]" print >> sys.stderr, " nX=[number]" sys.exit(1) # ---------------------------------------------------------------- # Brownian bridges using B_t - t B_1. T = 1 nt = 1000 nX = 20 argc = len(sys.argv) for argi in range(1, argc): if re.match(r'^T=', sys.argv[argi]): T = float(sys.argv[argi][2:]) elif re.match(r'^nt=', sys.argv[argi]): nt = int (sys.argv[argi][3:]) elif re.match(r'^nX=', sys.argv[argi]): nX = int (sys.argv[argi][3:]) else: usage() X0 = 0.0 t = 0 dt = T/nt Bt = [0.0] * nX sqrtdt = sqrt(dt) I = [0.0] * nX ts = [] bridges = [] # First pass to find B(1) while (t < T): for j in range(0, nX): dB = random.gauss(0, sqrtdt) Bt[j] += dB ts.append(t) bridges.append(copy.copy(Bt)) t += dt # Second pass to subtract off t*B1 numt = len(bridges) for i in range(0, numt): t = ts[i] print "%11.7f" % (t), for j in range(0, nX): Bt = bridges[i][j] B1 = bridges[-1][j] Xt = Bt - t*B1 print "%11.7f" % (Xt), print