Class: TarfMonteCarlo::MC
- Inherits:
-
Object
- Object
- TarfMonteCarlo::MC
- Defined in:
- ext/tarf_monte_carlo/tarf_monte_carlo.c
Class Method Summary collapse
-
.box_muller ⇒ Object
eps = cos(2 * PI * z2) * sqrt( -2 * log( z1 ) ).
-
.run_monte_carlo(args) ⇒ Object
main method for running monte carlo simulation from sidekiq worker/outside method.
Class Method Details
.box_muller ⇒ Object
eps = cos(2 * PI * z2) * sqrt( -2 * log( z1 ) )
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'ext/tarf_monte_carlo/tarf_monte_carlo.c', line 78
VALUE method_box_muller( VALUE self ) {
double eps, z1, z2;
int i;
for (i = 0; i < 100; ++i) {
z1 = ( (double)rand() / (double)RAND_MAX );
}
for (i = 0; i < 100; ++i) {
z2 = ( (double)rand() / (double)RAND_MAX );
}
eps = cos( 2 * PI_VALUE * z2 ) * sqrt( -2 * log( z1 ) );
return DBL2NUM(eps);
}
|
.run_monte_carlo(args) ⇒ Object
main method for running monte carlo simulation from sidekiq worker/outside method
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 |
# File 'ext/tarf_monte_carlo/tarf_monte_carlo.c', line 114
VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
VALUE MCInputs = rb_ary_shift(args);
// rb_p(rb_str_new2("MC Inputs:"));
// rb_p(MCInputs);
// seed value for rand() function
srand( time(0) );
// initialize payoffs array
int leg, sim_count, metric, point_pos = 0;
double pvs_pos_sum = 0.0, pvs_neg_sum = 0.0;
int SCount = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("scount")) ) * 2;
int MCType = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("mc_type")) );
int NL = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("legs_count")) );
int BS = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("buy_sell")) );
double K = NUM2DBL( rb_hash_aref(MCInputs, rb_str_new2("knockout")) );
int CP = NUM2DBL( rb_hash_aref(MCInputs, rb_str_new2("callput")) );
int KType = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("knockout_type")) );
double S = NUM2DBL( rb_hash_aref(MCInputs, rb_str_new2("spot_rate")) );
int Ko_compare_mult = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("multiplier")) );
int ConvertNotional = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("convert_notional")) );
int dir_sign = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("direction_sign")) );
int dir2_sign = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("direction2_sign")) );
int cp_sign = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("callput_sign")) );
int conversion_sign = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("conversion_sign")) );
int rebate_conversion_sign = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("rebate_conversion_sign")) );
// assign leg specific attributes
double *pvs_pos = ( double* ) malloc( SCount * sizeof(double) );
double *pvs_neg = ( double* ) malloc( SCount * sizeof(double) );
double *Ls_array = ( double* ) malloc( NL * sizeof(double) );
double *Ts_array = ( double* ) malloc( NL * sizeof(double) );
double *Ns_array = ( double* ) malloc( NL * sizeof(double) );
double *Xs_array = ( double* ) malloc( NL * sizeof(double) );
double *vs_array = ( double* ) malloc( NL * sizeof(double) );
double *bs_array = ( double* ) malloc( NL * sizeof(double) );
double *DFs_array = ( double* ) malloc( NL * sizeof(double) );
double *Ps_array = ( double* ) malloc( NL * sizeof(double) );
double *CFs_array = ( double* ) malloc( NL * sizeof(double) );
double *LSts_array = ( double* ) malloc( NL * sizeof(double) );
double *USts_array = ( double* ) malloc( NL * sizeof(double) );
double *TempNs_array = ( double* ) malloc( NL * sizeof(double) );
double *Barrier_array = ( double* ) malloc( NL * sizeof(double) );
double *Barrier2_array = ( double* ) malloc( NL * sizeof(double) );
double *Rebate_array = ( double* ) malloc( NL * sizeof(double) );
VALUE Ls = rb_hash_aref(MCInputs, rb_str_new2("leverage_ratios") );
VALUE Ts = rb_hash_aref(MCInputs, rb_str_new2("expiration_times") );
VALUE Ns = rb_hash_aref(MCInputs, rb_str_new2("notionals") );
VALUE Xs = rb_hash_aref(MCInputs, rb_str_new2("strikes") );
VALUE vs = rb_hash_aref(MCInputs, rb_str_new2("volatilities") );
VALUE bs = rb_hash_aref(MCInputs, rb_str_new2("carry_rates") );
VALUE DFs = rb_hash_aref(MCInputs, rb_str_new2("discount_factors") );
VALUE Ps = rb_hash_aref(MCInputs, rb_str_new2("pivots") );
VALUE CFs = rb_hash_aref(MCInputs, rb_str_new2("caps") );
VALUE LSts = rb_hash_aref(MCInputs, rb_str_new2("lower_strikes") );
VALUE USts = rb_hash_aref(MCInputs, rb_str_new2("upper_strikes") );
VALUE Brs = rb_hash_aref(MCInputs, rb_str_new2("barriers") );
VALUE Brs2 = rb_hash_aref(MCInputs, rb_str_new2("barriers2") );
VALUE Rbts = rb_hash_aref(MCInputs, rb_str_new2("rebates") );
for (leg = 0; leg < NL; ++leg) {
Ls_array[leg] = NUM2DBL( rb_ary_entry(Ls, leg) );
Ts_array[leg] = NUM2DBL( rb_ary_entry(Ts, leg) );
Ns_array[leg] = NUM2DBL( rb_ary_entry(Ns, leg) );
Xs_array[leg] = NUM2DBL( rb_ary_entry(Xs, leg) );
vs_array[leg] = NUM2DBL( rb_ary_entry(vs, leg) );
bs_array[leg] = NUM2DBL( rb_ary_entry(bs, leg) );
DFs_array[leg] = NUM2DBL( rb_ary_entry(DFs, leg) );
TempNs_array[leg] = *(Ns_array + leg);
}
// extra tarf structures
if ( KType == PIVOT_POINTS || KType == PIVOT_ABSOLUTE || KType == PIVOT_LEGS ) {
for (leg = 0; leg < NL; ++leg) {
Ps_array[leg] = NUM2DBL( rb_ary_entry(Ps, leg) );
}
} else if ( KType == COLLAR_POINTS || KType == COLLAR_ABSOLUTE || KType == COLLAR_LEGS ) {
for (leg = 0; leg < NL; ++leg) {
CFs_array[leg] = NUM2DBL( rb_ary_entry(CFs, leg) );
}
} else if ( KType == DOUBLE_STRIKE_POINTS || KType == DOUBLE_STRIKE_ABSOLUTE || KType == DOUBLE_STRIKE_LEGS) {
for (leg = 0; leg < NL; ++leg) {
Ps_array[leg] = NUM2DBL( rb_ary_entry(Ps, leg) );
LSts_array[leg] = NUM2DBL( rb_ary_entry(LSts, leg) );
USts_array[leg] = NUM2DBL( rb_ary_entry(USts, leg) );
}
} else if (barrier_variations(KType)) {
for (leg = 0; leg < NL; ++leg) {
Barrier_array[leg] = NUM2DBL( rb_ary_entry(Brs, leg) );
Rebate_array[leg] = NUM2DBL( rb_ary_entry(Rbts, leg) );
if(double_barrier_variations(KType)){
Barrier2_array[leg] = NUM2DBL( rb_ary_entry(Brs2, leg) );
}
}
}
//
// first create a 1-D array of pointers, and then, for each array entry, create another 1-D array.
//
double **metrics;
if (barrier_variations(KType)){
metrics = ( double** ) malloc( BARRIER_DP * sizeof(double*) );
for( metric = 0; metric < BARRIER_DP; metric++ ) {
metrics[metric] = ( double* ) malloc( DATAPOINTS * sizeof(double) );
for(leg = 0; leg < DATAPOINTS; leg++) {
metrics[metric][leg] = 0.0;
}
}
}
else{
metrics = ( double** ) malloc( DATAPOINTS * sizeof(double*) );
for( metric = 0; metric < DATAPOINTS; metric++ ) {
metrics[metric] = ( double* ) malloc( NL * sizeof(double) );
for(leg = 0; leg < NL; leg++) {
metrics[metric][leg] = 0.0;
}
}
}
// run simulations loop
for( sim_count = 0; sim_count < SCount; sim_count += 2 ) {
// initial spot rate for each iteration would be current spot rate
double Spot = S, Spot_dash = S;
double sim[NL], sim_pos[NL], sim_neg[NL], sim_dash[NL], sim_dash_pos[NL], sim_dash_neg[NL];
// reset all simulation leg specific results
for ( leg = 0; leg < NL; ++leg ) {
sim[leg] = 0.0;
sim_pos[leg] = 0.0;
sim_neg[leg] = 0.0;
sim_dash[leg] = 0.0;
sim_dash_pos[leg] = 0.0;
sim_dash_neg[leg] = 0.0;
}
int knockedLeg = -1, knockedLeg_dash = -1, knockedLeg2 = -1, knockedLeg2_dash = -1;
// legs loop start
for( leg = 0; leg < NL; ++leg ) {
double eps, eps_dash, drift, vSqrdt, profit_loss, profit_loss_dash;
// get the random value for MC calculation
eps = NUM2DBL( method_box_muller( self ) );
eps_dash = eps * -1;
// main formula of MC simulation
drift = ( ( *( bs_array + leg ) ) - 0.5 * pow( ( *( vs_array + leg ) ), 2 ) ) * ( *( Ts_array + leg ) );
vSqrdt = ( *( vs_array + leg ) ) * sqrt( *( Ts_array + leg ) );
// spot calculation with epsilon & epsilon reverse
Spot = Spot * exp( drift + vSqrdt * eps );
Spot_dash = Spot_dash * exp( drift + vSqrdt * eps_dash );
if(KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE || KType == FX_AMERICAN_BARRIER_BINARY_IN_DISCRETE || KType == FX_AMERICAN_BARRIER_BINARY_OUT_DISCRETE || KType == FX_AMERICAN_BARRIER_BINARY_CALLPUT_IN_DISCRETE || KType == FX_AMERICAN_BARRIER_BINARY_CALLPUT_OUT_DISCRETE){
profit_loss = Spot;
profit_loss_dash = Spot_dash;
double knockin = (Spot - *(Barrier_array + leg)) * dir2_sign;
double knockin_dash = (Spot_dash - *(Barrier_array + leg)) * dir2_sign;
if(knockedLeg == -1 && knockin >= 0){
knockedLeg = leg;
}
if(knockedLeg_dash == -1 && knockin_dash >= 0){
knockedLeg_dash = leg;
}
} else if(KType == FX_AMERICAN_BARRIER_KIKO_UNTIL_KI || KType == FX_AMERICAN_BARRIER_KIKO_UNTIL_EXP){
profit_loss = Spot;
profit_loss_dash = Spot_dash;
// Both Until KI & Exp
if ( ((*(Barrier_array + leg) - *(Xs_array + leg)) * cp_sign <= 0) && ((*(Barrier2_array + leg) - *(Xs_array + leg)) * cp_sign >= 0) ){
if ((knockedLeg == -1) && ((*(Barrier_array + leg) - Spot) * cp_sign >= 0)){ knockedLeg = leg; }
if ((knockedLeg2 == -1) && ((*(Barrier2_array + leg) - Spot) * cp_sign <= 0)){ knockedLeg2 = leg; }
if ((knockedLeg_dash == -1) && ((*(Barrier_array + leg) - Spot_dash) * cp_sign >= 0)){ knockedLeg_dash = leg; }
if ((knockedLeg2_dash == -1) && ((*(Barrier2_array + leg) - Spot_dash) * cp_sign <= 0)){ knockedLeg2_dash = leg; }
}
else if( ((*(Barrier_array + leg) - *(Xs_array + leg)) * cp_sign >= 0) && ((*(Barrier2_array + leg) - *(Xs_array + leg)) * cp_sign <= 0) ){
if ((knockedLeg == -1) && ((*(Barrier_array + leg) - Spot) * cp_sign <= 0)){ knockedLeg = leg; }
if ((knockedLeg2 == -1) && ((*(Barrier2_array + leg) - Spot) * cp_sign >= 0)){ knockedLeg2 = leg; }
if ((knockedLeg_dash == -1) && ((*(Barrier_array + leg) - Spot_dash) * cp_sign <= 0)){ knockedLeg_dash = leg; }
if ((knockedLeg2_dash == -1) && ((*(Barrier2_array + leg) - Spot_dash) * cp_sign >= 0)){ knockedLeg2_dash = leg; }
}
// Only Until Exp
else if( (KType == FX_AMERICAN_BARRIER_KIKO_UNTIL_EXP) && ((*(Barrier_array + leg) - *(Xs_array + leg)) * cp_sign >= 0) && ((*(Barrier2_array + leg) - *(Barrier_array + leg)) * cp_sign >= 0) ){
if ((knockedLeg == -1) && ((*(Barrier_array + leg) - Spot) * cp_sign <= 0)){ knockedLeg = leg; }
if ((knockedLeg2 == -1) && ((*(Barrier2_array + leg) - Spot) * cp_sign <= 0)){ knockedLeg2 = leg; }
if ((knockedLeg_dash == -1) && ((*(Barrier_array + leg) - Spot_dash) * cp_sign <= 0)){ knockedLeg_dash = leg; }
if ((knockedLeg2_dash == -1) && ((*(Barrier2_array + leg) - Spot_dash) * cp_sign <= 0)){ knockedLeg2_dash = leg; }
}
else if( (KType == FX_AMERICAN_BARRIER_KIKO_UNTIL_EXP) && ((*(Barrier_array + leg) - *(Xs_array + leg)) * cp_sign <= 0) && ((*(Barrier2_array + leg) - *(Barrier_array + leg)) * cp_sign <= 0) ){
if ((knockedLeg == -1) && ((*(Barrier_array + leg) - Spot) * cp_sign >= 0)){ knockedLeg = leg; }
if ((knockedLeg2 == -1) && ((*(Barrier2_array + leg) - Spot) * cp_sign >= 0)){ knockedLeg2 = leg; }
if ((knockedLeg_dash == -1) && ((*(Barrier_array + leg) - Spot_dash) * cp_sign >= 0)){ knockedLeg_dash = leg; }
if ((knockedLeg2_dash == -1) && ((*(Barrier2_array + leg) - Spot_dash) * cp_sign >= 0)){ knockedLeg2_dash = leg; }
}
} else if(KType == FX_AMERICAN_BARRIER_DOUBLE_KNOCKIN || KType == FX_AMERICAN_BARRIER_DOUBLE_KNOCKOUT){
profit_loss = Spot;
profit_loss_dash = Spot_dash;
if(knockedLeg == -1 && (Spot <= *(Barrier_array + leg) || Spot >= *(Barrier2_array + leg))){
knockedLeg = leg;
}
if(knockedLeg_dash == -1 && (Spot_dash <= *(Barrier_array + leg) || Spot_dash >= *(Barrier2_array + leg))){
knockedLeg_dash = leg;
}
} else if ( KType == DOUBLE_STRIKE_POINTS || KType == DOUBLE_STRIKE_ABSOLUTE || KType == DOUBLE_STRIKE_LEGS ) {
if ( Spot < *( LSts_array + leg ) ) {
profit_loss = Spot - (*( LSts_array + leg ));
} else if(Spot > *( USts_array + leg )){
profit_loss = (*( USts_array + leg ) - Spot);
} else if(Spot >= *( LSts_array + leg ) && (Spot < *(Ps_array + leg))) {
profit_loss = Spot - (*( LSts_array + leg ));
} else {
profit_loss = (*( USts_array + leg )) - Spot;
}
if (ConvertNotional == 1){
if(Spot < *( Ps_array + leg ) || Spot_dash < *( Ps_array + leg )){
Ns_array[leg] = *(TempNs_array + leg) / *( LSts_array + leg );
} else {
Ns_array[leg] = *(TempNs_array + leg) / *( USts_array + leg );
}
}
// dash
if ( Spot_dash < *( LSts_array + leg ) ) {
profit_loss_dash = Spot_dash - (*( LSts_array + leg ));
} else if(Spot_dash > *( USts_array + leg )){
profit_loss_dash = (*( USts_array + leg ) - Spot_dash);
} else if(Spot_dash >= *( LSts_array + leg ) && (Spot_dash < *(Ps_array + leg))) {
profit_loss_dash = Spot_dash - (*( LSts_array + leg ));
} else {
profit_loss_dash = (*( USts_array + leg )) - Spot_dash;
}
} else if ( BS == BUY ) {
if ( KType == PIVOT_POINTS || KType == PIVOT_ABSOLUTE || KType == PIVOT_LEGS ) {
if ( *( Xs_array + leg ) > Spot && Spot > *( Ps_array + leg ) ) {
profit_loss = 0.0;
} else {
profit_loss = Spot - (*( Xs_array + leg ));
}
// dash
if ( *( Xs_array + leg ) > Spot_dash && Spot_dash > *( Ps_array + leg ) ) {
profit_loss_dash = 0.0;
} else {
profit_loss_dash = Spot_dash - (*( Xs_array + leg ));
}
} else if ( KType == COLLAR_POINTS || KType == COLLAR_ABSOLUTE || KType == COLLAR_LEGS ) {
if ( *( Xs_array + leg ) > Spot && Spot > *( CFs_array + leg ) ) {
profit_loss = 0.0;
} else if(Spot <= *( CFs_array + leg )){
profit_loss = Spot - (*( CFs_array + leg ));
if (ConvertNotional == 1){
Ns_array[leg] = *(TempNs_array + leg) / *( CFs_array + leg );
}
} else {
profit_loss = Spot - (*( Xs_array + leg ));
}
// dash
if ( *( Xs_array + leg ) > Spot_dash && Spot_dash > *( CFs_array + leg ) ) {
profit_loss_dash = 0.0;
} else if(Spot_dash <= *( CFs_array + leg )){
profit_loss_dash = Spot_dash - (*( CFs_array + leg ));
if (ConvertNotional == 1){
Ns_array[leg] = *(TempNs_array + leg) / *( CFs_array + leg );
}
} else {
profit_loss_dash = Spot_dash - (*( Xs_array + leg ));
}
} else {
// profit if spot is higher than market price
profit_loss = ( Spot - ( *( Xs_array + leg ) ) );
// profit if spot is higher than market price
profit_loss_dash = ( Spot_dash - ( *( Xs_array + leg ) ) );
}
} else if ( BS == SELL ) {
if ( KType == PIVOT_POINTS || KType == PIVOT_ABSOLUTE || KType == PIVOT_LEGS ) {
if ( *( Xs_array + leg ) < Spot && Spot < *( Ps_array + leg ) ) {
profit_loss = 0.0;
} else {
profit_loss = ( ( *( Xs_array + leg ) ) - Spot );
}
// dash
if ( *( Xs_array + leg ) < Spot_dash && Spot_dash < *( Ps_array + leg ) ) {
profit_loss_dash = 0.0;
} else {
profit_loss_dash = ( ( *( Xs_array + leg ) ) - Spot_dash );
}
} else if ( KType == COLLAR_POINTS || KType == COLLAR_ABSOLUTE || KType == COLLAR_LEGS ) {
if ( *( Xs_array + leg ) < Spot && Spot < *( CFs_array + leg ) ) {
profit_loss = 0.0;
} else if(Spot >= *( CFs_array + leg )){
profit_loss = ( ( *( CFs_array + leg ) ) - Spot );
if (ConvertNotional == 1){
Ns_array[leg] = *(TempNs_array + leg) / *( CFs_array + leg );
}
} else {
profit_loss = ( ( *( Xs_array + leg ) ) - Spot );
}
// dash
if ( *( Xs_array + leg ) < Spot_dash && Spot_dash < *( CFs_array + leg ) ) {
profit_loss_dash = 0.0;
} else if(Spot_dash >= *( CFs_array + leg )){
profit_loss_dash = ( ( *( CFs_array + leg ) ) - Spot_dash );
if (ConvertNotional == 1){
Ns_array[leg] = *(TempNs_array + leg) / *( CFs_array + leg );
}
} else {
profit_loss_dash = ( ( *( Xs_array + leg ) ) - Spot_dash );
}
} else {
// profit if spot is lower than market price
profit_loss = ( ( *( Xs_array + leg ) ) - Spot );
// profit if spot is lower than market price
profit_loss_dash = ( ( *( Xs_array + leg ) ) - Spot_dash );
}
}
sim[leg] = profit_loss;
sim_dash[leg] = profit_loss_dash;
//
// Store spot and spot dash
//
if (!(barrier_variations(KType))){
if( point_pos <= SIM_LIMIT && (sim_count + 2) % INTERVAL == 0 ) {
// rb_p(rb_str_new2("Leg:Spots"));
metrics[ point_pos ][ leg ] = Spot;
metrics[ point_pos + 2 ][ leg ] = Spot_dash;
}
}
// if excuted UNCHAINED method
// always use the current spot rate
if ( MCType == UNCHAINED ) {
Spot = S;
Spot_dash = S;
}
}
// legs loop end
// start from the Knock value
double ko_so_far = K, ko_so_far_dash = K;
if(KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_DOUBLE_KNOCKIN){
if(knockedLeg >= 0){
double equivalent_notional = *( Ns_array + knockedLeg );
sim_pos[NL-1] = european_payoff(( *( Xs_array + (knockedLeg) ) ), sim[NL-1], cp_sign, dir_sign, equivalent_notional);
}
else{
sim_pos[NL-1] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + 0 ) ), sim[NL-1], dir_sign);
}
if(knockedLeg_dash >= 0 ){
double equivalent_notional = *( Ns_array + knockedLeg_dash );
sim_dash_pos[NL-1] = european_payoff(( *( Xs_array + (knockedLeg_dash) ) ), sim_dash[NL-1], cp_sign, dir_sign, equivalent_notional);
}
else{
sim_dash_pos[NL-1] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + 0 ) ), sim_dash[NL-1], dir_sign);
}
if ((point_pos < DATAPOINTS) && (sim_count % INTERVAL) == 0){
metrics[0][point_pos] = sim[NL-1];
metrics[1][point_pos] = (knockedLeg >= 0) ? ( *( Xs_array + (knockedLeg) ) ) : 0;
metrics[3][point_pos] = knockedLeg;
metrics[0][point_pos+1] = sim_dash[NL-1];
metrics[1][point_pos+1] = (knockedLeg_dash >= 0) ? ( *( Xs_array + (knockedLeg_dash) ) ) : 0;
metrics[3][point_pos+1] = knockedLeg_dash;
}
} else if(KType == FX_AMERICAN_BARRIER_BINARY_IN_DISCRETE || KType == FX_AMERICAN_BARRIER_BINARY_CALLPUT_IN_DISCRETE){
if(knockedLeg >= 0){
int legIndex = (CP == 2) ? knockedLeg : (NL - 1);
int itm = true;
if (CP <= 1){
double tempPayOff = european_payoff(( *( Xs_array + legIndex ) ), sim[NL-1], cp_sign, dir_sign, 1);
if(tempPayOff == 0){
itm = false;
}
}
if (itm){
sim_pos[legIndex] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + 0 ) ), sim[legIndex], dir_sign);
}
}
if(knockedLeg_dash >= 0){
int legIndex_dash = (CP == 2) ? knockedLeg_dash : (NL - 1);
int itm = true;
if (CP <= 1){
double tempPayOff = european_payoff(( *( Xs_array + legIndex_dash ) ), sim_dash[NL-1], cp_sign, dir_sign, 1);
if(tempPayOff == 0){
itm = false;
}
}
if (itm){
sim_dash_pos[legIndex_dash] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + 0 ) ), sim_dash[legIndex_dash], dir_sign);
}
}
if ((point_pos < DATAPOINTS) && (sim_count % INTERVAL) == 0){
metrics[0][point_pos] = sim[NL-1];
metrics[1][point_pos] = *( Xs_array + 0 );
metrics[0][point_pos+1] = sim_dash[NL-1];
metrics[1][point_pos+1] = *( Xs_array + 0 );
}
} else if(KType == FX_AMERICAN_BARRIER_BINARY_OUT_DISCRETE || KType == FX_AMERICAN_BARRIER_BINARY_CALLPUT_OUT_DISCRETE){
if(knockedLeg == -1){
int itm = true;
if (CP <= 1){
double tempPayOff = european_payoff(( *( Xs_array + 0 ) ), sim[NL-1], cp_sign, dir_sign, 1);
if(tempPayOff == 0){
itm = false;
}
}
if (itm){
sim_pos[NL - 1] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + 0 ) ), sim[NL - 1], dir_sign);
}
}
if(knockedLeg_dash == -1){
int itm = true;
if (CP <= 1){
double tempPayOff = european_payoff(( *( Xs_array + 0 ) ), sim_dash[NL-1], cp_sign, dir_sign, 1);
if(tempPayOff == 0){
itm = false;
}
}
if (itm){
sim_dash_pos[NL - 1] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + 0 ) ), sim_dash[NL - 1], dir_sign);
}
}
if ((point_pos < DATAPOINTS) && (sim_count % INTERVAL) == 0){
metrics[0][point_pos] = sim[NL-1];
metrics[1][point_pos] = *( Xs_array + 0 );
metrics[0][point_pos+1] = sim_dash[NL-1];
metrics[1][point_pos+1] = *( Xs_array + 0 );
}
} else if(KType == FX_AMERICAN_BARRIER_KIKO_UNTIL_EXP){
if(knockedLeg2 >= 0){
sim_pos[knockedLeg2] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + knockedLeg2 ) ), sim[knockedLeg2], dir_sign);
} else if(knockedLeg >= 0) {
double equivalent_notional = *( Ns_array + knockedLeg );
sim_pos[NL-1] = european_payoff(( *( Xs_array + (knockedLeg) ) ), sim[NL-1], cp_sign, dir_sign, equivalent_notional);
}
if(knockedLeg2_dash >= 0){
sim_dash_pos[knockedLeg2_dash] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + knockedLeg2 ) ), sim_dash[knockedLeg2_dash], dir_sign);
} else if(knockedLeg_dash >= 0) {
double equivalent_notional = *( Ns_array + knockedLeg_dash );
sim_dash_pos[NL-1] = european_payoff(( *( Xs_array + (knockedLeg_dash) ) ), sim_dash[NL-1], cp_sign, dir_sign, equivalent_notional);
}
if ((point_pos < DATAPOINTS) && (sim_count % INTERVAL) == 0){
metrics[0][point_pos] = sim[NL-1];
metrics[1][point_pos] = (knockedLeg >= 0) ? ( *( Xs_array + (knockedLeg) ) ) : 0;
metrics[3][point_pos] = knockedLeg;
metrics[0][point_pos+1] = sim_dash[NL-1];
metrics[1][point_pos+1] = (knockedLeg_dash >= 0) ? ( *( Xs_array + (knockedLeg_dash) ) ) : 0;
metrics[3][point_pos+1] = knockedLeg_dash;
}
} else if(KType == FX_AMERICAN_BARRIER_KIKO_UNTIL_KI){
if(knockedLeg >= 0 && knockedLeg2 >= 0){
if(knockedLeg < knockedLeg2){
double equivalent_notional = *( Ns_array + knockedLeg );
sim_pos[NL-1] = european_payoff(( *( Xs_array + (knockedLeg) ) ), sim[NL-1], cp_sign, dir_sign, equivalent_notional);
} else {
sim_pos[knockedLeg2] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + knockedLeg2 ) ), sim[knockedLeg2], dir_sign);
}
} else if(knockedLeg2 >= 0){
sim_pos[knockedLeg2] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + knockedLeg2 ) ), sim[knockedLeg2], dir_sign);
} else if(knockedLeg >= 0) {
double equivalent_notional = *( Ns_array + knockedLeg );
sim_pos[NL-1] = european_payoff(( *( Xs_array + (knockedLeg) ) ), sim[NL-1], cp_sign, dir_sign, equivalent_notional);
}
if(knockedLeg_dash >= 0 && knockedLeg2_dash >= 0){
if(knockedLeg_dash < knockedLeg2_dash){
double equivalent_notional = *( Ns_array + knockedLeg_dash );
sim_dash_pos[NL-1] = european_payoff(( *( Xs_array + (knockedLeg_dash) ) ), sim_dash[NL-1], cp_sign, dir_sign, equivalent_notional);
} else {
sim_dash_pos[knockedLeg2_dash] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + knockedLeg2_dash ) ), sim_dash[knockedLeg2_dash], dir_sign);
}
} else if(knockedLeg2_dash >= 0){
sim_dash_pos[knockedLeg2_dash] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + knockedLeg2_dash ) ), sim_dash[knockedLeg2_dash], dir_sign);
} else if(knockedLeg_dash >= 0) {
double equivalent_notional = *( Ns_array + knockedLeg_dash );
sim_dash_pos[NL-1] = european_payoff(( *( Xs_array + (knockedLeg_dash) ) ), sim_dash[NL-1], cp_sign, dir_sign, equivalent_notional);
}
if ((point_pos < DATAPOINTS) && (sim_count % INTERVAL) == 0){
metrics[0][point_pos] = sim[NL-1];
metrics[1][point_pos] = (knockedLeg >= 0) ? ( *( Xs_array + (knockedLeg) ) ) : 0;
metrics[3][point_pos] = knockedLeg;
metrics[0][point_pos+1] = sim_dash[NL-1];
metrics[1][point_pos+1] = (knockedLeg_dash >= 0) ? ( *( Xs_array + (knockedLeg_dash) ) ) : 0;
metrics[3][point_pos+1] = knockedLeg_dash;
}
} else if(KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE || KType == FX_AMERICAN_BARRIER_DOUBLE_KNOCKOUT){
if(knockedLeg >= 0){
sim_pos[knockedLeg] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + knockedLeg ) ), sim[knockedLeg], dir_sign);
}
else{
double equivalent_notional = *( Ns_array + 0 );
sim_pos[NL-1] = european_payoff(( *( Xs_array + 0 ) ), sim[NL-1], cp_sign, dir_sign, equivalent_notional);
}
if(knockedLeg_dash >= 0 ){
sim_dash_pos[knockedLeg_dash] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + knockedLeg_dash ) ), sim[knockedLeg_dash], dir_sign);
}
else{
double equivalent_notional = *( Ns_array + 0 );
sim_dash_pos[NL-1] = european_payoff(( *( Xs_array + 0 ) ), sim_dash[NL-1], cp_sign, dir_sign, equivalent_notional);
}
if ((point_pos < DATAPOINTS) && (sim_count % INTERVAL) == 0){
metrics[0][point_pos] = (knockedLeg >= 0) ? sim[knockedLeg] : sim[NL-1];
metrics[1][point_pos] = ( *( Xs_array + 0 ) );
metrics[3][point_pos] = knockedLeg;
metrics[0][point_pos+1] = (knockedLeg_dash >= 0) ? sim[knockedLeg_dash] : sim[NL-1];
metrics[1][point_pos+1] = ( *( Xs_array + 0 ) );
metrics[3][point_pos+1] = knockedLeg_dash;
}
} else if( KType == ABSOLUTE || KType == PIVOT_ABSOLUTE || KType == COLLAR_ABSOLUTE || KType == DOUBLE_STRIKE_ABSOLUTE ) {
for( leg = 0; leg < NL; ++leg ) {
// simulation normal
if( ko_so_far > 0.0 ) {
if( sim[leg] >= 0.0 ) {
// knock out condition
double temp_payoff = sim[leg] * ( *( Ns_array + leg ) );
double temp_payoff_ko_ccy = temp_payoff * pow( *( Xs_array + leg ), Ko_compare_mult );
if( temp_payoff_ko_ccy > ko_so_far ) {
sim_pos[leg] = ko_so_far * pow( *( Xs_array + leg ), (-1 * Ko_compare_mult) );
ko_so_far = 0.0;
} else if( temp_payoff_ko_ccy <= ko_so_far ) {
sim_pos[leg] = temp_payoff; // take the payoff in +ve's
ko_so_far -= temp_payoff_ko_ccy; // update the knock out
}
} else {
sim_neg[leg] = sim[leg] * ( *( Ls_array + leg ) ) * ( *( Ns_array + leg ) );
}
}
// simulation reverse dash
if ( ko_so_far_dash > 0.0 ) {
if( sim_dash[leg] >= 0.0 ) {
// knock out condition
double temp_payoff_dash = sim_dash[leg] * ( *( Ns_array + leg ) );
double temp_payoff_ko_ccy = temp_payoff_dash * pow( *( Xs_array + leg ), Ko_compare_mult );
if( temp_payoff_ko_ccy > ko_so_far_dash ) {
sim_dash_pos[leg] = ko_so_far_dash * pow( *( Xs_array + leg ), (-1 * Ko_compare_mult) );
ko_so_far_dash = 0.0;
} else if( temp_payoff_ko_ccy <= ko_so_far_dash ) {
sim_dash_pos[leg] = temp_payoff_dash; // take the payoff in +ve's
ko_so_far_dash -= temp_payoff_ko_ccy; // update the knock out
}
} else {
sim_dash_neg[leg] = sim_dash[leg] * ( *( Ls_array + leg ) ) * ( *( Ns_array + leg ) );
}
}
}
} else if( KType == POINTS || KType == PIVOT_POINTS || KType == COLLAR_POINTS || KType == DOUBLE_STRIKE_POINTS ) {
for( leg = 0; leg < NL; ++leg ) {
// simulation normal
// rb_p(DBL2NUM(*(Ns_array + leg )));
if ( ko_so_far > 0.0 ) {
if( sim[leg] >= 0.0 ) {
// knock out condition
if( sim[leg] > ko_so_far ) {
sim_pos[leg] = ko_so_far * ( *( Ns_array + leg ) ); // discuss this
ko_so_far = 0.0;
} else if( sim[leg] <= ko_so_far ) {
sim_pos[leg] = sim[leg] * ( *( Ns_array + leg ) );
ko_so_far -= sim[leg];
}
} else {
sim_neg[leg] = sim[leg] * ( *( Ls_array + leg ) ) * ( *( Ns_array + leg ) );
}
}
// simulation reverse dash
if ( ko_so_far_dash > 0.0 ) {
if( sim_dash[leg] >= 0.0 ) {
// knock out condition
if( sim_dash[leg] > ko_so_far_dash ) {
sim_dash_pos[leg] = ko_so_far_dash * ( *( Ns_array + leg ) ); // discuss this
ko_so_far_dash = 0.0;
} else if( sim_dash[leg] <= ko_so_far_dash ) {
sim_dash_pos[leg] = sim_dash[leg] * ( *( Ns_array + leg ) );
ko_so_far_dash -= sim_dash[leg];
}
} else {
sim_dash_neg[leg] = sim_dash[leg] * ( *( Ls_array + leg ) ) * ( *( Ns_array + leg ) );
}
}
}
} else if( KType == LEGS || KType == PIVOT_LEGS || KType == COLLAR_LEGS || KType == DOUBLE_STRIKE_LEGS ) {
for( leg = 0; leg < NL; ++leg ) {
// simulation normal
if ( ko_so_far > 0.0 ) {
if( sim[leg] >= 0.0 ) {
// knock out condition
sim_pos[leg] = sim[leg] * ( *( Ns_array + leg ) );
ko_so_far -= 1;
} else {
sim_neg[leg] = sim[leg] * ( *( Ls_array + leg ) ) * ( *( Ns_array + leg ) );
}
}
// simulation reverse dash
if ( ko_so_far_dash > 0.0 ) {
if( sim_dash[leg] >= 0.0 ) {
// knock out condition
sim_dash_pos[leg] = sim_dash[leg] * ( *( Ns_array + leg ) );
ko_so_far_dash -= 1;
} else {
sim_dash_neg[leg] = sim_dash[leg] * ( *( Ls_array + leg ) ) * ( *( Ns_array + leg ) );
}
}
}
}
// take sum of positive and negative payoffs and calculate pvs
double sim_pos_sum = 0.0, sim_neg_sum = 0.0, sim_dash_pos_sum = 0.0, sim_dash_neg_sum = 0.0;
for( leg = 0; leg < NL; ++leg ) {
//
// store and send whichever payoff you want
//
if (!(barrier_variations(KType))){
if( point_pos <= SIM_LIMIT && (sim_count + 2) % INTERVAL == 0 ) {
// rb_p(rb_str_new2("Leg:Payoffs"));
metrics[ point_pos + 1 ][ leg ] = (sim_pos[leg] + sim_neg[leg]);
metrics[ point_pos + 3 ][ leg ] = (sim_dash_pos[leg] + sim_dash_neg[leg]);
}
}
sim_pos_sum += sim_pos[leg] * ( *( DFs_array + leg ) );
sim_neg_sum += sim_neg[leg] * ( *( DFs_array + leg ) );
sim_dash_pos_sum += sim_dash_pos[leg] * ( *( DFs_array + leg ) );
sim_dash_neg_sum += sim_dash_neg[leg] * ( *( DFs_array + leg ) );
}
// calculate +pv and -ve pv for individual leg
pvs_pos[sim_count] = sim_pos_sum;
pvs_pos[sim_count + 1] = sim_dash_pos_sum;
pvs_neg[sim_count] = sim_neg_sum;
pvs_neg[sim_count + 1] = sim_dash_neg_sum;
if (barrier_variations(KType)){
if ((point_pos < DATAPOINTS) && (sim_count % INTERVAL) == 0){
metrics[2][point_pos] = sim_pos_sum;
metrics[2][point_pos+1] = sim_dash_pos_sum;
point_pos += 2;
}
}
else{
//
// increment metric storing point by 4
// Note: no limit condition here
//
if( (sim_count + 2) % INTERVAL == 0 ) {
// rb_p(rb_str_new2("T:"));
// rb_p(INT2NUM(point_pos));
point_pos += 4;
}
}
}
// run simulations loop
// rb_p(rb_str_new2("Simulation loop done"));
//
// sum all final +ve and -ve payoffs
//
// rb_p(rb_str_new2("Calculating final PVs"));
for(sim_count = 0; sim_count < SCount; ++sim_count) {
pvs_pos_sum += *(pvs_pos + sim_count);
pvs_neg_sum += *(pvs_neg + sim_count);
}
//
// free dynamically alloted heap memory
//
// rb_p(rb_str_new2("Free allocated arrays"));
free(pvs_pos);
free(pvs_neg);
free(Ls_array);
free(Ts_array);
free(Ns_array);
free(Xs_array);
free(vs_array);
free(bs_array);
free(DFs_array);
//
// return both payoffs
//
// rb_p(rb_str_new2("Converting metrics"));
VALUE final_metrics = rb_ary_new();
if (barrier_variations(KType)){
for(metric = 0; metric < BARRIER_DP; metric++) {
VALUE leg_metrics = rb_ary_new();
for(leg = 0; leg < DATAPOINTS; leg++) {
rb_ary_push( leg_metrics, DBL2NUM( metrics[metric][leg] ) );
}
rb_ary_push(final_metrics, leg_metrics);
}
}
else{
for(metric = 0; metric < DATAPOINTS; metric++) {
VALUE leg_metrics = rb_ary_new();
for(leg = 0; leg < NL; leg++) {
rb_ary_push( leg_metrics, DBL2NUM( metrics[metric][leg] ) );
}
rb_ary_push(final_metrics, leg_metrics);
}
}
// rb_p(rb_str_new2("Generating output hash"));
VALUE final_pvs = rb_hash_new();
rb_hash_aset(final_pvs, rb_str_new2("positive_pv"), DBL2NUM( pvs_pos_sum / SCount ));
rb_hash_aset(final_pvs, rb_str_new2("negative_pv"), DBL2NUM( pvs_neg_sum / SCount ));
rb_hash_aset(final_pvs, rb_str_new2("total_pv"), DBL2NUM( (pvs_neg_sum + pvs_pos_sum) / SCount ));
rb_hash_aset(final_pvs, rb_str_new2("metrics"), final_metrics);
//
// free your arrays from memory once you're done using them
//
int dp = barrier_variations(KType) ? BARRIER_DP : DATAPOINTS;
for(metric = 0; metric < dp; metric++) {
free( metrics[metric] );
}
free(metrics);
return final_pvs;
}
|