Example #1
0
def calculateIVCoefficientArray(S0, strike, T, r, q, sigmaBSM, N1, N2,
                                quantile):
    (a, b) = preprocessing.calculateToleranceInterval(S0, strike, T, r, q,
                                                      sigmaBSM, quantile)
    # (a, b) = preprocessing.calculateToleranceIntervalWithoutSigma(S0, strike, T, r, q, quantile)
    m = (r - q) * T + np.log(S0 / strike)
    ckArray = np.array([k * 2 * np.pi / (b - a) for k in range(N1)])
    complexCkArray = np.array([k * 2.j * np.pi / (b - a) for k in range(N1)],
                              dtype=np.complex128)
    VkArray = calculate_Vk_put_CFS(a, b, N1, strike)
    coeffArray = np.zeros(N2, dtype=np.float64)
    for l in range(N2):
        # coeff1 = np.exp(-complex(0,ckArray*m))
        coeff1 = np.exp(-complexCkArray * m)
        # print("coeff1[",l,"]",coeff1)
        coeff2 = ((complexCkArray + complexCkArray**2) / 2)**l / factorial(l)
        # print("coeff2[", l, "]", coeff2)

        # coeffArray[l] = np.sum(coeff1*coeff2*VkArray)

        coeffArray[l] = np.sum(coeff1 * coeff2 * VkArray).real

        # coeff2 = np.array([(complex(-ck**2,ck)/2)**l / factorial(l) for ck in ckArray])
        # coeffArray[l] = np.sum(coeff1*coeff2*VkArray)
        # coeffArray[l] = np.sum([complex(-.5*ck**2,.5*ck)**l/factorial(l)*np.exp(complex(0,-m*ck))*Vk for ck,Vk in zip(ckArray,VkArray)])

    # print("coffs",coeffArray)
    return coeffArray
Example #2
0
def testify_CFS_IV(S0, strike, T, r, q, fixVol, N1, N2, quantile):
    import BlackScholesOption
    import matplotlib.pyplot as plt
    numLoop = 10
    (a, b) = preprocessing.calculateToleranceInterval(S0, strike, T, r, q,
                                                      fixVol, quantile)
    coeffs_var = calculateImpliedVarianceCoefficientArray(
        S0, strike, T, r, q, fixVol, N1, N2, quantile, a, b)
    inverse_coeffs = series_reversion.inverseSeries(coeffs_var)
    # V0 = BlackScholesOption.putOptionPriceBSM(S0, strike, T, r, q, sigma)
    targetSigma = np.array([(i + 1) * 0.1 for i in range(10)])
    target_vars = np.power(targetSigma, 2)
    var_estimations = np.zeros(numLoop)
    for i in range(numLoop):
        target_sigma_i = targetSigma[i]
        V_i = BlackScholesOption.putOptionPriceBSM(S0, strike, T, r, q,
                                                   target_sigma_i)
        var_i = target_sigma_i**2
        Vi_List = [(V_i - coeffs_var[0])**l
                   for l in range(len(inverse_coeffs))]
        var_estimations[i] = np.exp(-r * T) * np.dot(inverse_coeffs, Vi_List)

    print("CFS: target var", target_vars)
    print("CFS: var estimation", var_estimations)

    return
Example #3
0
def putOptionPriceCOS(S0,strike,T,r,q,sigmaBSM,quantile,numGrid,showDuration=False):
    tick = time.time()
    (a,b) = preprocessing.calculateToleranceInterval(S0,strike,T,r,q,sigmaBSM,quantile)
    # (a, b) = preprocessing.calculateToleranceIntervalWithoutSigma(S0, strike, T, r, q, quantile)
    m = preprocessing.calculateConstantTerm(S0,strike,T,r,q,a)
    Vk = calculateVkPut(strike,a,b,numGrid)
    # Rk = calculateRk(m,T,sigmaBSM,a,b,numGrid)
    Rk = calculateRk_chf(S0,strike,T,r,q,sigmaBSM,a,b,numGrid)
    putPrice = np.exp(-r * T) * np.dot(Rk,Vk)
    tack = time.time()
    if (showDuration == True):
        print("consuming time for put option using COS:", tack - tick)
    return putPrice
