public void Test() { var market = Market(); const double lambda = 0.01; var sigma = new StepFunction(new[] { 0.0, 1.0, 2.0 }, new[] { 0.007, 0.004, 0.0065 }, 0.0); var hw1 = new Hw1Model(TimeMeasure.Act365(market.RefDate), Currency.Eur, lambda, sigma); var mcConfig = new MonteCarloConfig(20000, RandomGenerators.GaussianSobol(SobolDirection.JoeKuoD5)); var mcPricer = McPricer.WithDetails(mcConfig); var fixedLeg = FixedLeg(market.RefDate); var mcPriceResult = (PriceResult)mcPricer.Price(fixedLeg, hw1, market); var mcCoupons = mcPriceResult.Details.Map(p => p.Item3.Value); var refCoupons = mcPriceResult.Details.Map(pi => market.DiscountCurve(pi.Item2.Financing).Zc(pi.Item2.Date)); var errAbs = Math.Abs(mcCoupons.Sum() - refCoupons.Sum()); Assert.LessOrEqual(errAbs, 7.0e-5); var errRel = Math.Abs(mcCoupons.Sum() / refCoupons.Sum() - 1.0); Assert.LessOrEqual(errRel, 8.0e-6); }
public void ProbaMeasure(double lambda, RrFunction sigma, Duration probaMaturity, Duration simulationMaturity, int quadratureNbPoints, double precision) { var refDate = DateTime.Parse("07/06/2009"); var hw1 = new Hw1Model(TimeMeasure.Act365(refDate), Currency.Eur, lambda, sigma); var probaMeasure = new PaymentInfo(hw1.Currency, refDate + probaMaturity); var t = refDate + simulationMaturity; var hw1PathGen = new Hw1ModelPathGeneratorFactory().Build(hw1, null, probaMeasure, new[] { t }); var brownianBridge = BrownianBridge.Create(hw1PathGen.AllSimulatedDates); var hw1Zc = new Hw1ModelZcRepresentation(hw1); var numeraireZc = hw1Zc.Zc(t, probaMeasure.Date, 1.0); Assert.AreEqual(hw1PathGen.AllSimulatedDates.Length, 1); double[] x, w; GaussHermite.GetQuadrature(quadratureNbPoints, out x, out w); double flow = 0.0; for (int i = 0; i < x.Length; i++) { var dw = brownianBridge.PathIncrements(new[] { x[i] }, 1); var ornstein = hw1PathGen.Path(dw).GetProcessValue(0); flow += w[i] * 1.0 / numeraireZc.Eval(ornstein); } var error = Math.Abs(flow - 1.0); Assert.LessOrEqual(error, precision); }
private static Market Market() { var refDate = DateTime.Parse("07/06/2009"); var time = TimeMeasure.Act365(refDate); var pillars = new[] { refDate + Duration.Year, refDate + 2 * Duration.Year, refDate + 3 * Duration.Year, refDate + 5 * Duration.Year }; var zcRates = new[] { 0.0010, 0.003, 0.005, 0.008 }; var zcs = zcRates.Select((r, i) => Math.Exp(-time[pillars[i]] * r)).ToArray(); DiscountCurve discountCurve = DiscountCurve.LinearRateInterpol( FinancingId.RiskFree(Currency.Eur), pillars, zcs, time); var market = new Market(new[] { discountCurve }, new AssetMarket[0]); return(market); }
private static AssetMarket[] ProcessAssetMkt(object[,] bag, DateTime refDate, DiscountCurve[] discountCurves) { var eqtyTime = TimeMeasure.Act365(refDate); var assetRawDatas = bag.ProcessLabelledMatrix("Asset", o => o.ToString(), o => o.ToString(), o => o); TimeMatrixDatas repoRawDatas = bag.ProcessTimeMatrixDatas("Repo"); var repoPillars = repoRawDatas.RowLabels .Select(dOrDur => dOrDur.ToDate(refDate)).ToArray(); var assetMkts = new List <AssetMarket>(); for (int i = 0; i < assetRawDatas.RowLabels.Length; i++) { var assetName = assetRawDatas.RowLabels[i]; var rawCurrency = assetRawDatas.GetColFromLabel("Currency")[i].ToString(); object rawSpot = assetRawDatas.GetColFromLabel("Spot")[i]; var currency = Currency.Parse(rawCurrency); var assetId = new AssetId(assetName, currency); double spot; if (!NumberConverter.TryConvertDouble(rawSpot, out spot)) { throw new ArgumentException(String.Format("AssetMarketFactory, invalid {0} spot : {1}", assetName, rawSpot)); } double[] repoRates = repoRawDatas.GetColFromLabel(assetName); var repoZcs = repoRates.Select((r, idx) => Math.Exp(-eqtyTime[repoPillars[idx]] * r)).ToArray(); var repoCurve = DiscountCurve.LinearRateInterpol(FinancingId.AssetCollat(assetId), repoPillars, repoZcs, eqtyTime); var divQuotes = ProcessDiv(bag, refDate, assetName); var volMatrix = AssetVolMatrix(bag, eqtyTime, assetName); var riskFreeDiscount = discountCurves.Single(curve => curve.Financing.Equals(FinancingId.RiskFree(currency))); var mkt = new AssetMarket(assetId, refDate, eqtyTime, spot, riskFreeDiscount, repoCurve, divQuotes, volMatrix); assetMkts.Add(mkt); } return(assetMkts.ToArray()); }
public void Zc(double lambda, Duration zcStart, Duration zcDuration, int quadratureNbPoints, double precision) { var sigma = new StepFunction(new[] { 0.0, 1.0, 2.0 }, new[] { 0.007, 0.004, 0.0065 }, 0.0); var refDate = DateTime.Parse("07/06/2009"); var hw1 = new Hw1Model(TimeMeasure.Act365(refDate), Currency.Eur, lambda, sigma); var hw1Zc = new Hw1ModelZcRepresentation(hw1); var zcDate = refDate + zcStart; var zc = hw1Zc.Zc(zcDate, zcDate + zcDuration, 1.0); var drift = hw1.DriftTerm(); var stdDev = Math.Sqrt(drift.Eval(hw1.Time[zcDate])); double[] x, w; GaussHermite.GetQuadrature(quadratureNbPoints, out x, out w); double zcQuad = x.Select((t, i) => w[i] * zc.Eval(new[] { stdDev *t })).Sum(); var error = Math.Abs(zcQuad - 1.0); Assert.LessOrEqual(error, precision); }
public static ITimeMeasure DefaultTime(DateTime refDate) { return(TimeMeasure.Act365(refDate)); }
private static ITimeMeasure RateTimeMeasure(DateTime refDate) { return(TimeMeasure.Act365(refDate)); }
public static DiscountCurve Flat(FinancingId financing, DateTime refDate, double zcRate) { var time = TimeMeasure.Act365(refDate); var zc1Y = Math.Exp(-zcRate * time[refDate + Duration.Year]); return LinearRateInterpol(financing, new[] { refDate + Duration.Year }, new[] { zc1Y }, time); }