diff --git a/.gitignore b/.gitignore index 717067c..cf30922 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ compile_commands.json #ide .idea - +test.cpp +a.out diff --git a/examples/monte-carlo-calculator/main.cpp b/examples/monte-carlo-calculator/main.cpp index 18dfab7..e3f9ecc 100644 --- a/examples/monte-carlo-calculator/main.cpp +++ b/examples/monte-carlo-calculator/main.cpp @@ -1,48 +1,29 @@ #include #include #include -#include // Needed for the "max" function +#include #include #include +#include #include #include using namespace aws::lambda_runtime; using namespace Aws::Utils::Json; - - -// A simple implementation of the Box-Muller algorithm, used to generate -// gaussian random numbers - necessary for the Monte Carlo method below -// Note that C++11 actually provides std::normal_distribution<> in -// the library, which can be used instead of this function -double gaussian_box_muller() { - double x = 0.0; - double y = 0.0; - double euclid_sq = 0.0; - - // Continue generating two uniform random variables - // until the square of their "euclidean distance" - // is less than unity - do { - x = 2.0 * rand() / static_cast(RAND_MAX)-1; - y = 2.0 * rand() / static_cast(RAND_MAX)-1; - euclid_sq = x*x + y*y; - } while (euclid_sq >= 1.0); - - return x*sqrt(-2*log(euclid_sq)/euclid_sq); -} - // Pricing a European vanilla call option with a Monte Carlo method double monte_carlo_call_price(const int& num_sims, const double& S, const double& K, const double& r, const double& v, const double& T) { - double S_adjust = S * exp(T*(r-0.5*v*v)); - double S_cur = 0.0; + double s_adjust = S * exp(T*(r-0.5*v*v)); + double s_cur = 0.0; double payoff_sum = 0.0; + std::random_device rd {}; + std::mt19937 prng {rd()}; + std::normal_distribution<> d {0, 1}; + for (int i=0; i(num_sims)) * exp(-r*T);