Example #4
0
def sensitivityAnalysis(S0,T,strike,r,q,fixVol,N1,quantile):
    a,b = preprocessing.calculateToleranceInterval(S0,strike,T,r,q,fixVol,quantile)
    # N2=preprocessing.calculateNumGrid2(N1,T,fixVol,a,b)
    N2 = 16
    print("Parameters setting:")
    print("S0",S0,"strike",strike,"T",T,"r",r,"q",q,"fixvol",fixVol,"N1",N1,"N2",N2,"a",a,"b",b,"quantile",quantile)
    IV_expansion_utils.testify_IV_iteration(S0, strike, T, r, q, quantile, N1, N2, fixVol,n_iter=1,testSigma=[0.1,0.15,0.2,0.25,0.3])
    print("********************************** n_iter=20 **************************")
    IV_expansion_utils.testify_IV_iteration(S0, strike, T, r, q, quantile, N1, N2, fixVol, n_iter=40,testSigma=[0.1, 0.2, 0.3])
    # IV_expansion_utils.testify_IV(S0,strike,T,r,q,a,b,N1,N2,quantile,fixVol)
    # CFS_expansion_utils.testify_CFS_IV(S0=S0,strike=strike,T=T,r=r,q=q,
    #                                fixVol=fixVol,N1=N1,N2=N2,quantile=quantile)
    return
Example #5
0
def calculateImpliedVolatilityByPutOptionPrice(S0,
                                               strike,
                                               T,
                                               r,
                                               q,
                                               price,
                                               quantile,
                                               N1,
                                               N2,
                                               fixPoint,
                                               showDuration=False):
    (a, b) = preprocessing.calculateToleranceInterval(S0, strike, T, r, q,
                                                      fixPoint, quantile)
    m = preprocessing.calculateConstantTerm(S0, strike, T, r, q, a)
    tick = time.time()

    # coeffs = calculateCoefficientList(strike, m, a, b, numGrid=N1, truncationOrder=N2)
    # inverseCoeffs = inverseSeries(coeffs)
    # y = price * np.exp(r * T) - coeffs[0]
    # omega = polyval(y, inverseCoeffs)
    # # print(omega,T)
    # volEstimation = np.sqrt(omega/T)
    coeffs = calculateCoefficientList(strike,
                                      m,
                                      a,
                                      b,
                                      numGrid=N1,
                                      truncationOrder=N2)
    # print("coeff for COS:", coeffs)
    # inverseCoeffs_old = inverseSeries_old(coeffs)
    inverseCoeffs = inverseSeries(coeffs)
    print("new", inverseCoeffs)
    # print("old", inverseCoeffs_old)

    # print(inverseCoeffs)
    y = price * np.exp(r * T) - coeffs[0]
    w = polyval(y, inverseCoeffs)
    print("w", w)
    # print("T*sigmaBSM**2", sigmaBSM ** 2 * T)
    # print("absolute error:", (w - sigmaBSM ** 2 * T))
    volEstimation = np.sqrt(w / T)

    tack = time.time()
    if (showDuration == True):
        print("consuming time for calculating implied volatility:",
              tack - tick)
    return volEstimation
Example #6
0
def putOptionPriceCFS(S0,
                      strike,
                      T,
                      r,
                      q,
                      sigma,
                      quantile,
                      numGrid,
                      showDuration=False):
    (a, b) = preprocessing.calculateToleranceInterval(S0, strike, T, r, q,
                                                      sigma, quantile)
    # (a, b) = preprocessing.calculateToleranceIntervalWithoutSigma(S0, strike, T, r, q, quantile)
    tick = time.time()
    chfk = calculate_chfk_BSM_CFS(S0, strike, T, r, q, sigma, a, b, numGrid)
    Vk = calculate_Vk_put_CFS(a, b, numGrid, strike)
    putOptionPrice = np.sum(np.exp(-r * T) * chfk * Vk).real
    tack = time.time()
    if (showDuration == True):
        print("consuming time for call option using CFS:", tack - tick)
    return putOptionPrice
Example #7
0
def putOptionPriceIV(S0,
                     strike,
                     T,
                     r,
                     q,
                     sigmaBSM,
                     quantile,
                     numGrid,
                     truncationOrder,
                     showDuration=False):
    tick = time.time()
    (a, b) = preprocessing.calculateToleranceInterval(S0, strike, T, r, q,
                                                      sigmaBSM, quantile)
    m = preprocessing.calculateConstantTerm(S0, strike, T, r, q, a)
    coeffs = calculateCoefficientList(strike, m, a, b, numGrid,
                                      truncationOrder)
    w = T * sigmaBSM**2
    wList = np.array([w**l for l in range(len(coeffs))])
    # putPrice = np.exp(-r * T) * polyval(T * sigmaBSM ** 2, coeffs)
    putPrice = np.exp(-r * T) * np.dot(wList, coeffs)
    tack = time.time()
    if (showDuration == True):
        print("consuming time for call option using IV:", tack - tick)
    return putPrice
