tarf_monte_carlo 3.5 → 3.13

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: 67b1ad29c02a6eaa79c27b2bcd092d8d58736de856228479553c01275c74aa23
4
- data.tar.gz: a0f8a59cc28cec075cdaf3181aa41df49068efa7ef53613fb840d7582f91194d
3
+ metadata.gz: bf901445c70d80f370053ffa90ba2cc6fb3e12b9ad7c372777cae63b92ceecdb
4
+ data.tar.gz: 04331203b779e146db0e48a7eb9d1a0452c31aee17311c5f3c863d2104bc080b
5
5
  SHA512:
6
- metadata.gz: 71691b20d4de8c2c218280998dbd67a2c89d829001b46181186560ed15f8e644b1cc6e9c83b737c4f59e77adfe22e65bc34f2184d9e8d9f1f4d59efff8a2193e
7
- data.tar.gz: c8b637dfab5f1153e02bd61ff4e48742cd15507fd3dba45af07bad6a63224ce962ed8afbc88011d241cb82a919f94901fd76fbaa6a8e6f192beb3518c2fa8888
6
+ metadata.gz: b912836d2d1a64d62c9b1d394be9ca89c5dbeb245312ce18d9490751bf61fd1fc9ade361a60446d1a0b1df7928882e5e4d0691b0e8c7528733f9bdfc49ccf6b2
7
+ data.tar.gz: '08248d29944ff3fb8981d7bda9506bd056869575e54cb1ec6aa3be403b945cbe1b11c6bcb1cc42193f5617330d1be6633f4f1c2ab331cd2cc70d4cf455ef7df1'
data/.gitignore CHANGED
File without changes
@@ -28,6 +28,9 @@
28
28
  #define ABSOLUTE 1 // knockout by absolute
29
29
  #define POINTS 2 // knockout by points
30
30
  #define LEGS 3 // knockout by legs
31
+ #define PIVOT_POINTS 4
32
+ #define COLLAR_POINTS 5
33
+ #define DUAL_STRIKE_POINTS 6
31
34
  #define DATAPOINTS 200 // data for plotting
32
35
  #define INTERVAL 50
33
36
  #define SIM_LIMIT 196 // 196 + 4 = 200 simulations nedded
@@ -67,8 +70,8 @@ VALUE method_box_muller( VALUE self ) {
67
70
  // main method for running monte carlo simulation from sidekiq worker/outside method
68
71
  VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
69
72
  VALUE MCInputs = rb_ary_shift(args);
70
- rb_p(rb_str_new2("MC Inputs:"));
71
- rb_p(MCInputs);
73
+ // rb_p(rb_str_new2("MC Inputs:"));
74
+ // rb_p(MCInputs);
72
75
 
73
76
  // seed value for rand() function
74
77
  srand( time(0) );
@@ -87,15 +90,19 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
87
90
  int Ko_compare_mult = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("multiplier")) );
88
91
 
89
92
  // assign leg specific attributes
90
- double *pvs_pos = ( double* ) malloc( SCount * sizeof(double) );
91
- double *pvs_neg = ( double* ) malloc( SCount * sizeof(double) );
92
- double *Ls_array = ( double* ) malloc( NL * sizeof(double) );
93
- double *Ts_array = ( double* ) malloc( NL * sizeof(double) );
94
- double *Ns_array = ( double* ) malloc( NL * sizeof(double) );
95
- double *Xs_array = ( double* ) malloc( NL * sizeof(double) );
96
- double *vs_array = ( double* ) malloc( NL * sizeof(double) );
97
- double *bs_array = ( double* ) malloc( NL * sizeof(double) );
98
- double *DFs_array = ( double* ) malloc( NL * sizeof(double) );
93
+ double *pvs_pos = ( double* ) malloc( SCount * sizeof(double) );
94
+ double *pvs_neg = ( double* ) malloc( SCount * sizeof(double) );
95
+ double *Ls_array = ( double* ) malloc( NL * sizeof(double) );
96
+ double *Ts_array = ( double* ) malloc( NL * sizeof(double) );
97
+ double *Ns_array = ( double* ) malloc( NL * sizeof(double) );
98
+ double *Xs_array = ( double* ) malloc( NL * sizeof(double) );
99
+ double *vs_array = ( double* ) malloc( NL * sizeof(double) );
100
+ double *bs_array = ( double* ) malloc( NL * sizeof(double) );
101
+ double *DFs_array = ( double* ) malloc( NL * sizeof(double) );
102
+ double *Ps_array = ( double* ) malloc( NL * sizeof(double) );
103
+ double *CFs_array = ( double* ) malloc( NL * sizeof(double) );
104
+ double *LSts_array = ( double* ) malloc( NL * sizeof(double) );
105
+ double *USts_array = ( double* ) malloc( NL * sizeof(double) );
99
106
 
