国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Homework #2 for MAFS 5220

2019-11-14 09:55:56
字體:
來源:轉載
供稿:網友
"""@author: Stan Wangassignment 2 : binomial tree for American options;"""import networkx as nxfrom math import *# build the binomial tree. At each node, the value stands for stock PRice;def bgoptiongrid(s,T,r,sigma,n): # some useful parameters; deltaT = T/n u = exp(sigma * sqrt(deltaT)) d = 1.0/u a = exp(r*deltaT) p = (a-d)/(u-d) # G stands for the binomial tree; G = nx.Graph() G.add_node((0,0),value=s,time=0) for i in range(0,n+1): for j in range(0,i+1): if i<n: currentvalue = G.node[(i,j)]['value'] G.add_node((i+1,j),value = currentvalue*d, time = (i+1)*deltaT) G.add_node((i+1,j+1), value = currentvalue*u, time = (i+1)*deltaT) G.add_edge((i,j),(i+1,j),value = 1.0 - p) G.add_edge((i,j),(i+1,j+1), value = p) return Gclass SimpleCall: def __init__(self,strike,maturity): self.strike = strike self.maturity = maturity def payoff(self,price): return max(price - self.strike, 0)class SimplePut(SimpleCall): def payoff(self,price): return max(self.strike - price, 0)def SetEuropeanPayoff(G,n,derivative): for i in range(0,n+1): G.node[(n,i)]['option'] = derivative.payoff(G.node[(n,i)]['value'])def EuropeanBackwardInduction(G,n,discount): for i in range(n-1,-1,-1): for j in range(0,i+1): nextdown= G.node[(i+1,j)]['option'] nextup =G.node[(i+1,j+1)]['option'] nextdownprob = G[(i,j)][(i+1,j)]['value'] nextupprob = G[(i,j)][(i+1,j+1)]['value'] undis = nextup * nextupprob + nextdown * nextdownprob G.node[(i,j)]['option'] = discount * undis return G.node[(0,0)]['option']def AmericanBackwardInduction(G,n,discount): for i in range(n-1,-1,-1): for j in range(0,i+1): nextdown= G.node[(i+1,j)]['option'] nextup =G.node[(i+1,j+1)]['option'] nextdownprob = G[(i,j)][(i+1,j)]['value'] nextupprob = G[(i,j)][(i+1,j+1)]['value'] undis = nextup * nextupprob + nextdown * nextdownprob G.node[(i,j)]['option'] = max(discount * undis,derivative.payoff(G.node[(i,j)]['value'])) return G.node[(0,0)]['option']s = 100sigma = 0.1strike = 100T = 1r = 0.05n = 5stepdiscount = exp(-r * T/n)derivative = SimpleCall(strike,T)# derivative = SimplePut(strike,T)G = bgoptiongrid(s,T,r,sigma,n)SetEuropeanPayoff(G,n,derivative)price_euro = EuropeanBackwardInduction(G,n,stepdiscount)price_amer = AmericanBackwardInduction(G,n,stepdiscount)print (price_euro)print (price_amer)#Remark: for call options, the European type and the American type have the same price if the underlying asset does not pay enough dividends; but for the put value, the American type is more valueable than the European type.# another way to codedef binomialcall(s,x,T,r,sigma,n, e_a # 0 means European,1 means Ameican; ): deltaT = T/n u = exp(sigma * sqrt(deltaT)) d = 1.0/u a = exp(r*deltaT) p = (a-d)/(u-d) v = [[0 for j in range(i+1)] for i in range(n+1)] for j in range(0,n+1): v[n][j] = max(s * u **j * d ** (n-j) - x,0.0) for i in range(n-1,-1,-1): for j in range(0,i+1): v[i][j] = max(exp(- r * deltaT) * (p * v[i+1][j+1] + (1.0 - p) * v[i+1][j]), e_a*max(s*u**j*d**(i-j)-x,0.0)) return v[0][0]def binomialput(s,x,T,r,sigma,n, e_a # 0 means European,1 means Ameican; ): deltaT = T/n u = exp(sigma * sqrt(deltaT)) d = 1.0/u a = exp(r*deltaT) p = (a-d)/(u-d) v = [[0 for j in range(i+1)] for i in range(n+1)] for j in range(0,n+1): v[n][j] = max(x - s * u **j * d ** (n-j),0.0) for i in range(n-1,-1,-1): for j in range(0,i+1): v[i][j] = max(exp(- r * deltaT) * (p * v[i+1][j+1] + (1.0 - p) * v[i+1][j]), e_a*max(x - s*u**j*d**(i-j),0.0)) return v[0][0]e_a = 1print (binomialput(s,strike,T,r,sigma,n,e_a))print (binomialcall(s,strike,T,r,sigma,n,e_a))
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 普宁市| 桃源县| 育儿| 湘乡市| 方正县| 微山县| 泸西县| 湖北省| 图木舒克市| 英德市| 庆城县| 三都| 晋州市| 固安县| 乌恰县| 东丰县| 饶阳县| 囊谦县| 上饶县| 万山特区| 廉江市| 原平市| 北碚区| 柯坪县| 临沭县| 蕲春县| 肇庆市| 阿坝县| 阿巴嘎旗| 水富县| 宁明县| 仁怀市| 厦门市| 福州市| 上高县| 叶城县| 缙云县| 嘉兴市| 武宁县| 林西县| 定兴县|