private MultiFactorSpotSimResults <Day> SimulateForZeroVolatility()
        {
            int numSims        = 10;
            var zeroFactorVols = new Dictionary <Day, double>
            {
                { new Day(2020, 08, 01), 0.0 },
                { new Day(2021, 01, 15), 0.0 },
                { new Day(2021, 07, 30), 0.0 }
            };
            var multiFactorParameters = new MultiFactorParameters <Day>(new [, ]
            {
                { 1.0, 0.6, 0.3 },
                { 0.6, 1.0, 0.4 },
                { 0.3, 0.4, 1.0 }
            },
                                                                        new Factor <Day>(0.0, zeroFactorVols),
                                                                        new Factor <Day>(2.5, zeroFactorVols),
                                                                        new Factor <Day>(16.2, zeroFactorVols)
                                                                        );

            Day[] simulatedPeriods = _dailyForwardCurve.Keys.OrderBy(day => day).ToArray();
            var   normalSimulator  = new MersenneTwisterGenerator(_seed, true);

            var simulator = new MultiFactorSpotPriceSimulator <Day>(multiFactorParameters, _currentDate, _dailyForwardCurve,
                                                                    simulatedPeriods, TimeFunctions.Act365, normalSimulator);

            return(simulator.Simulate(numSims));
        }
        private MultiFactorSpotSimResults <Day> SimulateForTwoNonMeanRevertingFactors(bool antithetic)
        {
            int    numSims    = 100000;
            double factorCorr = 0.74;

            _twoNonMeanRevertingFactorsParams = MultiFactorParameters.For2Factors(factorCorr,
                                                                                  new Factor <Day>(0.0, new Dictionary <Day, double>
            {
                { new Day(2020, 08, 01), 0.15 },
                { new Day(2021, 01, 15), 0.12 },
                { new Day(2021, 07, 30), 0.13 }
            }),
                                                                                  new Factor <Day>(0.0, new Dictionary <Day, double>
            {
                { new Day(2020, 08, 01), 0.11 },
                { new Day(2021, 01, 15), 0.19 },
                { new Day(2021, 07, 30), 0.15 }
            })
                                                                                  );

            Day[] simulatedPeriods = _dailyForwardCurve.Keys.OrderBy(day => day).ToArray();
            var   normalSimulator  = new MersenneTwisterGenerator(_seed, antithetic);

            var simulator = new MultiFactorSpotPriceSimulator <Day>(_twoNonMeanRevertingFactorsParams, _currentDate, _dailyForwardCurve,
                                                                    simulatedPeriods, TimeFunctions.Act365, normalSimulator);

            return(simulator.Simulate(numSims));
        }
示例#3
0
    public IEnumerable <int> Generate(int min, int max, int valuesCount)
    {
        var values = new List <int>(valuesCount);

        if (valuesCount == 0)
        {
            return(values);
        }

        var rand = new MersenneTwisterGenerator();

        const int maxIterationCount = int.MaxValue;

        for (int i = 0; i < maxIterationCount && values.Count < valuesCount; ++i)
        {
            var val = (int)rand.Next((ulong)min, (ulong)max);
            if (!values.Contains(val))
            {
                values.Add(val);
            }
        }

        if (values.Count != valuesCount)
        {
            throw new Exception("Failed to generate values");
        }

        return(values);
    }
        private MultiFactorSpotSimResults <Day> SimulateForSingleNonMeanRevertingFactor(bool antithetic)
        {
            int    numSims       = 1000000;
            double meanReversion = 0.0;
            var    spotVols      = new Dictionary <Day, double>
            {
                { new Day(2020, 08, 01), 0.45 },
                { new Day(2021, 01, 15), 0.42 },
                { new Day(2021, 07, 30), 0.33 }
            };

            _singleNonMeanRevertingFactorParams = MultiFactorParameters.For1Factor(meanReversion, spotVols);

            Day[] simulatedPeriods = _dailyForwardCurve.Keys.OrderBy(day => day).ToArray();
            var   normalSimulator  = new MersenneTwisterGenerator(_seed, antithetic);

            var simulator = new MultiFactorSpotPriceSimulator <Day>(_singleNonMeanRevertingFactorParams, _currentDate, _dailyForwardCurve,
                                                                    simulatedPeriods, TimeFunctions.Act365, normalSimulator);

            return(simulator.Simulate(numSims));
        }
        private MultiFactorSpotSimResults <Day> SimulateForMeanAndNonMeanRevertingFactors(bool antithetic)
        {
            int numSims = 100000;

            _meanAndNonMeanRevertingFactorsParams = new MultiFactorParameters <Day>(new[, ]
            {
                { 1.0, 0.6, 0.3 },
                { 0.6, 1.0, 0.4 },
                { 0.3, 0.4, 1.0 }
            },
                                                                                    new Factor <Day>(0.0, new Dictionary <Day, double>
            {
                { new Day(2020, 08, 01), 0.35 },
                { new Day(2021, 01, 15), 0.29 },
                { new Day(2021, 07, 30), 0.32 }
            }),
                                                                                    new Factor <Day>(2.5, new Dictionary <Day, double>
            {
                { new Day(2020, 08, 01), 0.15 },
                { new Day(2021, 01, 15), 0.18 },
                { new Day(2021, 07, 30), 0.21 }
            }),
                                                                                    new Factor <Day>(16.2, new Dictionary <Day, double>
            {
                { new Day(2020, 08, 01), 0.95 },
                { new Day(2021, 01, 15), 0.92 },
                { new Day(2021, 07, 30), 0.89 }
            })
                                                                                    );

            Day[] simulatedPeriods = _dailyForwardCurve.Keys.OrderBy(day => day).ToArray();
            var   normalSimulator  = new MersenneTwisterGenerator(_seed, antithetic);

            var simulator = new MultiFactorSpotPriceSimulator <Day>(_meanAndNonMeanRevertingFactorsParams, _currentDate, _dailyForwardCurve,
                                                                    simulatedPeriods, TimeFunctions.Act365, normalSimulator);

            return(simulator.Simulate(numSims));
        }
