Made program multi-threaded

This commit is contained in:
Kevin
2017-07-30 21:06:53 -04:00
parent b1ca1c9097
commit 0ec0e69bfe
3 changed files with 63 additions and 38 deletions

Binary file not shown.

View File

@@ -28,14 +28,15 @@ double gbm_simulation(double spot, double rfr, double vol, double tte, double ra
void *run_simulations(void *opt_ptr) void *run_simulations(void *opt_ptr)
{ {
int i; int i;
double tte, theta_tte, expiry_date, value_date, level, rand, df; double tte, theta_tte, expiry_date, value_date, level, rand, df;
double delta_shift = 0, vega_shift = 0, theta_shift = 0, rho_shift = 0; double delta_shift = 0, vega_shift = 0, theta_shift = 0, rho_shift = 0;
double gamma_shift = 0, base_gamma = 0, upper_gamma = 0, lower_gamma = 0; double gamma_shift = 0, base_gamma = 0, upper_gamma = 0, lower_gamma = 0;
double price, delta, gamma, vega, theta, rho; double price, delta, gamma, vega, theta, rho;
double base = 0; double base = 0;
double max_rand = 0;
struct Option *opt = (struct Option*) opt_ptr;
struct Option *opt = (struct Option*) opt_ptr;
if (opt->sims < 1) opt->sims = 1; if (opt->sims < 1) opt->sims = 1;
@@ -45,7 +46,8 @@ void *run_simulations(void *opt_ptr)
theta_tte = tte - 1. / 365; theta_tte = tte - 1. / 365;
for (i=0; i<opt->sims; i++) { for (i=0; i<opt->sims; i++) {
rand = gaussrand(); rand = opt->randoms[i];
max_rand = rand > max_rand ? rand : max_rand;
/* Base scenario */ /* Base scenario */
level = gbm_simulation(opt->spot, opt->rfr, opt->vol, tte, rand); level = gbm_simulation(opt->spot, opt->rfr, opt->vol, tte, rand);
base += max((level - opt->strike) * opt->type, 0); base += max((level - opt->strike) * opt->type, 0);
@@ -59,8 +61,8 @@ void *run_simulations(void *opt_ptr)
base_gamma = gbm_simulation(opt->spot, opt->rfr, opt->vol, tte, rand); base_gamma = gbm_simulation(opt->spot, opt->rfr, opt->vol, tte, rand);
lower_gamma = gbm_simulation(opt->spot - 0.00001, opt->rfr, opt->vol, tte, rand); lower_gamma = gbm_simulation(opt->spot - 0.00001, opt->rfr, opt->vol, tte, rand);
gamma_shift += (max((upper_gamma - opt->strike) * opt->type, 0) - gamma_shift += (max((upper_gamma - opt->strike) * opt->type, 0) -
2 * max((base_gamma - opt->strike) * opt->type, 0) + 2 * max((base_gamma - opt->strike) * opt->type, 0) +
max((lower_gamma - opt->strike) * opt->type, 0)) / max((lower_gamma - opt->strike) * opt->type, 0)) /
pow(0.00001, 2); pow(0.00001, 2);
/* Vega scenario */ /* Vega scenario */
@@ -91,45 +93,67 @@ void *run_simulations(void *opt_ptr)
opt->theta = (theta - price) / (tte - theta_tte); opt->theta = (theta - price) / (tte - theta_tte);
opt->rho = (rho - price); opt->rho = (rho - price);
return (void *) opt; return (void *) opt;
} }
void gbm(struct Option *opt) void gbm(struct Option *opt)
{ {
int i; int i = 0, j = 0;
pthread_t *threads; pthread_t *threads;
struct Option *options; struct Option *options;
options = malloc(sizeof(struct Option) * NUM_THREADS); double **randoms;
opt->sims = opt->sims / NUM_THREADS; options = malloc(sizeof(struct Option) * NUM_THREADS);
for(i=0; i<NUM_THREADS; i++) { randoms = malloc(sizeof(double*) * NUM_THREADS);
options[i] = *opt; for(i=0; i<NUM_THREADS; i++) {
options[i].expiry_date = opt->expiry_date; randoms[i] = malloc(sizeof(double) * opt->sims / NUM_THREADS);
options[i].value_date = opt->value_date; }
}
threads = malloc(sizeof(pthread_t) * NUM_THREADS); /* generate array of normal randoms */
for(i=0;;) {
if (j == (opt->sims / NUM_THREADS)) {
j = 0;
i += 1;
}
for(i=0; i<NUM_THREADS; i++) { if (i == NUM_THREADS) {
if (pthread_create(&threads[i], NULL, run_simulations, &options[i])) { break;
printf("Error in thread creation\n"); }
}
}
opt->sims = 0;
for(i=0; i<NUM_THREADS; i++) { randoms[i][j] = gaussrand();
void *res; j++;
struct Option *result; }
pthread_join(threads[i], &res);
result = (struct Option*) res; opt->sims = opt->sims / NUM_THREADS;
opt->fv += result->fv / NUM_THREADS; for(i=0; i<NUM_THREADS; i++) {
opt->delta += result->delta / NUM_THREADS; options[i] = *opt;
opt->gamma += result->gamma / NUM_THREADS; options[i].expiry_date = opt->expiry_date;
opt->vega += result->vega / NUM_THREADS; options[i].value_date = opt->value_date;
opt->theta += result->theta / NUM_THREADS; options[i].randoms = randoms[i];
opt->rho += result->rho / NUM_THREADS; }
opt->sims += result->sims;
} threads = malloc(sizeof(pthread_t) * NUM_THREADS);
for(i=0; i<NUM_THREADS; i++) {
if (pthread_create(&threads[i], NULL, run_simulations, &options[i])) {
printf("Error in thread creation\n");
}
}
opt->sims = 0;
for(i=0; i<NUM_THREADS; i++) {
void *res;
struct Option *result;
pthread_join(threads[i], &res);
result = (struct Option*) res;
opt->fv += result->fv / NUM_THREADS;
opt->delta += result->delta / NUM_THREADS;
opt->gamma += result->gamma / NUM_THREADS;
opt->vega += result->vega / NUM_THREADS;
opt->theta += result->theta / NUM_THREADS;
opt->rho += result->rho / NUM_THREADS;
opt->sims += result->sims;
}
} }

View File

@@ -21,6 +21,7 @@ struct Option {
double rfr; double rfr;
double vol; double vol;
long sims; long sims;
double *randoms;
/* fv and greeks */ /* fv and greeks */
double fv; double fv;