C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_vectorized_binomial_model.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* Vectorized Binomial Option Pricing Model *
6
* Author: Arash Partow (1999-2025) *
7
* URL: https://www.partow.net/programming/exprtk/index.html *
8
* *
9
* Copyright notice: *
10
* Free use of the Mathematical Expression Toolkit Library is *
11
* permitted under the guidelines and in accordance with the *
12
* most current version of the MIT License. *
13
* https://www.opensource.org/licenses/MIT *
14
* SPDX-License-Identifier: MIT *
15
* *
16
**************************************************************
17
*/
18
19
20
#include <cstdio>
21
#include <string>
22
23
#include "
exprtk.hpp
"
24
25
26
template
<
typename
T>
27
void
vectorized_binomial_option_pricing_model
()
28
{
29
typedef
exprtk::symbol_table<T>
symbol_table_t;
30
typedef
exprtk::expression<T>
expression_t;
31
typedef
exprtk::parser<T>
parser_t;
32
33
const
std::string european_option_binomial_model_program =
34
" var dt := t / n; "
35
" var z := exp(r * dt); "
36
" var z_inv := 1 / z; "
37
" var u := exp(v * sqrt(dt)); "
38
" var u_inv := 1 / u; "
39
" var p_up := (z - u_inv) / (u - u_inv); "
40
" var p_down := 1 - p_up; "
41
" "
42
" var option_price[n + 1] := [0]; "
43
" "
44
" for (var i := 0; i <= n; i += 1) "
45
" { "
46
" var base_price := s * u^(n - 2i); "
47
" option_price[i] := "
48
" switch "
49
" { "
50
" case callput_flag == 'call' : max(base_price - k, 0); "
51
" case callput_flag == 'put' : max(k - base_price, 0); "
52
" }; "
53
" }; "
54
" "
55
" var p_u_zinv := z_inv * p_up; "
56
" var p_d_zinv := z_inv * p_down; "
57
" "
58
" for (var j := n - 1; j >= 0; j -= 1) "
59
" { "
60
" /* y_i <- a * x_i + b * y_(i+1) i:[0,j] */ "
61
" axpbsy(p_u_zinv, option_price, "
62
" p_d_zinv, 1, option_price, "
63
" 0, j); "
64
" }; "
65
" "
66
" option_price[0]; "
;
67
68
69
T s = T( 100.00);
// Spot / Stock / Underlying / Base price
70
T k = T( 110.00);
// Strike price
71
T v = T( 0.30);
// Volatility
72
T t = T( 2.22);
// Years to maturity
73
T r = T( 0.05);
// Risk free rate
74
T n = T(2000.00);
// Number of time steps
75
76
std::string callput_flag;
77
78
exprtk::rtl::vecops::package<T>
vecops_package;
79
80
symbol_table_t symbol_table;
81
symbol_table.add_variable(
"s"
,s);
82
symbol_table.add_variable(
"k"
,k);
83
symbol_table.add_variable(
"t"
,t);
84
symbol_table.add_variable(
"r"
,r);
85
symbol_table.add_variable(
"v"
,v);
86
symbol_table.add_constant(
"n"
,n);
87
symbol_table.add_stringvar(
"callput_flag"
,callput_flag);
88
symbol_table.add_package(vecops_package);
89
90
expression_t expression;
91
expression.register_symbol_table(symbol_table);
92
93
parser_t parser;
94
parser.compile(european_option_binomial_model_program,expression);
95
96
const
std::size_t
rounds
= 2000;
97
98
T binomial_call_option_price = T(0);
99
T binomial_put_option_price = T(0);
100
101
{
102
callput_flag =
"call"
;
103
104
exprtk::timer
timer;
105
timer.
start
();
106
107
for
(std::size_t i = 0; i <
rounds
; ++i)
108
{
109
binomial_call_option_price = expression.value();
110
}
111
112
timer.
stop
();
113
114
printf(
"BinomialOptionPrice(Type: %4s, BasePx: %5.3f, Strike: %5.3f, Time: %5.3f, RFR: %5.3f, Vol: %5.3f, Steps: %4.1f) = %10.6f "
115
"total time: %6.3fsec rate: %6.3fcalc/sec\n"
,
116
callput_flag.c_str(),
117
s, k, t, r, v, n,
118
binomial_call_option_price,
119
timer.
time
(),
120
rounds
/ timer.
time
());
121
}
122
123
{
124
callput_flag =
"put"
;
125
126
exprtk::timer
timer;
127
timer.
start
();
128
129
for
(std::size_t i = 0; i <
rounds
; ++i)
130
{
131
binomial_put_option_price = expression.value();
132
}
133
134
timer.
stop
();
135
136
printf(
"BinomialOptionPrice(Type: %4s, BasePx: %5.3f, Strike: %5.3f, Time: %5.3f, RFR: %5.3f, Vol: %5.3f, Steps: %4.1f) = %10.6f "
137
"total time: %6.3fsec rate: %6.3fcalc/sec\n"
,
138
callput_flag.c_str(),
139
s, k, t, r, v, n,
140
binomial_put_option_price,
141
timer.
time
(),
142
rounds
/ timer.
time
());
143
}
144
145
const
T put_call_parity_diff =
146
(binomial_call_option_price - binomial_put_option_price) -
147
(s - k * std::exp(-r * t));
148
149
printf(
"Put-Call parity difference: %20.17f\n"
, put_call_parity_diff);
150
}
151
152
int
main
()
153
{
154
vectorized_binomial_option_pricing_model<double>
();
155
return
0;
156
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk::timer
Definition
exprtk.hpp:44056
exprtk::timer::stop
void stop()
Definition
exprtk.hpp:44103
exprtk::timer::time
double time() const
Definition
exprtk.hpp:44125
exprtk::timer::start
void start()
Definition
exprtk.hpp:44097
exprtk.hpp
rounds
static const std::size_t rounds
Definition
exprtk_bsm_benchmark.cpp:26
vectorized_binomial_option_pricing_model
void vectorized_binomial_option_pricing_model()
Definition
exprtk_vectorized_binomial_model.cpp:27
main
int main()
Definition
exprtk_vectorized_binomial_model.cpp:152
exprtk::rtl::vecops::package
Definition
exprtk.hpp:46626
Generated by
1.17.0