public void TestEquitySimulatorForwardAndVol()
        {
            var N           = 100000;
            var sharePrices = Matrix.Zeros(N, 3);
            var sim         = new EquitySimulator(shares, prices, vols, divYields, correlations, discountCurve,
                                                  rateForecastCurves);

            sim.Reset();
            var simDate = new List <Date> {
                anchorDate.AddMonths(120)
            };
            double dt = (simDate[0] - anchorDate) / 365;

            sim.SetRequiredDates(shares[0], simDate);
            sim.Prepare();
            for (var i = 0; i < N; i++)
            {
                sim.RunSimulation(i);
                sharePrices[i, 0] = sim.GetIndices(shares[0], simDate)[0];
                sharePrices[i, 1] = sim.GetIndices(shares[1], simDate)[0];
                sharePrices[i, 2] = sim.GetIndices(shares[2], simDate)[0];
            }

            var mean     = sharePrices.GetColumn(0).Mean();
            var refValue = prices[0] * Math.Exp(-divYields[0] * dt) / discountCurve.GetDF(simDate[0]);

            Assert.AreEqual(refValue, mean, 2.0);

            var corr = sharePrices.Log().Correlation();

            Assert.AreEqual(corr[1, 0], 0.4, 0.05);
            Assert.AreEqual(corr[2, 0], 0.5, 0.05);
            Assert.AreEqual(corr[2, 1], 0.6, 0.05);
        }
        public void TestProductWrapperEquityValuation()
        {
            Product product1 = new ProductWrapperEquitySample1();
            Product product2 = new ProductWrapperEquitySample2();

            // The model
            Date anchorDate = new Date(2016, 09, 30);

            Share[]  shares    = new Share[] { new Share("ALSI", Currency.ZAR), new Share("AAA", Currency.ZAR), new Share("BBB", Currency.ZAR) };
            double[] prices    = { 200, 50, 100 };
            double[] vols      = { 0.22, 0.52, 0.4 };
            double[] divYields = { 0.03, 0.0, 0.0 };
            double[,] correlations = { { 1.0, 0.5, 0.5 },
                                       { 0.5, 1.0, 0.5 },
                                       { 0.5, 0.5, 1.0 } };
            IDiscountingSource discountCurve = new DatesAndRates(Currency.ZAR, anchorDate,
                                                                 new Date[] { anchorDate, anchorDate.AddMonths(36) },
                                                                 new double[] { 0.07, 0.07 });
            EquitySimulator sim         = new EquitySimulator(shares, prices, vols, divYields, correlations, discountCurve, new IFloatingRateSource[0]);
            Coordinator     coordinator = new Coordinator(sim, new List <Simulator> {
            }, 40000);

            //Valuation
            double value1 = coordinator.Value(new Product[] { product1 }, anchorDate);
            double value2 = coordinator.Value(new Product[] { product2 }, anchorDate);

            Assert.AreEqual(value1, value2, value1 * 0.05);
        }
        public void TestValuationCoordinator()
        {
            var     exerciseDate = new Date(2017, 08, 28);
            var     shareCode    = "AAA";
            var     strike       = 100.0;
            Product p            = new EuropeanOption(new Share(shareCode, Currency.ZAR), strike, exerciseDate);

            var shares = new[]
            {
                new Share(shareCode, Currency.ZAR)
            }; // One needs to know the index that will be required by the product to simulate it.
            var valueDate    = new Date(2016, 08, 28);
            var divYield     = new[] { 0.02 };
            var vol          = new[] { 0.22 };
            var spotPrice    = new[] { 100.0 };
            var correlations = new[, ] {
                { 1.0 }
            };
            IDiscountingSource discountCurve = new DatesAndRates(Currency.ZAR, valueDate,
                                                                 new[] { valueDate, valueDate.AddMonths(120) },
                                                                 new[] { 0.07, 0.07 });
            var rateForecastCurves = new IFloatingRateSource[0];

            var sim = new EquitySimulator(shares, spotPrice, vol, divYield,
                                          correlations, discountCurve, rateForecastCurves);

            var coordinator = new Coordinator(sim, new List <Simulator>(), 10000);
            var value       = coordinator.Value(new[] { p }, valueDate);
            var refValue    = BlackEtc.BlackScholes(PutOrCall.Call, strike, (exerciseDate - valueDate) / 365.0,
                                                    spotPrice[0],
                                                    vol[0], 0.07, divYield[0]);

            Assert.AreEqual(refValue, value, refValue * 0.05);
        }
        public void TestIndexOnlyProvidedOnce()
        {
            var     exerciseDate = new Date(2017, 08, 28);
            var     shareCode    = "AAA";
            var     strike       = 100.0;
            Product p            = new EuropeanOption(new Share(shareCode, Currency.ZAR), strike, exerciseDate);

            var shares = new[]
            {
                new Share("AAA", Currency.ZAR)
            }; // One needs to know the index that will be required by the product to simulate it.
            var valueDate    = new Date(2016, 08, 28);
            var divYield     = new[] { 0.02 };
            var vol          = new[] { 0.22 };
            var spotPrice    = new[] { 100.0 };
            var correlations = new[, ] {
                { 1.0 }
            };
            IDiscountingSource discountCurve = new DatesAndRates(Currency.ZAR, valueDate,
                                                                 new[] { valueDate, valueDate.AddMonths(120) },
                                                                 new[] { 0.07, 0.07 });
            var rateForecastCurves = new IFloatingRateSource[0];

            var sim = new EquitySimulator(shares, spotPrice, vol, divYield,
                                          correlations, discountCurve, rateForecastCurves);

            var coordinator = new Coordinator(sim, new List <Simulator> {
                sim
            }, 1000);

            var value = coordinator.Value(new[] { p }, valueDate);
        }
        public void TestDynamicCallFromFile()
        {
            Stopwatch watch;
            // Make a product at runtime
            var runtimeProduct = RuntimeProduct.CreateFromScript(@"ScriptEuropeanOption.txt");

            // Setup an appropriate simulation
            var shares = new[]
            {
                new Share("AAA", TestHelpers.ZAR)
            }; // One needs to know the index that will be required by the product to simulate it.
            var valueDate = new Date(2016, 08, 28);

            var divYield     = new[] { 0.02 };
            var vol          = new[] { 0.22 };
            var spotPrice    = new[] { 100.0 };
            var correlations = new[, ] {
                { 1.0 }
            };
            IDiscountingSource discountCurve = new DatesAndRates(TestHelpers.ZAR, valueDate,
                                                                 new[] { valueDate, valueDate.AddMonths(120) },
                                                                 new[] { 0.07, 0.07 });
            var rateForecastCurves = new IFloatingRateSource[0];

            var sim = new EquitySimulator(shares, spotPrice, vol, divYield,
                                          correlations, discountCurve, rateForecastCurves);

            // Value the runtime product
            Coordinator coordinator;

            coordinator = new Coordinator(sim, new List <Simulator>(), 100000);
            watch       = Stopwatch.StartNew();
            var valueRuntime = coordinator.Value(new[] { runtimeProduct }, valueDate);

            watch.Stop();
            var timeRuntime = watch.ElapsedMilliseconds;

            // Setup the same product statically
            var     exerciseDate  = new Date(2017, 08, 28);
            var     strike        = 100.0;
            Product staticProduct = new EuropeanOption(new Share("AAA", TestHelpers.ZAR), strike, exerciseDate);

            // Value the static product
            coordinator = new Coordinator(sim, new List <Simulator>(), 100000);
            watch       = Stopwatch.StartNew();
            var valueStatic = coordinator.Value(new[] { staticProduct }, valueDate);

            watch.Stop();
            var timeStatic = watch.ElapsedMilliseconds;

            var refValue = BlackEtc.BlackScholes(PutOrCall.Call, strike, (exerciseDate - valueDate) / 365, spotPrice[0],
                                                 vol[0], 0.07, divYield[0]);

            Assert.AreEqual(refValue, valueRuntime, refValue * 0.03);
            Assert.AreEqual(refValue, valueStatic, refValue * 0.03);
        }
        public void EquitySimualtor_CanCloneViaSerialize()
        {
            var share = new Share("ABC", TestHelpers.ZAR);
            var model = new EquitySimulator(new[] { share }, new[] { 100.0 }, new[] { 0.2 }, new[] { 0.0 },
                                            new[, ] {
                { 1.0 }
            }, TestHelpers.FlatDiscountCurve(), null);

            var newModel = (EquitySimulator)Cloner.Clone(model);
        }
        public void TestEquitySimulatorProductWithRateForecast()
        {
            Product product = new ProductWithDiviAndFwd();
            // The model
            var sim = new EquitySimulator(shares, prices, vols, divYields, correlations, discountCurve,
                                          rateForecastCurves);

            var coordinator = new Coordinator(sim, new List <Simulator>(), 10000);
            var value       = coordinator.Value(new[] { product }, anchorDate);

            Assert.AreEqual(31.6, value, 1.0);
        }
        public void TestEquitySimulatorMultiAssetCall()
        {
            // The model
            var sim = new EquitySimulator(shares, prices, vols, divYields, correlations, discountCurve,
                                          new IFloatingRateSource[0]);
            var coordinator = new Coordinator(sim, new List <Simulator>(), 10000);

            // Products
            var    exerciseDate = new Date(2017, 08, 28);
            int    p;
            double strike;

            // ALSI
            p      = 0;
            strike = prices[p] * 1.05;
            Product call0     = new EuropeanOption(shares[p], strike, exerciseDate);
            var     value0    = coordinator.Value(new[] { call0 }, anchorDate);
            var     refValue0 = BlackEtc.BlackScholes(PutOrCall.Call, strike, (exerciseDate - anchorDate) / 365.0,
                                                      prices[p],
                                                      vols[p], 0.07, divYields[p]);

            Assert.AreEqual(refValue0, value0, refValue0 * 0.05);

            // AAA
            p      = 1;
            strike = prices[p] * 1.05;
            Product call1     = new EuropeanOption(shares[p], strike, exerciseDate);
            var     value1    = coordinator.Value(new[] { call1 }, anchorDate);
            var     refValue1 = BlackEtc.BlackScholes(PutOrCall.Call, strike, (exerciseDate - anchorDate) / 365.0,
                                                      prices[p],
                                                      vols[p], 0.07, divYields[p]);

            Assert.AreEqual(refValue1, value1, refValue1 * 0.05);

            // BBB
            p      = 2;
            strike = prices[p] * 1.05;
            Product call2     = new EuropeanOption(shares[p], strike, exerciseDate);
            var     value2    = coordinator.Value(new[] { call2 }, anchorDate);
            var     refValue2 = BlackEtc.BlackScholes(PutOrCall.Call, strike, (exerciseDate - anchorDate) / 365.0,
                                                      prices[p],
                                                      vols[p], 0.07, divYields[p]);

            Assert.AreEqual(refValue1, value1, refValue1 * 0.05);

            // All at once
            var valueAll = coordinator.Value(new[] { call0, call1, call2 }, anchorDate);
            var refTotal = refValue0 + refValue1 + refValue2;

            Assert.AreEqual(refTotal, valueAll, refTotal * 0.05);
        }
        public void TestDynamicCallFromString()
        {
            var source =
                @"Date exerciseDate = new Date(2017, 08, 28);
Share share = new Share(""AAA"", new Currency(""ZAR""));
double strike = 100.0;

public override List<Cashflow> GetCFs()
{
    double amount = Math.Max(0, Get(share, exerciseDate) - strike);
    return new List<Cashflow> { new Cashflow(exerciseDate, amount, share.Currency) };
}";
            // Make a product at runtime
            var runtimeProduct = RuntimeProduct.CreateFromString("MyEuropeanOption", source);

            // Setup an approriate simulation
            var shares = new[]
            {
                new Share("AAA", TestHelpers.ZAR)
            }; // One needs to know the index that will be required by the product to simulate it.
            var valueDate = new Date(2016, 08, 28);

            var divYield     = new[] { 0.02 };
            var vol          = new[] { 0.22 };
            var spotPrice    = new[] { 100.0 };
            var correlations = new[, ] {
                { 1.0 }
            };
            IDiscountingSource discountCurve = new DatesAndRates(TestHelpers.ZAR, valueDate,
                                                                 new[] { valueDate, valueDate.AddMonths(120) },
                                                                 new[] { 0.07, 0.07 });
            var rateForecastCurves = new IFloatingRateSource[0];

            var sim = new EquitySimulator(shares, spotPrice, vol, divYield,
                                          correlations, discountCurve, rateForecastCurves);

            // Value the runtime product
            Coordinator coordinator;

            coordinator = new Coordinator(sim, new List <Simulator>(), 100000);
            var valueRuntime = coordinator.Value(new[] { runtimeProduct }, valueDate);

            var exerciseDate = new Date(2017, 08, 28);
            var strike       = 100.0;
            var refValue     = BlackEtc.BlackScholes(PutOrCall.Call, strike, (exerciseDate - valueDate) / 365, spotPrice[0],
                                                     vol[0], 0.07, divYield[0]);

            Assert.AreEqual(refValue, valueRuntime, refValue * 0.03);
        }
        public void TestEquitySimulatorDivis()
        {
            EquitySimulator sim   = new EquitySimulator(shares, prices, vols, divYields, correlations, discountCurve, rateForecastCurves);
            List <Date>     dates = new List <Date> {
                anchorDate, anchorDate.AddMonths(6), anchorDate.AddMonths(12)
            };
            Dividend divi = new Dividend(shares[0]);

            sim.Reset();
            sim.SetRequiredDates(divi, dates);
            sim.SetRequiredDates(shares[0], dates);
            sim.Prepare();
            sim.RunSimulation(0);
            double[] divs        = sim.GetIndices(divi, dates);
            double[] shareprices = sim.GetIndices(shares[0], dates);
            double[] fwdRates    = sim.GetIndices(FloatingIndex.JIBAR3M, dates);
            Assert.AreEqual(shareprices[1] * 184.0 / 365 * 0.03, divs[2], 0.01);
            Assert.AreEqual(rateForecastCurves[0].GetForwardRate(dates[1]), fwdRates[1], 0.0001);
        }
Exemple #11
0
        public void TestEquitySimulatorDivis()
        {
            var sim = new EquitySimulator(_shares, _prices, _vols, _divYields, _correlations, _discountCurve,
                                          _rateForecastCurves);
            var dates = new List <Date> {
                _anchorDate, _anchorDate.AddMonths(6), _anchorDate.AddMonths(12)
            };
            var divi = new Dividend(_shares[0]);

            sim.Reset();
            sim.SetRequiredDates(divi, dates);
            sim.SetRequiredDates(_shares[0], dates);
            sim.Prepare(_anchorDate);
            sim.RunSimulation(0);
            var divs        = sim.GetIndices(divi, dates);
            var shareprices = sim.GetIndices(_shares[0], dates);
            var fwdRates    = sim.GetIndices(TestHelpers.Jibar3M, dates);

            Assert.AreEqual(shareprices[1] * 184.0 / 365 * 0.03, divs[2], 0.01);
            Assert.AreEqual(_rateForecastCurves[0].GetForwardRate(dates[1]), fwdRates[1], 0.0001);
        }