#March 5, 2015: implementing Section 1.5 of Alison Etheridge's book "A course in Financial Calculus" #C13.txt (done remotely) Help:=proc(): print(` FuturePossibleValuesOfPortfolio(D1,theta), HedgingPortfolio(D1,C), OptionPrice(S0,D1,C) `): end: #FuturePossibleValuesOfPortfolio(D1,theta): inputs a list of lists of numbers, D1, where D1 has length N, and each of #its components is a list of length n, where N is the number of stocks (aka as assets, aka as secutities) #(including, if one wishes, a Government bond) #and there are n possible scenarios for the future state of the economy (only treating a SINGLE period) #D1[i][j] is the price of stock Number i if the economy would be in state number j. #It also inputs a vector theta of length N, describing the investor's portolfi, where theta[i] (1<=i<=N) #is the quantity he or she purchases (at time 0) of stock Number i. #It outputs the vector of length n #whose j-th component is the future value of the portfolio if the economy is in state j (1<=j<=n) #Note: If one of the assets is a Government bond, the corresponding price-list would be [exp(r*T), ..., exp(r*T)] #where r is the interest rate, and T is the duration of the ONE step (but r and T are not inputs of this #abstract procedure, they are only used in applying it) FuturePossibleValuesOfPortfolio:=proc(D1,theta) local n,N,i,j: if not type(D1,list) or D1=[] then print(`bad input `): RETURN(FAIL): fi: N:=nops(D1): if not {seq(type(D1[i],list),i=1..nops(D1))}={true} then print(`bad input `): RETURN(FAIL): fi: n:=nops(D1[1]): if {seq(nops(D1[i]),i=1..N)}<>{n} then print(`bad input `): RETURN(FAIL): fi: [seq(add(D1[i][j]*theta[i],i=1..N),j=1..n)]: end: #HedgingPortfolio(D1,C): Inputs a list of lists D1 (see above), and a list C, of length n, where #C[j] is the amount that the seller of the option's obligation to pay the buyer of the option SHOULD the economy wind-up in state j (1<=j<=n) #If the seller does not meet his obligation, he is arrested. The output is the portfolio that would exactly meet his obligation #(but with no left-over!), in each and every one of the n possible future states of the economy HedgingPortfolio:=proc(D1,C) local n,N,i,j,FV,theta,eq,var: if not type(D1,list) or D1=[] then print(`bad input `): RETURN(FAIL): fi: N:=nops(D1): if not {seq(type(D1[i],list),i=1..nops(D1))}={true} then print(`bad input `): RETURN(FAIL): fi: N:=nops(D1): n:=nops(D1[1]): if not (type(C,list) and nops(C)=n) then RETURN(FAIL): fi: FV:=FuturePossibleValuesOfPortfolio(D1,[seq(theta[i],i=1..N)]): if FV=FAIL then RETURN(FAIL): fi: var:={seq(theta[j],j=1..N)}: eq:={seq(FV[j]=C[j],j=1..n)}: var:=solve(eq,var): if var=NULL then RETURN(FAIL): fi: [seq(subs(var,theta[i]),i=1..N)]: end: #OptionPrice(S0,D1,C): Inputs a list S0, or length N, say, where S0[i] is today's price of stock number i, #D1 and C as above. #Outputs the fair (no arbitrage) price of an option described by the list C, of length n, where #C[j] is the seller's commitment to pay the buyer if the economy is in state j, and the list of lists D1 #of length N, is such that D1[i][j] is the future price of stock number i IF the economy would wind-up in state number j OptionPrice:=proc(S0,D1,C) local HP,i: if not type(S0,list) then RETURN(FAIL): fi: HP:=HedgingPortfolio(D1,C): if HP=FAIL then RETURN(FAIL): fi: if nops(HP)<>nops(S0) then RETURN(FAIL): fi: add(S0[i]*HP[i],i=1..nops(S0)): end: