tarf_monte_carlo 3.7 → 3.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/tarf_monte_carlo/tarf_monte_carlo.c +57 -24
- data/lib/.DS_Store +0 -0
- data/lib/tarf_monte_carlo.rb +5 -5
- data/lib/tarf_monte_carlo/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 850a097b6405e2fa4e128216700ef8c0a4748e9eddbc08ecadf7e0b1987c7e48
|
4
|
+
data.tar.gz: 2184d334e6b788c45a555e526a0fcb00788722de7068755dac602523699f80f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de2b4441025568d072934b8c530c6c81f50f659a72347f3e06e4e561d283241c37681ad9425c405054dae83c7838714b8edeb37a298703f8da84ca4ebf337ec3
|
7
|
+
data.tar.gz: da7fec9328f798922d3fa1e16f6840d9a48654ae597936a3e3d0fe01c73611e08404ab05c9deabfaadd7ef0a95fa55dba7ce2539b8586e10a7dc65be0c35cb50
|
@@ -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
|
91
|
-
double *pvs_neg
|
92
|
-
double *Ls_array
|
93
|
-
double *Ts_array
|
94
|
-
double *Ns_array
|
95
|
-
double *Xs_array
|
96
|
-
double *vs_array
|
97
|
-
double *bs_array
|
98
|
-
double *DFs_array
|
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) );
|
@@ -113,6 +124,10 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
|
|
113
124
|
vs_array[leg] = NUM2DBL( rb_ary_entry(vs, leg) );
|
114
125
|
bs_array[leg] = NUM2DBL( rb_ary_entry(bs, leg) );
|
115
126
|
DFs_array[leg] = NUM2DBL( rb_ary_entry(DFs, leg) );
|
127
|
+
Ps_array[leg] = NUM2DBL( rb_ary_entry(Ps, leg) );
|
128
|
+
CFs_array[leg] = NUM2DBL( rb_ary_entry(CFs, leg) );
|
129
|
+
LSts_array[leg] = NUM2DBL( rb_ary_entry(LSts, leg) );
|
130
|
+
USts_array[leg] = NUM2DBL( rb_ary_entry(USts, leg) );
|
116
131
|
}
|
117
132
|
|
118
133
|
//
|
@@ -166,10 +181,28 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
|
|
166
181
|
// profit if spot is higher than market price
|
167
182
|
profit_loss_dash = ( Spot_dash - ( *( Xs_array + leg ) ) );
|
168
183
|
} else if ( BS == SELL ) {
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
184
|
+
if ( KType == PIVOT_POINTS ) {
|
185
|
+
if ( Spot <= *( Xs_array + leg ) && Spot < *( Ps_array + leg ) ) {
|
186
|
+
profit_loss = *( Xs_array + leg ) - Spot;
|
187
|
+
} else if ( Spot > *( Xs_array + leg ) && Spot >= *( Ps_array + leg ) ) {
|
188
|
+
profit_loss = *( Xs_array + leg ) - Spot;
|
189
|
+
} else {
|
190
|
+
profit_loss = 0.0;
|
191
|
+
}
|
192
|
+
// dash
|
193
|
+
if ( Spot_dash <= *( Xs_array + leg ) && Spot_dash < *( Ps_array + leg ) ) {
|
194
|
+
profit_loss_dash = *( Xs_array + leg ) - Spot_dash;
|
195
|
+
} else if ( Spot_dash > *( Xs_array + leg ) && Spot_dash >= *( Ps_array + leg ) ) {
|
196
|
+
profit_loss_dash = *( Xs_array + leg ) - Spot_dash;
|
197
|
+
} else {
|
198
|
+
profit_loss_dash = 0.0;
|
199
|
+
}
|
200
|
+
} else {
|
201
|
+
// profit if spot is lower than market price
|
202
|
+
profit_loss = ( ( *( Xs_array + leg ) ) - Spot );
|
203
|
+
// profit if spot is lower than market price
|
204
|
+
profit_loss_dash = ( ( *( Xs_array + leg ) ) - Spot_dash );
|
205
|
+
}
|
173
206
|
}
|
174
207
|
|
175
208
|
sim[leg] = profit_loss;
|
@@ -179,7 +212,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
|
|
179
212
|
// Store spot and spot dash
|
180
213
|
//
|
181
214
|
if( point_pos <= SIM_LIMIT && (sim_count + 2) % INTERVAL == 0 ) {
|
182
|
-
rb_p(rb_str_new2("Leg:Spots"));
|
215
|
+
// rb_p(rb_str_new2("Leg:Spots"));
|
183
216
|
metrics[ point_pos ][ leg ] = Spot;
|
184
217
|
metrics[ point_pos + 2 ][ leg ] = Spot_dash;
|
185
218
|
}
|
@@ -302,7 +335,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
|
|
302
335
|
// store and send whichever payoff you want
|
303
336
|
//
|
304
337
|
if( point_pos <= SIM_LIMIT && (sim_count + 2) % INTERVAL == 0 ) {
|
305
|
-
rb_p(rb_str_new2("Leg:Payoffs"));
|
338
|
+
// rb_p(rb_str_new2("Leg:Payoffs"));
|
306
339
|
metrics[ point_pos + 1 ][ leg ] = (sim_pos[leg] + sim_neg[leg]);
|
307
340
|
metrics[ point_pos + 3 ][ leg ] = (sim_dash_pos[leg] + sim_dash_neg[leg]);
|
308
341
|
}
|
@@ -324,18 +357,18 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
|
|
324
357
|
// Note: no limit condition here
|
325
358
|
//
|
326
359
|
if( (sim_count + 2) % INTERVAL == 0 ) {
|
327
|
-
rb_p(rb_str_new2("T:"));
|
328
|
-
rb_p(INT2NUM(point_pos));
|
360
|
+
// rb_p(rb_str_new2("T:"));
|
361
|
+
// rb_p(INT2NUM(point_pos));
|
329
362
|
point_pos += 4;
|
330
363
|
}
|
331
364
|
}
|
332
365
|
// run simulations loop
|
333
|
-
rb_p(rb_str_new2("Simulation loop done"));
|
366
|
+
// rb_p(rb_str_new2("Simulation loop done"));
|
334
367
|
|
335
368
|
//
|
336
369
|
// sum all final +ve and -ve payoffs
|
337
370
|
//
|
338
|
-
rb_p(rb_str_new2("Calculating final PVs"));
|
371
|
+
// rb_p(rb_str_new2("Calculating final PVs"));
|
339
372
|
for(sim_count = 0; sim_count < SCount; ++sim_count) {
|
340
373
|
pvs_pos_sum += *(pvs_pos + sim_count);
|
341
374
|
pvs_neg_sum += *(pvs_neg + sim_count);
|
@@ -344,7 +377,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
|
|
344
377
|
//
|
345
378
|
// free dynamically alloted heap memory
|
346
379
|
//
|
347
|
-
rb_p(rb_str_new2("Free allocated arrays"));
|
380
|
+
// rb_p(rb_str_new2("Free allocated arrays"));
|
348
381
|
free(pvs_pos);
|
349
382
|
free(pvs_neg);
|
350
383
|
free(Ls_array);
|
@@ -358,7 +391,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
|
|
358
391
|
//
|
359
392
|
// return both payoffs
|
360
393
|
//
|
361
|
-
rb_p(rb_str_new2("Converting metrics"));
|
394
|
+
// rb_p(rb_str_new2("Converting metrics"));
|
362
395
|
VALUE final_metrics = rb_ary_new();
|
363
396
|
for(metric = 0; metric < DATAPOINTS; metric++) {
|
364
397
|
VALUE leg_metrics = rb_ary_new();
|
@@ -370,7 +403,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
|
|
370
403
|
rb_ary_push(final_metrics, leg_metrics);
|
371
404
|
}
|
372
405
|
|
373
|
-
rb_p(rb_str_new2("Generating output hash"));
|
406
|
+
// rb_p(rb_str_new2("Generating output hash"));
|
374
407
|
VALUE final_pvs = rb_hash_new();
|
375
408
|
rb_hash_aset(final_pvs, rb_str_new2("positive_pv"), DBL2NUM( pvs_pos_sum / SCount ));
|
376
409
|
rb_hash_aset(final_pvs, rb_str_new2("negative_pv"), DBL2NUM( pvs_neg_sum / SCount ));
|
data/lib/.DS_Store
ADDED
Binary file
|
data/lib/tarf_monte_carlo.rb
CHANGED
@@ -4,11 +4,11 @@ require "tarf_monte_carlo/version"
|
|
4
4
|
# my_malloc/my_malloc will be the shared object built when the gem is
|
5
5
|
# installed then copied into lib/my_malloc/.
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
begin
|
8
|
+
require "tarf_monte_carlo/#{RUBY_VERSION[/\d+.\d+/]}/tarf_monte_carlo"
|
9
|
+
rescue LoadError
|
10
|
+
require "tarf_monte_carlo/tarf_monte_carlo"
|
11
|
+
end
|
12
12
|
|
13
13
|
module TarfMonteCarlo
|
14
14
|
# Your code goes here...
|
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.
|
4
|
+
version: '3.11'
|
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-
|
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
|