Function BondIR(SettDate As Date, StartDate As Date, MatDate As Date, Cpn As Double, CpnsPerYear As Long, Buckets As Object, DateVec As Object, PVFact As Object) As Variant ' Computes the interest risk of a bond (change in price per % change in interest) Const NormFakt = 0.0001 Const DY = 365.25 Dim IRVec() As Double, BVec() As Double Dim n As Long, i As Long, j As Long n = Buckets.Columns.Count ReDim IRVec(1 To n) ReDim BVec(1 To n) For i = 1 To n BVec(i) = (Buckets(1, i) - SettDate) / DY IRVec(i) = 0 Next i Dim BondAct As Double, IR As Double, PayAmt As Double, CpnPrd As Double, PV As Double, df As Double CpnPrd = 1# / CpnsPerYear Dim t_mat As Double, t_start As Double Dim InterDate As Date t_start = (StartDate - SettDate) / DY t_mat = (MatDate - SettDate) / DY BondAct = t_mat i = 0 PV = 0 Do While BondAct >= 0 If (BondAct - t_start) > CpnPrd Then PayAmt = Cpn * CpnPrd Else PayAmt = Cpn * (BondAct - t_start) End If If i = 0 Then PayAmt = PayAmt + 100 If BondAct = t_start Then PayAmt = -PV InterDate = SettDate + BondAct * DY df = inter2(DateVec, PVFact, InterDate) PV = PV + PayAmt * df IR = -BondAct * PayAmt * df * NormFakt If BondAct >= BVec(n) Then IRVec(n) = IRVec(n) + IR ElseIf BondAct <= BVec(1) Then IRVec(1) = IRVec(1) + IR Else j = 1 Do While BondAct > BVec(j) j = j + 1 Loop j = j - 1 IRVec(j) = IRVec(j) + IR * (BVec(j + 1) - BondAct) / (BVec(j + 1) - BVec(j)) IRVec(j + 1) = IRVec(j + 1) + IR * (BondAct - BVec(j)) / (BVec(j + 1) - BVec(j)) End If If BondAct = t_start Then Exit Do i = i + 1 BondAct = BondAct - CpnPrd If BondAct < t_start Then BondAct = t_start Loop BondIR = IRVec End Function