我有一個通常采用常量引數并計算波動率的函式。我想傳入一個包含不同 C 和 K 的向量,以獲得一組與 C[i]、K[i] 相關聯的波動率
def vol_calc(S, T, C, K, r, q, sigma):
d1 = (np.log(S / K) (r - q 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))
vega = (1 / np.sqrt(2 * np.pi)) * np.exp(-q * T) * np.sqrt(T) * np.exp((-si.norm.cdf(d1, 0.0, 1.0) ** 2) * 0.5)
tolerance = 0.000001
x0 = sigma
xnew = x0
xold = x0 - 1
while abs(xnew - xold) > tolerance:
xold = xnew
xnew = (xnew - fx - C) / vega
return abs(xnew)
但是如果我想傳遞兩個陣列而不變成嵌套回圈,我想我可以這樣做:
def myfunction(S, T, r, q, sigma):
for x in K,C:
return same size as K,C
但我無法讓它作業
uj5u.com熱心網友回復:
這個怎么樣?
def vol_calc(S, T, C, K, r, q, sigma):
import numpy as np
output = np.zeros(len(C))
for num, (c, k) in enumerate(zip(C, K)):
d1 = (np.log(S / k) (r - q 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))
vega = (1 / np.sqrt(2 * np.pi)) * np.exp(-q * T) * np.sqrt(T) * np.exp((-si.norm.cdf(d1, 0.0, 1.0) ** 2) * 0.5)
tolerance = 0.000001
x0 = sigma
xnew = x0
xold = x0 - 1
while abs(xnew - xold) > tolerance:
xold = xnew
xnew = (xnew - fx - c) / vega
output[num] = abs(xnew)
return output
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/434349.html
上一篇:如何檢查陣列中的型別?
