tarf_monte_carlo 3.50 → 3.55

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d378d28787178efe7bf21a742d34f732738ca1dd0d6cc88aa82726676fb1c767
4
- data.tar.gz: 51ccc1a2352a018d9db80dd1b95acb51739c8ff51e6e80f73a8799444f381b21
3
+ metadata.gz: '06542868a0a9e3446d59880a36f2cc1f2d46c09ecf446c71fcad3ec235b08938'
4
+ data.tar.gz: a6922adce8b10804cf0b7c09e837d91736863c7f97de606d1d7bd0ec2c80fe11
5
5
  SHA512:
6
- metadata.gz: 6cac6595d2997a12b7d39835fbe6e072e6ab052ad90aa9f0579fb4d3e3355bb0ef947ed818b36308ccde5239aef682f299844fda9fdd4d1f6925dc2238f642fb
7
- data.tar.gz: 6df19f6a90f00d43c43b76a86e287ba2358ea63b47e68b75aa5fbc104ed562408dfbc0de249609b4446dafdda6a2fc54ab29cadc00f0d85d6d4025be02bd1b0a
6
+ metadata.gz: 6508270dcdc5a5958d2de0aec826a3141d0a315a72561798903315b9d0b37a49f960a9376e7b302f79a408996098cb13e715096fd123a0f536a15414d06c029a
7
+ data.tar.gz: 8a6fb8e1a8554fbda71200c360db164ec4bf07953f672b7bd6982f4c2b7d394c7930dac88931f801a93438568e5976be490d31d25c334beb82d8748ca18d49d9
@@ -45,6 +45,8 @@
45
45
  #define FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE 14
46
46
  #define FX_AMERICAN_BARRIER_WINDOW_KNOCKIN 15
47
47
  #define FX_AMERICAN_BARRIER_WINDOW_KNOCKOUT 16
48
+ #define FX_AMERICAN_BARRIER_KIKO_UNTIL_EXP 17
49
+ #define FX_AMERICAN_BARRIER_KIKO_UNTIL_KI 18
48
50
 
49
51
  #define DATAPOINTS 200 // data for plotting
50
52
  #define BARRIER_DP 4 // data for plotting
@@ -62,7 +64,8 @@ void Init_tarf_monte_carlo();
62
64
  double european_payoff(double, double, int, int, double);
63
65
  double get_equivalent_notional(int, double, float);
64
66
  double get_equivalent_rebate(int, double, float, int);
65
-
67
+ bool barrier_variations(int);
68
+ bool double_barrier_variations(int);
66
69
  // Prototype for our methods - methods are prefixed by 'method_' here
67
70
  VALUE method_box_muller( VALUE );
68
71
  VALUE method_run_monte_carlo( VALUE, VALUE );
@@ -98,9 +101,16 @@ double get_equivalent_notional(int conversion_sign, double notional, float rate)
98
101
 
99
102
  double get_equivalent_rebate(int conversion_sign, double rebate, float rate, int dir_sign){
100
103
  double total = (conversion_sign == 1) ? (rebate * rate) : rebate;
101
- return (-1 * dir_sign * total);
104
+ return (dir_sign * total);
105
+ }
106
+
107
+ bool barrier_variations(int KType){
108
+ return (KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE || KType == FX_AMERICAN_BARRIER_WINDOW_KNOCKIN || KType == FX_AMERICAN_BARRIER_WINDOW_KNOCKOUT || KType == FX_AMERICAN_BARRIER_KIKO_UNTIL_EXP || KType == FX_AMERICAN_BARRIER_KIKO_UNTIL_KI);
102
109
  }
103
110
 
111
+ bool double_barrier_variations(int KType){
112
+ return (KType == FX_AMERICAN_BARRIER_WINDOW_KNOCKIN || KType == FX_AMERICAN_BARRIER_WINDOW_KNOCKOUT || KType == FX_AMERICAN_BARRIER_KIKO_UNTIL_EXP || KType == FX_AMERICAN_BARRIER_KIKO_UNTIL_KI);
113
+ }
104
114
  // main method for running monte carlo simulation from sidekiq worker/outside method
105
115
  VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
106
116
  VALUE MCInputs = rb_ary_shift(args);
@@ -188,11 +198,11 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
188
198
  LSts_array[leg] = NUM2DBL( rb_ary_entry(LSts, leg) );
