public void BlackScholesPerformance()
        {
            double k = 90;
            var volatility = 0.2;
            var t = 5;

            var s = new List<NArray>();
            int batchCount = 1000;
            int vectorLength = 5000;

            using (var stream = new RandomNumberStream(StorageLocation.Host, RandomNumberGeneratorType.MRG32K3A))
            {
                var normal = new Normal(stream, 0, 1);
                for (int i = 0; i < batchCount; ++i)
                {
                    s.Add(100 * NMath.Exp(NArray.CreateRandom(vectorLength, normal) * 0.2));
                }
            }

            var result = NArray.CreateLike(s.First());

            double elapsedTime = TestHelpers.TimeitSeconds(() =>
            {
                for (int i = 0; i < batchCount; ++i)
                {
                    NArray.Evaluate(() =>
                    {
                        return Finance.BlackScholes(CallPut.Call, s[i], k, volatility, t);
                    }, new List<NArray>(), Aggregator.ElementwiseAdd, new List<NArray> { result });
                }
            });

            Console.WriteLine(string.Format("Time per option price (single core): {0} ns", elapsedTime * 1e9 / (batchCount * vectorLength)));
            Console.WriteLine(string.Format("Valuations per second (single core): {0:F0} million", batchCount * vectorLength / (elapsedTime * 1e6)));
        }
        public void TestRandomNumberGeneration()
        {
            var location = StorageLocation.Host;

            // We create the stream.
            // This identifies the stream and stores the state or pointer to the state (in the case
            // of IntelMKL, CUDA, etc).
            using (var randomStream = new RandomNumberStream(location, RandomNumberGeneratorType.MRG32K3A, 111))
            {
                // We then create a distribution based on the stream.
                // This stores only specific parameters of the distribution (i.e. mean and standard deviation).
                // The purpose of the design is to push scaling and offsetting of random numbers to the Provider,
                // which can do this efficiently whilst generating.
                var normal = new Normal(randomStream, 0, 1);

                var a = new NArray(location, 1000);
                var b = new NArray(location, 1000);

                // When we call FillRandom, we need to use a Provider that is appropriate to the stream.
                a.FillRandom(normal);
                b.FillRandom(normal);

                var v = a * b;
            }
        }
        public void BlackScholes()
        {
            var simpleCheck = Math.Exp(-0.1 * 0.5) * Finance.BlackScholes(
                CallPut.Call, 42 * Math.Exp(0.1 * 0.5), 40, 0.2, 0.5);

            Assert.IsTrue(TestHelpers.AgreesAbsolute(simpleCheck, NArray.CreateScalar(4.75942239)));

            NArray s;
            using (var stream = new RandomNumberStream(StorageLocation.Host, RandomNumberGeneratorType.MRG32K3A))
            {
                var normal = new Normal(stream, 0, 1);
                s = 100 * NMath.Exp(NArray.CreateRandom(5, normal) * 0.2);
            }

            double k = 90;
            var volatility = 0.2;
            var t = 5;
            var df = Math.Exp(-0.005 * t);

            var result = NArray.Evaluate(() =>
            {
                var f = s / df;
                return df * Finance.BlackScholes(CallPut.Call, f, k, volatility, t);
            }, s);

            var sds = s + 1e-6;
            var check = (df * Finance.BlackScholes(CallPut.Call, sds / df, k, volatility, t)
                - df * Finance.BlackScholes(CallPut.Call, s / df, k, volatility, t)) * 1e6;

            Assert.IsTrue(TestHelpers.AgreesAbsolute(result[1], check));
        }
        public void SimpleSpeedTest()
        {
            using (var randomStream = new RandomNumberStream(StorageLocation.Host, RandomNumberGeneratorType.MRG32K3A, 111))
            {
                var normalDistribution = new Normal(randomStream, 0, 1);

                var variates = NArray.CreateRandom(5000, normalDistribution);
                var variatesArray = GetArray(variates);

                var variates2 = NArray.CreateRandom(5000, normalDistribution);
                var variatesArray2 = GetArray(variates);

                var target = NArray.CreateRandom(5000, normalDistribution);

                var targetArray = GetArray(target);

                TestHelpers.Timeit(() =>
                {
                    for (int i = 0; i < 1000; ++i)
                    {
                        //IntelMathKernelLibrary.Multiply(variatesArray, 0, variatesArray2, 0, targetArray, 0, 5000);
                        //IntelMathKernelLibrary.Exp(variatesArray, 0, targetArray, 0, variatesArray.Length);
                        IntelMathKernelLibrary.ConstantAddMultiply(variatesArray, 0, 5, 0, targetArray, 0, 5000);
                    }
                });
            }
        }
        public void CheckExponentialPerformance()
        {
            IntelMathKernelLibrary.SetSequential();

            var location = StorageLocation.Host;

            using (var randomStream = new RandomNumberStream(location, RandomNumberGeneratorType.MRG32K3A, 111))
            {
                var normalDistribution = new Normal(randomStream, 0, 1);

                var a = new NArray(location, 5000);
                var b = new NArray(location, 5000);
                a.FillRandom(normalDistribution);

                var aArray = GetArray(a);
                var bArray = GetArray(b);

                var watch = new Stopwatch(); watch.Start();

                for (int i = 0; i < 1000; ++i)
                {
                    IntelMathKernelLibrary.Exp(aArray, 0, bArray, 0, 5000);
                }

                var baseline = watch.ElapsedMilliseconds;
                Console.WriteLine("5 millions in place Exps");
                Console.WriteLine(baseline);
            }
        }
        public void SimpleDeferredOperationsTest()
        {
            var location = StorageLocation.Host;
            var factory = new NArrayFactory(location);

            using (var randomStream =
                new RandomNumberStream(location, RandomNumberGeneratorType.MRG32K3A, 111))
            {
                var normalDistribution = new Normal(randomStream, 0, 1);

                var epsilon = factory.CreateNArray(1000, 1);
                epsilon.FillRandom(normalDistribution);

                var x = epsilon * Math.Sqrt(0.25) * 0.2;
            }
        }
