My Project
financial/monte_carlo_options.cpp
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <arrayfire.h>
#include <af/util.h>
using namespace af;
template<class ty> dtype get_dtype();
template<> dtype get_dtype<float>() { return f32; }
template<> dtype get_dtype<double>() { return f64; }
template<class ty, bool use_barrier>
static ty monte_carlo_barrier(int N, ty K, ty t, ty vol, ty r, ty strike, int steps, ty B)
{
dtype pres = get_dtype<ty>();
array payoff = constant(0, N, 1, pres);
ty dt = t / (ty)(steps - 1);
array s = constant(strike, N, 1, pres);
array randmat = randn(N, steps - 1, pres);
randmat = exp((r - (vol * vol * 0.5)) * dt + vol * sqrt(dt) * randmat);
array S = product(join(1, s, randmat), 1);
if (use_barrier) {
S = S * allTrue(S < B, 1);
}
payoff = max(0.0, S - K);
ty P = mean<ty>(payoff) * exp(-r * t);
return P;
}
template<class ty, bool use_barrier>
double monte_carlo_bench(int N)
{
int steps = 180;
ty stock_price = 100.0;
ty maturity = 0.5;
ty volatility = .30;
ty rate = .01;
ty strike = 100;
ty barrier = 115.0;
for (int i = 0; i < 10; i++) {
monte_carlo_barrier<ty, use_barrier>(N, stock_price, maturity, volatility,
rate, strike, steps, barrier);
}
return timer::stop() / 10;
}
int main()
{
try {
// Warm up and caching
monte_carlo_bench<float, false>(1000);
monte_carlo_bench<float, true>(1000);
for (int n = 10000; n <= 100000; n += 10000) {
printf("Time for %7d paths - "
"vanilla method: %4.3f ms, "
"barrier method: %4.3f ms\n", n,
1000 * monte_carlo_bench<float, false>(n),
1000 * monte_carlo_bench<float, true>(n));
}
} catch (af::exception &ae) {
std::cerr << ae.what() << std::endl;
}
return 0;
}
util.h
af::exp
AFAPI array exp(const array &in)
C++ Interface for exponential of an array.
af::constant
array constant(T val, const dim4 &dims, const dtype ty=(af_dtype) dtype_traits< T >::ctype)
af::join
AFAPI array join(const int dim, const array &first, const array &second)
Join 2 arrays along dim.
af::product
AFAPI array product(const array &in, const int dim=-1)
C++ Interface for product of elements in an array.
af::timer::start
static AFAPI timer start()
af::array
A multi dimensional data container.
Definition: array.h:32
af
Definition: algorithm.h:13
af::max
AFAPI array max(const array &in, const int dim=-1)
C++ Interface for maximum values in an array.
af_dtype
af_dtype
Definition: defines.h:194
f64
64-bit complex floating point values
Definition: defines.h:197
af::sqrt
AFAPI array sqrt(const array &in)
C++ Interface for square root of input.
af::exception
Definition: exception.h:24
af::randn
AFAPI array randn(const dim4 &dims, const dtype ty=f32)
af::timer::stop
static AFAPI double stop()
arrayfire.h
af::allTrue
AFAPI array allTrue(const array &in, const int dim=-1)
C++ Interface for checking all true values in an array.
af::exception::what
virtual const char * what() const
Definition: exception.h:45
f32
32-bit floating point values
Definition: defines.h:195