示例#6
0
        public void Generate_Antithetic_GeneratedMeanEqualsInputZero()
        {
            const int numSims        = 26;
            const int randomArrayLen = 10;
            var       randoms        = new double[numSims][];
            var       mtGen          = new MersenneTwisterGenerator(true);

            for (int i = 0; i < numSims; i++)
            {
                randoms[i] = new double[randomArrayLen];
                mtGen.Generate(randoms[i]);
            }

            for (int i = 0; i < randomArrayLen; i++)
            {
                double sum = 0.0;
                for (int j = 0; j < numSims; j++)
                {
                    sum += randoms[j][i];
                }
                double sampleMean = sum / numSims;
                Assert.AreEqual(0, sampleMean);
            }
        }
示例#7
0
        static void Main(string[] args)
        {
            var _seed        = 12;
            var _currentDate = new DateTime(2020, 07, 27);

            var _dailyForwardCurve = new Dictionary <Day, double>
            {
                { new Day(2020, 08, 01), 56.85 },
                { new Day(2021, 01, 15), 59.08 },
                { new Day(2021, 07, 30), 62.453 }
            };

            int    numSims       = 1000;
            double meanReversion = 0.0;
            bool   antithetic    = true;

            var spotVols = new Dictionary <Day, double>
            {
                { new Day(2020, 08, 01), 0.45 },
                { new Day(2021, 01, 15), 0.42 },
                { new Day(2021, 07, 30), 0.33 }
            };

            var _singleNonMeanRevertingFactorParams = MultiFactorParameters.For1Factor(meanReversion, spotVols);

            Day[] simulatedPeriods = _dailyForwardCurve.Keys.OrderBy(day => day).ToArray();
            var   normalSimulator  = new MersenneTwisterGenerator(_seed, antithetic);

            var simulator = new MultiFactorSpotPriceSimulator <Day>(
                _singleNonMeanRevertingFactorParams,
                _currentDate,
                _dailyForwardCurve,
                simulatedPeriods,
                TimeFunctions.Act365,
                normalSimulator
                );

            var simResults = simulator.Simulate(numSims);

            ReadOnlyMemory <double> simulatedSpotPrices0 =
                simResults.SpotPricesForStepIndex(0);

            ReadOnlyMemory <double> simulatedSpotPrices1 =
                simResults.SpotPricesForStepIndex(1);

            ReadOnlyMemory <double> simulatedSpotPrices2 =
                simResults.SpotPricesForStepIndex(2);

            using (StreamWriter file = new StreamWriter("sims.csv"))
            {
                foreach (var i in new [] { 0, 1, 2 })
                {
                    ReadOnlyMemory <double> simulatedSpotPrices = simResults.SpotPricesForStepIndex(i);
                    foreach (var item in simulatedSpotPrices.ToArray())
                    {
                        file.Write(item + ",");
                    }
                    file.Write(Environment.NewLine);
                }
            }

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }