public void BlackScholes() { 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 static void VectorForm(NArray variates, NArray optionPrices) { double deltaT = 1; double r = 0.1; double vol = 0.3; var logStockPrice = Math.Log(100) + variates * Math.Sqrt(deltaT) * vol + (r - 0.5 * vol * vol) * deltaT; var forwardStockPrices = NMath.Exp(logStockPrice + r * deltaT); optionPrices.Assign(Finance.BlackScholes(-1, forwardStockPrices, 90, 0.2, 1)); }
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 NArray Price(int timeIndex) { return(Finance.BlackScholes(-1, ForwardStockPrices(timeIndex), Strike, 0.2, 1)); }
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)); } }
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); }); }