#!/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, " X0=[number]" print >> sys.stderr, " nX=[number]" sys.exit(1) # ---------------------------------------------------------------- # SDE: dX = X dt + db_t # Solution: X_t = exp(t) * ( X_0 + int_0^t exp(-s) db_s ). T = 2 nt = 2000 X0 = 1 nX = 5 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'^X0=', sys.argv[argi]): X0 = float(sys.argv[argi][3:]) elif re.match(r'^nX=', sys.argv[argi]): nX = int (sys.argv[argi][3:]) else: usage() t = 0 dt = T/nt exact = [X0] * nX approx = [X0] * nX Bt = [0.0] * nX sqrtdt = sqrt(dt) I = [0.0] * nX while (t <= T): for k in range(0, nX): exact[k] = exp(t) * (X0 + I[k]) print "%11.7f" % (t), for k in range(0, nX): print "%11.7f %11.7f %9.3e" % \ (exact[k], approx[k], exact[k]-approx[k]), print "%11.7f" % (X0*exp(t)) # This is E[X_t]. for k in range(0, nX): dB = random.gauss(0, sqrtdt) approx[k] += approx[k] * dt + dB I[k] += exp(-t) * dB Bt[k] += dB t += dt