public void MultiVariateNormalModel() { var testDate = new DateTime(2015, 12, 1); var graph = new SimulationGraph(StorageLocation.Host, testDate); int weightsCount = 1000; var identifiers = CreateTestWeightsProvider(graph.Context, WeightsType.Returns, weightsCount); var factor1 = graph.RegisterFactor<NormalVariates>(identifiers[0]); var factor2 = graph.RegisterFactor<NormalVariates>(identifiers[1]); var factor3 = graph.RegisterFactor<NormalVariates>(identifiers[2]); var runner = graph.ToSimulationRunner(); // prepare to simulate runner.Prepare(); var intervals = graph.Context.Settings.SimulationIntervals; // simulate to the first time point; factors will hold values used to simulate across the specified interval runner.Step(intervals.First()); var check = NMath.Correlation(factor1.Value, factor2.Value); Assert.AreEqual(Math.Round(check, 2), 0.84); }
public void MeanRevertingModel() { var testDate = new DateTime(2015, 12, 1); var graph = new SimulationGraph(StorageLocation.Host, testDate); var identifiers = CreateTestWeightsProvider(graph.Context, WeightsType.Cholesky); var factor = graph.RegisterFactor<MeanRevertingNormal>(identifiers[0]); var runner = graph.ToSimulationRunner(); // prepare to simulate runner.Prepare(); var intervals = graph.Context.Settings.SimulationIntervals; var percentiles = new double[] { 1, 10, 50, 90, 99 }; var result = new SimulationResult(percentiles.Select(p => new PercentileMeasure(p))); // simulate to the first time point; factors will hold values used to simulate across the specified interval //VectorAccelerator.Plot.PlotHelper.QuickPlot(new double[] { 1, 2, 3 }, new double[] { 1.2, 5.6, 2.3 }); while (runner.StepNext()) { var values = NMath.Percentiles(factor.Value, percentiles).ToList(); } }
public void MultiVariateNormalModel() { var testDate = new DateTime(2015, 12, 1); var graph = new SimulationGraph(StorageLocation.Host, testDate); int weightsCount = 1000; var identifiers = CreateTestWeightsProvider(graph.Context, WeightsType.Returns, weightsCount); var factor1 = graph.RegisterFactor <NormalVariates>(identifiers[0]); var factor2 = graph.RegisterFactor <NormalVariates>(identifiers[1]); var factor3 = graph.RegisterFactor <NormalVariates>(identifiers[2]); var runner = graph.ToSimulationRunner(); // prepare to simulate runner.Prepare(); var intervals = graph.Context.Settings.SimulationIntervals; // simulate to the first time point; factors will hold values used to simulate across the specified interval runner.Step(intervals.First()); var check = NMath.Correlation(factor1.Value, factor2.Value); Assert.AreEqual(Math.Round(check, 2), 0.84); }
public void MeanRevertingModel() { var testDate = new DateTime(2015, 12, 1); var graph = new SimulationGraph(StorageLocation.Host, testDate); var identifiers = CreateTestWeightsProvider(graph.Context, WeightsType.Cholesky); var factor = graph.RegisterFactor <MeanRevertingNormal>(identifiers[0]); var runner = graph.ToSimulationRunner(); // prepare to simulate runner.Prepare(); var intervals = graph.Context.Settings.SimulationIntervals; var percentiles = new double[] { 1, 10, 50, 90, 99 }; var result = new SimulationResult(percentiles.Select(p => new PercentileMeasure(p))); // simulate to the first time point; factors will hold values used to simulate across the specified interval //VectorAccelerator.Plot.PlotHelper.QuickPlot(new double[] { 1, 2, 3 }, new double[] { 1.2, 5.6, 2.3 }); while (runner.StepNext()) { var values = NMath.Percentiles(factor.Value, percentiles).ToList(); } }
/// <summary> /// Simulate state variables needed for the pricing step of the exposure calculation. State variables /// are not independent variables of the adjoint algorithmic differentation, and therefore can be simulated in advance. /// </summary> /// <param name="model"></param> /// <param name="allZeroRatesT0"></param> /// <param name="timePoints"></param> /// <param name="graph"></param> /// <param name="pricers"></param> public void SimulateModel(out LinearGaussianModel model, out IEnumerable<NArray> allZeroRatesT0, out TimePoint[] timePoints, out SimulationGraph graph, out List<IPricer> pricers) { var testDate = new DateTime(2015, 12, 1); graph = new SimulationGraph(StorageLocation.Host, testDate); var context = graph.Context; context.Settings.SimulationCount = 5000; var fixedLeg = Enumerable.Range(0, 40).Select(i => new FixedCashflowDeal() { Notional = -1e6, Rate = 0.002, Currency = Currency.EUR, StartDate = testDate.AddMonths(3 * i), EndDate = testDate.AddMonths(3 * (i + 1)) }); var floatingLeg = Enumerable.Range(0, 40).Select(i => new FloatingCashflowDeal() { Notional = 1e6, Currency = Currency.EUR, StartDate = testDate.AddMonths(3 * i), EndDate = testDate.AddMonths(3 * (i + 1)) }); var correlationMatrix = context.Factory.CreateNArray(new double[,] { { 1.0, -0.92, 0.5}, { -0.92, 1.0, -0.8}, { 0.5, -0.8, 1.0} }); var identifiers = new string[] { "IR_DiscountFactor_EUR_Factor0", "IR_DiscountFactor_EUR_Factor1", "IR_DiscountFactor_EUR_Factor2"}; CorrelationHelper.AddMultivariateModelWeightsProvider(context, identifiers, correlationMatrix); pricers = fixedLeg.Select(d => new FixedCashflowPricer(d) as IPricer) .Concat(floatingLeg.Select(d => new FloatingCashflowPricer(d) as IPricer)).ToList(); foreach (var pricer in pricers) pricer.Register(graph); var testVariates0 = graph.RegisterFactor<NormalVariates>("IR_DiscountFactor_EUR_Factor0"); var testVariates1 = graph.RegisterFactor<NormalVariates>("IR_DiscountFactor_EUR_Factor1"); var testVariates2 = graph.RegisterFactor<NormalVariates>("IR_DiscountFactor_EUR_Factor2"); var testFactor = graph.RegisterModel<MeanRevertingNormalPathModel>("IR_DiscountFactor_EUR_Factor0"); model = graph.RegisterModel<LinearGaussianModel>("EUR"); var numeraire = graph.RegisterModel<NumeraireModel>("EUR"); model.Factors[0].Sigma = 0.02; model.Factors[0].Lambda = 0.05; model.Factors[1].Sigma = 0.03; model.Factors[1].Lambda = 0.2; model.Factors[2].Sigma = 0.01; model.Factors[2].Lambda = 1.0; var years = new double[] { 0, 1/365.35, 7/365.25, 14/365.25, 1/12, 2/12, 0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60 }; var zeroRatesInPercent = new double[] { -0.3, -0.3, -0.29, -0.29, -0.3, -0.31, -0.31, -0.32, -0.32, -0.33, -0.34, -0.35, -0.35, -0.33, -0.28, -0.2, -0.11, -0.01, 0.1, 0.2, 0.37, 0.56, 0.71, 0.75, 0.76, 0.75, 0.75, 0.72, 0.7, 0.67, 0.64 }; model.ZeroRatesT0 = new Curve(years.Zip(zeroRatesInPercent, (y, r) => new DataPoint( testDate.AddDays(y * 365.25), NArray.CreateScalar(r / 100) )).ToArray()); var runner = graph.ToSimulationRunner(); runner.Prepare(); runner.Simulate(); timePoints = graph.Context.Settings.SimulationTimePoints; allZeroRatesT0 = model.ZeroRatesT0.Data.Select(d => d.Value); }
/// <summary> /// Simulate state variables needed for the pricing step of the exposure calculation. State variables /// are not independent variables of the adjoint algorithmic differentation, and therefore can be simulated in advance. /// </summary> /// <param name="model"></param> /// <param name="allZeroRatesT0"></param> /// <param name="timePoints"></param> /// <param name="graph"></param> /// <param name="pricers"></param> public void SimulateModel(out LinearGaussianModel model, out IEnumerable <NArray> allZeroRatesT0, out TimePoint[] timePoints, out SimulationGraph graph, out List <IPricer> pricers) { var testDate = new DateTime(2015, 12, 1); graph = new SimulationGraph(StorageLocation.Host, testDate); var context = graph.Context; context.Settings.SimulationCount = 5000; var fixedLeg = Enumerable.Range(0, 40).Select(i => new FixedCashflowDeal() { Notional = -1e6, Rate = 0.002, Currency = Currency.EUR, StartDate = testDate.AddMonths(3 * i), EndDate = testDate.AddMonths(3 * (i + 1)) }); var floatingLeg = Enumerable.Range(0, 40).Select(i => new FloatingCashflowDeal() { Notional = 1e6, Currency = Currency.EUR, StartDate = testDate.AddMonths(3 * i), EndDate = testDate.AddMonths(3 * (i + 1)) }); var correlationMatrix = context.Factory.CreateNArray(new double[, ] { { 1.0, -0.92, 0.5 }, { -0.92, 1.0, -0.8 }, { 0.5, -0.8, 1.0 } }); var identifiers = new string[] { "IR_DiscountFactor_EUR_Factor0", "IR_DiscountFactor_EUR_Factor1", "IR_DiscountFactor_EUR_Factor2" }; CorrelationHelper.AddMultivariateModelWeightsProvider(context, identifiers, correlationMatrix); pricers = fixedLeg.Select(d => new FixedCashflowPricer(d) as IPricer) .Concat(floatingLeg.Select(d => new FloatingCashflowPricer(d) as IPricer)).ToList(); foreach (var pricer in pricers) { pricer.Register(graph); } var testVariates0 = graph.RegisterFactor <NormalVariates>("IR_DiscountFactor_EUR_Factor0"); var testVariates1 = graph.RegisterFactor <NormalVariates>("IR_DiscountFactor_EUR_Factor1"); var testVariates2 = graph.RegisterFactor <NormalVariates>("IR_DiscountFactor_EUR_Factor2"); var testFactor = graph.RegisterModel <MeanRevertingNormalPathModel>("IR_DiscountFactor_EUR_Factor0"); model = graph.RegisterModel <LinearGaussianModel>("EUR"); var numeraire = graph.RegisterModel <NumeraireModel>("EUR"); model.Factors[0].Sigma = 0.02; model.Factors[0].Lambda = 0.05; model.Factors[1].Sigma = 0.03; model.Factors[1].Lambda = 0.2; model.Factors[2].Sigma = 0.01; model.Factors[2].Lambda = 1.0; var years = new double[] { 0, 1 / 365.35, 7 / 365.25, 14 / 365.25, 1 / 12, 2 / 12, 0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60 }; var zeroRatesInPercent = new double[] { -0.3, -0.3, -0.29, -0.29, -0.3, -0.31, -0.31, -0.32, -0.32, -0.33, -0.34, -0.35, -0.35, -0.33, -0.28, -0.2, -0.11, -0.01, 0.1, 0.2, 0.37, 0.56, 0.71, 0.75, 0.76, 0.75, 0.75, 0.72, 0.7, 0.67, 0.64 }; model.ZeroRatesT0 = new Curve(years.Zip(zeroRatesInPercent, (y, r) => new DataPoint( testDate.AddDays(y * 365.25), NArray.CreateScalar(r / 100) )).ToArray()); var runner = graph.ToSimulationRunner(); runner.Prepare(); runner.Simulate(); timePoints = graph.Context.Settings.SimulationTimePoints; allZeroRatesT0 = model.ZeroRatesT0.Data.Select(d => d.Value); }