189
199
  USts_array[leg] = NUM2DBL( rb_ary_entry(USts, leg) );
190
200
  }
191
- } else if (KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE) {
201
+ } else if (barrier_variations(KType)) {
192
202
  for (leg = 0; leg < NL; ++leg) {
193
203
  Barrier_array[leg] = NUM2DBL( rb_ary_entry(Brs, leg) );
194
204
  Rebate_array[leg] = NUM2DBL( rb_ary_entry(Rbts, leg) );
195
- if(KType == FX_AMERICAN_BARRIER_WINDOW_KNOCKIN || KType == FX_AMERICAN_BARRIER_WINDOW_KNOCKOUT){
205
+ if(double_barrier_variations(KType)){
196
206
  Barrier2_array[leg] = NUM2DBL( rb_ary_entry(Brs2, leg) );
197
207
  }
198
208
  }
@@ -202,7 +212,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
202
212
  // first create a 1-D array of pointers, and then, for each array entry, create another 1-D array.
203
213
  //
204
214
  double **metrics;
205
- if (KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE){
215
+ if (barrier_variations(KType)){
206
216
  metrics = ( double** ) malloc( BARRIER_DP * sizeof(double*) );
207
217
  for( metric = 0; metric < BARRIER_DP; metric++ ) {
208
218
  metrics[metric] = ( double* ) malloc( DATAPOINTS * sizeof(double) );
@@ -238,8 +248,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
238
248
  sim_dash_neg[leg] = 0.0;
239
249
  }
240
250
 
241
- int knockedLeg = -1;
242
- int knockedLeg_dash = -1;
251
+ int knockedLeg = -1, knockedLeg_dash = -1, knockedLeg2 = -1, knockedLeg2_dash = -1;
243
252
  // legs loop start
244
253
  for( leg = 0; leg < NL; ++leg ) {
245
254
  double eps, eps_dash, drift, vSqrdt, profit_loss, profit_loss_dash;
@@ -269,6 +278,23 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
269
278
  if(knockedLeg_dash == -1 && knockin_dash >= 0){
270
279
  knockedLeg_dash = leg;
271
280
  }
281
+ } else if(KType == FX_AMERICAN_BARRIER_KIKO_UNTIL_KI || KType == FX_AMERICAN_BARRIER_KIKO_UNTIL_EXP){
282
+ profit_loss = Spot;
283
+ profit_loss_dash = Spot_dash;
284
+
285
+ if(knockedLeg == -1 && (Spot >= *(Barrier_array + leg))){
286
+ knockedLeg = leg;
287
+ }
288
+ if(knockedLeg_dash == -1 && (Spot_dash >= *(Barrier_array + leg))){
289
+ knockedLeg_dash = leg;
290
+ }
291
+
292
+ if(knockedLeg2 == -1 && (Spot >= *(Barrier2_array + leg))){
293
+ knockedLeg2 = leg;
294
+ }
295
+ if(knockedLeg2_dash == -1 && (Spot_dash >= *(Barrier2_array + leg))){
296
+ knockedLeg2_dash = leg;
297
+ }
272
298
  } else if(KType == FX_AMERICAN_BARRIER_WINDOW_KNOCKIN || KType == FX_AMERICAN_BARRIER_WINDOW_KNOCKOUT){
273
299
  profit_loss = Spot;
274
300
  profit_loss_dash = Spot_dash;
@@ -402,7 +428,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
402
428
  //
403
429
  // Store spot and spot dash
404
430
  //
405
- if (!(KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE)){
431
+ if (!(barrier_variations(KType))){
406
432
  if( point_pos <= SIM_LIMIT && (sim_count + 2) % INTERVAL == 0 ) {
407
433
  // rb_p(rb_str_new2("Leg:Spots"));
408
434
  metrics[ point_pos ][ leg ] = Spot;
@@ -420,7 +446,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
420
446
  // legs loop end
421
447
  // start from the Knock value
422
448
  double ko_so_far = K, ko_so_far_dash = K;
423
- if(KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE){
449
+ if(KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_WINDOW_KNOCKIN){
424
450
  if(knockedLeg >= 0){
425
451
  double equivalent_notional = get_equivalent_notional(conversion_sign, ( *( Ns_array + knockedLeg ) ), sim[NL-1]);
426
452
  sim_pos[NL-1] = european_payoff(( *( Xs_array + (knockedLeg) ) ), sim[NL-1], cp_sign, dir_sign, equivalent_notional);
@@ -443,7 +469,66 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
443
469
  metrics[1][point_pos+1] = (knockedLeg_dash >= 0) ? ( *( Xs_array + (knockedLeg_dash) ) ) : 0;
444
470
  metrics[3][point_pos+1] = knockedLeg_dash;
445
471
  }
446
- } else if(KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE){
472
+ } else if(KType == FX_AMERICAN_BARRIER_KIKO_UNTIL_EXP){
473
+ if(knockedLeg2 >= 0){
474
+ sim_pos[NL-1] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + 0 ) ), sim[NL-1], dir_sign);
475
+ } else if(knockedLeg >= 0) {
476
+ double equivalent_notional = get_equivalent_notional(conversion_sign, ( *( Ns_array + knockedLeg ) ), sim[NL-1]);
477
+ sim_pos[NL-1] = european_payoff(( *( Xs_array + (knockedLeg) ) ), sim[NL-1], cp_sign, dir_sign, equivalent_notional);
478
+ }
479
+ if(knockedLeg2_dash >= 0){
480
+ sim_dash_pos[NL-1] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + 0 ) ), sim_dash[NL-1], dir_sign);
481
+ } else if(knockedLeg_dash >= 0) {
482
+ double equivalent_notional = get_equivalent_notional(conversion_sign, ( *( Ns_array + knockedLeg_dash ) ), sim_dash[NL-1]);
483
+ sim_dash_pos[NL-1] = european_payoff(( *( Xs_array + (knockedLeg_dash) ) ), sim_dash[NL-1], cp_sign, dir_sign, equivalent_notional);
484
+ }
485
+
486
+ if ((point_pos < DATAPOINTS) && (sim_count % INTERVAL) == 0){
487
+ metrics[0][point_pos] = sim[NL-1];
488
+ metrics[1][point_pos] = (knockedLeg >= 0) ? ( *( Xs_array + (knockedLeg) ) ) : 0;
489
+ metrics[3][point_pos] = knockedLeg;
490
+ metrics[0][point_pos+1] = sim_dash[NL-1];
491
+ metrics[1][point_pos+1] = (knockedLeg_dash >= 0) ? ( *( Xs_array + (knockedLeg_dash) ) ) : 0;
492
+ metrics[3][point_pos+1] = knockedLeg_dash;
493
+ }
494
+ } else if(KType == FX_AMERICAN_BARRIER_KIKO_UNTIL_KI){
495
+ if(knockedLeg >= 0 && knockedLeg2 >= 0){
496
+ if(knockedLeg < knockedLeg2){
497
+ double equivalent_notional = get_equivalent_notional(conversion_sign, ( *( Ns_array + knockedLeg ) ), sim[NL-1]);
498
+ sim_pos[NL-1] = european_payoff(( *( Xs_array + (knockedLeg) ) ), sim[NL-1], cp_sign, dir_sign, equivalent_notional);
499
+ } else {
500
+ sim_pos[knockedLeg2] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + 0 ) ), sim[knockedLeg2], dir_sign);
501
+ }
502
+ } else if(knockedLeg2 >= 0){
503
+ sim_pos[knockedLeg2] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + 0 ) ), sim[knockedLeg2], dir_sign);
504
+ } else if(knockedLeg >= 0) {
505
+ double equivalent_notional = get_equivalent_notional(conversion_sign, ( *( Ns_array + knockedLeg ) ), sim[NL-1]);
506
+ sim_pos[NL-1] = european_payoff(( *( Xs_array + (knockedLeg) ) ), sim[NL-1], cp_sign, dir_sign, equivalent_notional);
507
+ }
508
+
509
+ if(knockedLeg_dash >= 0 && knockedLeg2_dash >= 0){
510
+ if(knockedLeg_dash < knockedLeg2_dash){
511
+ double equivalent_notional = get_equivalent_notional(conversion_sign, ( *( Ns_array + knockedLeg_dash ) ), sim_dash[NL-1]);
512
+ sim_dash_pos[NL-1] = european_payoff(( *( Xs_array + (knockedLeg_dash) ) ), sim_dash[NL-1], cp_sign, dir_sign, equivalent_notional);
513
+ } else {
514
+ sim_dash_pos[knockedLeg2_dash] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + 0 ) ), sim_dash[knockedLeg2_dash], dir_sign);
515
+ }
516
+ } else if(knockedLeg2_dash >= 0){
517
+ sim_dash_pos[knockedLeg2_dash] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + 0 ) ), sim_dash[knockedLeg2_dash], dir_sign);
518
+ } else if(knockedLeg_dash >= 0) {
519
+ double equivalent_notional = get_equivalent_notional(conversion_sign, ( *( Ns_array + knockedLeg_dash ) ), sim_dash[NL-1]);
520
+ sim_dash_pos[NL-1] = european_payoff(( *( Xs_array + (knockedLeg_dash) ) ), sim_dash[NL-1], cp_sign, dir_sign, equivalent_notional);
521
+ }
522
+
523
+ if ((point_pos < DATAPOINTS) && (sim_count % INTERVAL) == 0){
524
+ metrics[0][point_pos] = sim[NL-1];
525
+ metrics[1][point_pos] = (knockedLeg >= 0) ? ( *( Xs_array + (knockedLeg) ) ) : 0;
526
+ metrics[3][point_pos] = knockedLeg;
527
+ metrics[0][point_pos+1] = sim_dash[NL-1];
528
+ metrics[1][point_pos+1] = (knockedLeg_dash >= 0) ? ( *( Xs_array + (knockedLeg_dash) ) ) : 0;
529
+ metrics[3][point_pos+1] = knockedLeg_dash;
530
+ }
531
+ } else if(KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE || KType == FX_AMERICAN_BARRIER_WINDOW_KNOCKOUT){
447
532
  if(knockedLeg >= 0){
448
533
  sim_pos[knockedLeg] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + knockedLeg ) ), sim[knockedLeg], dir_sign);
449
534
  }
@@ -575,7 +660,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
575
660
  //
576
661
  // store and send whichever payoff you want
577
662
  //
578
- if (!(KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE)){
663
+ if (!(barrier_variations(KType))){
579
664
  if( point_pos <= SIM_LIMIT && (sim_count + 2) % INTERVAL == 0 ) {
580
665
  // rb_p(rb_str_new2("Leg:Payoffs"));
581
666
  metrics[ point_pos + 1 ][ leg ] = (sim_pos[leg] + sim_neg[leg]);
@@ -595,8 +680,8 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
595
680
  pvs_neg[sim_count] = sim_neg_sum;
596
681
  pvs_neg[sim_count + 1] = sim_dash_neg_sum;
597
682
 
598
-
599
- if (KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE){
683
+
684
+ if (barrier_variations(KType)){
600
685
  if ((point_pos < DATAPOINTS) && (sim_count % INTERVAL) == 0){
601
686
  metrics[2][point_pos] = sim_pos_sum;
602
687
  metrics[2][point_pos+1] = sim_dash_pos_sum;
@@ -646,8 +731,8 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
646
731
  //
647
732
  // rb_p(rb_str_new2("Converting metrics"));
648
733
  VALUE final_metrics = rb_ary_new();
649
-
650
- if (KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE){
734
+
735
+ if (barrier_variations(KType)){
651
736
  for(metric = 0; metric < BARRIER_DP; metric++) {
652
737
  VALUE leg_metrics = rb_ary_new();
653
738
 
@@ -680,7 +765,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
680
765
  //
681
766
  // free your arrays from memory once you're done using them
682
767
  //
683
- int dp = (KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE) ? BARRIER_DP : DATAPOINTS;
768
+ int dp = barrier_variations(KType) ? BARRIER_DP : DATAPOINTS;
684
769
  for(metric = 0; metric < dp; metric++) {
685
770
  free( metrics[metric] );
686
771
  }
@@ -3,5 +3,5 @@
3
3
  # gem yank tarf_monte_carlo -v 2.3
4
4
 
5
5
  module TarfMonteCarlo
6
- VERSION = "3.50"
6
+ VERSION = "3.55"
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tarf_monte_carlo
3
3
  version: !ruby/object:Gem::Version
4
- version: '3.50'
4
+ version: '3.55'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vivek Routh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-07 00:00:00.000000000 Z
11
+ date: 2020-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler