tarf_monte_carlo 2.2 → 2.3

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: c8ba833192173c38b6d335d681469b91ba509ed41d330db20f63354bd439492f
4
- data.tar.gz: afc0ec5bcabd7caaa37e908ac438e7e22f224ea43aaac86c850b3cd01e980f20
3
+ metadata.gz: b440e7a3a2b9484d1c8edf20e7d74b3a0623a14a902afafea54b6be162ea7939
4
+ data.tar.gz: dd67f54665c6211ce93f96ada1342dc929e2f44ab601f4de6284af01e5d2de1e
5
5
  SHA512:
6
- metadata.gz: 8f7f2fd84332ee001f2ab4140cf93f1222f0052da5780547078d4f3719d34f54911003e9917c005787b7230244d345c6a154e041a895ec626ec6afaa3ebe90e6
7
- data.tar.gz: 188688ac908a15933b3767e74a3d2ecef71db25c30a597800d173fa3435d74e5d888afbbc1641eca51e5d60b1ab3813bb1d81ae57f6bcf910793229e24944ca9
6
+ metadata.gz: 6461fc8999a13d1d4a44894a482c8436545b48d464d711d5d626bdf11831b406af1eabe3877782e9a16ce5537cef8d248f641bca327ebe5110041c1c6954e96a
7
+ data.tar.gz: aaec3c1f7fa06c60ab74b9dfcba834d64581b945cf50b5c9add81a5c4ee9f2e098751d69f239243ee8583ed05a7413720de6202ac9f7e99e904e762689fe75f6
@@ -28,6 +28,7 @@
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 DATAPOINTS 200 // data for plotting
31
32
 
32
33
  // Defining a space for information and references
33
34
  // about the module to be stored internally
@@ -64,6 +65,7 @@ VALUE method_box_muller( VALUE self ) {
64
65
  // main method for running monte carlo simulation from sidekiq worker/outside method
65
66
  VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
66
67
  VALUE MCInputs = rb_ary_shift(args);
68
+ rb_p("MC Inputs: ")
67
69
  rb_p(MCInputs);
68
70
 
69
71
  // seed value for rand() function
@@ -110,6 +112,22 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
110
112
  DFs_array[leg] = NUM2DBL( rb_ary_entry(DFs, leg) );
111
113
  }
112
114
 
115
+ //
116
+ // first create a 1-D array of pointers, and then, for each array entry, create another 1-D array.
117
+ //
118
+ int metric;
119
+ double **metrics;
120
+ metrics = ( double** ) malloc( DATAPOINTS * sizeof(double*) );
121
+ for( metric = 0; metric < DATAPOINTS; metric++ ) {
122
+ metrics[metric] = ( double* ) malloc( NL * sizeof(double) );
123
+ }
124
+ for(metric = 0; metric < DATAPOINTS; metric++) {
125
+ for(leg = 0; leg < ML; leg++) {
126
+ metrics[metric][leg] = 0.0;
127
+ }
128
+ }
129
+ int point_pos = 0;
130
+
113
131
  // run simulations loop
