forked from ArashPartow/exprtk-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exprtk_bsm_benchmark.cpp
288 lines (230 loc) · 9.35 KB
/
exprtk_bsm_benchmark.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* ExprTk Black Scholes Merton Benchmark *
* Author: Arash Partow (1999-2021) *
* URL: http://www.partow.net/programming/exprtk/index.html *
* *
* Copyright notice: *
* Free use of the Mathematical Expression Toolkit Library is *
* permitted under the guidelines and in accordance with the *
* most current version of the MIT License. *
* http://www.opensource.org/licenses/MIT *
* *
**************************************************************
*/
#include <cstdio>
#include <string>
#include "exprtk.hpp"
static const std::size_t rounds = 20000000;
template <typename T>
struct bsm_parameters
{
T s; // Stock price
T x; // Strike price
T t; // Years to maturity
T r; // Risk free rate
T v; // Volatility
};
const bsm_parameters<double> bsm_list[] =
{
{ 60.11, 65.11, 0.25, 0.08, 0.31 },
{ 60.22, 65.22, 0.25, 0.08, 0.32 },
{ 60.33, 65.33, 0.25, 0.08, 0.33 },
{ 60.44, 65.44, 0.25, 0.08, 0.34 },
{ 60.55, 65.55, 0.25, 0.08, 0.35 },
{ 60.66, 65.66, 0.25, 0.08, 0.36 },
{ 60.77, 65.77, 0.25, 0.08, 0.37 },
{ 60.88, 65.88, 0.25, 0.08, 0.38 },
{ 60.11, 65.11, 0.25, 0.08, 0.31 },
{ 60.22, 65.22, 0.25, 0.08, 0.32 },
{ 60.33, 65.33, 0.25, 0.08, 0.33 },
{ 60.44, 65.44, 0.25, 0.08, 0.34 },
{ 60.55, 65.55, 0.25, 0.08, 0.35 },
{ 60.66, 65.66, 0.25, 0.08, 0.36 },
{ 60.77, 65.77, 0.25, 0.08, 0.37 },
{ 60.88, 65.88, 0.25, 0.08, 0.38 }
};
const std::size_t bsm_list_size = sizeof (bsm_list) / sizeof(bsm_parameters<double>);
template <typename T>
void black_scholes_merton_model()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
const std::string bsm_model_program =
" var d1 := (log(s / x) + (r + v^2 / 2) * t) / (v * sqrt(t)); "
" var d2 := d1 - v * sqrt(t); "
" "
" if (callput_flag == 'call') "
" s * ncdf(d1) - x * e^(-r * t) * ncdf(d2); "
" else if (callput_flag == 'put') "
" x * e^(-r * t) * ncdf(-d2) - s * ncdf(-d1); "
" ";
const std::string bsm_model_program_opt1 =
" var v_sqrtt := (v * sqrt(t)); "
" var d1 := (log(s / x) + (r + v * v / 2) * t) / v_sqrtt; "
" var d2 := d1 - v_sqrtt; "
" var xert := x * exp(-r * t); "
" "
" if (callput_flag == 'call') "
" s * ncdf(d1) - xert * ncdf(d2); "
" else if (callput_flag == 'put') "
" xert * ncdf(-d2) - s * ncdf(-d1); "
" ";
const std::string bsm_model_program_opt2 =
" var v_sqrtt := (v * sqrt(t)); "
" var d1 := (log(s / x) + (r + v * v / 2) * t) / v_sqrtt; "
" var d2 := d1 - v_sqrtt; "
" "
" if (callput_flag == 'call') "
" s * ncdf(d1) - (x * exp(-r * t)) * ncdf(d2); "
" else if (callput_flag == 'put') "
" (x * exp(-r * t)) * ncdf(-d2) - s * ncdf(-d1); "
" ";
T s = T(0);
T x = T(0);
T t = T(0);
T r = T(0);
T v = T(0);
std::string callput_flag;
static const T e = exprtk::details::numeric::constant::e;
symbol_table_t symbol_table;
symbol_table.add_variable("s",s);
symbol_table.add_variable("x",x);
symbol_table.add_variable("t",t);
symbol_table.add_variable("r",r);
symbol_table.add_variable("v",v);
symbol_table.add_constant("e",e);
symbol_table.add_stringvar("callput_flag",callput_flag);
expression_t bsm_expression (symbol_table);
expression_t bsm_expression_opt1(symbol_table);
expression_t bsm_expression_opt2(symbol_table);
parser_t parser;
parser.compile(bsm_model_program, bsm_expression );
parser.compile(bsm_model_program_opt1, bsm_expression_opt1);
parser.compile(bsm_model_program_opt2, bsm_expression_opt2);
{
exprtk::timer timer;
timer.start();
T total = T(0);
for (std::size_t k = 0; k < rounds; ++k)
{
const bsm_parameters<T>& params = bsm_list[k % bsm_list_size];
s = params.s;
x = params.x;
t = params.t;
r = params.r;
v = params.v;
callput_flag = "call";
total += bsm_expression.value();
callput_flag = "put";
total += bsm_expression.value();
}
timer.stop();
printf("[exprtk0] Total: %16.5f\tTime:%8.3fsec\tRate:%16.3f\n",
total,
timer.time(),
(2.0 * rounds) / timer.time());
}
{
exprtk::timer timer;
timer.start();
T total = T(0);
for (std::size_t k = 0; k < rounds; ++k)
{
const bsm_parameters<T>& params = bsm_list[k % bsm_list_size];
s = params.s;
x = params.x;
t = params.t;
r = params.r;
v = params.v;
callput_flag = "call";
total += bsm_expression_opt1.value();
callput_flag = "put";
total += bsm_expression_opt1.value();
}
timer.stop();
printf("[exprtk1] Total: %16.5f\tTime:%8.3fsec\tRate:%16.3f\n",
total,
timer.time(),
(2.0 * rounds) / timer.time());
}
{
exprtk::timer timer;
timer.start();
T total = T(0);
for (std::size_t k = 0; k < rounds; ++k)
{
const bsm_parameters<T>& params = bsm_list[k % bsm_list_size];
s = params.s;
x = params.x;
t = params.t;
r = params.r;
v = params.v;
callput_flag = "call";
total += bsm_expression_opt2.value();
callput_flag = "put";
total += bsm_expression_opt2.value();
}
timer.stop();
printf("[exprtk2] Total: %16.5f\tTime:%8.3fsec\tRate:%16.3f\n",
total,
timer.time(),
(2.0 * rounds) / timer.time());
}
}
template <typename T>
inline T bsm_model(const std::string& callput_flag, const T s, const T x, const T t, const T r, const T v)
{
const T d1 = (std::log(s / x) + (r + (v * v) / 2) * t) / (v * std::sqrt(t));
const T d2 = d1 - v * sqrt(t);
if (callput_flag == "call")
return s * exprtk::details::numeric::ncdf(d1) - x * exp(-r * t) * exprtk::details::numeric::ncdf(d2);
else if (callput_flag == "put")
return x * exp(-r * t) * exprtk::details::numeric::ncdf(-d2) - s * exprtk::details::numeric::ncdf(-d1);
else
return T(0);
}
template <typename T>
void bsm_native()
{
T s = T(0);
T x = T(0);
T t = T(0);
T r = T(0);
T v = T(0);
std::string callput_flag;
exprtk::timer timer;
timer.start();
T total = T(0);
for (std::size_t k = 0; k < rounds; ++k)
{
const bsm_parameters<T>& params = bsm_list[k % bsm_list_size];
s = params.s;
x = params.x;
t = params.t;
r = params.r;
v = params.v;
callput_flag = "call";
total += bsm_model(callput_flag, s, x, t, r, v);
callput_flag = "put";
total += bsm_model(callput_flag, s, x, t, r, v);
}
timer.stop();
printf("[native ] Total: %16.5f\tTime:%8.3fsec\tRate:%16.3f\n",
total,
timer.time(),
(2.0 * rounds) / timer.time());
}
int main()
{
black_scholes_merton_model<double>();
bsm_native<double>();
return 0;
}
/*
Build command:
c++ -pedantic-errors -Wall -Wextra -Werror -Wno-long-long -flto -march=native -O3 -o exprtk_bsm_benchmark exprtk_bsm_benchmark.cpp -L/usr/lib -lstdc++ -lm
*/