C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_montecarlo_option_pricing_model.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* ExprTk Monte-Carlo Based European 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 <algorithm>
21
#include <array>
22
#include <cstdio>
23
#include <random>
24
#include <string>
25
26
#include "
exprtk.hpp
"
27
28
29
template
<
typename
T>
30
struct
normal_distribution
final :
public
exprtk::ifunction
<T>
31
{
32
using
exprtk::ifunction
<T>::operator();
33
34
normal_distribution
()
35
:
exprtk
::
ifunction
<T>(2)
36
{
37
std::random_device device;
38
std::array<unsigned int,std::mt19937::state_size> seed;
39
std::generate_n(seed.data(), seed.size(), std::ref(device));
40
std::seed_seq seq(std::begin(seed), std::end(seed));
41
generator
.seed(seq);
42
}
43
44
inline
T
operator()
(
const
T& mean,
const
T& stddev)
override
45
{
46
std::normal_distribution<T> distribution{mean, stddev};
47
return
distribution(
generator
);
48
}
49
50
std::mt19937
generator
;
51
};
52
53
template
<
typename
T>
54
void
exprtk_montecarlo_option_pricing_model
()
55
{
56
typedef
exprtk::symbol_table<T>
symbol_table_t;
57
typedef
exprtk::expression<T>
expression_t;
58
typedef
exprtk::parser<T>
parser_t;
59
60
const
std::string exprtk_montecarlo_option_pricing_model_program =
61
" var payoff_sum := 0; "
62
" var sqrt_t := sqrt(t); "
63
" "
64
" for (var i := 0; i < n; i += 1) "
65
" { "
66
" var s_t := s * exp((r - v^2 / 2) * t + v * sqrt_t * normal(0,1)); "
67
" payoff_sum += "
68
" switch "
69
" { "
70
" case callput_flag == 'call' : max(s_t - k, 0); "
71
" case callput_flag == 'put' : max(k - s_t, 0); "
72
" }; "
73
" }; "
74
" "
75
" exp(-r * t) * payoff_sum / n; "
;
76
77
T s = T(100.00);
// Spot / Stock / Underlying / Base price
78
T k = T(110.00);
// Strike price
79
T v = T( 0.30);
// Volatility
80
T t = T( 2.22);
// Years to maturity
81
T r = T( 0.05);
// Risk free rate
82
T n = T( 2.0e7);
// Number of simulations
83
84
std::string callput_flag;
85
86
normal_distribution<T>
normal;
87
88
symbol_table_t symbol_table(symbol_table_t::e_immutable);
89
symbol_table.add_variable(
"s"
,s);
90
symbol_table.add_variable(
"k"
,k);
91
symbol_table.add_variable(
"t"
,t);
92
symbol_table.add_variable(
"r"
,r);
93
symbol_table.add_variable(
"v"
,v);
94
symbol_table.add_constant(
"n"
,n);
95
symbol_table.add_stringvar(
"callput_flag"
, callput_flag);
96
symbol_table.add_function (
"normal"
, normal );
97
98
expression_t expression;
99
expression.register_symbol_table(symbol_table);
100
101
parser_t parser;
102
parser.compile(exprtk_montecarlo_option_pricing_model_program, expression);
103
104
callput_flag =
"call"
;
105
106
const
T montecarlo_call_option_price = expression.value();
107
108
callput_flag =
"put"
;
109
110
const
T montecarlo_put_option_price = expression.value();
111
112
printf(
"MCOptionPrice(call, %5.3f, %5.3f, %5.3f, %5.3f, %5.3f) = %10.6f\n"
,
113
s, k, t, r, v,
114
montecarlo_call_option_price);
115
116
printf(
"MCOptionPrice(put , %5.3f, %5.3f, %5.3f, %5.3f, %5.3f) = %10.6f\n"
,
117
s, k, t, r, v,
118
montecarlo_put_option_price);
119
120
const
double
put_call_parity_diff =
121
(montecarlo_call_option_price - montecarlo_put_option_price) -
122
(s - k * std::exp(-r * t));
123
124
printf(
"Put-Call parity difference: %20.17f\n"
, put_call_parity_diff);
125
}
126
127
int
main
()
128
{
129
exprtk_montecarlo_option_pricing_model<double>
();
130
return
0;
131
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::ifunction
Definition
exprtk.hpp:19861
exprtk::ifunction::ifunction
ifunction(const std::size_t &pc)
Definition
exprtk.hpp:19864
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk.hpp
exprtk_montecarlo_option_pricing_model
void exprtk_montecarlo_option_pricing_model()
Definition
exprtk_montecarlo_option_pricing_model.cpp:54
main
int main()
Definition
exprtk_montecarlo_option_pricing_model.cpp:127
exprtk
Definition
exprtk.hpp:60
normal_distribution
Definition
exprtk_montecarlo_option_pricing_model.cpp:31
normal_distribution::generator
std::mt19937 generator
Definition
exprtk_montecarlo_option_pricing_model.cpp:50
normal_distribution::normal_distribution
normal_distribution()
Definition
exprtk_montecarlo_option_pricing_model.cpp:34
normal_distribution::operator()
T operator()(const T &mean, const T &stddev) override
Definition
exprtk_montecarlo_option_pricing_model.cpp:44
Generated by
1.17.0