114
132
  for( sim_count = 0; sim_count < SCount; sim_count += 2 ) {
115
133
  // initial spot rate for each iteration would be current spot rate
@@ -134,7 +152,6 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
134
152
  eps = NUM2DBL( method_box_muller( self ) );
135
153
  eps_dash = eps * -1;
136
154
 
137
- //NOTE: NEED NOT RUN THIS ON EVERY SIMULATION
138
155
  // main formula of MC simulation
139
156
  drift = ( ( *( bs_array + leg ) ) - 0.5 * pow( ( *( vs_array + leg ) ), 2 ) ) * ( *( Ts_array + leg ) );
140
157
  vSqrdt = ( *( vs_array + leg ) ) * sqrt( *( Ts_array + leg ) );
@@ -158,6 +175,14 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
158
175
  sim[leg] = profit_loss;
159
176
  sim_dash[leg] = profit_loss_dash;
160
177
 
178
+ //
179
+ // Store spot and spot dash
180
+ //
181
+ if( (sim_count + 1) % 50 == 0 ) {
182
+ metrics[ point_pos ][ leg ] = Spot;
183
+ metrics[ point_pos + 2 ][ leg ] = Spot_dash;
184
+ }
185
+
161
186
  // if excuted UNCHAINED method
162
187
  // always use the current spot rate
163
188
  if ( MCType == UNCHAINED ) {
@@ -272,6 +297,14 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
272
297
  // take sum of positive and negative payoffs and calculate pvs
273
298
  double sim_pos_sum = 0.0, sim_neg_sum = 0.0, sim_dash_pos_sum = 0.0, sim_dash_neg_sum = 0.0;
274
299
  for( leg = 0; leg < NL; ++leg ) {
300
+ //
301
+ // store and send whichever payoff you want
302
+ //
303
+ if( (sim_count + 1) % 50 == 0 ) {
304
+ metrics[ point_pos + 1 ][ leg ] = sim_pos[leg];
305
+ metrics[ point_pos + 3 ][ leg ] = sim_dash_pos[leg];
306
+ }
307
+
275
308
  sim_pos_sum += sim_pos[leg] * ( *( DFs_array + leg ) );
276
309
  sim_neg_sum += sim_neg[leg] * ( *( DFs_array + leg ) );
277
310
  sim_dash_pos_sum += sim_dash_pos[leg] * ( *( DFs_array + leg ) );
@@ -283,15 +316,26 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
283
316
  pvs_pos[sim_count + 1] = sim_dash_pos_sum;
284
317
  pvs_neg[sim_count] = sim_neg_sum;
285
318
  pvs_neg[sim_count + 1] = sim_dash_neg_sum;
319
+
320
+ //
321
+ // increment metric storing point by 4
322
+ //
323
+ if( (sim_count + 1) % 50 == 0 ) {
324
+ point_pos += 4;
325
+ }
286
326
  }
287
327
 
328
+ //
288
329
  // sum all final +ve and -ve payoffs
330
+ //
289
331
  for(sim_count = 0; sim_count < SCount; ++sim_count) {
290
332
  pvs_pos_sum += *(pvs_pos + sim_count);
291
333
  pvs_neg_sum += *(pvs_neg + sim_count);
292
334
  }
293
335
 
336
+ //
294
337
  // free dynamically alloted heap memory
338
+ //
295
339
  free(pvs_pos);
296
340
  free(pvs_neg);
297
341
  free(Ls_array);
@@ -302,11 +346,34 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
302
346
  free(bs_array);
303
347
  free(DFs_array);
304
348
 
349
+ //
305
350
  // return both payoffs
351
+ //
352
+ VALUE final_metrics = rb_ary_new();
353
+ for(metric = 0; metric < DATAPOINTS; metric++) {
354
+ VALUE leg_metrics = rb_ary_new();
355
+
356
+ for(leg = 0; leg < NL; leg++) {
357
+ rb_ary_push( leg_metrics, DBL2NUM( metrics[metric][leg] ) );
358
+ }
359
+
360
+ rb_ary_push(final_metrics, leg_metrics);
361
+ }
362
+
306
363
  VALUE final_pvs = rb_hash_new();
307
364
  rb_hash_aset(final_pvs, rb_str_new2("positive_pv"), DBL2NUM( pvs_pos_sum / SCount ));
308
365
  rb_hash_aset(final_pvs, rb_str_new2("negative_pv"), DBL2NUM( pvs_neg_sum / SCount ));
309
366
  rb_hash_aset(final_pvs, rb_str_new2("total_pv"), DBL2NUM( (pvs_neg_sum + pvs_pos_sum) / SCount ));
367
+ rb_hash_aset(final_pvs, rb_str_new2("metrics"), final_metrics);
368
+
369
+ //
370
+ // free your arrays from memory once you're done using them
371
+ //
372
+ for(metric = 0; metric < DATAPOINTS; metric++) {
373
+ free( metrics[metric] );
374
+ }
375
+ free(metrics);
376
+
310
377
  return final_pvs;
311
378
  }
312
379
 
@@ -1,7 +1,7 @@
1
1
  # gem build tarf_monte_carlo.gemspec
2
- # gem push -v tarf_monte_carlo-2.1.gem
3
- # gem yank tarf_monte_carlo -v 2.1
2
+ # gem push -v tarf_monte_carlo-2.3.gem
3
+ # gem yank tarf_monte_carlo -v 2.3
4
4
 
5
5
  module TarfMonteCarlo
6
- VERSION = "2.2"
6
+ VERSION = "2.3"
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tarf_monte_carlo
3
3
  version: !ruby/object:Gem::Version
4
- version: '2.2'
4
+ version: '2.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vivek Routh