tarf_monte_carlo 3.50 → 3.51
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 +4 -4
- data/ext/tarf_monte_carlo/tarf_monte_carlo.c +14 -11
- data/lib/tarf_monte_carlo/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a032374020fceff1a3ab39e9c45e4073a93e2919a3c129a78e8e29c888864e43
|
4
|
+
data.tar.gz: d79b3c339ce40660d27b38e88abdf3ec50c0cb57062c28354bb1cc393f0269a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 926cf7788193cf069f41290b4c973bb11a0698210305a039fca191077e5d8a5d3c850e9245eed4c1cbf392a1ad9b95335b3ba7a3eeb96401ebbf18205b2a9b33
|
7
|
+
data.tar.gz: 423b8457497ba25e45539180f7a03a9ebc0224598455e0c250e880635489b6c447d8a459eb755aaadc34c5cf98676f8262832f18699c4d93608a62488d3ee1ef
|
@@ -62,7 +62,7 @@ void Init_tarf_monte_carlo();
|
|
62
62
|
double european_payoff(double, double, int, int, double);
|
63
63
|
double get_equivalent_notional(int, double, float);
|
64
64
|
double get_equivalent_rebate(int, double, float, int);
|
65
|
-
|
65
|
+
bool barrier_variations(int);
|
66
66
|
// Prototype for our methods - methods are prefixed by 'method_' here
|
67
67
|
VALUE method_box_muller( VALUE );
|
68
68
|
VALUE method_run_monte_carlo( VALUE, VALUE );
|
@@ -101,6 +101,9 @@ double get_equivalent_rebate(int conversion_sign, double rebate, float rate, int
|
|
101
101
|
return (-1 * dir_sign * total);
|
102
102
|
}
|
103
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
|
+
}
|
104
107
|
// main method for running monte carlo simulation from sidekiq worker/outside method
|
105
108
|
VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
|
106
109
|
VALUE MCInputs = rb_ary_shift(args);
|
@@ -202,7 +205,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
|
|
202
205
|
// first create a 1-D array of pointers, and then, for each array entry, create another 1-D array.
|
203
206
|
//
|
204
207
|
double **metrics;
|
205
|
-
if (KType
|
208
|
+
if (barrier_variations(KType)){
|
206
209
|
metrics = ( double** ) malloc( BARRIER_DP * sizeof(double*) );
|
207
210
|
for( metric = 0; metric < BARRIER_DP; metric++ ) {
|
208
211
|
metrics[metric] = ( double* ) malloc( DATAPOINTS * sizeof(double) );
|
@@ -402,7 +405,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
|
|
402
405
|
//
|
403
406
|
// Store spot and spot dash
|
404
407
|
//
|
405
|
-
if (!(KType
|
408
|
+
if (!(barrier_variations(KType))){
|
406
409
|
if( point_pos <= SIM_LIMIT && (sim_count + 2) % INTERVAL == 0 ) {
|
407
410
|
// rb_p(rb_str_new2("Leg:Spots"));
|
408
411
|
metrics[ point_pos ][ leg ] = Spot;
|
@@ -420,7 +423,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
|
|
420
423
|
// legs loop end
|
421
424
|
// start from the Knock value
|
422
425
|
double ko_so_far = K, ko_so_far_dash = K;
|
423
|
-
if(KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE){
|
426
|
+
if(KType == FX_AMERICAN_BARRIER_KNOCKIN_DISCRETE || KType == FX_AMERICAN_BARRIER_WINDOW_KNOCKIN){
|
424
427
|
if(knockedLeg >= 0){
|
425
428
|
double equivalent_notional = get_equivalent_notional(conversion_sign, ( *( Ns_array + knockedLeg ) ), sim[NL-1]);
|
426
429
|
sim_pos[NL-1] = european_payoff(( *( Xs_array + (knockedLeg) ) ), sim[NL-1], cp_sign, dir_sign, equivalent_notional);
|
@@ -443,7 +446,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
|
|
443
446
|
metrics[1][point_pos+1] = (knockedLeg_dash >= 0) ? ( *( Xs_array + (knockedLeg_dash) ) ) : 0;
|
444
447
|
metrics[3][point_pos+1] = knockedLeg_dash;
|
445
448
|
}
|
446
|
-
} else if(KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE){
|
449
|
+
} else if(KType == FX_AMERICAN_BARRIER_KNOCKOUT_DISCRETE || KType == FX_AMERICAN_BARRIER_WINDOW_KNOCKOUT){
|
447
450
|
if(knockedLeg >= 0){
|
448
451
|
sim_pos[knockedLeg] = get_equivalent_rebate(rebate_conversion_sign, ( *( Rebate_array + knockedLeg ) ), sim[knockedLeg], dir_sign);
|
449
452
|
}
|
@@ -575,7 +578,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
|
|
575
578
|
//
|
576
579
|
// store and send whichever payoff you want
|
577
580
|
//
|
578
|
-
if (!(KType
|
581
|
+
if (!(barrier_variations(KType))){
|
579
582
|
if( point_pos <= SIM_LIMIT && (sim_count + 2) % INTERVAL == 0 ) {
|
580
583
|
// rb_p(rb_str_new2("Leg:Payoffs"));
|
581
584
|
metrics[ point_pos + 1 ][ leg ] = (sim_pos[leg] + sim_neg[leg]);
|
@@ -595,8 +598,8 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
|
|
595
598
|
pvs_neg[sim_count] = sim_neg_sum;
|
596
599
|
pvs_neg[sim_count + 1] = sim_dash_neg_sum;
|
597
600
|
|
598
|
-
|
599
|
-
if (KType
|
601
|
+
|
602
|
+
if (barrier_variations(KType)){
|
600
603
|
if ((point_pos < DATAPOINTS) && (sim_count % INTERVAL) == 0){
|
601
604
|
metrics[2][point_pos] = sim_pos_sum;
|
602
605
|
metrics[2][point_pos+1] = sim_dash_pos_sum;
|
@@ -646,8 +649,8 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
|
|
646
649
|
//
|
647
650
|
// rb_p(rb_str_new2("Converting metrics"));
|
648
651
|
VALUE final_metrics = rb_ary_new();
|
649
|
-
|
650
|
-
if (KType
|
652
|
+
|
653
|
+
if (barrier_variations(KType)){
|
651
654
|
for(metric = 0; metric < BARRIER_DP; metric++) {
|
652
655
|
VALUE leg_metrics = rb_ary_new();
|
653
656
|
|
@@ -680,7 +683,7 @@ VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
|
|
680
683
|
//
|
681
684
|
// free your arrays from memory once you're done using them
|
682
685
|
//
|
683
|
-
int dp = (KType
|
686
|
+
int dp = barrier_variations(KType) ? BARRIER_DP : DATAPOINTS;
|
684
687
|
for(metric = 0; metric < dp; metric++) {
|
685
688
|
free( metrics[metric] );
|
686
689
|
}
|