Loading of Libraries and Classes.


In [1]:
%matplotlib inline
from datetime import date
import time
import pandas as pd
import numpy as np
pd.options.display.max_colwidth = 60
from Curves.Corporates.CorporateDailyVasicek import CorporateRates
from Boostrappers.CDSBootstrapper.CDSVasicekBootstrapper import BootstrapperCDSLadder
from MonteCarloSimulators.Vasicek.vasicekMCSim import MC_Vasicek_Sim
from Products.Rates.CouponBond import CouponBond
from Products.Credit.CDS import CDS
from Scheduler.Scheduler import Scheduler
import quandl
import matplotlib.pyplot as plt
from parameters import WORKING_DIR
import itertools
marker = itertools.cycle((',', '+', '.', 'o', '*'))
from IPython.core.pylabtools import figsize
figsize(15, 4)
from pandas import ExcelWriter
import numpy.random as nprnd
from pprint import pprint


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-42f8bc91bcdc> in <module>()
      5 import numpy as np
      6 pd.options.display.max_colwidth = 60
----> 7 from Curves.Corporates.CorporateDailyVasicek import CorporateRates
      8 from Boostrappers.CDSBootstrapper.CDSVasicekBootstrapper import BootstrapperCDSLadder
      9 from MonteCarloSimulators.Vasicek.vasicekMCSim import MC_Vasicek_Sim

ImportError: No module named 'Curves'

Create forward bond future PV (Exposure) time profile

Setting up parameters


In [11]:
t_step = 1.0 / 365.0
simNumber = 10
trim_start = date(2005,3,10)
trim_end = date(2010,12,31)  # Last Date of the Portfolio
start = date(2005, 3, 10)
referenceDate = date(2005, 5, 10)

Data input for the CouponBond portfolio

The word portfolio is used to describe just a dict of CouponBonds.

This line creates a referenceDateList

myScheduler = Scheduler()

ReferenceDateList = myScheduler.getSchedule(start=referenceDate,end=trim_end,freq="1M", referencedate=referenceDate)

Create Simulator

This section creates Monte Carlo Trajectories in a wide range. Notice that the BondCoupon maturities have to be

inside the Monte Carlo simulation range [trim_start,trim_end]

Sigma has been artificially increased (OIS has smaller sigma) to allow for visualization of distinct trajectories.

    # SDE parameters - Vasicek SDE
    # dr(t) = k(θ − r(t))dt + σdW(t)
    self.kappa = x[0]
    self.theta = x[1]
    self.sigma = x[2]
    self.r0 = x[3]

myVasicek = MC_Vasicek_Sim() xOIS = [ 3.0, 0.07536509, -0.208477, 0.07536509] myVasicek.setVasicek(x=xOIS,minDay=trim_start,maxDay=trim_end,simNumber=simNumber,t_step=1/365.0) myVasicek.getLibor()

Create Coupon Bond with several startDates.

SixMonthDelay = myScheduler.extractDelay("6M") TwoYearsDelay = myScheduler.extractDelay("2Y") startDates = [referenceDate + nprnd.randint(0,3)*SixMonthDelay for r in range(10)]

For debugging uncomment this to choose a single date for the forward bond

print(startDates)

startDates = [date(2005,3,10)] # or

startDates = [date(2005,3,10) + SixMonthDelay]

maturities = [(x+TwoYearsDelay) for x in startDates]

You can change the coupon and see its effect on the Exposure Profile. The breakevenRate is calculated, for simplicity, always at referenceDate=self.start, that is, at the first day of the CouponBond life.

Below is a way to create random long/short bond portfolio of any size. The notional only affects the product class at the last stage of calculation. In my case, the only parameters affected are Exposure (PV on referenceDate), pvAvg(average PV on referenceDate)

myPortfolio = {} coupon = 0.07536509 for i in range(len(startDates)): notional=(-1.0)**i myPortfolio[i] = CouponBond(fee=1.0,start=startDates[i],coupon=coupon,notional=notional, maturity= maturities[i], freq="3M", referencedate=referenceDate)


In [12]:
myScheduler = Scheduler()
ReferenceDateList = myScheduler.getSchedule(start=referenceDate,end=trim_end,freq="1M", referencedate=referenceDate)
# Create Simulator
xOIS = [ 3.0,  0.07536509, -0.208477,  0.07536509]
myVasicek = MC_Vasicek_Sim(datelist = [trim_start,trim_end],x = xOIS,simNumber = simNumber,t_step =1/365.0 )
#myVasicek.setVasicek(x=xOIS,minDay=trim_start,maxDay=trim_end,simNumber=simNumber,t_step=1/365.0)
myVasicek.getLibor()

