Esempio n. 1
0
        public override void Initialise(SimulationGraph graph)
        {
            base.Initialise(graph);

            var factorIdentifiers = GetFactorIdentifiers().ToArray();

            _factorPaths = GetFactorIdentifiers().Select(id =>
                                                         graph.RegisterModel <MeanRevertingNormalPathModel>(id))
                           .ToArray();

            _factors = _factorPaths.Select(p => p.Process).ToArray();

            var weightsProvider = graph.Context.Data.CalibrationParametersProviderOfType <WeightsProvider>();

            _factorCorrelation = new double[factorCount, factorCount];

            for (int i = 0; i < factorCount; ++i)
            {
                for (int j = 0; j < factorCount; ++j)
                {
                    _factorCorrelation[i, j] = NMath.Dot(
                        weightsProvider.Value(factorIdentifiers[i]),
                        weightsProvider.Value(factorIdentifiers[j]));
                }
            }
        }
        public override void Initialise(SimulationGraph graph)
        {
            base.Initialise(graph);

            var factorIdentifiers = GetFactorIdentifiers().ToArray();

            _factorPaths = GetFactorIdentifiers().Select(id =>
                graph.RegisterModel<MeanRevertingNormalPathModel>(id))
                .ToArray();

            _factors = _factorPaths.Select(p => p.Process).ToArray();

            var weightsProvider = graph.Context.Data.CalibrationParametersProviderOfType<WeightsProvider>();

            _factorCorrelation = new double[factorCount, factorCount];

            for (int i = 0; i < factorCount; ++i)
            {
                for (int j = 0; j < factorCount; ++j)
                {
                    _factorCorrelation[i, j] = NMath.Dot(
                        weightsProvider.Value(factorIdentifiers[i]),
                        weightsProvider.Value(factorIdentifiers[j]));
                }
            }
        }
        public override void Initialise(SimulationGraph graph)
        {
            // we get the (single) instance of the MultiVariateNormalModel
            var model = graph.RegisterModel <MultiVariateNormalModel>("General");

            // and store the factor
            _factor = model.AddFactor(Identifier, graph);
        }
        public override void Initialise(SimulationGraph graph)
        {
            base.Initialise(graph);

            _state = new NArray[graph.Context.Settings.SimulationTimePoints.Length];

            _weinerPaths = GetFactorIdentifiers().Select(id =>
                                                         graph.RegisterModel <WienerPathModel>(id))
                           .ToArray();
        }
        public override void Initialise(SimulationGraph graph)
        {
            base.Initialise(graph);

            _state = new NArray[graph.Context.Settings.SimulationTimePoints.Length];

            _weinerPaths = GetFactorIdentifiers().Select(id =>
                graph.RegisterModel<WienerPathModel>(id))
                .ToArray();
        }
        /// <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);
        }