Example #8
0
sigmaBSM = 0.2
<<<<<<< HEAD
quantile = 10
N1 = 32
N2 = 8 #that is we want to obtain (convergent rapidly)
=======

N1 = 16
# todo:quantile setting for convergency
quantile = 10
# quantile = (np.pi*(N1-1))**2/2+1
>>>>>>> 9e83db0e790146c4eff11148dd77e3d2e13dcca4
# numStrikes = 10
#############################
# Derivative parameters:
(a,b) = preprocessing.calculateToleranceInterval(S0,strike,T,r,q,sigmaBSM,quantile)

# (a,b) = preprocessing.calculateToleranceIntervalWithoutSigma(S0,strike,T,r,q,quantile)
# N2 = preprocessing.calculateNumGrid2(N1,T,sigmaBSM,a,b)# that is we want to obtain (convergent rapidly)
N2=32
# N2 = 32  #  >12 since we use the reversion =10

#############################
print("***************************************************************************")
print("Hyperparameters:")
print("S0:",S0,"strike",strike,"T",T,"r",r,"q",q,"sigmaBSM",sigmaBSM,"quantile",quantile)
print("N1:",N1)
print("N2", N2)
print("***************************************************************************")
print("Derivative parameters:")
print("a and b (truncated interval):",(a,b))
Example #9
0
def testify_IV_iteration(S0, strike, T, r, q, quantile, N1, N2, fixVol, n_iter,
                         testSigma):
    # import matplotlib.pyplot as plt
    # sigma=fixVol
    len_coeffs = 2 * (N2 // 2)
    sigmaList = np.array(testSigma)
    varEstimation = np.zeros(10)
    sigmaEstimation = np.zeros(len(testSigma))
    # wEstimation = np.zeros(10)

    # m = preprocessing.calculateConstantTerm(S0,strike,T,r,q,a)
    # coeffs = calculateCoefficientList(strike, m, a, b, N1, N2)
    # inverseCoeffs = inverseSeries(coeffs)
    putPriceTrue = []
    putPriceIV = []
    for i in range(len(sigmaList)):
        sigma = fixVol
        putPriceEstimation = 0
        target_sigma = sigmaList[i]
        putPrice = BlackScholesOption.putOptionPriceBSM(
            S0, strike, T, r, q, target_sigma)
        putPriceTrue.append(putPrice)
        targit_w = [(target_sigma**2 * T)**l for l in range(len_coeffs)]
        for j in range(n_iter):
            (a,
             b) = preprocessing.calculateToleranceInterval(S0,
                                                           strike,
                                                           T,
                                                           r,
                                                           q,
                                                           sigmaBSM=sigma,
                                                           quantile=quantile)
            # print("a,b",a,b)
            m = preprocessing.calculateConstantTerm(S0, strike, T, r, q, a)
            coeffs = calculateCoefficientList(strike, m, a, b, N1, N2)
            inverseCoeffs = inverseSeries(coeffs)

            w = [(target_sigma**2 * T)**l for l in range(len(coeffs))]

            y = putPrice * np.exp(r * T) - coeffs[0]
            yList = [y**l for l in range(len(inverseCoeffs))]
            # todo: cann't use len(coeffs)
            w2 = np.dot(yList, inverseCoeffs)

            # if w2<0:
            #     w2=-w2

            sigma = np.sqrt(w2 / T)
            # print("n_iter:", j, "var:", w2,"sigma",sigma)
            putPriceEstimation = np.dot(coeffs, w) * np.exp(-r * T)

        putPriceIV.append(putPriceEstimation)
        sigmaEstimation[i] = sigma
        varEstimation[i] = sigma**2
        # wEstimation[i] = w
    # print(sigmaEstimation-sigmaList)
    print("COS_it: fixVol", fixVol)
    print("COS_it: sigma list", sigmaList)
    print("COS_it: target price", putPriceTrue)
    print("COS_it: price estimations", putPriceIV)
    print("COS_it: target sigmas", sigmaList)
    print("COS_it: sigma estimations", sigmaEstimation)

    # plt.plot(-sigmaList+sigmaEstimation)
    # plt.plot(wEstimation-sigmaList**2*T)

    # plt.plot((-sigmaList+sigmaEstimation)/sigmaList)
    # plt.show()
    return