100
107
  VALUE Ls = rb_hash_aref(MCInputs, rb_str_new2("leverage_ratios") );
101
108
  VALUE Ts = rb_hash_aref(MCInputs, rb_str_new2("expiration_times") );
@@ -104,6 +111,10 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
104
111
  VALUE vs = rb_hash_aref(MCInputs, rb_str_new2("volatilities") );
105
112
  VALUE bs = rb_hash_aref(MCInputs, rb_str_new2("carry_rates") );
106
113
  VALUE DFs = rb_hash_aref(MCInputs, rb_str_new2("discount_factors") );
114
+ VALUE Ps = rb_hash_aref(MCInputs, rb_str_new2("pivots") );
115
+ VALUE CFs = rb_hash_aref(MCInputs, rb_str_new2("caps") );
116
+ VALUE LSts = rb_hash_aref(MCInputs, rb_str_new2("lower_strikes") );
117
+ VALUE USts = rb_hash_aref(MCInputs, rb_str_new2("upper_strikes") );
107
118
 
108
119
  for (leg = 0; leg < NL; ++leg) {
109
120
  Ls_array[leg] = NUM2DBL( rb_ary_entry(Ls, leg) );
@@ -114,6 +125,21 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
114
125
  bs_array[leg] = NUM2DBL( rb_ary_entry(bs, leg) );
115
126
  DFs_array[leg] = NUM2DBL( rb_ary_entry(DFs, leg) );
116
127
  }
128
+ // extra tarf structures
129
+ if ( KType == PIVOT_POINTS ) {
130
+ for (leg = 0; leg < NL; ++leg) {
131
+ Ps_array[leg] = NUM2DBL( rb_ary_entry(Ps, leg) );
132
+ }
133
+ } else if ( KType == COLLAR_POINTS ) {
134
+ for (leg = 0; leg < NL; ++leg) {
135
+ CFs_array[leg] = NUM2DBL( rb_ary_entry(CFs, leg) );
136
+ }
137
+ } else if ( KType == DUAL_STRIKE_POINTS ) {
138
+ for (leg = 0; leg < NL; ++leg) {
139
+ LSts_array[leg] = NUM2DBL( rb_ary_entry(LSts, leg) );
140
+ USts_array[leg] = NUM2DBL( rb_ary_entry(USts, leg) );
141
+ }
142
+ }
117
143
 
118
144
  //
119
145
  // first create a 1-D array of pointers, and then, for each array entry, create another 1-D array.
@@ -166,10 +192,24 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
166
192
  // profit if spot is higher than market price
167
193
  profit_loss_dash = ( Spot_dash - ( *( Xs_array + leg ) ) );
