tarf_monte_carlo 3.6 → 3.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -0
- data/ext/tarf_monte_carlo/tarf_monte_carlo.c +65 -26
- 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: d8d2906e29ad90b4dc0ff480eec64fed2a8d7aaca0e5933de6fa1d672185a38b
|
4
|
+
data.tar.gz: d8afc4b56ea885bcd67299273ab887b2c97b6e73e6d218729a9e39b54433a240
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae61b13786e7deb50903881364d793c96db0d453f34f54dd6dd4040a4eb3f86cfaeb6217be6433cbc8f9827c7ba12caabcfc2953edf3a18088a17eceda03fafc
|
7
|
+
data.tar.gz: 546c462c9ccb5038e9ebb385aec5ccc1f66745d011d72f55c68afa9ec0a83847b0d8beea21bc531610453bd473e82e3a6792c0dc9138125247c32afd2228539d
|
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
|
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) );
|
@@ -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
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
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 ) {
|
@@ -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 ));
|
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.14'
|
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
|