Exemplo n.º 1
0
def test_FinInterpolatedForwards():

    import matplotlib.pyplot as plt

    tValues = np.array([0.0, 3.0, 5.0, 10.0])
    rValues = np.array([0.04, 0.07, 0.08, 0.09])
    dfValues = np.exp(-tValues*rValues)
    tInterpValues = np.linspace(0.0, 12.0, 200)

    print(tValues)
    print(rValues)
    print(dfValues)

    curveDate = FinDate(2019,1,3)
    for method in FinInterpMethods:

        discountCurve = FinDiscountCurve(curveDate, tValues, dfValues, method)
        dfInterpValues = discountCurve.df(tInterpValues)
        fwdInterpValues = discountCurve.fwd(tInterpValues)
        zeroInterpValues = discountCurve.zeroRate(tInterpValues)

        if PLOT_GRAPHS:
            plt.figure(figsize=(8, 6))
            plt.plot(tValues, dfValues, 'o', color='g', label="DFS:")
            plt.plot(tInterpValues, dfInterpValues, color='r', label="DF:" + str(method))
            plt.legend()
            plt.figure(figsize=(8, 6))
            plt.plot(tInterpValues, fwdInterpValues, color='r', label="FWD:" + str(method))
            plt.plot(tInterpValues, zeroInterpValues, color='b', label="ZERO:" + str(method))
            plt.plot(tValues, rValues, 'o', color='g',  label="ZERO RATES")
            plt.legend()
Exemplo n.º 2
0
def test_FinDiscountCurve():

    startDate = FinDate(2018, 1, 1)
    times = np.linspace(0, 10.0, 10)
    rate = 0.05
    values = np.exp(-rate * times)

    curve = FinDiscountCurve(startDate, times, values,
                             FinInterpMethods.FLAT_FORWARDS)

    testCases.header("T", "DF")

    for t in np.linspace(0, 10, 21):
        df = curve.df(t)
        testCases.print(t, df)
Exemplo n.º 3
0
def test_CDSFastApproximation():

    valueDate = FinDate(2018, 6, 20)
    # I build a discount curve that requires no bootstrap
    times = np.linspace(0, 10.0, 11)
    r = 0.05

    discountFactors = np.power((1.0 + r), -times)
    dates = valueDate.addYears(times)

    liborCurve = FinDiscountCurve(valueDate,
                                  dates,
                                  discountFactors,
                                  FinInterpTypes.FLAT_FWD_RATES)

    ##########################################################################

    maturityDate = valueDate.nextCDSDate(120)
    t = (maturityDate - valueDate) / 365.242
    z = liborCurve.df(maturityDate)
    r = -np.log(z) / t

    recoveryRate = 0.40

    contractCoupon = 0.010

    testCases.header("MKT_SPD", "EXACT_VALUE", "APPROX_VALUE", "DIFF(%NOT)")

    for mktCoupon in np.linspace(0.000, 0.05, 21):

        cdsContracts = []

        cdsMkt = FinCDS(valueDate, maturityDate, mktCoupon, ONE_MILLION)

        cdsContracts.append(cdsMkt)

        issuerCurve = FinCDSCurve(valueDate,
                                  cdsContracts,
                                  liborCurve,
                                  recoveryRate)

        cdsContract = FinCDS(valueDate, maturityDate, contractCoupon)
        v_exact = cdsContract.value(
            valueDate, issuerCurve, recoveryRate)['full_pv']
        v_approx = cdsContract.valueFastApprox(
            valueDate, r, mktCoupon, recoveryRate)[0]
        pctdiff = (v_exact - v_approx) / ONE_MILLION * 100.0
        testCases.print(mktCoupon * 10000, v_exact, v_approx, pctdiff)
Exemplo n.º 4
0
def test_FinInterpolatedForwards():

    import matplotlib.pyplot as plt

    tValues = np.array([0.0, 3.0, 5.0, 10.0])
    rValues = np.array([0.04, 0.07, 0.08, 0.09])
    dfValues = np.exp(-tValues * rValues)
    tInterpValues = np.linspace(0.0, 12.0, 49)

    curveDate = FinDate(1, 1, 2019)

    tDates = curveDate.addYears(tValues)
    tInterpDates = curveDate.addYears(tInterpValues)

    for interpType in FinInterpTypes:

        discountCurve = FinDiscountCurve(curveDate, tDates, dfValues,
                                         interpType)
        dfInterpValues = discountCurve.df(tInterpDates)
        fwdInterpValues = discountCurve.fwd(tInterpDates)
        zeroInterpValues = discountCurve.zeroRate(tInterpDates)

        if PLOT_GRAPHS:
            plt.figure(figsize=(8, 6))
            plt.plot(tValues, dfValues, 'o', color='g', label="DFS:")
            plt.plot(tInterpValues,
                     dfInterpValues,
                     color='r',
                     label="DF:" + str(interpType))
            plt.legend()
            plt.figure(figsize=(8, 6))
            plt.plot(tInterpValues,
                     fwdInterpValues,
                     color='r',
                     label="FWD:" + str(interpType))
            plt.plot(tInterpValues,
                     zeroInterpValues,
                     color='b',
                     label="ZERO:" + str(interpType))
            plt.plot(tValues, rValues, 'o', color='g', label="ZERO RATES")
            plt.legend()