public
Created

  • Download Gist
g.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <Rcpp.h>
using namespace Rcpp;
 
// [[Rcpp::export]]
NumericVector g(NumericMatrix x) {
double sigma = 0.25;
double S0 = 1;
double Time = 1;
double r = 0;
double K = 1;
int nrow = x.nrow(), ncol = x.ncol();
NumericVector out(ncol);
NumericMatrix temp(nrow,ncol);
for (int j = 0; j < ncol; j++) {
double acc = 0;
for(int i = 0; i < nrow; i++) {
acc += x(i,j);
temp(i,j) = acc;
}
}
for (int i = 0; i < nrow; i++) {
temp(i,_) = S0*exp(sigma*temp(i,_)+(r-pow(sigma,2)/2)*i/nrow*Time) ; // Works until here but seems to do not give the exact same numerical value
}
for (int j = 0; j < ncol; j++) { //Breaks from here
out(j) = 0;
for(int i = 0; i < nrow; i++) {
out(j) += temp(i,j);
}
out(j) *= 1/nrow;
if ( out(j) > K ) {out(j) -= K;} else {out(j)=0;}
}
return out;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.