168
194
  } else if ( BS == SELL ) {
169
- // profit if spot is lower than market price
170
- profit_loss = ( ( *( Xs_array + leg ) ) - Spot );
171
- // profit if spot is lower than market price
172
- profit_loss_dash = ( ( *( Xs_array + leg ) ) - Spot_dash );
195
+ if ( KType == PIVOT_POINTS ) {
196
+ if ( *( Xs_array + leg ) < Spot && Spot < *( Ps_array + leg ) ) {
197
+ profit_loss = 0.0;
198
+ } else {
199
+ profit_loss = ( ( *( Xs_array + leg ) ) - Spot );
200
+ }
201
+ // dash
202
+ if ( *( Xs_array + leg ) < Spot_dash && Spot_dash < *( Ps_array + leg ) ) {
203
+ profit_loss_dash = 0.0;
204
+ } else {
205
+ profit_loss_dash = ( ( *( Xs_array + leg ) ) - Spot_dash );
206
+ }
207
+ } else {
208
+ // profit if spot is lower than market price
209
+ profit_loss = ( ( *( Xs_array + leg ) ) - Spot );
210
+ // profit if spot is lower than market price
211
+ profit_loss_dash = ( ( *( Xs_array + leg ) ) - Spot_dash );
212
+ }
173
213
  }
174
214
 
175
215
  sim[leg] = profit_loss;
@@ -179,7 +219,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
179
219
  // Store spot and spot dash
180
220
  //
181
221
  if( point_pos <= SIM_LIMIT && (sim_count + 2) % INTERVAL == 0 ) {
182
- rb_p(rb_str_new2("Leg:Spots"));
222
+ // rb_p(rb_str_new2("Leg:Spots"));
183
223
  metrics[ point_pos ][ leg ] = Spot;
184
224
  metrics[ point_pos + 2 ][ leg ] = Spot_dash;
185
225
  }
@@ -192,7 +232,6 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
192
232
  }
193
233
  }
194
234
  // legs loop end
195
-
196
235
  // start from the Knock value
197
236
  double ko_so_far = K, ko_so_far_dash = K;
198
237
  if( KType == ABSOLUTE ) {
@@ -205,7 +244,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
205
244
  double temp_payoff_ko_ccy = temp_payoff * pow( *( Xs_array + leg ), Ko_compare_mult );
206
245
 
207
246
  if( temp_payoff_ko_ccy > ko_so_far ) {
208
- sim_pos[leg] = ko_so_far * pow( *( Xs_array + leg ), Ko_compare_mult );
247
+ sim_pos[leg] = ko_so_far * pow( *( Xs_array + leg ), (-1 * Ko_compare_mult) );
209
248
  ko_so_far = 0.0;
210
249
  } else if( temp_payoff_ko_ccy <= ko_so_far ) {
211
250
  sim_pos[leg] = temp_payoff; // take the payoff in +ve's
@@ -224,7 +263,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
224
263
  double temp_payoff_ko_ccy = temp_payoff_dash * pow( *( Xs_array + leg ), Ko_compare_mult );
225
264
 
226
265
  if( temp_payoff_ko_ccy > ko_so_far_dash ) {
227
- sim_dash_pos[leg] = ko_so_far_dash * pow( *( Xs_array + leg ), Ko_compare_mult );
266
+ sim_dash_pos[leg] = ko_so_far_dash * pow( *( Xs_array + leg ), (-1 * Ko_compare_mult) );
228
267
  ko_so_far_dash = 0.0;
229
268
  } else if( temp_payoff_ko_ccy <= ko_so_far_dash ) {
230
269
  sim_dash_pos[leg] = temp_payoff_dash; // take the payoff in +ve's
@@ -235,7 +274,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
235
274
  }
236
275
  }
237
276
  }