# Create Coupon Bond with several startDates.
SixMonthDelay = myScheduler.extractDelay("6M")
TwoYearsDelay = myScheduler.extractDelay("2Y")
startDates = [referenceDate + nprnd.randint(0,3)*SixMonthDelay for r in range(10)]

# For debugging uncomment this to choose a single date for the forward bond
# print(startDates)
startDates = [date(2005,3,10)+SixMonthDelay,date(2005,3,10)+TwoYearsDelay ]
maturities = [(x+TwoYearsDelay) for x in startDates]

myPortfolio = {}
coupon = 0.07536509
for i in range(len(startDates)):
    notional=(-1.0)**i
    myPortfolio[i] = CouponBond(fee=1.0,start=startDates[i],coupon=coupon,notional=notional,
                                maturity= maturities[i], freq="3M", referencedate=referenceDate)

Create Libor and portfolioScheduleOfCF. This datelist contains all dates

to be used in any calculation of the portfolio positions.

BondCoupon class has to have a method getScheduleComplete, which return

fullSet on [0] and datelist on [1], calculated by BondCoupon as:

def getScheduleComplete(self): self.datelist=self.myScheduler.getSchedule(start=self.start,end=self.maturity,freq=self.freq,referencedate=self.referencedate) self.ntimes = len(self.datelist) fullset = sorted(set(self.datelist) .union([self.referencedate]) .union([self.start]) .union([self.maturity]) ) return fullset,self.datelist

portfolioScheduleOfCF is the concatenation of all fullsets. It defines the set of all dates for which Libor should be known.


In [13]:
portfolioScheduleOfCF = set(ReferenceDateList)
for i in range(len(myPortfolio)):
    portfolioScheduleOfCF=portfolioScheduleOfCF.union(myPortfolio[i].getScheduleComplete()[0]
)
portfolioScheduleOfCF = sorted(portfolioScheduleOfCF.union(ReferenceDateList))
OIS = myVasicek.getSmallLibor(datelist=portfolioScheduleOfCF)
# at this point OIS contains all dates for which the discount curve should be known.
# If the OIS doesn't contain that date, it would not be able to discount the cashflows and the calcualtion would faill.

In [14]:
print(OIS)


                   0         1         2         3         4         5  \
