tarf_monte_carlo 3.47 → 3.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a69379430c97fbbf86b791e49a7564c7504a21fa73f3ea44ede3ec1d3a876bc6
4
- data.tar.gz: 3fcd6ef1020d223148c3279c80694c007373f933f434ee48258a62938188c130
3
+ metadata.gz: 60b2cf5e21d6d9a92dbef8d1988f1a0988b31279d7010aed5eec76327968bd29
4
+ data.tar.gz: b93ce27d9a66eb82683d3b1dff6aeb9c2bd98796bca3ca07fca85ab0fdf007d3
5
5
  SHA512:
6
- metadata.gz: 55528c53ec418a966b6e8e405ba177038e097993ac5a532bb99c4bfbc8d32048eea25ca8893749207c0ccb6e950ff0fdff6a573e99ccd71a5d627e40b3204a87
7
- data.tar.gz: fd79143e72702699469613433aeffcc7fe75fa001c69a6f759582bb72059ba8847862eea11357dc365d73acf06e34a0362d3e09518ac08a865cc0480da13e3ed
6
+ metadata.gz: 6231ef9420bd8dd40b08344732b75b741b76e6665d2aa1b89948b9f4ea96540b460759a1810cb07e64d2595dd3b978554eca426ca250d9f2f650b6955598eab5
7
+ data.tar.gz: 68ee5e27fb0c8789362bce8af4f69fa1f7e428e04752f298d6b053f4c23d53f703968eb3eb2c0ce011f20e315237bd3ee5701c702def7f748367d9bab0525a3c
@@ -43,6 +43,8 @@
43
43
 
44
44
  #define FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE 13
45
45
  #define FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE 14
46
+ #define FX_AMERICAN_BARRIER_WINDOW_KNOCKIN 15
47
+ #define FX_AMERICAN_BARRIER_WINDOW_KNOCKOUT 16
46
48
 
47
49
  #define DATAPOINTS 200 // data for plotting
48
50
  #define BARRIER_DP 4 // data for plotting
@@ -60,7 +62,7 @@ void Init_tarf_monte_carlo();
60
62
  double european_payoff(double, double, int, int, double);
61
63
  double get_equivalent_notional(int, double, float);
62
64
  double get_equivalent_rebate(int, double, float, int);
63
-
65
+ bool barrier_variations(int);
64
66
  // Prototype for our methods - methods are prefixed by 'method_' here
65
67
  VALUE method_box_muller( VALUE );
66
68
  VALUE method_run_monte_carlo( VALUE, VALUE );
@@ -96,9 +98,12 @@ double get_equivalent_notional(int conversion_sign, double notional, float rate)
96
98
 
97
99
  double get_equivalent_rebate(int conversion_sign, double rebate, float rate, int dir_sign){
98
100
  double total = (conversion_sign == 1) ? (rebate * rate) : rebate;
99
- return (-1 * dir_sign * total);
101
+ return (dir_sign * total);
100
102
  }
101
103
 
104
+ bool barrier_variations(int KType){
105
+ 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);
106
+ }
102
107
  // main method for running monte carlo simulation from sidekiq worker/outside method
103
108
  VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
104
109
  VALUE MCInputs = rb_ary_shift(args);
@@ -143,6 +148,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
143
148
  double *USts_array = ( double* ) malloc( NL * sizeof(double) );
144
149
  double *TempNs_array = ( double* ) malloc( NL * sizeof(double) );
145
150
  double *Barrier_array = ( double* ) malloc( NL * sizeof(double) );
151
+ double *Barrier2_array = ( double* ) malloc( NL * sizeof(double) );
146
152
  double *Rebate_array = ( double* ) malloc( NL * sizeof(double) );
147
153
 
148
154
  VALUE Ls = rb_hash_aref(MCInputs, rb_str_new2("leverage_ratios") );
@@ -157,6 +163,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
157
163
  VALUE LSts = rb_hash_aref(MCInputs, rb_str_new2("lower_strikes") );
