Example #1
0
def test_BDTExampleOne():
    # HULL BOOK NOTES
    # http://www-2.rotman.utoronto.ca/~hull/technicalnotes/TechnicalNote23.pdf

    valuationDate = FinDate(1, 1, 2020)
    years = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
    zeroDates = valuationDate.addYears(years)
    zeroRates = [0.00, 0.10, 0.11, 0.12, 0.125, 0.13]

    testCases.header("DATES")
    testCases.print(zeroDates)

    testCases.header("RATES")
    testCases.print(zeroRates)

    curve = FinDiscountCurveZeros(valuationDate, zeroDates, zeroRates,
                                  FinFrequencyTypes.ANNUAL)

    yieldVol = 0.16

    numTimeSteps = 5
    tmat = years[-1]
    dfs = curve.df(zeroDates)

    testCases.print("DFS")
    testCases.print(dfs)

    years = np.array(years)
    dfs = np.array(dfs)

    model = FinModelRatesBDT(yieldVol, numTimeSteps)
    model.buildTree(tmat, years, dfs)
def test_FinDiscountCurveZeros():

    startDate = FinDate(1, 1, 2018)
    times = np.linspace(1.0, 10.0, 10)
    dates = startDate.addYears(times)
    zeroRates = np.linspace(5.0, 6.0, 10) / 100
    freqType = FinFrequencyTypes.ANNUAL
    dayCountType = FinDayCountTypes.ACT_ACT_ISDA

    curve = FinDiscountCurveZeros(startDate, dates, zeroRates, freqType,
                                  dayCountType, FinInterpTypes.FLAT_FWD_RATES)

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

    years = np.linspace(0, 10, 21)
    dates = startDate.addYears(years)
    for dt in dates:
        df = curve.df(dt)
        testCases.print(dt, df)

#    print(curve)

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

    numRepeats = 100

    start = time.time()

    for i in range(0, numRepeats):
        freqType = FinFrequencyTypes.ANNUAL
        dayCountType = FinDayCountTypes.ACT_ACT_ISDA

        dates = [
            FinDate(14, 6, 2016),
            FinDate(14, 9, 2016),
            FinDate(14, 12, 2016),
            FinDate(14, 6, 2017),
            FinDate(14, 6, 2019),
            FinDate(14, 6, 2021),
            FinDate(15, 6, 2026),
            FinDate(16, 6, 2031),
            FinDate(16, 6, 2036),
            FinDate(14, 6, 2046)
        ]

        zeroRates = [
            0.000000, 0.006616, 0.007049, 0.007795, 0.009599, 0.011203,
            0.015068, 0.017583, 0.018998, 0.020080
        ]

        startDate = dates[0]

        curve = FinDiscountCurveZeros(startDate, dates, zeroRates, freqType,
                                      dayCountType,
                                      FinInterpTypes.FLAT_FWD_RATES)

    end = time.time()
    period = end - start
Example #3
0
def test_FinLiborCapFloorQLExample():

    valuationDate = FinDate(14, 6, 2016)

    dates = [
        FinDate(14, 6, 2016),
        FinDate(14, 9, 2016),
        FinDate(14, 12, 2016),
        FinDate(14, 6, 2017),
        FinDate(14, 6, 2019),
        FinDate(14, 6, 2021),
        FinDate(15, 6, 2026),
        FinDate(16, 6, 2031),
        FinDate(16, 6, 2036),
        FinDate(14, 6, 2046)
    ]

    rates = [
        0.000000, 0.006616, 0.007049, 0.007795, 0.009599, 0.011203, 0.015068,
        0.017583, 0.018998, 0.020080
    ]

    frequencyType = FinFrequencyTypes.ANNUAL
    dayCountType = FinDayCountTypes.ACT_ACT_ISDA

    discountCurve = FinDiscountCurveZeros(valuationDate, dates, rates,
                                          frequencyType, dayCountType,
                                          FinInterpTypes.LINEAR_ZERO_RATES)

    startDate = FinDate(14, 6, 2016)
    endDate = FinDate(14, 6, 2026)
    calendarType = FinCalendarTypes.US
    busDayAdjustType = FinBusDayAdjustTypes.MODIFIED_FOLLOWING
    frequencyType = FinFrequencyTypes.QUARTERLY
    dateGenRuleType = FinDateGenRuleTypes.FORWARD
    lastFixing = 0.0065560
    notional = 1000000
    dayCountType = FinDayCountTypes.ACT_360
    optionType = FinLiborCapFloorTypes.CAP
    strikeRate = 0.02

    cap = FinLiborCapFloor(startDate, endDate, optionType, strikeRate,
                           lastFixing, frequencyType, dayCountType, notional,
                           calendarType, busDayAdjustType, dateGenRuleType)

    blackVol = 0.547295
    model = FinModelBlack(blackVol)

    start = time.time()
    numRepeats = 10
    for i in range(0, numRepeats):
        v = cap.value(valuationDate, discountCurve, model)

    end = time.time()
    period = end - start
    print(v, period / numRepeats)
Example #4
0
def test_FinInflationBondStack():

    ##########################################################################
    # https://stackoverflow.com/questions/57676724/failing-to-obtain-correct-accrued-interest-with-quantlib-inflation-bond-pricer-i
    ##########################################################################

    testCases.banner("=============================")
    testCases.banner("QUANT FINANCE US TIPS EXAMPLE")
    testCases.banner("=============================")
    settlementDate = FinDate(23, 8, 2019)
    issueDate = FinDate(25, 9, 2013)
    maturityDate = FinDate(22, 3, 2068)
    coupon = 0.00125
    freqType = FinFrequencyTypes.SEMI_ANNUAL
    accrualType = FinDayCountTypes.ACT_ACT_ICMA
    face = 100.0
    baseCPIValue = 249.70

    ###########################################################################
    # Discount curve
    discountCurve = FinDiscountCurveFlat(settlementDate,
                                         0.01033692,
                                         FinFrequencyTypes.ANNUAL,
                                         FinDayCountTypes.ACT_ACT_ISDA)

    lag = 3
    fixingCPI = 244.65884
    fixingDate = settlementDate.addMonths(-lag)

    ###########################################################################
    # Create Index Curve
    months = range(0, 12, 1)
    fixingDates = FinDate(31, 8, 2018).addMonths(months)
    fixingRates = [284.2, 284.1, 284.5, 284.6, 285.6, 283.0, 285.0,
                   285.1, 288.2, 289.2, 289.6, 289.5]    
    inflationIndex = FinInflationIndexCurve(fixingDates, fixingRates, lag)
    print(inflationIndex)
    ###########################################################################
        
    zciisData = [(FinDate(31,7,2020), 3.1500000000137085),
                 (FinDate(31,7,2021), 3.547500000013759),
                 (FinDate(31,7,2022), 3.675000000013573),
                 (FinDate(31,7,2023), 3.7250000000134342),
                 (FinDate(31,7,2024), 3.750000000013265),
                 (FinDate(31,7,2025), 3.7430000000129526),
                 (FinDate(31,7,2026), 3.741200000012679),
                 (FinDate(31,7,2027), 3.7337000000123632),
                 (FinDate(31,7,2028), 3.725000000011902),
                 (FinDate(31,7,2029), 3.720000000011603),
                 (FinDate(31,7,2030), 3.712517289063011),
                 (FinDate(31,7,2031), 3.7013000000108764),
                 (FinDate(31,7,2032), 3.686986039205209),
                 (FinDate(31,7,2033), 3.671102614032895),
                 (FinDate(31,7,2034), 3.655000000009778),
                 (FinDate(31,7,2035), 3.6394715951305834),
                 (FinDate(31,7,2036), 3.624362044800966),
                 (FinDate(31,7,2037), 3.6093619727979087),
                 (FinDate(31,7,2038), 3.59421438364369),
                 (FinDate(31,7,2039), 3.5787000000081948),
                 (FinDate(31,7,2040), 3.5626192748395624),
                 (FinDate(31,7,2041), 3.545765016376823),
                 (FinDate(31,7,2042), 3.527943521613608),
                 (FinDate(31,7,2043), 3.508977137925462),
                 (FinDate(31,7,2044), 3.48870000000685),
                 (FinDate(31,7,2045), 3.467083068721011),
                 (FinDate(31,7,2046), 3.4445738220594935),
                 (FinDate(31,7,2047), 3.4216470902302065),
                 (FinDate(31,7,2048), 3.3986861494999188),
                 (FinDate(31,7,2049), 3.376000000005752),
                 (FinDate(31,7,2050), 3.3538412080641233),
                 (FinDate(31,7,2051), 3.3324275806807746),
                 (FinDate(31,7,2052), 3.311938788306623),
                 (FinDate(31,7,2053), 3.2925208131865835),
                 (FinDate(31,7,2054), 3.274293040759302),
                 (FinDate(31,7,2055), 3.2573541974782794),
                 (FinDate(31,7,2056), 3.241787355503245),
                 (FinDate(31,7,2057), 3.227664186159851),
                 (FinDate(31,7,2058), 3.2150486140060774),
                 (FinDate(31,7,2059), 3.204000000004159),
                 (FinDate(31,7,2060), 3.1945334946674064),
                 (FinDate(31,7,2061), 3.1865047145143377),
                 (FinDate(31,7,2062), 3.179753073456304),
                 (FinDate(31,7,2063), 3.1741427790361154),
                 (FinDate(31,7,2064), 3.1695593261025223),
                 (FinDate(31,7,2065), 3.1659065919088736),
                 (FinDate(31,7,2066), 3.163104428386987),
                 (FinDate(31,7,2067), 3.1610866681252903),
                 (FinDate(31,7,2068), 3.1597994770515836),
                 (FinDate(31,7,2069), 3.159200000003204),
                 (FinDate(31,7,2070), 3.159242349440139),
                 (FinDate(31,7,2071), 3.1598400898057433),
                 (FinDate(31,7,2072), 3.16090721831932),
                 (FinDate(31,7,2073), 3.162369676612098),
                 (FinDate(31,7,2074), 3.1641636543027207)]

    zcDates = []
    zcRates = []
    for i in range(0, len(zciisData)):
        zcDates.append(zciisData[i][0])
        zcRates.append(zciisData[i][1]/100.0)

    inflationZeroCurve = FinDiscountCurveZeros(settlementDate,
                                               zcDates,
                                               zcRates,
                                               FinFrequencyTypes.ANNUAL,
                                               FinDayCountTypes.ACT_ACT_ISDA)

    print(inflationZeroCurve)

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

    bond = FinInflationBond(issueDate,
                            maturityDate,
                            coupon,
                            freqType,
                            accrualType,
                            face,
                            baseCPIValue)

    testCases.header("FIELD", "VALUE")
    cleanPrice = 104.03502

    yld = bond.currentYield(cleanPrice)
    testCases.print("Current Yield = ", yld)

    ###########################################################################
    # Inherited functions that just calculate real yield without CPI adjustments
    ###########################################################################

    ytm = bond.yieldToMaturity(settlementDate,
                               cleanPrice,
                               FinYTMCalcType.UK_DMO)

    testCases.print("UK DMO REAL Yield To Maturity = ", ytm)

    ytm = bond.yieldToMaturity(settlementDate,
                               cleanPrice,
                               FinYTMCalcType.US_STREET)

    testCases.print("US STREET REAL Yield To Maturity = ", ytm)

    ytm = bond.yieldToMaturity(settlementDate,
                               cleanPrice,
                               FinYTMCalcType.US_TREASURY)

    testCases.print("US TREASURY REAL Yield To Maturity = ", ytm)

    fullPrice = bond.fullPriceFromYTM(settlementDate, ytm)
    testCases.print("Full Price from REAL YTM = ", fullPrice)

    cleanPrice = bond.cleanPriceFromYTM(settlementDate, ytm)
    testCases.print("Clean Price from Real YTM = ", cleanPrice)

    accddays = bond._accruedDays
    testCases.print("Accrued Days = ", accddays)

    accd = bond._accruedInterest
    testCases.print("REAL Accrued Interest = ", accd)

    ###########################################################################
    # Inflation functions that calculate nominal yield with CPI adjustment
    ###########################################################################

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

    cleanPrice = bond.cleanPriceFromYTM(settlementDate, ytm)
    testCases.print("Clean Price from Real YTM = ", cleanPrice)

    inflationAccd = bond.calcInflationAccruedInterest(settlementDate, 
                                                      refCPIValue)

    testCases.print("Inflation Accrued = ", inflationAccd)

    lastCpnCPIValue = 244.61839

    cleanPrice = bond.flatPriceFromYieldToMaturity(settlementDate, ytm,
                                                   lastCpnCPIValue,
                                                   FinYTMCalcType.US_TREASURY)

    testCases.print("Flat Price from Real YTM = ", cleanPrice)

    principal = bond.inflationPrincipal(settlementDate, 
                                        ytm,
                                        refCPIValue,
                                        FinYTMCalcType.US_TREASURY)

    testCases.print("Inflation Principal = ", principal)

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

    duration = bond.dollarDuration(settlementDate, ytm)
    testCases.print("Dollar Duration = ", duration)

    modifiedDuration = bond.modifiedDuration(settlementDate, ytm)
    testCases.print("Modified Duration = ", modifiedDuration)

    macauleyDuration = bond.macauleyDuration(settlementDate, ytm)
    testCases.print("Macauley Duration = ", macauleyDuration)

    conv = bond.convexityFromYTM(settlementDate, ytm)
    testCases.print("Convexity = ", conv)
Example #5
0
def testFinLiborSwaptionMatlabExamples():

    # We value a European swaption using Black's model and try to replicate a
    # ML example at https://fr.mathworks.com/help/fininst/swaptionbyblk.html

    testCases.header("=======================================")
    testCases.header("MATLAB EXAMPLE WITH FLAT TERM STRUCTURE")
    testCases.header("=======================================")

    valuationDate = FinDate(1, 1, 2010)
    liborCurve = FinDiscountCurveFlat(valuationDate, 0.06,
                                      FinFrequencyTypes.CONTINUOUS,
                                      FinDayCountTypes.THIRTY_E_360)

    settlementDate = FinDate(1, 1, 2011)
    exerciseDate = FinDate(1, 1, 2016)
    maturityDate = FinDate(1, 1, 2019)

    fixedCoupon = 0.062
    fixedFrequencyType = FinFrequencyTypes.SEMI_ANNUAL
    fixedDayCountType = FinDayCountTypes.THIRTY_E_360_ISDA
    notional = 100.0

    # Pricing a PAYER
    swaptionType = FinLiborSwapTypes.PAYER
    swaption = FinLiborSwaption(settlementDate, exerciseDate, maturityDate,
                                swaptionType, fixedCoupon, fixedFrequencyType,
                                fixedDayCountType, notional)

    model = FinModelBlack(0.20)
    v_finpy = swaption.value(valuationDate, liborCurve, model)
    v_matlab = 2.071

    testCases.header("LABEL", "VALUE")
    testCases.print("FP Price:", v_finpy)
    testCases.print("MATLAB Prix:", v_matlab)
    testCases.print("DIFF:", v_finpy - v_matlab)

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

    testCases.header("===================================")
    testCases.header("MATLAB EXAMPLE WITH TERM STRUCTURE")
    testCases.header("===================================")

    valuationDate = FinDate(1, 1, 2010)

    dates = [
        FinDate(1, 1, 2011),
        FinDate(1, 1, 2012),
        FinDate(1, 1, 2013),
        FinDate(1, 1, 2014),
        FinDate(1, 1, 2015)
    ]

    zeroRates = [0.03, 0.034, 0.037, 0.039, 0.040]

    contFreq = FinFrequencyTypes.CONTINUOUS
    interpType = FinInterpTypes.LINEAR_ZERO_RATES
    dayCountType = FinDayCountTypes.THIRTY_E_360

    liborCurve = FinDiscountCurveZeros(valuationDate, dates, zeroRates,
                                       contFreq, dayCountType, interpType)

    settlementDate = FinDate(1, 1, 2011)
    exerciseDate = FinDate(1, 1, 2012)
    maturityDate = FinDate(1, 1, 2017)
    fixedCoupon = 0.03

    fixedFrequencyType = FinFrequencyTypes.SEMI_ANNUAL
    fixedDayCountType = FinDayCountTypes.THIRTY_E_360
    floatFrequencyType = FinFrequencyTypes.SEMI_ANNUAL
    floatDayCountType = FinDayCountTypes.THIRTY_E_360
    notional = 1000.0

    # Pricing a put
    swaptionType = FinLiborSwapTypes.RECEIVER
    swaption = FinLiborSwaption(settlementDate, exerciseDate, maturityDate,
                                swaptionType, fixedCoupon, fixedFrequencyType,
                                fixedDayCountType, notional,
                                floatFrequencyType, floatDayCountType)

    model = FinModelBlack(0.21)
    v_finpy = swaption.value(valuationDate, liborCurve, model)
    v_matlab = 0.5771

    testCases.header("LABEL", "VALUE")
    testCases.print("FP Price:", v_finpy)
    testCases.print("MATLAB Prix:", v_matlab)
    testCases.print("DIFF:", v_finpy - v_matlab)

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

    testCases.header("===================================")
    testCases.header("MATLAB EXAMPLE WITH SHIFTED BLACK")
    testCases.header("===================================")

    valuationDate = FinDate(1, 1, 2016)

    dates = [
        FinDate(1, 1, 2017),
        FinDate(1, 1, 2018),
        FinDate(1, 1, 2019),
        FinDate(1, 1, 2020),
        FinDate(1, 1, 2021)
    ]

    zeroRates = np.array([-0.02, 0.024, 0.047, 0.090, 0.12]) / 100.0

    contFreq = FinFrequencyTypes.ANNUAL
    interpType = FinInterpTypes.LINEAR_ZERO_RATES
    dayCountType = FinDayCountTypes.THIRTY_E_360

    liborCurve = FinDiscountCurveZeros(valuationDate, dates, zeroRates,
                                       contFreq, dayCountType, interpType)

    settlementDate = FinDate(1, 1, 2016)
    exerciseDate = FinDate(1, 1, 2017)
    maturityDate = FinDate(1, 1, 2020)
    fixedCoupon = -0.003

    fixedFrequencyType = FinFrequencyTypes.SEMI_ANNUAL
    fixedDayCountType = FinDayCountTypes.THIRTY_E_360_ISDA
    floatFrequencyType = FinFrequencyTypes.SEMI_ANNUAL
    floatDayCountType = FinDayCountTypes.THIRTY_E_360_ISDA
    notional = 1000.0

    # Pricing a PAYER
    swaptionType = FinLiborSwapTypes.PAYER
    swaption = FinLiborSwaption(settlementDate, exerciseDate, maturityDate,
                                swaptionType, fixedCoupon, fixedFrequencyType,
                                fixedDayCountType, notional,
                                floatFrequencyType, floatDayCountType)

    model = FinModelBlackShifted(0.31, 0.008)
    v_finpy = swaption.value(valuationDate, liborCurve, model)
    v_matlab = 12.8301

    testCases.header("LABEL", "VALUE")
    testCases.print("FP Price:", v_finpy)
    testCases.print("MATLAB Prix:", v_matlab)
    testCases.print("DIFF:", v_finpy - v_matlab)

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

    testCases.header("===================================")
    testCases.header("MATLAB EXAMPLE WITH HULL WHITE")
    testCases.header("===================================")

    # https://fr.mathworks.com/help/fininst/swaptionbyhw.html

    valuationDate = FinDate(1, 1, 2007)

    dates = [
        FinDate(1, 1, 2007),
        FinDate(1, 7, 2007),
        FinDate(1, 1, 2008),
        FinDate(1, 7, 2008),
        FinDate(1, 1, 2009),
        FinDate(1, 7, 2009),
        FinDate(1, 1, 2010),
        FinDate(1, 7, 2010),
        FinDate(1, 1, 2011),
        FinDate(1, 7, 2011),
        FinDate(1, 1, 2012)
    ]

    zeroRates = np.array([0.075] * 11)
    interpType = FinInterpTypes.FLAT_FORWARDS
    dayCountType = FinDayCountTypes.THIRTY_E_360_ISDA
    contFreq = FinFrequencyTypes.SEMI_ANNUAL

    liborCurve = FinDiscountCurveZeros(valuationDate, dates, zeroRates,
                                       contFreq, dayCountType, interpType)

    settlementDate = valuationDate
    exerciseDate = FinDate(1, 1, 2010)
    maturityDate = FinDate(1, 1, 2012)
    fixedCoupon = 0.04

    fixedFrequencyType = FinFrequencyTypes.SEMI_ANNUAL
    fixedDayCountType = FinDayCountTypes.THIRTY_E_360_ISDA
    notional = 100.0

    swaptionType = FinLiborSwapTypes.RECEIVER
    swaption = FinLiborSwaption(settlementDate, exerciseDate, maturityDate,
                                swaptionType, fixedCoupon, fixedFrequencyType,
                                fixedDayCountType, notional)

    model = FinModelRatesHW(0.05, 0.01)
    v_finpy = swaption.value(valuationDate, liborCurve, model)
    v_matlab = 2.9201

    testCases.header("LABEL", "VALUE")
    testCases.print("FP Price:", v_finpy)
    testCases.print("MATLAB Prix:", v_matlab)
    testCases.print("DIFF:", v_finpy - v_matlab)

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

    testCases.header("====================================")
    testCases.header("MATLAB EXAMPLE WITH BLACK KARASINSKI")
    testCases.header("====================================")

    # https://fr.mathworks.com/help/fininst/swaptionbybk.html
    valuationDate = FinDate(1, 1, 2007)

    dates = [
        FinDate(1, 1, 2007),
        FinDate(1, 7, 2007),
        FinDate(1, 1, 2008),
        FinDate(1, 7, 2008),
        FinDate(1, 1, 2009),
        FinDate(1, 7, 2009),
        FinDate(1, 1, 2010),
        FinDate(1, 7, 2010),
        FinDate(1, 1, 2011),
        FinDate(1, 7, 2011),
        FinDate(1, 1, 2012)
    ]

    zeroRates = np.array([0.07] * 11)

    interpType = FinInterpTypes.FLAT_FORWARDS
    dayCountType = FinDayCountTypes.THIRTY_E_360_ISDA
    contFreq = FinFrequencyTypes.SEMI_ANNUAL

    liborCurve = FinDiscountCurveZeros(valuationDate, dates, zeroRates,
                                       contFreq, dayCountType, interpType)

    settlementDate = valuationDate
    exerciseDate = FinDate(1, 1, 2011)
    maturityDate = FinDate(1, 1, 2012)

    fixedFrequencyType = FinFrequencyTypes.SEMI_ANNUAL
    fixedDayCountType = FinDayCountTypes.THIRTY_E_360_ISDA
    notional = 100.0

    model = FinModelRatesBK(0.1, 0.05, 200)

    fixedCoupon = 0.07
    swaptionType = FinLiborSwapTypes.PAYER
    swaption = FinLiborSwaption(settlementDate, exerciseDate, maturityDate,
                                swaptionType, fixedCoupon, fixedFrequencyType,
                                fixedDayCountType, notional)

    v_finpy = swaption.value(valuationDate, liborCurve, model)
    v_matlab = 0.3634

    testCases.header("LABEL", "VALUE")
    testCases.print("FP Price:", v_finpy)
    testCases.print("MATLAB Prix:", v_matlab)
    testCases.print("DIFF:", v_finpy - v_matlab)

    fixedCoupon = 0.0725
    swaptionType = FinLiborSwapTypes.RECEIVER
    swaption = FinLiborSwaption(settlementDate, exerciseDate, maturityDate,
                                swaptionType, fixedCoupon, fixedFrequencyType,
                                fixedDayCountType, notional)

    v_finpy = swaption.value(valuationDate, liborCurve, model)
    v_matlab = 0.4798

    testCases.header("LABEL", "VALUE")
    testCases.print("FP Price:", v_finpy)
    testCases.print("MATLAB Prix:", v_matlab)
    testCases.print("DIFF:", v_finpy - v_matlab)

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

    testCases.header("====================================")
    testCases.header("MATLAB EXAMPLE WITH BLACK-DERMAN-TOY")
    testCases.header("====================================")

    # https://fr.mathworks.com/help/fininst/swaptionbybdt.html

    valuationDate = FinDate(1, 1, 2007)

    dates = [
        FinDate(1, 1, 2007),
        FinDate(1, 7, 2007),
        FinDate(1, 1, 2008),
        FinDate(1, 7, 2008),
        FinDate(1, 1, 2009),
        FinDate(1, 7, 2009),
        FinDate(1, 1, 2010),
        FinDate(1, 7, 2010),
        FinDate(1, 1, 2011),
        FinDate(1, 7, 2011),
        FinDate(1, 1, 2012)
    ]

    zeroRates = np.array([0.06] * 11)

    interpType = FinInterpTypes.FLAT_FORWARDS
    dayCountType = FinDayCountTypes.THIRTY_E_360_ISDA
    contFreq = FinFrequencyTypes.ANNUAL

    liborCurve = FinDiscountCurveZeros(valuationDate, dates, zeroRates,
                                       contFreq, dayCountType, interpType)

    settlementDate = valuationDate
    exerciseDate = FinDate(1, 1, 2012)
    maturityDate = FinDate(1, 1, 2015)

    fixedFrequencyType = FinFrequencyTypes.ANNUAL
    fixedDayCountType = FinDayCountTypes.THIRTY_E_360_ISDA
    notional = 100.0

    fixedCoupon = 0.062
    swaptionType = FinLiborSwapTypes.PAYER
    swaption = FinLiborSwaption(settlementDate, exerciseDate, maturityDate,
                                swaptionType, fixedCoupon, fixedFrequencyType,
                                fixedDayCountType, notional)

    model = FinModelRatesBDT(0.20, 200)
    v_finpy = swaption.value(valuationDate, liborCurve, model)
    v_matlab = 2.0592

    testCases.header("LABEL", "VALUE")
    testCases.print("FP Price:", v_finpy)
    testCases.print("MATLAB Prix:", v_matlab)
    testCases.print("DIFF:", v_finpy - v_matlab)