2005-05-10  0.984088  0.991872  0.981774  0.986068  0.987300  0.998230   
2005-05-31  0.977086  0.984309  0.974699  0.977595  0.984295  0.997771   
2005-06-30  0.966169  0.969501  0.965633  0.962113  0.985148  0.996189   
2005-07-31  0.958750  0.958747  0.955968  0.947552  0.987641  1.001542   
2005-08-31  0.956035  0.951574  0.948740  0.934513  0.984228  1.012260   
2005-09-10  0.955677  0.949611  0.946653  0.931181  0.981424  1.015136   
2005-09-30  0.954981  0.943211  0.942986  0.925730  0.975765  1.020695   
2005-10-31  0.952321  0.931296  0.934793  0.920294  0.966648  1.026026   
2005-11-30  0.948872  0.918033  0.925199  0.918061  0.953018  1.024059   
2005-12-31  0.947159  0.907200  0.917521  0.912426  0.940340  1.015003   
2006-01-31  0.950548  0.896972  0.914209  0.903565  0.928476  1.006036   
2006-02-28  0.954254  0.887636  0.908710  0.894497  0.922006  0.999423   
2006-03-31  0.954360  0.876559  0.896959  0.882248  0.917514  0.987469   
2006-04-30  0.950705  0.862996  0.884332  0.873040  0.914632  0.972251   
2006-05-31  0.948909  0.848888  0.877165  0.865693  0.908111  0.958337   
2006-06-30  0.945434  0.835215  0.873067  0.857668  0.897819  0.954114   
2006-07-31  0.944840  0.818452  0.868760  0.848023  0.886276  0.951524   
2006-08-31  0.947520  0.803539  0.860522  0.836586  0.877204  0.947985   
2006-09-30  0.951249  0.790855  0.850439  0.822752  0.869392  0.948014   
2006-10-31  0.960190  0.780493  0.843763  0.805208  0.863955  0.953003   
2006-11-30  0.967858  0.771708  0.838503  0.787375  0.862291  0.959851   
2006-12-31  0.972455  0.766446  0.835112  0.773342  0.858574  0.964231   
2007-01-31  0.975219  0.759896  0.837581  0.763958  0.851010  0.965745   
2007-02-28  0.976857  0.756470  0.836301  0.759820  0.844226  0.963183   
2007-03-10  0.977337  0.755692  0.834752  0.760475  0.842177  0.962315   
2007-03-31  0.982569  0.754013  0.833617  0.762221  0.836353  0.959123   
2007-04-30  0.988730  0.750874  0.830526  0.761630  0.827717  0.954173   
2007-05-31  0.991429  0.743007  0.822338  0.758425  0.821387  0.952888   
2007-06-30  0.992433  0.736733  0.818867  0.754594  0.816732  0.960897   
2007-07-31  0.993909  0.731211  0.812188  0.750725  0.811589  0.966664   
...              ...       ...       ...       ...       ...       ...   
2008-08-31  0.890898  0.708482  0.758393  0.706571  0.762126  0.910675   
2008-09-30  0.880093  0.706363  0.757917  0.704528  0.761647  0.906938   
2008-10-31  0.867007  0.702919  0.755442  0.699037  0.763966  0.897585   
2008-11-30  0.855063  0.701540  0.752119  0.695078  0.765523  0.890334   
2008-12-31  0.840281  0.698659  0.746175  0.695241  0.758796  0.883357   
2009-01-31  0.835611  0.689047  0.736492  0.699047  0.745167  0.877143   
2009-02-28  0.835199  0.683100  0.730683  0.703764  0.735601  0.870842   
2009-03-10  0.833691  0.680938  0.729131  0.704569  0.731953  0.868182   
2009-03-31  0.833060  0.678078  0.724483  0.704621  0.725886  0.860658   
2009-04-30  0.829761  0.672676  0.714914  0.707131  0.718020  0.848605   
2009-05-31  0.829233  0.665567  0.705768  0.707042  0.709769  0.841678   
2009-06-30  0.836047  0.659515  0.697901  0.708760  0.703964  0.834969   
2009-07-31  0.847786  0.654357  0.695198  0.710974  0.699839  0.825195   
2009-08-31  0.860098  0.647174  0.695532  0.712920  0.693169  0.819231   
2009-09-30  0.871972  0.645009  0.697801  0.714335  0.684837  0.814874   
2009-10-31  0.883598  0.641729  0.695128  0.717852  0.676708  0.810160   
2009-11-30  0.890637  0.636466  0.692896  0.723605  0.665538  0.806647   
2009-12-31  0.896645  0.629481  0.693474  0.724737  0.657270  0.803574   
2010-01-31  0.896542  0.621128  0.692637  0.718712  0.652321  0.799980   
2010-02-28  0.895100  0.611991  0.689626  0.716837  0.648467  0.794444   
2010-03-31  0.891951  0.603733  0.685941  0.712642  0.643137  0.783254   
2010-04-30  0.889496  0.599356  0.677134  0.707737  0.635697  0.772378   
2010-05-31  0.884536  0.592273  0.670843  0.703600  0.627223  0.765733   
2010-06-30  0.877498  0.584376  0.659179  0.701279  0.617512  0.760248   
2010-07-31  0.871293  0.577808  0.647791  0.699737  0.611083  0.753318   
2010-08-31  0.866410  0.570914  0.639904  0.698020  0.602717  0.745069   
2010-09-30  0.860219  0.564314  0.628499  0.691392  0.590900  0.731484   
2010-10-31  0.852638  0.558354  0.620984  0.684491  0.582542  0.716469   
2010-11-30  0.844255  0.555015  0.615836  0.679398  0.577631  0.704773   
2010-12-31  0.838457  0.548834  0.609813  0.676919  0.575892  0.696768   

                   6         7         8         9  
2005-05-10  0.988623  1.001268  0.972311  0.985363  
2005-05-31  0.985204  1.000342  0.962490  0.978165  
2005-06-30  0.981538  0.999348  0.951040  0.968010  
2005-07-31  0.976732  0.996424  0.942566  0.954127  
2005-08-31  0.970048  0.989633  0.934772  0.943915  
2005-09-10  0.967776  0.986989  0.933616  0.941822  
2005-09-30  0.963008  0.980455  0.932512  0.935565  
2005-10-31  0.953362  0.974963  0.937158  0.921650  
2005-11-30  0.946618  0.969746  0.944389  0.908584  
2005-12-31  0.942169  0.967149  0.944508  0.892984  
2006-01-31  0.940722  0.967694  0.942813  0.874983  
2006-02-28  0.936534  0.963981  0.934638  0.859481  
2006-03-31  0.934221  0.961768  0.923094  0.848041  
2006-04-30  0.938091  0.956910  0.911968  0.841580  
2006-05-31  0.935313  0.951351  0.902676  0.835839  
2006-06-30  0.932663  0.951801  0.894825  0.833095  
2006-07-31  0.931178  0.952037  0.888042  0.825548  
2006-08-31  0.931013  0.940626  0.881847  0.815263  
2006-09-30  0.927725  0.928528  0.876974  0.803460  
2006-10-31  0.924040  0.917045  0.870764  0.791332  
2006-11-30  0.923708  0.909763  0.866218  0.782338  
2006-12-31  0.922653  0.905350  0.861675  0.776339  
2007-01-31  0.920448  0.902430  0.860369  0.770454  
2007-02-28  0.919273  0.896695  0.860393  0.762739  
2007-03-10  0.918570  0.892103  0.861395  0.760169  
2007-03-31  0.917109  0.882418  0.863980  0.755974  
2007-04-30  0.911695  0.870145  0.873519  0.749228  
2007-05-31  0.911393  0.858110  0.879752  0.740945  
2007-06-30  0.913930  0.844692  0.884100  0.738097  
2007-07-31  0.922431  0.829112  0.884199  0.737853  
...              ...       ...       ...       ...  
2008-08-31  0.869731  0.737413  0.762230  0.684608  
2008-09-30  0.862578  0.733735  0.751458  0.679501  
2008-10-31  0.850393  0.728679  0.745789  0.675857  
2008-11-30  0.836552  0.722622  0.742687  0.670977  
2008-12-31  0.821810  0.718568  0.741132  0.668525  
2009-01-31  0.810185  0.713850  0.739917  0.663862  
2009-02-28  0.801479  0.709955  0.736428  0.659515  
2009-03-10  0.798420  0.710103  0.734398  0.657383  
2009-03-31  0.790569  0.711449  0.729331  0.651080  
2009-04-30  0.781416  0.709170  0.715308  0.641004  
2009-05-31  0.776142  0.701012  0.697711  0.632159  
2009-06-30  0.768757  0.692751  0.683249  0.623179  
2009-07-31  0.757891  0.689566  0.669609  0.613984  
2009-08-31  0.751412  0.684878  0.655800  0.601560  
2009-09-30  0.747787  0.680229  0.644805  0.593368  
2009-10-31  0.746892  0.675290  0.636824  0.586985  
2009-11-30  0.745943  0.670028  0.630201  0.583891  
2009-12-31  0.739524  0.668153  0.623572  0.581615  
2010-01-31  0.729306  0.663879  0.618134  0.580156  
2010-02-28  0.721615  0.659213  0.613501  0.576847  
2010-03-31  0.712669  0.655685  0.606360  0.569582  
2010-04-30  0.706960  0.655552  0.600606  0.561718  
2010-05-31  0.703203  0.652077  0.595120  0.554850  
2010-06-30  0.695398  0.647703  0.587914  0.549874  
2010-07-31  0.686723  0.641550  0.579805  0.541771  
2010-08-31  0.678986  0.635854  0.577091  0.537166  
2010-09-30  0.674957  0.629001  0.573847  0.533304  
2010-10-31  0.673073  0.623058  0.565413  0.527523  
2010-11-30  0.675625  0.618686  0.555506  0.523332  
2010-12-31  0.676339  0.612046  0.547957  0.516624  

[73 rows x 10 columns]

In [15]:
pvs={}
for t in portfolioScheduleOfCF:
    pvs[t] = np.zeros([1,simNumber])
    for i in range(len(myPortfolio)):
        myPortfolio[i].setLibor(OIS)
        pvs[t] = pvs[t] + myPortfolio[i].getExposure(referencedate=t).values

#print(portfolioScheduleOfCF)        
#print(pvs)
pvsPlot = pd.DataFrame.from_dict(list(pvs.items()))
pvsPlot.index= list(pvs.keys())
pvs1={}
for i,t in zip(pvsPlot.values,pvsPlot.index):
    pvs1[t]=i[1][0]
pvs = pd.DataFrame.from_dict(data=pvs1,orient="index")
ax=pvs.plot(legend=False)
ax.set_xlabel("Year")
ax.set_ylabel("Coupon Bond Exposure")


Out[15]:
<matplotlib.text.Text at 0x10b2d9748>

In [ ]:


In [ ]: