void check_blackscholes(void) { double S = 60.0, X = 65.0, T = 0.25, r = 0.08, v = 0.30; assert_equal(blackscholes(1, S, X, T, r, v), 2.1334); assert_equal(blackscholes_call(S, X, T, r, v), 2.1334); S = 96.1469; X = 90.0; T = 0.75; r = 0.10; v = 0.25; assert_equal(blackscholes(1, S, X, T, r, v), 15.6465); assert_equal(blackscholes_call(S, X, T, r, v), 15.6465); }
int main() { typedef float T; double t(0),v(0); { std::vector<T> price (128000); std::vector<T> strike (128000); std::vector<T> rate (128000); std::vector<T> time (128000); std::vector<T> volatility(128000); std::vector<T> call (128000); std::vector<T> put (128000); std::fill(price.begin(),price.end(),1.); std::fill(strike.begin(),strike.end(),1.); std::fill(rate.begin(),rate.end(),1.); std::fill(time.begin(),time.end(),1.); std::fill(volatility.begin(),volatility.end(),1.); std::cout << "std::vector\n"; for(int k=0;k<100;++k) { nt2::tic(); for(std::size_t i=0;i<price.size();++i) blackscholes( price[i], strike[i] , rate[i] , time[i] , volatility[i] , call[i] , put[i] ); t += nt2::toc(false); } std::cout << t/100 << "\n"; } v = call(); std::cout << "Speed-up: " << t/v << "\n"; }
/* * We use NewtonRaphson() to compute the iv of an european option. * How do we test it? We use one of the other functions to compute * the value of an option, given a v. Then we feed the price into * NewtonRaphson() and see if the returned iv is OK. */ void check_NewtonRaphson(void) { int fCall = 1, ok; double iv, S = 100.0, X = 100.0, T = 0.5, r = 0.08, v = 0.20; double cm = blackscholes(fCall, S, X, T, r, v); ok = NewtonRaphson(fCall, S, X, T, r, cm, &iv); assert(ok); assert_equal(v, iv); fCall = 0; cm = blackscholes(fCall, S, X, T, r, v); ok = NewtonRaphson(fCall, S, X, T, r, cm, &iv); assert(ok); assert_equal(v, iv); }
double call() { typedef float T; nt2::table<T> price = nt2::ones(nt2::of_size(128000), nt2::meta::as_<T>()); nt2::table<T> strike = nt2::ones(nt2::of_size(128000), nt2::meta::as_<T>()); nt2::table<T> rate = nt2::ones(nt2::of_size(128000), nt2::meta::as_<T>()); nt2::table<T> time = nt2::ones(nt2::of_size(128000), nt2::meta::as_<T>()); nt2::table<T> volatility= nt2::ones(nt2::of_size(128000), nt2::meta::as_<T>()); nt2::table<T> call = nt2::ones(nt2::of_size(128000), nt2::meta::as_<T>()); nt2::table<T> put = nt2::ones(nt2::of_size(128000), nt2::meta::as_<T>()); std::cout << "expression\n"; double t(0); for(int k=0;k<100;++k) { nt2::tic(); blackscholes( price, strike , rate , time , volatility , call , put ); t += nt2::toc(false); } std::cout << t/100 << "\n"; return t; }
int main (int argc, char* argv[]) { const int width = 40; const int height = 40; float* input, *output1, *output2; float* input_sc, *output1_sc, *output2_sc; if(posix_memalign((void **) &input, 64, 4 * width*height * sizeof(float)) != 0) { exit(1); } if(posix_memalign((void **) &output1, 64, 4 * width*height * sizeof(float)) != 0) { exit(1); } if(posix_memalign((void **) &output2, 64, 4 * width*height * sizeof(float)) != 0) { exit(1); } if(posix_memalign((void **) &input_sc, 64, 4 * width*height * sizeof(float)) != 0) { exit(1); } if(posix_memalign((void **) &output1_sc, 64, 4 * width*height * sizeof(float)) != 0) { exit(1); } if(posix_memalign((void **) &output2_sc, 64, 4 * width*height * sizeof(float)) != 0) { exit(1); } int i; for (i=0; i<(width*height*4); i++) { input[i] = (i*0.9f)/(i+1); input_sc[i] = (i*0.9f)/(i+1); } blackscholes(width, height, input, output1, output2); blackscholes_sc(width, height, input_sc, output1_sc, output2_sc); for (i=0; i<(width*height*4); i++) { if(fabsf(input_sc[i] - input[i]) > 0.01f) { printf("ERROR\n"); exit(1); } if(fabsf(output1_sc[i] - output1[i]) > 0.01f) { printf("ERROR\n"); exit(1); } if(fabsf(output2_sc[i] - output2[i]) > 0.01f) { printf("ERROR\n"); exit(1); } } return 0; }