#Nathan Fox #Homework 17 #I give permission for this work to be posted online #Read procedures from class read(`C17.txt`): #Help procedure Help:=proc(): print(` POgV(H, r, dt, C, A) , POgBin(T, r, dt, C) `): print(` PogCP(L, r, dt, K, call) , PogCall(L, r, dt, K) , PogPut(L, r, dt, K) `): print(` DrawBinTg(T, r, dt, C, eps) `): end: ##PROBLEM 1## #POgV(H, r, dt, C, A): inputs the same arguments as #POg(H, r, dt, C), and in addition an ACTUAL history A (coded by a #binary integer between 0 and 2^k-1, where k is the number of #generations (=nops(H)-1)), and gives a blow by blow account of #the ups and down of the stock price, the current value of the #option and how to hedge (i.e. how to refinance, how much stock to #buy or sell, and how much more money to borrow or deposit) # #Returns the same exact thing as POg (evalf-ed) # #NOTE: This does not use FLv. POgV:=proc(H, r, dt, C, A) local T, k, i, j, b, s, stock, money, db, ds, BL, SB, UD: T:=evalf(POg(H, r, dt, C)): k:=nops(H)-1: BL:=[["lent", "borrowed"], ["lending", "borrowing"]]: SB:=["buying", "selling"]: UD:=["down", "up!"]: stock:=0: money:=0: j:=iquo(A, 2^k)+1: for i from 0 to k-1 do printf("The current price of the stock is %a\n", H[i+1][j]): printf("The value of the option is %a\n", T[i+1][j][2]): s:=T[i+1][j][3]: ds:=s-stock: printf("To hedge, it's time to own %a units of stock, which means %s %a units\n", s, SB[sign(ds)], abs(ds)): stock:=stock+ds: b:=T[i+1][j][4]: db:=b-money: printf("To hedge, it's time to have %s %a, which means %s %a\n", BL[1][sign(b)], abs(b), BL[2][sign(db)], abs(db)): money:=money+db: j:=iquo(A, 2^(k-i-1)): printf("\nThe stock went %s\n", UD[(j mod 2)+1]): j:=j+1: od: printf("The final price of the stock is %a\n", H[k+1][A+1]): printf("The payoff of the option is %a\n", T[i+1][A+1][2]): return T: end: ##PROBLEM 2## #POgBin(T, r, dt, C): an analog of POg that applies to the simpler #binomial trees. POgBin:=proc(T, r, dt, C) local FC, i, j, k: k:=nops(T)-1: FC[k+1]:=[seq([0, C[i], 0, 0], i=1..nops(C))]: for i from k-1 by -1 to 0 do for j from 0 to i do FC[i+1][j+1]:=FL(T[i+1][j+1], T[i+2][j+1], T[i+2][j+2], r, dt, [FC[i+2][j+1][2], FC[i+2][j+2][2]]): od: od: return [seq([seq(FC[i+1][j+1], j=0..i)], i=0..k)]: end: ##PROBLEM 3## #Auxiliary Procedure PogCP:=proc(L, r, dt, K, call) return POgBin(L, r, dt, [seq(max(call*(L[-1][i]-K, 0)), i=1..nops(L[-1]))]): end: #PogCall(L, r, dt, K): finds the prices, the q, the phi, and psi for #Euopean Call Options, with strike price K. L is a list of lists #describing the stock prices at each generation, from lowest to #highest. PogCall:=proc(L, r, dt, K) return PogCP(L, r, dt, K, 1): end: #PogPut(L, r, dt, K): finds the prices, the q, the phi, and psi for #Euopean Put Options, with strike price K. L is a list of lists #describing the stock prices at each generation, from lowest to #highest. PogPut:=proc(L, r, dt, K) return PogCP(L, r, dt, K, -1): end: #I hate capitalization... POgCP:=PogCP: POgCall:=PogCall: POgPut:=PogPut: Pog:=POg: PogV:=POgV: PogBin:=POgBin: ##PROBLEM 4## #DrawBinTg(T, r, dt, C, eps): Generalization of DrawBinT that, in #addition to the Binomial tree, T, also inputs an interest rate r, #and time unit dt, a list C the same length of nops(T[nops(T)]) #that indicates the values of the claims corresponding to the #stock price, and eps as before (eps=.2 looks good) and outputs a #drawing of the tree, that in addition to indicating the stock #prices at every node in black, also indicates the current value #of the claim in red, and phi, (what portion of the stock to hold #at that node), in blue. with(plots): DrawBinTg:=proc(T, r, dt, C, eps) local pic, k, i, j, L, voff, phioff: L:=POgBin(T, r, dt, C): k:=nops(T)-1: pic:=op([]): voff:=.3: phioff:=.6: for i from 0 to k do pic:=pic , plot([[0, i], [k-i, i]]), plot([[i, 0], [i, k-i]]): od: for i from 0 to k do for j from 0 to i do pic:=pic , textplot([i-j+eps, j+eps, T[i+1][j+1]]) , textplot([i-j+eps+voff, j+eps, L[i+1][j+1][2]], color=red) , textplot([i-j+eps+phioff, j+eps, L[i+1][j+1][3]], color=blue): od: od: return display(pic, axes=none, view=[-eps..k+eps+phioff, -eps..k+eps]): end: