Skip to content

Commit

Permalink
Adding copyright notice to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
domokane committed Aug 22, 2020
1 parent 19d918b commit 13e8635
Show file tree
Hide file tree
Showing 85 changed files with 302 additions and 516 deletions.
32 changes: 18 additions & 14 deletions financepy/products/credit/FinCDS.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,17 @@ def __init__(self,
if type(maturityDateOrTenor) == FinDate:
maturityDate = maturityDateOrTenor
else:
# To get the next CDS date we move on by the tenor and
# then roll to next CDS date after that
maturityDate = stepInDate.addTenor(maturityDateOrTenor)
maturityDate = maturityDate.nextCDSDate()

if stepInDate > maturityDate:
raise FinError("Step in date after maturity date")

self._stepInDate = stepInDate
self._maturityDate = maturityDate
self._coupon = runningCoupon
self._runningCoupon = runningCoupon
self._notional = notional
self._longProtection = longProtection
self._dayCountType = dayCountType
Expand Down Expand Up @@ -318,7 +321,7 @@ def _calcFlows(self):
t0 = paymentDates[it - 1]
t1 = paymentDates[it]
accrualFactor = dayCount.yearFrac(t0, t1)
flow = accrualFactor * self._coupon * self._notional
flow = accrualFactor * self._runningCoupon * self._notional

self._accrualFactors.append(accrualFactor)
self._flows.append(flow)
Expand Down Expand Up @@ -356,9 +359,9 @@ def value(self,
longProt = -1

fullPV = fwdDf * longProt * \
(protPV - self._coupon * fullRPV01 * self._notional)
(protPV - self._runningCoupon * fullRPV01 * self._notional)
cleanPV = fwdDf * longProt * \
(protPV - self._coupon * cleanRPV01 * self._notional)
(protPV - self._runningCoupon * cleanRPV01 * self._notional)

return {'full_pv': fullPV, 'clean_pv': cleanPV}

Expand All @@ -385,7 +388,7 @@ def creditDV01(self,

bump = 0.0001
for cds in issuerCurve._cdsContracts:
cds._coupon += bump
cds._runningCoupon += bump
issuerCurve._buildCurve()

v1 = self.value(valuationDate,
Expand All @@ -397,7 +400,7 @@ def creditDV01(self,

# NEED TO UNDO CHANGES TO CURVE OBJECT - NO NEED TO REBUILD !!!
for cds in issuerCurve._cdsContracts:
cds._coupon -= bump
cds._runningCoupon -= bump
issuerCurve._values = survProbs

creditDV01 = (v1['full_pv'] - v0['full_pv'])
Expand Down Expand Up @@ -505,7 +508,8 @@ def cleanPrice(self,

fwdDf = 1.0

cleanPV = fwdDf * (protPV - self._coupon * cleanRPV01 * self._notional)
cleanPV = fwdDf * (protPV - self._runningCoupon * cleanRPV01
* self._notional)
cleanPrice = (self._notional - cleanPV) / self._notional * 100.0
return cleanPrice

Expand Down Expand Up @@ -621,7 +625,7 @@ def accruedInterest(self):
paymentDates = self._adjustedDates
pcd = paymentDates[0]
accrualFactor = dayCount.yearFrac(pcd, self._stepInDate)
accruedInterest = accrualFactor * self._notional * self._coupon
accruedInterest = accrualFactor * self._notional * self._runningCoupon

if self._longProtection:
accruedInterest *= -1.0
Expand Down Expand Up @@ -711,7 +715,7 @@ def premiumLegPV(self,
issuerCurve,
pv01Method)['full_rpv01']

v = fullRPV01 * self._notional * self._coupon
v = fullRPV01 * self._notional * self._runningCoupon
return v

##########################################################################
Expand Down Expand Up @@ -779,7 +783,7 @@ def valueFastApprox(self,
protPV = h * (1.0 - contractRecovery) * (exp(-w*t_eff)-exp(-w*t_mat))\
/ w * self._notional
cleanPV = fwdDf * longProtection * \
(protPV - self._coupon * cleanRPV01 * self._notional)
(protPV - self._runningCoupon * cleanRPV01 * self._notional)
fullPV = cleanPV + fwdDf * longProtection * accrued

bumpSize = 0.0001
Expand All @@ -793,7 +797,7 @@ def valueFastApprox(self,
protPV = h * (1.0 - contractRecovery) * (exp(-w*t_eff)-exp(-w*t_mat)) \
/ w * self._notional
cleanPV_credit_bumped = fwdDf * longProtection * \
(protPV - self._coupon * cleanRPV01 * self._notional)
(protPV - self._runningCoupon * cleanRPV01 * self._notional)
fullPV_credit_bumped = cleanPV_credit_bumped \
+ fwdDf * longProtection * accrued
credit01 = fullPV_credit_bumped - fullPV
Expand All @@ -806,7 +810,7 @@ def valueFastApprox(self,
protPV = h * (1.0 - contractRecovery) * (exp(-w*t_eff)-exp(-w*t_mat))\
/ w * self._notional
cleanPV_ir_bumped = fwdDf * longProtection * \
(protPV - self._coupon * cleanRPV01 * self._notional)
(protPV - self._runningCoupon * cleanRPV01 * self._notional)
fullPV_ir_bumped = cleanPV_ir_bumped + fwdDf * longProtection * accrued
ir01 = fullPV_ir_bumped - fullPV

Expand Down Expand Up @@ -837,13 +841,13 @@ def __repr__(self):
s = labelToString("STEPINDATE", self._stepInDate)
s += labelToString("MATURITY", self._maturityDate)
s += labelToString("NOTIONAL", self._notional)
s += labelToString("RUNNING COUPON", self._coupon * 10000, "bp\n")
s += labelToString("RUNNING COUPON", self._runningCoupon*10000, "bp\n")
s += labelToString("DAYCOUNT", self._dayCountType)
s += labelToString("FREQUENCY", self._frequencyType)
s += labelToString("CALENDAR", self._calendarType)
s += labelToString("BUSDAYRULE", self._busDayAdjustType)
s += labelToString("DATEGENRULE", self._dateGenRuleType)
s += labelToString("ACCRUED DAYS:", self.accruedDays())
s += labelToString("ACCRUED DAYS", self.accruedDays())

header = "PAYMENT_DATE, YEAR_FRAC, FLOW"
valueTable = [self._adjustedDates, self._accrualFactors, self._flows]
Expand Down
2 changes: 1 addition & 1 deletion financepy/products/credit/FinCDSCurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def __init__(self,
###############################################################################

def _validate(self, cdsContracts):
''' Ensure that contracts are in increasinbg maturity. '''
''' Ensure that contracts are in increasing maturity. '''

if len(cdsContracts) == 0:
raise FinError("No CDS contracts have been supplied.")
Expand Down
2 changes: 1 addition & 1 deletion financepy/products/libor/FinLiborLMMProducts.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def simulate1F(self,
if numPaths < 2 or numPaths > 1000000:
raise FinError("NumPaths must be between 2 and 1 million")

if discountCurve._curveDate != self._startDate:
if discountCurve._valuationDate != self._startDate:
raise FinError("Curve anchor date not the same as LMM start date.")

gammas = np.zeros(len(self._gridTimes))
Expand Down
8 changes: 1 addition & 7 deletions tests/TestFinBond.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@
from financepy.products.libor.FinLiborSwap import FinLiborSwap
from financepy.products.libor.FinLiborDeposit import FinLiborDeposit
from financepy.products.libor.FinLiborCurve import FinLiborCurve

from financepy.finutils.FinHelperFunctions import dump


# from financepy.products.bonds.FinBond import FinBond, FinYieldConventions

from financepy.products.bonds import *
from financepy.products.bonds.FinBond import FinBond, FinYieldConventions

import sys
sys.path.append("..\\..")
Expand Down
10 changes: 4 additions & 6 deletions tests/TestFinBondAnnuity.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 30 10:52:29 2018
###############################################################################
# Copyright (C) 2018, 2019, 2020 Dominic O'Kane
###############################################################################

@author: Dominic O'Kane
"""
# TODO Set up test cases correctly

from FinTestCases import FinTestCases, globalTestCaseMode
Expand All @@ -17,7 +15,7 @@
from financepy.products.libor.FinLiborCurve import FinLiborCurve

# from financepy.products.bonds.FinBondAnnuity import FinBondAnnuity
from financepy.products.bonds import *
from financepy.products.bonds import FinBondAnnuity

testCases = FinTestCases(__file__, globalTestCaseMode)

Expand Down
6 changes: 5 additions & 1 deletion tests/TestFinBondConvertible.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
###############################################################################
# Copyright (C) 2018, 2019, 2020 Dominic O'Kane
###############################################################################

# TODO
import time
Expand All @@ -17,6 +19,7 @@

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


def test_FinBondConvertible():

settlementDate = FinDate(31, 12, 2003)
Expand Down Expand Up @@ -124,5 +127,6 @@ def test_FinBondConvertible():

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


test_FinBondConvertible()
testCases.compareTestCases()
4 changes: 3 additions & 1 deletion tests/TestFinBondEmbeddedOptionBK.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
###############################################################################
# Copyright (C) 2018, 2019, 2020 Dominic O'Kane
###############################################################################

import matplotlib.pyplot as plt
import time
Expand Down
7 changes: 5 additions & 2 deletions tests/TestFinBondEmbeddedOptionHW.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
###############################################################################
# Copyright (C) 2018, 2019, 2020 Dominic O'Kane
###############################################################################

import matplotlib.pyplot as plt
import time
Expand Down Expand Up @@ -81,7 +83,8 @@ def test_FinBondEmbeddedOptionMATLAB():
v = puttableBond.value(settlementDate, discountCurve, model)
end = time.time()
period = end - start
testCases.print(period, numTimeSteps, v['bondwithoption'], v['bondpure'])
testCases.print(period, numTimeSteps, v['bondwithoption'],
v['bondpure'])
values.append(v['bondwithoption'])

plt.figure()
Expand Down
9 changes: 3 additions & 6 deletions tests/TestFinBondFRN.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 5 09:43:40 2019
@author: Dominic
"""
###############################################################################
# Copyright (C) 2018, 2019, 2020 Dominic O'Kane
###############################################################################

from FinTestCases import FinTestCases, globalTestCaseMode

Expand Down
10 changes: 4 additions & 6 deletions tests/TestFinBondFutures.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 12 16:51:05 2019
###############################################################################
# Copyright (C) 2018, 2019, 2020 Dominic O'Kane
###############################################################################

@author: Dominic O'Kane
"""
from FinTestCases import FinTestCases, globalTestCaseMode

from financepy.products.bonds.FinBondFuture import FinBondFuture
Expand All @@ -20,7 +18,7 @@

def test_FinBondFuture():

# Example taken from Martellini and Priaulet page 360
# Example taken from Martellini and Priaulet page 360
freq = FinFrequencyTypes.SEMI_ANNUAL
basis = FinDayCountTypes.ACT_ACT_ICMA

Expand Down
15 changes: 5 additions & 10 deletions tests/TestFinBondMortgage.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 5 16:23:12 2019
@author: Dominic
"""
###############################################################################
# Copyright (C) 2018, 2019, 2020 Dominic O'Kane
###############################################################################

import sys

from FinTestCases import FinTestCases, globalTestCaseMode

from financepy.finutils.FinDate import FinDate
from financepy.finutils.FinDayCount import FinDayCountTypes
from financepy.finutils.FinFrequency import FinFrequencyTypes
from financepy.finutils.FinCalendar import FinCalendarTypes
from financepy.products.bonds.FinBondMortgage import FinBondMortgage, FinBondMortgageTypes
from financepy.products.bonds.FinBondMortgage import FinBondMortgage
from financepy.products.bonds.FinBondMortgage import FinBondMortgageTypes

sys.path.append("..//..")

Expand Down
4 changes: 3 additions & 1 deletion tests/TestFinBondOptionBKModel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
###############################################################################
# Copyright (C) 2018, 2019, 2020 Dominic O'Kane
###############################################################################

import numpy as np
import time
Expand Down
4 changes: 3 additions & 1 deletion tests/TestFinBondOptionHWModel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
###############################################################################
# Copyright (C) 2018, 2019, 2020 Dominic O'Kane
###############################################################################

import numpy as np
import time
Expand Down
9 changes: 3 additions & 6 deletions tests/TestFinBondPortfolio.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 12 16:51:05 2016
@author: Dominic O'Kane
"""
###############################################################################
# Copyright (C) 2018, 2019, 2020 Dominic O'Kane
###############################################################################

import os

Expand Down
10 changes: 4 additions & 6 deletions tests/TestFinBondYieldCurve.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 08 09:26:27 2016
@author: Dominic O'Kane
"""
###############################################################################
# Copyright (C) 2018, 2019, 2020 Dominic O'Kane
###############################################################################

import datetime as dt

Expand All @@ -18,6 +15,7 @@
from financepy.products.bonds.FinBond import FinBond
from financepy.products.bonds.FinBondYieldCurve import FinBondYieldCurve
from financepy.products.bonds.FinBondYieldCurveModel import *

sys.path.append("..//..")

testCases = FinTestCases(__file__, globalTestCaseMode)
Expand Down
13 changes: 4 additions & 9 deletions tests/TestFinBondZeroCurve.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 08 09:26:27 2016
@author: Dominic O'Kane
"""

###############################################################################
# Copyright (C) 2018, 2019, 2020 Dominic O'Kane
###############################################################################

import sys
import os
Expand All @@ -23,8 +19,7 @@
sys.path.append("..//..")
sys.path.append("..")

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


def test_FinBondZeroCurve():
Expand Down
8 changes: 3 additions & 5 deletions tests/TestFinCDS.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 13 21:52:16 2019
###############################################################################
# Copyright (C) 2018, 2019, 2020 Dominic O'Kane
###############################################################################

@author: Dominic O'Kane
"""
import time
from FinTestCases import FinTestCases, globalTestCaseMode

Expand Down
9 changes: 3 additions & 6 deletions tests/TestFinCDSBasket.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 13 21:52:16 2019
@author: Dominic O'Kane
"""
###############################################################################
# Copyright (C) 2018, 2019, 2020 Dominic O'Kane
###############################################################################

from FinTestCases import FinTestCases, globalTestCaseMode

Expand Down
Loading

0 comments on commit 13e8635

Please sign in to comment.