Skip to content

Instantly share code, notes, and snippets.

@aussetg
Created December 23, 2013 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aussetg/8095741 to your computer and use it in GitHub Desktop.
Save aussetg/8095741 to your computer and use it in GitHub Desktop.
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment