%pylab inline
%precision 2
# Parameters
u = 1.1
d = .9
S0 = 10
r = .01
N = 20
# Risk neutral probabilities
p = (1 + r - d) / (u-d)
q = (u - 1 -r) / (u - d)
# Up rebate option: Pays A the first time stock price crosses U
A = 1
U = 1.2 * S0
# Choose the state process Y = (S, M), where S is the stock price and M the running maximum
# Compute range first. (This requires a forward loop from 0 to N)
#
# Beware -- for more complicated models you may have to worry about round off errors
dom = empty( N+1, dtype=object )
dom[0] = set( [(S0,S0)] )
for n in range(N):
dom[n+1] = set( (u*s, max(u*s,m)) for (s,m) in dom[n] )
dom[n+1].update( (d*s, m) for (s,m) in dom[n] )
# Compute price. f[n](s, m) gives the price at time n when stock price is s and running max is m
f = empty( N+1, dtype=object )
f[N] = { (s,m): (A if m >= U else 0) for (s,m) in dom[N] }
def Rn(n, s, m):
# Rollback operator
return ( f[n+1][(u*s, max(u*s,m))]*p + f[n+1][(d*s,m)]*q )/(1+r)
for n in range(N-1, -1, -1):
f[n] = { (s,m): (A if m >= U else Rn(n, s, m)) for (s,m) in dom[n] }
# Print a few values of the arbitrage free price
f[0]
f[1]
f[5]