Example #6
0
def test_FinDiscountCurves():

    # Create a curve from times and discount factors
    valuationDate = FinDate(1, 1, 2018)
    years = [1.0, 2.0, 3.0, 4.0, 5.0]
    dates = valuationDate.addYears(years)
    years2 = []

    for dt in dates:
        y = (dt - valuationDate) / gDaysInYear
        years2.append(y)

    rates = np.array([0.05, 0.06, 0.065, 0.07, 0.075])
    discountFactors = np.exp(-np.array(rates) * np.array(years2))
    curvesList = []

    finDiscountCurve = FinDiscountCurve(valuationDate, dates, discountFactors,
                                        FinInterpTypes.FLAT_FORWARDS)
    curvesList.append(finDiscountCurve)

    finDiscountCurveFlat = FinDiscountCurveFlat(valuationDate, 0.05)
    curvesList.append(finDiscountCurveFlat)

    finDiscountCurveNS = FinDiscountCurveNS(valuationDate, 0.0305, -0.01, 0.08,
                                            10.0)
    curvesList.append(finDiscountCurveNS)

    finDiscountCurveNSS = FinDiscountCurveNSS(valuationDate, 0.035, -0.02,
                                              0.09, 0.1, 1.0, 2.0)
    curvesList.append(finDiscountCurveNSS)

    finDiscountCurvePoly = FinDiscountCurvePoly(valuationDate,
                                                [0.05, 0.002, -0.00005])
    curvesList.append(finDiscountCurvePoly)

    finDiscountCurvePWF = FinDiscountCurvePWF(valuationDate, dates, rates)
    curvesList.append(finDiscountCurvePWF)

    finDiscountCurvePWL = FinDiscountCurvePWL(valuationDate, dates, rates)
    curvesList.append(finDiscountCurvePWL)

    finDiscountCurveZeros = FinDiscountCurveZeros(valuationDate, dates, rates)
    curvesList.append(finDiscountCurveZeros)

    curveNames = []
    for curve in curvesList:
        curveNames.append(type(curve).__name__)

    testCases.banner("SINGLE CALLS NO VECTORS")
    testCases.header("CURVE", "DATE", "ZERO", "DF", "CCFWD", "MMFWD", "SWAP")

    years = np.linspace(1, 10, 10)
    fwdMaturityDates = valuationDate.addYears(years)

    testCases.banner("######################################################")
    testCases.banner("SINGLE CALLS")
    testCases.banner("######################################################")

    for name, curve in zip(curveNames, curvesList):
        for fwdMaturityDate in fwdMaturityDates:
            tenor = "3M"
            zeroRate = curve.zeroRate(fwdMaturityDate)
            fwd = curve.fwd(fwdMaturityDate)
            fwdRate = curve.fwdRate(fwdMaturityDate, tenor)
            swapRate = curve.swapRate(valuationDate, fwdMaturityDate)
            df = curve.df(fwdMaturityDate)

            testCases.print("%-20s" % name, "%-12s" % fwdMaturityDate,
                            "%7.6f" % (zeroRate), "%8.7f" % (df),
                            "%7.6f" % (fwd), "%7.6f" % (fwdRate),
                            "%7.6f" % (swapRate))

    # Examine vectorisation
    testCases.banner("######################################################")
    testCases.banner("VECTORISATIONS")
    testCases.banner("######################################################")

    for name, curve in zip(curveNames, curvesList):
        tenor = "3M"
        zeroRate = curve.zeroRate(fwdMaturityDates)
        fwd = curve.fwd(fwdMaturityDates)
        fwdRate = curve.fwdRate(fwdMaturityDates, tenor)
        swapRate = curve.swapRate(valuationDate, fwdMaturityDates)
        df = curve.df(fwdMaturityDates)

        for i in range(0, len(fwdMaturityDates)):
            testCases.print("%-20s" % name, "%-12s" % fwdMaturityDate,
                            "%7.6f" % (zeroRate[i]), "%8.7f" % (df[i]),
                            "%7.6f" % (fwd[i]), "%7.6f" % (fwdRate[i]),
                            "%7.6f" % (swapRate[i]))

    if PLOT_GRAPHS:

        years = np.linspace(0, 10, 121)
        years2 = years + 1.0
        fwdDates = valuationDate.addYears(years)
        fwdDates2 = valuationDate.addYears(years2)

        plt.figure()
        for name, curve in zip(curveNames, curvesList):
            zeroRates = curve.zeroRate(fwdDates)
            plt.plot(years, zeroRates, label=name)
        plt.legend()
        plt.title("Zero Rates")

        plt.figure()
        for name, curve in zip(curveNames, curvesList):
            fwdRates = curve.fwd(fwdDates)
            plt.plot(years, fwdRates, label=name)
        plt.legend()
        plt.title("CC Fwd Rates")

        plt.figure()
        for name, curve in zip(curveNames, curvesList):
            fwdRates = curve.fwdRate(fwdDates, fwdDates2)
            plt.plot(years, fwdRates, label=name)
        plt.legend()
        plt.title("CC Fwd Rates")

        plt.figure()
        for name, curve in zip(curveNames, curvesList):
            dfs = curve.df(fwdDates)
            plt.plot(years, dfs, label=name)
        plt.legend()
        plt.title("Discount Factors")