Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 编辑(重新制作后):使用二分法定理发现美式看跌期权的隐含波动率_Python_Math_Finance_Quantitative Finance_Volatility - Fatal编程技术网

Python 编辑(重新制作后):使用二分法定理发现美式看跌期权的隐含波动率

Python 编辑(重新制作后):使用二分法定理发现美式看跌期权的隐含波动率,python,math,finance,quantitative-finance,volatility,Python,Math,Finance,Quantitative Finance,Volatility,我被分配了这个任务: “使用美式算法和二分法算法,找到具有以下参数的期权的隐含波动率 dt = T/N (delta t) R = 0.03 (continuously compounded) u= 1/d (up factor) N=4 S0 = 100 (starting price) T= 1.0 (time) K = 100 (strike price) [sigma 'a', sigma 'b'] = [0.10, 0.40] 停止您的算法。当观察到的期权价格和模型期权之间的差异小

我被分配了这个任务:

“使用美式算法和二分法算法,找到具有以下参数的期权的隐含波动率

dt = T/N (delta t)
R = 0.03 (continuously compounded)
u= 1/d (up factor)
N=4 
S0 = 100 (starting price)
T= 1.0 (time)
K = 100 (strike price)
[sigma 'a', sigma 'b'] = [0.10, 0.40]
停止您的算法。当观察到的期权价格和模型期权之间的差异小于ε=10^-4时。报告此美式期权的二项式隐含波动率。“

在纸上,我知道如何应用二分法定理来解决这个问题(也就是说,如果我被给出,让我们说2次迭代)。然而,我几个月前才开始学习python,现在有点挣扎

到目前为止,在做了相当多的在线阅读和书籍之后,许多程序使用Black-Scholes模型(我的教授不希望我们使用)。我已经能够写出这段代码,其中大部分来自“Yves Hilpish的Python衍生品分析”

我的问题是:


1) 为什么在程序结束时没有输出任何内容?(或者只是我没有给python足够的时间运行)

2) 我需要创建另一个函数来使用二分法定理吗。也就是说,如果V(0,0)小于或>原始观察价格9.25美元,波动性参数将发生变化)


3) 我在代码中缺少了什么来完成它?

“为什么在这个程序的末尾没有输出任何东西?我在代码中缺少了什么来完成它?”因为代码所做的只是定义一些函数;在高层,没有什么可以调用它们中的任何一个,对结果也没有任何影响。您需要调用
CRR\u选项\u valuation
,并对结果进行处理(例如,
print
it)。“为什么在这个程序结束时没有输出任何内容?我在代码中缺少什么来完成它?”因为代码所做的只是定义一些函数;在高层,没有什么可以调用它们中的任何一个,对结果也没有任何影响。您需要调用
CRR\u选项\u估价
,并对结果进行处理(例如,
print
it)。
import math
import numpy as np
def set_parameters(otype = 1, M=4): # M = N
    otype: int
    if otype ==1:
        S0 = 100
        T = 1
        r = 0.03
        sigmaA = 0.10
        sigmaB = 0.40
        sigmaC = ((sigmaA+sigmaB)/2)
        dt = T/M #Time interval
        df = math.exp(-r*dt) #Discount factor
        u = math.exp(sigmaC*math.sqrt(dt)) #up factor
        d = 1/u #down factor
        p=(math.exp(r*dt) - d) /(u-d)

        return S0, T, r, sigmaC, M, dt, df, u, d, p

def inner_value(S, otype=1):
    otype: int
    if otype ==1:
        return np.maximum(100-S, 0)

def CRR_option_valuation(otype, M=4):
    S0, T, r, sigmaC, M, dt, df, u, d, p = set_parameters(otype, M)
    #Array generation for stock prices
    mu = np.arange(M+1)
    mu = np.resize(mu, (M+1, M+1))
    md = np.transpose(mu)
    mu = u**(mu-md)
    md = d**md
    S = S0*mu*md

    #Valuation by backwards induction
    h = inner_value(S, otype) #Inner value matrix
    V = inner_value(S, otype) #value matrix
    C = np.zeros((M+1, M+1), dtype = np.float)
    ex = np.zeros((M+1, M+1), dtype=np.float) #Exercise matrix

    z = 0
    for i in range(M-1, -1, -1):
        C[0:M-z, i] = (p*V[0:M-z, i+1] + (1-p)*V[1:M-z+1, i+1])*df
        V[0:M-z, i] = np.where(h[0:M-z, i] > C[0:M-z, i], h[0:M-z, i], C[0:M-z, i])
        ex[0:M-z, i] = np.where(h[0:M-z,i] > C[0:M-z, i], 1, 0)
        z+=1
    return V[0,0]