C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_jump_diffusion_process.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* Merton Jump Diffusion Process 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
european_option_merton_jump_diffusion_process
()
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
typedef
exprtk::function_compositor<T>
compositor_t;
33
typedef
typename
compositor_t::function function_t;
34
35
const
std::string european_option_merton_jump_diffusion_process_program =
36
" var lambda_t := lambda * t; "
37
" var v_sqr := v^2; "
38
" var sigmaJ_sqr := sigmaJ^2; "
39
" "
40
" var option_price := 0; "
41
" var factorial := 1; "
42
" "
43
" for (var i := 0; i < n; i += 1) "
44
" { "
45
" var prob := exp(-lambda_t) * lambda_t^i / factorial; "
46
" var r_i := r - lambda * muJ + (i / t) * log(1 + muJ); "
47
" var sigma_i := sqrt(v_sqr + (i * sigmaJ_sqr) / t); "
48
" "
49
" option_price += "
50
" switch "
51
" { "
52
" case callput_flag == 'call' : prob * bsm_call(s, k, r_i, t, sigma_i); "
53
" case callput_flag == 'put' : prob * bsm_put (s, k, r_i, t, sigma_i); "
54
" }; "
55
" "
56
" factorial *= (i > 1) ? i : 1; "
57
" }; "
58
" "
59
" option_price; "
;
60
61
T s = T(100.00);
// Spot / Stock / Underlying / Base price
62
T k = T(110.00);
// Strike price
63
T v = T( 0.30);
// Volatility
64
T t = T( 2.22);
// Years to maturity
65
T r = T( 0.05);
// Risk free rate
66
T lambda = T(0.0001);
// Jump intensity (average jumps per year)
67
T muJ = T( -0.05);
// Mean jump size (negative for downward jumps)
68
T sigmaJ = T( 0.30);
// Standard deviation of the jump size
69
T n = T( 50.00);
// Number of terms in the Poisson sum
70
71
std::string callput_flag;
72
73
symbol_table_t symbol_table(symbol_table_t::e_immutable);
74
symbol_table.add_variable (
"s"
, s );
75
symbol_table.add_variable (
"k"
, k );
76
symbol_table.add_variable (
"v"
, v );
77
symbol_table.add_variable (
"t"
, t );
78
symbol_table.add_variable (
"r"
, r );
79
symbol_table.add_variable (
"lambda"
, lambda);
80
symbol_table.add_variable (
"muJ"
, muJ );
81
symbol_table.add_variable (
"sigmaJ"
, sigmaJ);
82
symbol_table.add_variable (
"n"
, n );
83
symbol_table.add_stringvar(
"callput_flag"
,callput_flag);
84
symbol_table.add_pi();
85
86
compositor_t compositor(symbol_table);
87
88
compositor.add(
89
function_t(
"bsm_call"
)
90
.vars(
"s"
,
"k"
,
"r"
,
"t"
,
"v"
)
91
.expression
92
(
93
" var d1 := (log(s / k) + (r + v^2 / 2) * t) / (v * sqrt(t)); "
94
" var d2 := d1 - v * sqrt(t); "
95
" s * ncdf(d1) - k * exp(-r * t) * ncdf(d2); "
96
));
97
98
compositor.add(
99
function_t(
"bsm_put"
)
100
.vars(
"s"
,
"k"
,
"r"
,
"t"
,
"v"
)
101
.expression
102
(
103
" var d1 := (log(s / k) + (r + v^2 / 2) * t) / (v * sqrt(t)); "
104
" var d2 := d1 - v * sqrt(t); "
105
" k * exp(-r * t) * ncdf(-d2) - s * ncdf(-d1); "
106
));
107
108
expression_t expression;
109
expression.register_symbol_table(symbol_table);
110
111
parser_t parser;
112
parser.compile(european_option_merton_jump_diffusion_process_program, expression);
113
114
callput_flag =
"call"
;
115
116
const
T jdp_call_option_price = expression.value();
117
118
callput_flag =
"put"
;
119
120
const
T jdp_put_option_price = expression.value();
121
122
printf(
"JDPPrice(%4s, %5.3f, %5.3f, %5.3f, %5.3f, %5.3f) = %10.6f\n"
,
123
"call"
,
124
s, k, t, r, v,
125
jdp_call_option_price);
126
127
printf(
"JDPPrice(%4s, %5.3f, %5.3f, %5.3f, %5.3f, %5.3f) = %10.6f\n"
,
128
"put"
,
129
s, k, t, r, v,
130
jdp_put_option_price);
131
132
const
T put_call_parity_diff =
133
(jdp_call_option_price - jdp_put_option_price) -
134
(s - k * std::exp(-r * t));
135
136
printf(
"Put-Call parity difference: %20.17f\n"
, put_call_parity_diff);
137
}
138
139
int
main
()
140
{
141
european_option_merton_jump_diffusion_process<double>
();
142
return
0;
143
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::function_compositor
Definition
exprtk.hpp:43057
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk.hpp
european_option_merton_jump_diffusion_process
void european_option_merton_jump_diffusion_process()
Definition
exprtk_jump_diffusion_process.cpp:27
main
int main()
Definition
exprtk_jump_diffusion_process.cpp:139
Generated by
1.17.0