238
- } else if( KType == POINTS ) {
277
+ } else if( KType == POINTS || KType == PIVOT_POINTS || KType == COLLAR_POINTS || KType == DUAL_STRIKE_POINTS ) {
239
278
  for( leg = 0; leg < NL; ++leg ) {
240
279
  // simulation normal
241
280
  if ( ko_so_far > 0.0 ) {
@@ -302,7 +341,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
302
341
  // store and send whichever payoff you want
303
342
  //
304
343
  if( point_pos <= SIM_LIMIT && (sim_count + 2) % INTERVAL == 0 ) {
305
- rb_p(rb_str_new2("Leg:Payoffs"));
344
+ // rb_p(rb_str_new2("Leg:Payoffs"));
306
345
  metrics[ point_pos + 1 ][ leg ] = (sim_pos[leg] + sim_neg[leg]);
307
346
  metrics[ point_pos + 3 ][ leg ] = (sim_dash_pos[leg] + sim_dash_neg[leg]);
308
347
  }
@@ -324,18 +363,18 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
324
363
  // Note: no limit condition here
325
364
  //
326
365
  if( (sim_count + 2) % INTERVAL == 0 ) {
327
- rb_p(rb_str_new2("T:"));
328
- rb_p(INT2NUM(point_pos));
366
+ // rb_p(rb_str_new2("T:"));
367
+ // rb_p(INT2NUM(point_pos));
329
368
  point_pos += 4;
330
369
  }
331
370
  }
332
371
  // run simulations loop
333
- rb_p(rb_str_new2("Simulation loop done"));
372
+ // rb_p(rb_str_new2("Simulation loop done"));
334
373
 
335
374
  //
336
375
  // sum all final +ve and -ve payoffs
337
376
  //
338
- rb_p(rb_str_new2("Calculating final PVs"));
377
+ // rb_p(rb_str_new2("Calculating final PVs"));
339
378
  for(sim_count = 0; sim_count < SCount; ++sim_count) {
340
379
  pvs_pos_sum += *(pvs_pos + sim_count);
341
380
  pvs_neg_sum += *(pvs_neg + sim_count);
@@ -344,7 +383,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
344
383
  //
345
384
  // free dynamically alloted heap memory
346
385
  //
347
- rb_p(rb_str_new2("Free allocated arrays"));
386
+ // rb_p(rb_str_new2("Free allocated arrays"));
348
387
  free(pvs_pos);
349
388
  free(pvs_neg);
350
389
  free(Ls_array);
@@ -358,7 +397,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
358
397
  //
359
398
  // return both payoffs
360
399
  //
361
- rb_p(rb_str_new2("Converting metrics"));
400
+ // rb_p(rb_str_new2("Converting metrics"));
362
401
  VALUE final_metrics = rb_ary_new();
363
402
  for(metric = 0; metric < DATAPOINTS; metric++) {
364
403
  VALUE leg_metrics = rb_ary_new();
@@ -370,7 +409,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
370
409
  rb_ary_push(final_metrics, leg_metrics);
371
410
  }
372
411
 
373
- rb_p(rb_str_new2("Generating output hash"));
412
+ // rb_p(rb_str_new2("Generating output hash"));
374
413
  VALUE final_pvs = rb_hash_new();
375
414
  rb_hash_aset(final_pvs, rb_str_new2("positive_pv"), DBL2NUM( pvs_pos_sum / SCount ));
376
415
  rb_hash_aset(final_pvs, rb_str_new2("negative_pv"), DBL2NUM( pvs_neg_sum / SCount ));
@@ -402,4 +441,3 @@ void Init_tarf_monte_carlo() {
402
441
  rb_define_singleton_method(cTarfMonteCarlo, "box_muller", method_box_muller, 0);
403
442
  rb_define_singleton_method(cTarfMonteCarlo, "run_monte_carlo", method_run_monte_carlo, -2);
404
443
  }
405
-
Binary file
@@ -3,5 +3,5 @@
3
3
  # gem yank tarf_monte_carlo -v 2.3
4
4
 
5
5
  module TarfMonteCarlo
6
- VERSION = "3.5"
6
+ VERSION = "3.13"
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.5'
4
+ version: '3.13'
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-04-05 00:00:00.000000000 Z
11
+ date: 2020-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -116,6 +116,7 @@ files:
116
116
  - bin/setup
117
117
  - ext/tarf_monte_carlo/extconf.rb
118
118
  - ext/tarf_monte_carlo/tarf_monte_carlo.c
119
+ - lib/.DS_Store
119
120
  - lib/tarf_monte_carlo.rb
120
121
  - lib/tarf_monte_carlo/version.rb
121
122
  - tarf_monte_carlo.gemspec