#include <math.h>
#include <stdio.h>
#include <iostream>
template<class ty>
template<>
dtype get_dtype<float>() {
}
template<>
dtype get_dtype<double>() {
}
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>();
ty dt = t / (ty)(steps - 1);
randmat =
exp((r - (vol * vol * 0.5)) * dt + vol *
sqrt(dt) * randmat);
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;
timer::start();
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 {
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));
}
return 0;
}
A multi dimensional data container.
An ArrayFire exception class.
virtual const char * what() const
Returns an error message for the exception in a string format.
@ f32
32-bit floating point values
@ f64
64-bit floating point values
AFAPI array exp(const array &in)
C++ Interface to evaluate the exponential.
AFAPI array sqrt(const array &in)
C++ Interface to evaluate the square root.
array constant(T val, const dim4 &dims, const dtype ty=(af_dtype) dtype_traits< T >::ctype)
C++ Interface to generate an array with elements set to a specified value.
AFAPI array join(const int dim, const array &first, const array &second)
C++ Interface to join 2 arrays along a dimension.
AFAPI array randn(const dim4 &dims, const dtype ty, randomEngine &r)
C++ Interface to create an array of random numbers normally distributed.
AFAPI array allTrue(const array &in, const int dim=-1)
C++ Interface to check if all values along a given dimension are true.
AFAPI array max(const array &in, const int dim=-1)
C++ Interface to return the maximum along a given dimension.
AFAPI array product(const array &in, const int dim=-1)
C++ Interface to multiply array elements over a given dimension.