[python-arrayfire] 163/250: Adding black scholes example

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Mon Mar 28 22:59:44 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/master
in repository python-arrayfire.

commit 2b4970e6c3cc66f4bf38946acd3e2851db4846e5
Author: Pavan Yalamanchili <pavan at arrayfire.com>
Date:   Wed Dec 9 16:28:56 2015 -0500

    Adding black scholes example
---
 examples/financial/black_scholes_options.py | 80 +++++++++++++++++++++++++++++
 1 file changed, 80 insertions(+)

diff --git a/examples/financial/black_scholes_options.py b/examples/financial/black_scholes_options.py
new file mode 100644
index 0000000..f665428
--- /dev/null
+++ b/examples/financial/black_scholes_options.py
@@ -0,0 +1,80 @@
+#!/usr/bin/python
+
+#######################################################
+# Copyright (c) 2015, ArrayFire
+# All rights reserved.
+#
+# This file is distributed under 3-clause BSD license.
+# The complete license agreement can be obtained at:
+# http://arrayfire.com/licenses/BSD-3-Clause
+########################################################
+
+import arrayfire as af
+from time import time
+import math
+import sys
+
+sqrt2 = math.sqrt(2.0)
+
+def cnd(x):
+    temp = (x > 0)
+    return temp * (0.5 + af.erf(x/sqrt2)/2) + (1 - temp) * (0.5 - af.erf((-x)/sqrt2)/2)
+
+def black_scholes(S, X, R, V, T):
+    # S = Underlying stock price
+    # X = Strike Price
+    # R = Risk free rate of interest
+    # V = Volatility
+    # T = Time to maturity
+
+    d1 = af.log(S / X)
+    d1 = d1 + (R + (V * V) * 0.5) * T
+    d1 = d1 / (V * af.sqrt(T))
+
+    d2 = d1 - (V * af.sqrt(T))
+    cnd_d1 = cnd(d1)
+    cnd_d2 = cnd(d2)
+
+    C = S * cnd_d1 - (X * af.exp((-R) * T) * cnd_d2)
+    P = X * af.exp((-R) * T) * (1 - cnd_d2) - (S * (1 -cnd_d1))
+
+    return (C, P)
+
+if __name__ == "__main__":
+    if (len(sys.argv) > 1):
+        af.set_device(int(sys.argv[1]))
+    af.info()
+
+    M = 4000
+
+    S = af.randu(M, 1)
+    X = af.randu(M, 1)
+    R = af.randu(M, 1)
+    V = af.randu(M, 1)
+    T = af.randu(M, 1)
+
+    (C, P) = black_scholes(S, X, R, V, T)
+    af.eval(C)
+    af.eval(P)
+    af.sync()
+
+    num_iter = 5
+    for N in range(50, 501, 50):
+        S = af.randu(M, N)
+        X = af.randu(M, N)
+        R = af.randu(M, N)
+        V = af.randu(M, N)
+        T = af.randu(M, N)
+        af.sync()
+
+        print("Input data size: %d elements" % (M * N))
+
+        start = time()
+        for i in range(num_iter):
+            (C, P) = black_scholes(S, X, R, V, T)
+            af.eval(C)
+            af.eval(P)
+        af.sync()
+        sec = (time() - start) / num_iter
+
+        print("Mean GPU Time: %0.6f ms\n\n" % (1000.0 * sec))

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/python-arrayfire.git



More information about the debian-science-commits mailing list