158
164
  VALUE USts = rb_hash_aref(MCInputs, rb_str_new2("upper_strikes") );
159
165
  VALUE Brs = rb_hash_aref(MCInputs, rb_str_new2("barriers") );
166
+ VALUE Brs2 = rb_hash_aref(MCInputs, rb_str_new2("barriers2") );
160
167
  VALUE Rbts = rb_hash_aref(MCInputs, rb_str_new2("rebates") );
161
168
 
162
169
  for (leg = 0; leg < NL; ++leg) {
@@ -184,10 +191,13 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
184
191
  LSts_array[leg] = NUM2DBL( rb_ary_entry(LSts, leg) );
185
192
  USts_array[leg] = NUM2DBL( rb_ary_entry(USts, leg) );
186
193
  }
187
- } else if (KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE) {
194
+ } else if (barrier_variations(KType)) {
188
195
  for (leg = 0; leg < NL; ++leg) {
189
196
  Barrier_array[leg] = NUM2DBL( rb_ary_entry(Brs, leg) );
190
197
  Rebate_array[leg] = NUM2DBL( rb_ary_entry(Rbts, leg) );
198
+ if(KType == FX_AMERICAN_BARRIER_WINDOW_KNOCKIN || KType == FX_AMERICAN_BARRIER_WINDOW_KNOCKOUT){
199
+ Barrier2_array[leg] = NUM2DBL( rb_ary_entry(Brs2, leg) );
200
+ }
191
201
  }
192
202
  }
193
203
 
@@ -195,7 +205,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
195
205
  // first create a 1-D array of pointers, and then, for each array entry, create another 1-D array.
196
206
  //
197
207
  double **metrics;
198
- if (KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE){
208
+ if (barrier_variations(KType)){
199
209
  metrics = ( double** ) malloc( BARRIER_DP * sizeof(double*) );
200
210
  for( metric = 0; metric < BARRIER_DP; metric++ ) {
201
211
  metrics[metric] = ( double* ) malloc( DATAPOINTS * sizeof(double) );
@@ -204,7 +214,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
204
214
  }
205
215
  }
206
216
  }
