Exemplo n.º 1
0
 def test_RicattiAB(self):
     # CIR parameters
     r0 = 0.02
     chi_ = 0.03
     theta_ = 0.05
     sigma_ = 0.10
     CirModel = CoxIngersollRossModel(r0, chi_, theta_, sigma_)
     # Affine Short Rate Parameters
     modelTimes = np.array([10.0])
     chi = np.array([chi_])
     theta = np.array([theta_])
     sigma = np.array([sigma_])
     alpha = np.array([0.0])
     beta = np.array([1.0])
     AsrModel = AffineShortRateModel(r0, modelTimes, chi, theta, sigma,
                                     alpha, beta)
     #
     t = np.linspace(0.0, 10.0, 11)
     dt = np.linspace(0.0, 10.0, 11)
     #
     cirRicattiAB = np.array([
         CirModel.ricattiAB(t_, t_ + dt_, 0.0, 1.0) for t_ in t
         for dt_ in dt
     ])
     AsrRicattiAB = np.array([
         AsrModel.ricattiAB(t_, t_ + dt_, 0.0, 1.0) for t_ in t
         for dt_ in dt
     ])
     diff = AsrRicattiAB - cirRicattiAB
     # print(diff)
     # print(np.max(np.abs(diff)))
     self.assertLess(np.max(np.abs(diff)), 1.4e-6)
 def test_ModelCurve(self):
     # CIR parameters
     r0 = 0.02
     chi_ = 0.07
     theta_ = 0.05
     sigma_ = 0.10
     CirModel = CoxIngersollRossModel(r0, chi_, theta_, sigma_)
     model = CirModel
     T = np.linspace(0.0, 10.0, 11)
     dt = 1.0 / 365.0
     f = np.array([ np.log( \
         model.zeroBondPrice(0.0,T_,model.r0) / model.zeroBondPrice(0.0,T_+dt,model.r0)) / dt \
         for T_ in T ])
     # plt.plot(T,f,label='CIR')
     #
     curve = YieldCurve(0.03)
     model = ShiftedRatesModel(curve, CirModel)
     X0 = model.initialValues()
     f = np.array([ np.log( \
         model.zeroBond(0.0,T_,X0,None) / model.zeroBond(0.0,T_+dt,X0,None)) / dt \
         for T_ in T ])
     # plt.plot(T,f,label='Shifted')
     # plt.legend()
     # plt.show()
     # print(f - 0.03)
     self.assertLess(np.max(np.abs(f - 0.03)), 7.e-14)
Exemplo n.º 3
0
 def test_SqrtProcessEvolution(self):
     # CIR parameters
     r0 = 0.02
     chi_ = 0.03
     theta_ = 0.05
     sigma_ = 0.10
     model0 = CoxIngersollRossModel(r0, chi_, theta_, sigma_,
                                    fullTruncation)
     model1 = CoxIngersollRossModel(r0, chi_, theta_, sigma_,
                                    lognormalApproximation)
     model2 = CoxIngersollRossModel(r0, chi_, theta_, sigma_,
                                    quadraticExponential(1.5))
     models = [model0, model1, model2]
     # MC simulation params
     t = 5.0
     dt = 1.0
     rt = np.linspace(0.0, 0.10, 11)
     dw = np.linspace(-1.0, 1.0, 11)
     for rt_ in rt:
         results = []
         for dw_ in dw:
             res = np.zeros(3)
             for k, model in enumerate(models):
                 X1 = np.array([0.0, 0.0])
                 model.evolve(t, np.array([rt_, 0.0]), dt, np.array([dw_]),
                              X1)
                 res[k] = X1[0]
             results.append(res)
         results = np.array(results)
         #plt.plot(dw,results[:,0],label='FT')
         #plt.plot(dw,results[:,1],label='LN')
         #plt.plot(dw,results[:,2],label='QE')
         #plt.legend()
         #plt.show()
         #print(np.max(np.abs(results[:,0]-results[:,1])))
         #print(np.max(np.abs(results[:,1]-results[:,2])))
         self.assertLess(np.max(np.abs(results[:, 0] - results[:, 1])),
                         0.0048)
         self.assertLess(np.max(np.abs(results[:, 1] - results[:, 2])),
                         0.0027)
Exemplo n.º 4
0
 def test_ModelYieldCurve(self):
     # CIR parameters
     r0 = 0.02
     chi_ = 0.07
     theta_ = 0.05
     sigma_ = 0.10
     CirModel = CoxIngersollRossModel(r0, chi_, theta_, sigma_)
     model = CirModel
     # model = self.model
     T = np.linspace(0.0, 10.0, 11)
     dt = 1.0 / 365.0
     f = np.array([ np.log( \
         model.zeroBondPrice(0.0,T_,model.r0) / model.zeroBondPrice(0.0,T_+dt,model.r0)) / dt \
         for T_ in T ])
 def test_CirSimulation(self):
     r0         = 0.02
     chi_       = 0.07
     theta_     = 0.05
     sigma_     = 0.10
     modelFT = CoxIngersollRossModel(r0,chi_,theta_,sigma_,fullTruncation)
     modelLN = CoxIngersollRossModel(r0,chi_,theta_,sigma_,lognormalApproximation)
     modelQE = CoxIngersollRossModel(r0,chi_,theta_,sigma_,quadraticExponential(1.5))
     #
     dT = 5.0
     times = np.linspace(0.0, 10.0, 11)
     zcbs = np.array([ modelFT.zeroBondPrice(0.0,T + dT,r0) for T in times ])
     #
     nPaths = 2**13
     seed = 314159265359
     # risk-neutral simulation
     simFT = McSimulation(modelFT,times,nPaths,seed,showProgress=True)
     simLN = McSimulation(modelLN,times,nPaths,seed,showProgress=True)
     simQE = McSimulation(modelQE,times,nPaths,seed,showProgress=True)
     #
     zcbFT = np.mean(np.array([
         [ modelFT.zeroBond(times[t],times[t]+dT,simFT.X[p,t,:],None) / modelFT.numeraire(times[t],simFT.X[p,t,:]) for t in range(len(times)) ]
         for p in range(nPaths) ]), axis=0)
     zcbLN = np.mean(np.array([
         [ modelLN.zeroBond(times[t],times[t]+dT,simLN.X[p,t,:],None) / modelLN.numeraire(times[t],simLN.X[p,t,:]) for t in range(len(times)) ]
         for p in range(nPaths) ]), axis=0)
     zcbQE = np.mean(np.array([
         [ modelQE.zeroBond(times[t],times[t]+dT,simQE.X[p,t,:],None) / modelQE.numeraire(times[t],simQE.X[p,t,:]) for t in range(len(times)) ]
         for p in range(nPaths) ]), axis=0)
     #
     results = pd.DataFrame([ times, zcbs, zcbFT, zcbLN, zcbQE ]).T
     results.columns = ['times', 'zcbs', 'zcbFT', 'zcbLN', 'zcbQE']
     print(results)