1
1
#include < aws/lambda-runtime/runtime.h>
2
2
#include < iomanip>
3
3
#include < sstream>
4
- #include < algorithm> // Needed for the "max" function
4
+ #include < algorithm>
5
5
#include < cmath>
6
6
#include < iostream>
7
+ #include < random>
7
8
#include < aws/core/utils/json/JsonSerializer.h>
8
9
#include < aws/core/utils/memory/stl/SimpleStringStream.h>
9
10
10
11
using namespace aws ::lambda_runtime;
11
12
using namespace Aws ::Utils::Json;
12
13
13
-
14
-
15
- // A simple implementation of the Box-Muller algorithm, used to generate
16
- // gaussian random numbers - necessary for the Monte Carlo method below
17
- // Note that C++11 actually provides std::normal_distribution<> in
18
- // the <random> library, which can be used instead of this function
19
- double gaussian_box_muller () {
20
- double x = 0.0 ;
21
- double y = 0.0 ;
22
- double euclid_sq = 0.0 ;
23
-
24
- // Continue generating two uniform random variables
25
- // until the square of their "euclidean distance"
26
- // is less than unity
27
- do {
28
- x = 2.0 * rand () / static_cast <double >(RAND_MAX)-1 ;
29
- y = 2.0 * rand () / static_cast <double >(RAND_MAX)-1 ;
30
- euclid_sq = x*x + y*y;
31
- } while (euclid_sq >= 1.0 );
32
-
33
- return x*sqrt (-2 *log (euclid_sq)/euclid_sq);
34
- }
35
-
36
14
// Pricing a European vanilla call option with a Monte Carlo method
37
15
double monte_carlo_call_price (const int & num_sims, const double & S, const double & K, const double & r, const double & v, const double & T) {
38
- double S_adjust = S * exp (T*(r-0.5 *v*v));
39
- double S_cur = 0.0 ;
16
+ double s_adjust = S * exp (T*(r-0.5 *v*v));
17
+ double s_cur = 0.0 ;
40
18
double payoff_sum = 0.0 ;
41
19
20
+ std::random_device rd {};
21
+ std::mt19937 prng {rd ()};
22
+ std::normal_distribution<> d {0 , 1 };
23
+
42
24
for (int i=0 ; i<num_sims; i++) {
43
- double gauss_bm = gaussian_box_muller ();
44
- S_cur = S_adjust * exp (sqrt (v*v*T)*gauss_bm);
45
- payoff_sum += std::max (S_cur - K, 0.0 );
25
+ s_cur = s_adjust * exp (sqrt (v*v*T)*d (prng));
26
+ payoff_sum += std::max (s_cur - K, 0.0 );
46
27
}
47
28
48
29
return (payoff_sum / static_cast <double >(num_sims)) * exp (-r*T);
@@ -52,7 +33,7 @@ static invocation_response my_handler(invocation_request const& request) {
52
33
53
34
// parameter list
54
35
int num_sims = 100000 ; // Number of simulated asset paths; override with query parameter 'num_sims`
55
- double S = 100.0 ; // Option price
36
+ double S = 100.0 ; // Stock price
56
37
double K = 100.0 ; // Strike price
57
38
double r = 0.05 ; // Risk-free rate (5%)
58
39
double v = 0.2 ; // Volatility of the underlying (20%)
0 commit comments