-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from bh107/new_util
New util
- Loading branch information
Showing
55 changed files
with
1,078 additions
and
1,970 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
================== | ||
Benchmark Examples | ||
================== | ||
This submodule of benchpress consist of a broad range of benchmarks in different languages | ||
""" | ||
from __future__ import absolute_import | ||
|
||
from . import util as util |
This file was deleted.
Oops, something went wrong.
114 changes: 0 additions & 114 deletions
114
benchpress/benchmarks/black_scholes/cpp11_bxx/src/black_scholes.cpp
This file was deleted.
Oops, something went wrong.
82 changes: 37 additions & 45 deletions
82
benchpress/benchmarks/black_scholes/python_numpy/black_scholes.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,83 @@ | ||
from __future__ import print_function | ||
from benchpress import util | ||
from benchpress.benchmarks import util | ||
import numpy as np | ||
import math | ||
|
||
try: | ||
import numpy_force as npf | ||
except ImportError: | ||
import numpy as npf | ||
|
||
def model(N, B): | ||
bench = util.Benchmark("Black Scholes", "samples*iterations") | ||
|
||
|
||
def model(N): | ||
"""Construct pseudo-data representing price samples?""" | ||
# Create the outside of bohrium | ||
S = npf.random.random(N).astype(B.dtype)*4.0 + 58.0 # Price is between 58-62 | ||
S = npf.random.random(N).astype(bench.dtype) * 4.0 + 58.0 # Price is between 58-62 | ||
S = np.array(S) | ||
return S | ||
|
||
|
||
def CND(X): | ||
""" | ||
Cumulative normal distribution. | ||
Helper function used by BS(...). | ||
""" | ||
|
||
(a1,a2,a3,a4,a5) = (0.31938153, -0.356563782, 1.781477937, -1.821255978, 1.330274429) | ||
(a1, a2, a3, a4, a5) = (0.31938153, -0.356563782, 1.781477937, -1.821255978, 1.330274429) | ||
L = np.absolute(X) | ||
K = 1.0 / (1.0 + 0.2316419 * L) | ||
w = 1.0 - 1.0 / math.sqrt(2*np.pi)*np.exp(-L*L/2.) * \ | ||
w = 1.0 - 1.0 / math.sqrt(2 * np.pi) * np.exp(-L * L / 2.) * \ | ||
(a1 * K + \ | ||
a2 * (K**2) + \ | ||
a3 * (K**3) + \ | ||
a4 * (K**4) + \ | ||
a5 * (K**5)) | ||
a2 * (K ** 2) + \ | ||
a3 * (K ** 3) + \ | ||
a4 * (K ** 4) + \ | ||
a5 * (K ** 5)) | ||
|
||
mask = X<0 | ||
w = w * ~mask + (1.0-w)*mask | ||
mask = X < 0 | ||
w = w * ~mask + (1.0 - w) * mask | ||
|
||
return w | ||
|
||
|
||
def BS(CallPutFlag, S, X, T, r, v): | ||
"""Black Sholes Function.""" | ||
|
||
d1 = (np.log(S/X)+(r+v*v/2.)*T)/(v*math.sqrt(T)) | ||
d2 = d1-v*math.sqrt(T) | ||
if CallPutFlag=='c': | ||
return S*CND(d1)-X*math.exp(-r*T)*CND(d2) | ||
d1 = (np.log(S / X) + (r + v * v / 2.) * T) / (v * math.sqrt(T)) | ||
d2 = d1 - v * math.sqrt(T) | ||
if CallPutFlag == 'c': | ||
return S * CND(d1) - X * math.exp(-r * T) * CND(d2) | ||
else: | ||
return X*math.exp(-r*T)*CND(-d2)-S*CND(-d1) | ||
return X * math.exp(-r * T) * CND(-d2) - S * CND(-d1) | ||
|
||
def price(S, I, flag='c', X=65.0, dT=(1.0/365.0), r=0.08, v=0.3, visualize=False): | ||
|
||
def price(S, I, flag='c', X=65.0, dT=(1.0 / 365.0), r=0.08, v=0.3): | ||
"""Model price as a function of time.""" | ||
|
||
T = dT | ||
Ps = [] | ||
N = len(S) | ||
for i in range (I): | ||
T = dT | ||
Ps = [] | ||
N = len(S) | ||
for i in range(I): | ||
P = np.sum(BS(flag, S, X, T, r, v)) / N | ||
Ps.append(P) | ||
T += dT | ||
if visualize: #NB: this is only for experiments | ||
np.visualize(P, "3d", 0, 0.0, 10) | ||
util.Benchmark().flush() | ||
bench.flush() | ||
|
||
return Ps | ||
|
||
def main(): | ||
B = util.Benchmark() | ||
(N, I) = B.size | ||
|
||
if B.inputfn: | ||
S = B.load_array() | ||
else: | ||
S = model(N, B) # Construct pseudo-data | ||
|
||
if B.dumpinput: | ||
B.dump_arrays("black_scholes", {'input': S}) | ||
def main(): | ||
N = bench.args.size[0] | ||
I = bench.args.size[1] | ||
|
||
B.start() | ||
R = price(S, I, visualize=B.visualize) # Run the model | ||
B.stop() | ||
B.pprint() | ||
S = model(N) # Construct pseudo-data\ | ||
|
||
for i in range(len(R)): # Convert to one array | ||
if hasattr(R[i], "copy2numpy"): | ||
R[i] = R[i].copy2numpy()[()] | ||
if B.outputfn: | ||
B.tofile(B.outputfn, {'res':R}) | ||
bench.start() | ||
R = price(S, I) # Run the model | ||
bench.stop() | ||
bench.pprint() | ||
|
||
if B.verbose: | ||
print (R) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.