Skip to content

Commit

Permalink
Modified Duration
Browse files Browse the repository at this point in the history
  • Loading branch information
gavbrennan committed Jun 20, 2022
1 parent bce9799 commit 909a5dd
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Qwack.Math/BondUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static double PriceFromYtm(double couponRate, double faceValue, double yt


public static double PriceFromYTC(double couponRate, double callPrice, double tCall, double ytc) =>
couponRate / 2 * ((1 - System.Math.Pow(1 + ytc / 2, -2 * tCall)) / (ytc / 2)) + callPrice / System.Math.Pow( (1 + ytc / 2), 2 * tCall);
couponRate / 2 * ((1 - System.Math.Pow(1 + ytc / 2, -2 * tCall)) / (ytc / 2)) + callPrice / System.Math.Pow((1 + ytc / 2), 2 * tCall);

public static double YtcFromPrice(double couponRate, double callPrice, double tCall, double cleanPrice)
{
Expand All @@ -32,14 +32,14 @@ public static double YtcFromPrice(double couponRate, double callPrice, double tC
return Solvers.Brent.BrentsMethodSolve(solverFn, 1e-6, 1, 1e-6);
}

public static double MacaulayDuration (double couponRate, double faceValue, double ytm, double periodsPerYear, double tMaturity, double tNext, double cleanPrice)
public static double MacaulayDuration(double couponRate, double faceValue, double ytm, double periodsPerYear, double tMaturity, double tNext, double cleanPrice)
{
var nPeriods = (tMaturity-tNext) * periodsPerYear;
var nPeriods = (tMaturity - tNext) * periodsPerYear;
var couponFlow = couponRate * faceValue / periodsPerYear;
var divisor = 1 + ytm / periodsPerYear;
var sum = 0.0;

for(var i = 0; i <= nPeriods; i++)
for (var i = 0; i <= nPeriods; i++)
{
var df = System.Math.Pow(divisor, i + 1);
var rowFlow = couponFlow / df * (tNext + i / periodsPerYear) / cleanPrice;
Expand All @@ -51,5 +51,12 @@ public static double MacaulayDuration (double couponRate, double faceValue, doub
}
return sum;
}

public static double ModifiedDuration(double couponRate, double faceValue, double ytm, double periodsPerYear, double tMaturity, double tNext, double cleanPrice)
{
var mcD = MacaulayDuration(couponRate, faceValue, ytm, periodsPerYear, tMaturity, tNext, cleanPrice);
var modD = mcD / (1 + ytm / periodsPerYear);
return modD;
}
}
}

0 comments on commit 909a5dd

Please sign in to comment.