207
- else{
217
+ else{
208
218
  metrics = ( double** ) malloc( DATAPOINTS * sizeof(double*) );
209
219
  for( metric = 0; metric < DATAPOINTS; metric++ ) {
210
220
  metrics[metric] = ( double* ) malloc( NL * sizeof(double) );
@@ -262,6 +272,16 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
262
272
  if(knockedLeg_dash == -1 && knockin_dash >= 0){
263
273
  knockedLeg_dash = leg;
264
274
  }
275
+ } else if(KType == FX_AMERICAN_BARRIER_WINDOW_KNOCKIN || KType == FX_AMERICAN_BARRIER_WINDOW_KNOCKOUT){
276
+ profit_loss = Spot;
277
+ profit_loss_dash = Spot_dash;
278
+
279
+ if(knockedLeg == -1 && (Spot <= *(Barrier_array + leg) || Spot >= *(Barrier2_array + leg))){
280
+ knockedLeg = leg;
281
+ }
282
+ if(knockedLeg_dash == -1 && (Spot_dash <= *(Barrier_array + leg) || Spot_dash >= *(Barrier2_array + leg))){
283
+ knockedLeg_dash = leg;
284
+ }
265
285
  } else if ( KType == DOUBLE_STRIKE_POINTS || KType == DOUBLE_STRIKE_ABSOLUTE || KType == DOUBLE_STRIKE_LEGS ) {
266
286
  if ( Spot < *( LSts_array + leg ) ) {
267
287
  profit_loss = Spot - (*( LSts_array + leg ));
@@ -385,7 +405,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
385
405
  //
386
406
  // Store spot and spot dash
387
407
  //
388
- if (!(KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE)){
408
+ if (!(barrier_variations(KType))){
389
409
  if( point_pos <= SIM_LIMIT && (sim_count + 2) % INTERVAL == 0 ) {
390
410
  // rb_p(rb_str_new2("Leg:Spots"));
391
411
  metrics[ point_pos ][ leg ] = Spot;
@@ -403,7 +423,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
403
423
  // legs loop end
404
424
  // start from the Knock value
405
425
  double ko_so_far = K, ko_so_far_dash = K;
406
- if(KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE){
426
+ if(KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_WINDOW_KNOCKIN){
407
427
  if(knockedLeg >= 0){
408
428
  double equivalent_notional = get_equivalent_notional(conversion_sign, ( *( Ns_array + knockedLeg ) ), sim[NL-1]);
409
429
  sim_pos[NL-1] = european_payoff(( *( Xs_array + (knockedLeg) ) ), sim[NL-1], cp_sign, dir_sign, equivalent_notional);
@@ -426,7 +446,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
426
446
  metrics[1][point_pos+1] = (knockedLeg_dash >= 0) ? ( *( Xs_array + (knockedLeg_dash) ) ) : 0;
427
447
  metrics[3][point_pos+1] = knockedLeg_dash;
428
448
  }
429
- } else if(KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE){
449
+ } else if(KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE || KType == FX_AMERICAN_BARRIER_WINDOW_KNOCKOUT){
430
450
  if(knockedLeg >= 0){
431
451
  sim_pos[knockedLeg] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + knockedLeg ) ), sim[knockedLeg], dir_sign);
432
452
  }
@@ -558,7 +578,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
558
578
  //
559
579
  // store and send whichever payoff you want
560
580
  //
561
- if (!(KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE)){
581
+ if (!(barrier_variations(KType))){
562
582
  if( point_pos <= SIM_LIMIT && (sim_count + 2) % INTERVAL == 0 ) {
563
583
  // rb_p(rb_str_new2("Leg:Payoffs"));
564
584
  metrics[ point_pos + 1 ][ leg ] = (sim_pos[leg] + sim_neg[leg]);
@@ -578,8 +598,8 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
578
598
  pvs_neg[sim_count] = sim_neg_sum;
579
599
  pvs_neg[sim_count + 1] = sim_dash_neg_sum;
580
600
 
581
-
582
- if (KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE){
601
+
602
+ if (barrier_variations(KType)){
583
603
  if ((point_pos < DATAPOINTS) && (sim_count % INTERVAL) == 0){
584
604
  metrics[2][point_pos] = sim_pos_sum;
585
605
  metrics[2][point_pos+1] = sim_dash_pos_sum;
@@ -629,8 +649,8 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
629
649
  //
630
650
  // rb_p(rb_str_new2("Converting metrics"));
631
651
  VALUE final_metrics = rb_ary_new();
632
-
633
- if (KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE){
652
+
653
+ if (barrier_variations(KType)){
634
654
  for(metric = 0; metric < BARRIER_DP; metric++) {
635
655
  VALUE leg_metrics = rb_ary_new();
636
656
 
@@ -663,7 +683,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
663
683
  //
664
684
  // free your arrays from memory once you're done using them
665
685
  //
666
- int dp = (KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE) ? BARRIER_DP : DATAPOINTS;
686
+ int dp = barrier_variations(KType) ? BARRIER_DP : DATAPOINTS;
667
687
  for(metric = 0; metric < dp; metric++) {
668
688
  free( metrics[metric] );
669
689
  }
@@ -3,5 +3,5 @@
3
3
  # gem yank tarf_monte_carlo -v 2.3
4
4
 
5
5
  module TarfMonteCarlo
6
- VERSION = "3.47"
6
+ VERSION = "3.53"
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.47'
4
+ version: '3.53'
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-06 00:00:00.000000000 Z
11
+ date: 2020-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -140,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
140
  - !ruby/object:Gem::Version
141
141
  version: '0'
142
142
  requirements: []
143
- rubygems_version: 3.0.8
143
+ rubygems_version: 3.0.6
144
144
  signing_key:
145
145
  specification_version: 4
146
146
  summary: Monte Carlo Simulation.