C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_ornstein_uhlenbeck_process.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * ExprTk Ornstein-Uhlenbeck Process *
6 * Author: Arash Partow (1999-2024) *
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
29template <typename T>
30struct normal_distribution final : public exprtk::ifunction<T>
31{
32 using exprtk::ifunction<T>::operator();
33
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
53template <typename T>
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 ornstein_uhlenbeck_process_program =
61 " const var mu := 1; "
62 " const var sigma := 0.4; "
63 " const var theta := 2; "
64 " const var tau := 1 / theta; "
65 " const var T := 10 * tau; "
66 " const var dt := 0.01 * tau; "
67 " const var num_steps := floor(T / dt); "
68 " const var stddev := sqrt(sigma^2 / (2 * theta) * (1 - exp(-2 * theta * dt))); "
69 " const var x_0 := 1.0; "
70 " "
71 " var x[num_steps] := [0]; "
72 " "
73 " x[0] := x_0; "
74 " "
75 " for (var i := 0; i < num_steps - 1; i += 1) "
76 " { "
77 " var mean := x[i] * exp(-theta * dt) + mu * (1 - exp(-theta * dt)); "
78 " "
79 " x[i + 1] := normal(mean,stddev); "
80 " }; "
81 " "
82 " "
83 " for (var i := 0; i < x[]; i += 1) "
84 " { "
85 " println(i * dt, '\t', x[i]); "
86 " } ";
87
88
91
92 symbol_table_t symbol_table;
93 symbol_table.add_function("println", println);
94 symbol_table.add_function("normal" , normal );
95
96 expression_t expression;
97 expression.register_symbol_table(symbol_table);
98
99 parser_t parser;
100 parser.compile(ornstein_uhlenbeck_process_program,expression);
101
102 expression.value();
103}
104
105int main()
106{
107 ornstein_uhlenbeck_process<double>();
108 return 0;
109}
ifunction(const std::size_t &pc)
Definition exprtk.hpp:19545
void ornstein_uhlenbeck_process()
T operator()(const T &mean, const T &stddev) override