Example #7
0
        public void CreateInputs()
        {
            var location = StorageLocation.Host;

            using (var randomStream = new RandomNumberStream(location, RandomNumberGeneratorType.MRG32K3A, 111))
            {
                var normalDistribution = new Normal(randomStream, 0, 1);

                _a = new NArray(location, 5000);
                _a.FillRandom(normalDistribution);
                _a_array = GetArray(_a);

                _b = new NArray(location, 5000);
                _b.FillRandom(normalDistribution);
                _b_array = GetArray(_b);

                _c = new NArray(location, 5000);
                _c.FillRandom(normalDistribution);
                _c_array = GetArray(_c);
            }
        }
        public void OptionPricingTest()
        {
            var location = StorageLocation.Host;

            var a = new NArray(location, Enumerable.Range(0, 10).Select(i => (double)i).ToArray());
            var b = new NArray(location, Enumerable.Range(0, 10).Select(i => (double)i * 2).ToArray());
            var c = 5 - b;
            var check = c.First();
            IntelMathKernelLibrary.SetAccuracyMode(VMLAccuracy.LowAccuracy);
            IntelMathKernelLibrary.SetSequential();
            using (var randomStream = new RandomNumberStream(location, RandomNumberGeneratorType.MRG32K3A, 111))
            {
                var normalDistribution = new Normal(randomStream, 0, 1);

                var vectorOptions = new VectorExecutionOptions() { MultipleThreads = true };

                var watch = new Stopwatch();

                watch.Start();
                var optionPrices = Value(normalDistribution);
                Console.WriteLine(String.Format("Start-up: {0}ms", watch.ElapsedMilliseconds));

                randomStream.Reset();

                watch.Restart();
                optionPrices = Value(normalDistribution);
                Console.WriteLine(String.Format("Threaded, deferred 1: {0}ms", watch.ElapsedMilliseconds));

                randomStream.Reset();

                watch.Restart();
                var optionPricesDeferred = Value(normalDistribution);
                Console.WriteLine(String.Format("Threaded, deferred 2: {0}ms", watch.ElapsedMilliseconds)); watch.Restart();

                Console.WriteLine(TestHelpers.AgreesAbsoluteString(optionPrices, optionPricesDeferred));
            }
        }
 private NArray CalculateSyntheticReturns(NArray correlationMatrix, int returnsCount)
 {
     var location = StorageLocation.Host;
     var cholesky = NMath.CholeskyDecomposition(correlationMatrix);
     var variates = new NArray(location, returnsCount, correlationMatrix.RowCount);
     using (var stream = new RandomNumberStream(location))
     {
         var normal = new Normal(stream, 0, 1);
         variates.FillRandom(normal);
     }
     return variates * cholesky.Transpose();
 }
        public void Example1()
        {
            var location = StorageLocation.Host;

            var watch = new System.Diagnostics.Stopwatch(); watch.Start();
            using (var randomStream = new RandomNumberStream(location, RandomNumberGeneratorType.MRG32K3A, 111))
            {
                var normal = new Normal(randomStream, 0, 1);
                var test = NArray.CreateRandom(1000, normal);
                for (int i = 0; i < 363 * 100; ++i)
                {
                    test.FillRandom(normal);
                }
            }
            watch.Stop();
            var elapsed = watch.ElapsedMilliseconds * 5000 / (100 * 1000);

            NArray a, b;
            using (var randomStream = new RandomNumberStream(location, RandomNumberGeneratorType.MRG32K3A, 111))
            {
                var normal = new Normal(randomStream, 0, 1);
                a = NArray.CreateRandom(5000, normal);
                b = NArray.CreateRandom(5000, normal);
            }

            var expressions = new StringBuilder();
            var result = NArray.Evaluate(() =>
            {
                return a * b + a * NMath.Exp(b);
            }, expressions);

            var expressionsDiff = new StringBuilder();
            var resultDiff = NArray.Evaluate(() =>
            {
                return a * b + a * NMath.Exp(2 * b);
            }, expressionsDiff, a, b);

            var output = expressionsDiff.ToString();

            var expressionsDiff2 = new StringBuilder();

            var s = NMath.Exp(a) + 5;

            var resultDiff2 = NArray.Evaluate(() =>
            {
                return Finance.BlackScholes(CallPut.Call, s, 5, 1, 1);
                //return NMath.Exp(NMath.Sqrt(a));
            }, expressionsDiff2, s);

            var check = (Finance.BlackScholes(CallPut.Call, s + 1e-6, 5, 1, 1) - Finance.BlackScholes(CallPut.Call, s, 5, 1, 1)) / 1e-6;
            var expected = check.DebugDataView.Take(10).ToArray();
            var obtained = resultDiff2[1].DebugDataView.Take(10).ToArray();

            var output2 = expressionsDiff2.ToString();
            Console.Write(output2);

            VectorAccelerator.Tests.TestHelpers.Timeit(() =>
            {
                var resultTiming = NArray.Evaluate(() =>
                {
                    return Finance.BlackScholes(CallPut.Call, s, 5, 1, 1);
                }, expressionsDiff2);
            });
        }
        private NArray Value(Normal normalDistribution)
        {
            double deltaT = 1;
            double r = 0.1;
            double vol = 0.3;

            int vectorLength = 5000;
            var optionPrices = new NArray(StorageLocation.Host, vectorLength);

            var variates = NArray.CreateRandom(optionPrices.Length, normalDistribution);

            var logStockPrice = Math.Log(100)
                + variates * Math.Sqrt(deltaT) * vol + (r - 0.5 * vol * vol) * deltaT;

            var forwardStockPrices = NMath.Exp(logStockPrice + r * deltaT);

            // now create deals
            var strikes = Enumerable.Range(0, 1000).Select(i => 80 + 5.0 / 1000).ToArray();
            var deals = strikes.Select(s =>
                new Deal() { Strike = s, ForwardStockPrices = (i) => { return forwardStockPrices; } })
                .ToList();

            return AggregateValuations(deals, vectorLength);
        }
        public void WorkedExample()
        {
            NArray x0, x1;
            using (var stream = new RandomNumberStream(StorageLocation.Host, RandomNumberGeneratorType.MRG32K3A))
            {
                var normal = new Normal(stream, 0, 1);
                x0 = NArray.CreateRandom(5000, normal);
                x1 = NArray.CreateRandom(5000, normal);
            }

            var result = NArray.Evaluate(() =>
            {
                return x0 * x1 + x0 * NMath.Exp(2 * x1);
            }, x0, x1);

            var derivativeWRTx0 = result[1];
            var derivativeWRTx1 = result[2];

            Assert.IsTrue(TestHelpers.AgreesAbsolute(derivativeWRTx0, x1 + NMath.Exp(2 * x1)));
            Assert.IsTrue(TestHelpers.AgreesAbsolute(derivativeWRTx1, x0 + 2 * x0 * NMath.Exp(2 * x1)));

            // can also call in such a way that the expression list is appended to a StringBuilder:
            var logger = new StringBuilder();
            var loggedResult = NArray.Evaluate(() =>
            {
                return x0 * x1 + x0 * NMath.Exp(2 * x1);
            }, logger, x0, x1);

            Console.WriteLine(logger.ToString());
        }
 public void CUDA()
 {
     var location = StorageLocation.Host;
     var normal = new Normal(new RandomNumberStream(location), 0, 1);
     var input = NArray.CreateRandom(1000, normal);
     var result = NArray.CreateLike(input);
     NArray.Evaluate(() =>
     {
         return NMath.Exp(input * 0.2 + 6);
     });
 }
        public void TestBlackScholes()
        {
            var location = StorageLocation.Host;

            using (var randomStream = new RandomNumberStream(location, RandomNumberGeneratorType.MRG32K3A, 111))
            {
                var normalDistribution = new Normal(randomStream, 0, 1);

                double deltaT = 1;
                double r = 0.1;
                double vol = 0.3;

                var options = new ParallelOptions();
                options.MaxDegreeOfParallelism = 1;

                var variates = NArray.CreateRandom(5000, normalDistribution);
                NArray optionPrices = NArray.CreateLike(variates);
                var watch = new Stopwatch(); watch.Start();

                Parallel.For(0, 1000, options, (i) =>
                {
                    optionPrices = NArray.CreateLike(variates);
                    var logStockPrice = Math.Log(100)
                        + variates * Math.Sqrt(deltaT) * vol + (r - 0.5 * vol * vol) * deltaT;
                    var stockPrices = NMath.Exp(logStockPrice);

                    optionPrices.Assign(Finance.BlackScholes(-1, stockPrices, 90, 0.2, 1));
                });
                Console.WriteLine("Baseline sequential");
                Console.WriteLine(watch.ElapsedMilliseconds); watch.Restart();

                Parallel.For(0, 1000, (i) =>
                {
                    optionPrices = NArray.CreateLike(variates);
                    var logStockPrice = Math.Log(100)
                        + variates * Math.Sqrt(deltaT) * vol + (r - 0.5 * vol * vol) * deltaT;
                    var stockPrices = NMath.Exp(logStockPrice);

                    optionPrices.Assign(Finance.BlackScholes(-1, stockPrices, 90, 0.2, 1));
                });
                Console.WriteLine("Baseline threaded");
                Console.WriteLine(watch.ElapsedMilliseconds);

                NArray optionPrices2 = NArray.CreateLike(variates);

                watch.Restart();
                var vectorOptions = new DeferredExecution.VectorExecutionOptions() { MultipleThreads = true };
                Parallel.For(0, 1000, options, (i) =>
                {
                    optionPrices2 = NArray.CreateLike(variates);
                    NArray.Evaluate(optionPrices2, () =>
                    {
                        var logStockPrice = Math.Log(100)
                            + variates * Math.Sqrt(deltaT) * vol + (r - 0.5 * vol * vol) * deltaT;
                        var stockPrices = NMath.Exp(logStockPrice);

                        return Finance.BlackScholes(-1, stockPrices, 90, 0.2, 1);
                    });
                });
                Console.WriteLine("Deferred sequential");
                Console.WriteLine(watch.ElapsedMilliseconds);
                Console.WriteLine(CheckitString((optionPrices.Storage as ManagedStorage<double>).Array, (optionPrices2.Storage as ManagedStorage<double>).Array));
                watch.Restart();

                Parallel.For(0, 1000, (i) =>
                {
                    //optionPrices2 = NArray.CreateLike(variates);
                    optionPrices2 = NArray.Evaluate(() =>
                    {
                        var logStockPrice = Math.Log(100)
                            + variates * Math.Sqrt(deltaT) * vol + (r - 0.5 * vol * vol) * deltaT;
                        var stockPrices = NMath.Exp(logStockPrice);

                        return Finance.BlackScholes(-1, stockPrices, 90, 0.2, 1);
                    });
                });
                Console.WriteLine("Deferred threaded");
                Console.WriteLine(watch.ElapsedMilliseconds); watch.Restart();
                Console.WriteLine(CheckitString((optionPrices.Storage as ManagedStorage<double>).Array, (optionPrices2.Storage as ManagedStorage<double>).Array));
            }
        }