tarf_monte_carlo 2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/Gemfile +4 -0
- data/README.md +34 -0
- data/Rakefile +37 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ext/tarf_monte_carlo/extconf.rb +2 -0
- data/ext/tarf_monte_carlo/tarf_monte_carlo.c +327 -0
- data/lib/tarf_monte_carlo.rb +15 -0
- data/lib/tarf_monte_carlo/version.rb +7 -0
- data/tarf_monte_carlo.gemspec +46 -0
- metadata +145 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c8ba833192173c38b6d335d681469b91ba509ed41d330db20f63354bd439492f
|
4
|
+
data.tar.gz: afc0ec5bcabd7caaa37e908ac438e7e22f224ea43aaac86c850b3cd01e980f20
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8f7f2fd84332ee001f2ab4140cf93f1222f0052da5780547078d4f3719d34f54911003e9917c005787b7230244d345c6a154e041a895ec626ec6afaa3ebe90e6
|
7
|
+
data.tar.gz: 188688ac908a15933b3767e74a3d2ecef71db25c30a597800d173fa3435d74e5d888afbbc1641eca51e5d60b1ab3813bb1d81ae57f6bcf910793229e24944ca9
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# TARFMonteCarlo
|
2
|
+
|
3
|
+
TODO: ...
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'tarf_monte_carlo'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install tarf_monte_carlo
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: ...
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
28
|
+
|
29
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
TODO ...
|
34
|
+
# tarf_monte_carlo
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require "bundler"
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
require "rspec/core/rake_task"
|
4
|
+
require 'yard'
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
7
|
+
|
8
|
+
task :default => [:compile, :spec]
|
9
|
+
require "rake/extensiontask"
|
10
|
+
|
11
|
+
task :build => :compile
|
12
|
+
task :spec => :compile
|
13
|
+
|
14
|
+
desc 'Generate YARD document'
|
15
|
+
YARD::Rake::YardocTask.new(:doc) do |t|
|
16
|
+
t.files = ['ext/tarf_monte_carlo/tarf_monte_carlo.c']
|
17
|
+
t.options = []
|
18
|
+
t.options << '--debug' << '--verbose' if $trace
|
19
|
+
end
|
20
|
+
|
21
|
+
spec = eval File.read("tarf_monte_carlo.gemspec")
|
22
|
+
Rake::ExtensionTask.new("tarf_monte_carlo", spec) do |ext|
|
23
|
+
ext.ext_dir = 'ext/tarf_monte_carlo'
|
24
|
+
ext.cross_compile = true
|
25
|
+
ext.lib_dir = File.join(*['lib', 'tarf_monte_carlo', ENV['FAT_DIR']].compact)
|
26
|
+
# cross_platform names are of MRI's platform name
|
27
|
+
ext.cross_platform = ['x86-mingw32', 'x64-mingw32']
|
28
|
+
end
|
29
|
+
|
30
|
+
namespace :build do
|
31
|
+
desc 'Build gems for Windows per rake-compiler-dock'
|
32
|
+
task :windows do
|
33
|
+
require 'rake_compiler_dock'
|
34
|
+
RakeCompilerDock.sh 'bundle && rake cross native gem RUBY_CC_VERSION=2.0.0:2.1.6:2.2.2:2.3.0:2.4.0:2.5.0'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "tarf_monte_carlo"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,327 @@
|
|
1
|
+
// S - spot_rate
|
2
|
+
// X - strike price of forward
|
3
|
+
// b - difference of ccy2 and ccy1 risk free interest rates - cost of carry rate
|
4
|
+
// v - volatility
|
5
|
+
// T - time to expiration in number of years
|
6
|
+
// L - leverage ratio
|
7
|
+
// N - notional
|
8
|
+
// BS - direction of forward
|
9
|
+
// eps - epsilon
|
10
|
+
// NL - number of legs
|
11
|
+
// SCount - number of simulations
|
12
|
+
// MCType - Monte Carlo method type to run
|
13
|
+
// KType - Knockout Type
|
14
|
+
// DFs - discount factors for pv calculation
|
15
|
+
|
16
|
+
#include <ruby.h>
|
17
|
+
#include <stdio.h>
|
18
|
+
#include <stdlib.h>
|
19
|
+
#include <string.h>
|
20
|
+
#include <math.h>
|
21
|
+
#include <time.h>
|
22
|
+
#include <stdbool.h>
|
23
|
+
#define PI_VALUE 3.14159
|
24
|
+
#define BUY 1 // buy sell flag
|
25
|
+
#define SELL 2 // buy sell flag
|
26
|
+
#define CHAINED 1 // monte carlo method type to run
|
27
|
+
#define UNCHAINED 2 // monte carlo method type to run
|
28
|
+
#define ABSOLUTE 1 // knockout by absolute
|
29
|
+
#define POINTS 2 // knockout by points
|
30
|
+
#define LEGS 3 // knockout by legs
|
31
|
+
|
32
|
+
// Defining a space for information and references
|
33
|
+
// about the module to be stored internally
|
34
|
+
// Qnil - Ruby nil in C scope.
|
35
|
+
VALUE mTarfMonteCarlo = Qnil;
|
36
|
+
VALUE cTarfMonteCarlo = Qnil;
|
37
|
+
|
38
|
+
// Prototype for the initialization method - Ruby calls this, not you
|
39
|
+
void Init_tarf_monte_carlo();
|
40
|
+
|
41
|
+
// Prototype for our methods - methods are prefixed by 'method_' here
|
42
|
+
VALUE method_box_muller( VALUE );
|
43
|
+
VALUE method_run_monte_carlo( VALUE, VALUE );
|
44
|
+
|
45
|
+
// BoxMuller method for epsilon calculations
|
46
|
+
// eps = cos(2 * PI * z2) * sqrt( -2 * log( z1 ) )
|
47
|
+
VALUE method_box_muller( VALUE self ) {
|
48
|
+
double eps, z1, z2;
|
49
|
+
int i;
|
50
|
+
|
51
|
+
for (i = 0; i < 100; ++i) {
|
52
|
+
z1 = ( (double)rand() / (double)RAND_MAX );
|
53
|
+
}
|
54
|
+
|
55
|
+
for (i = 0; i < 100; ++i) {
|
56
|
+
z2 = ( (double)rand() / (double)RAND_MAX );
|
57
|
+
}
|
58
|
+
|
59
|
+
eps = cos( 2 * PI_VALUE * z2 ) * sqrt( -2 * log( z1 ) );
|
60
|
+
|
61
|
+
return DBL2NUM(eps);
|
62
|
+
}
|
63
|
+
|
64
|
+
// main method for running monte carlo simulation from sidekiq worker/outside method
|
65
|
+
VALUE method_run_monte_carlo( VALUE self, VALUE args ) {
|
66
|
+
VALUE MCInputs = rb_ary_shift(args);
|
67
|
+
rb_p(MCInputs);
|
68
|
+
|
69
|
+
// seed value for rand() function
|
70
|
+
srand( time(0) );
|
71
|
+
|
72
|
+
// initialize payoffs array
|
73
|
+
int leg, sim_count;
|
74
|
+
double pvs_pos_sum = 0.0, pvs_neg_sum = 0.0;
|
75
|
+
|
76
|
+
int SCount = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("scount")) ) * 2;
|
77
|
+
int MCType = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("mc_type")) );
|
78
|
+
int NL = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("legs_count")) );
|
79
|
+
int BS = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("buy_sell")) );
|
80
|
+
double K = NUM2DBL( rb_hash_aref(MCInputs, rb_str_new2("knockout")) );
|
81
|
+
int KType = NUM2INT( rb_hash_aref(MCInputs, rb_str_new2("knockout_type")) );
|
82
|
+
double S = NUM2DBL( rb_hash_aref(MCInputs, rb_str_new2("spot_rate")) );
|
83
|
+
|
84
|
+
// assign leg specific attributes
|
85
|
+
double *pvs_pos = ( double* ) malloc( SCount * sizeof(double) );
|
86
|
+
double *pvs_neg = ( double* ) malloc( SCount * sizeof(double) );
|
87
|
+
double *Ls_array = ( double* ) malloc( NL * sizeof(double) );
|
88
|
+
double *Ts_array = ( double* ) malloc( NL * sizeof(double) );
|
89
|
+
double *Ns_array = ( double* ) malloc( NL * sizeof(double) );
|
90
|
+
double *Xs_array = ( double* ) malloc( NL * sizeof(double) );
|
91
|
+
double *vs_array = ( double* ) malloc( NL * sizeof(double) );
|
92
|
+
double *bs_array = ( double* ) malloc( NL * sizeof(double) );
|
93
|
+
double *DFs_array = ( double* ) malloc( NL * sizeof(double) );
|
94
|
+
|
95
|
+
VALUE Ls = rb_hash_aref(MCInputs, rb_str_new2("leverage_ratios") );
|
96
|
+
VALUE Ts = rb_hash_aref(MCInputs, rb_str_new2("expiration_times") );
|
97
|
+
VALUE Ns = rb_hash_aref(MCInputs, rb_str_new2("notionals") );
|
98
|
+
VALUE Xs = rb_hash_aref(MCInputs, rb_str_new2("strikes") );
|
99
|
+
VALUE vs = rb_hash_aref(MCInputs, rb_str_new2("volatilities") );
|
100
|
+
VALUE bs = rb_hash_aref(MCInputs, rb_str_new2("carry_rates") );
|
101
|
+
VALUE DFs = rb_hash_aref(MCInputs, rb_str_new2("discount_factors") );
|
102
|
+
|
103
|
+
for (leg = 0; leg < NL; ++leg) {
|
104
|
+
Ls_array[leg] = NUM2DBL( rb_ary_entry(Ls, leg) );
|
105
|
+
Ts_array[leg] = NUM2DBL( rb_ary_entry(Ts, leg) );
|
106
|
+
Ns_array[leg] = NUM2DBL( rb_ary_entry(Ns, leg) );
|
107
|
+
Xs_array[leg] = NUM2DBL( rb_ary_entry(Xs, leg) );
|
108
|
+
vs_array[leg] = NUM2DBL( rb_ary_entry(vs, leg) );
|
109
|
+
bs_array[leg] = NUM2DBL( rb_ary_entry(bs, leg) );
|
110
|
+
DFs_array[leg] = NUM2DBL( rb_ary_entry(DFs, leg) );
|
111
|
+
}
|
112
|
+
|
113
|
+
// run simulations loop
|
114
|
+
for( sim_count = 0; sim_count < SCount; sim_count += 2 ) {
|
115
|
+
// initial spot rate for each iteration would be current spot rate
|
116
|
+
double Spot = S, Spot_dash = S;
|
117
|
+
double sim[NL], sim_pos[NL], sim_neg[NL], sim_dash[NL], sim_dash_pos[NL], sim_dash_neg[NL];
|
118
|
+
|
119
|
+
// reset all simulation leg specific results
|
120
|
+
for ( leg = 0; leg < NL; ++leg ) {
|
121
|
+
sim[leg] = 0.0;
|
122
|
+
sim_pos[leg] = 0.0;
|
123
|
+
sim_neg[leg] = 0.0;
|
124
|
+
sim_dash[leg] = 0.0;
|
125
|
+
sim_dash_pos[leg] = 0.0;
|
126
|
+
sim_dash_neg[leg] = 0.0;
|
127
|
+
}
|
128
|
+
|
129
|
+
// legs loop start
|
130
|
+
for( leg = 0; leg < NL; ++leg ) {
|
131
|
+
double eps, eps_dash, drift, vSqrdt, profit_loss, profit_loss_dash;
|
132
|
+
|
133
|
+
// get the random value for MC calculation
|
134
|
+
eps = NUM2DBL( method_box_muller( self ) );
|
135
|
+
eps_dash = eps * -1;
|
136
|
+
|
137
|
+
//NOTE: NEED NOT RUN THIS ON EVERY SIMULATION
|
138
|
+
// main formula of MC simulation
|
139
|
+
drift = ( ( *( bs_array + leg ) ) - 0.5 * pow( ( *( vs_array + leg ) ), 2 ) ) * ( *( Ts_array + leg ) );
|
140
|
+
vSqrdt = ( *( vs_array + leg ) ) * sqrt( *( Ts_array + leg ) );
|
141
|
+
|
142
|
+
// spot calculation with epsilon & epsilon reverse
|
143
|
+
Spot = Spot * exp( drift + vSqrdt * eps );
|
144
|
+
Spot_dash = Spot_dash * exp( drift + vSqrdt * eps_dash );
|
145
|
+
|
146
|
+
if ( BS == BUY ) {
|
147
|
+
// profit if spot is higher than market price
|
148
|
+
profit_loss = ( Spot - ( *( Xs_array + leg ) ) );
|
149
|
+
// profit if spot is higher than market price
|
150
|
+
profit_loss_dash = ( Spot_dash - ( *( Xs_array + leg ) ) );
|
151
|
+
} else if ( BS == SELL ) {
|
152
|
+
// profit if spot is lower than market price
|
153
|
+
profit_loss = ( ( *( Xs_array + leg ) ) - Spot );
|
154
|
+
// profit if spot is lower than market price
|
155
|
+
profit_loss_dash = ( ( *( Xs_array + leg ) ) - Spot_dash );
|
156
|
+
}
|
157
|
+
|
158
|
+
sim[leg] = profit_loss;
|
159
|
+
sim_dash[leg] = profit_loss_dash;
|
160
|
+
|
161
|
+
// if excuted UNCHAINED method
|
162
|
+
// always use the current spot rate
|
163
|
+
if ( MCType == UNCHAINED ) {
|
164
|
+
Spot = S;
|
165
|
+
Spot_dash = S;
|
166
|
+
// VALUE sk = DBL2NUM( Spot );
|
167
|
+
// rb_p(sk)
|
168
|
+
}
|
169
|
+
}
|
170
|
+
// legs loop end
|
171
|
+
|
172
|
+
// start from the Knock value
|
173
|
+
double ko_so_far = K, ko_so_far_dash = K;
|
174
|
+
if( KType == ABSOLUTE ) {
|
175
|
+
for( leg = 0; leg < NL; ++leg ) {
|
176
|
+
// simulation normal
|
177
|
+
if( ko_so_far > 0.0 ) {
|
178
|
+
if( sim[leg] >= 0.0 ) {
|
179
|
+
// knock out condition
|
180
|
+
double temp_payoff = sim[leg] * ( *( Ns_array + leg ) );
|
181
|
+
|
182
|
+
if( temp_payoff > ko_so_far ) {
|
183
|
+
sim_pos[leg] = ko_so_far;
|
184
|
+
ko_so_far = 0.0;
|
185
|
+
} else if( temp_payoff <= ko_so_far ) {
|
186
|
+
sim_pos[leg] = temp_payoff; // take the payoff in +ve's
|
187
|
+
ko_so_far -= temp_payoff; // update the knock out
|
188
|
+
}
|
189
|
+
} else {
|
190
|
+
sim_neg[leg] = sim[leg] * ( *( Ls_array + leg ) ) * ( *( Ns_array + leg ) );
|
191
|
+
}
|
192
|
+
}
|
193
|
+
|
194
|
+
// simulation reverse dash
|
195
|
+
if ( ko_so_far_dash > 0.0 ) {
|
196
|
+
if( sim_dash[leg] >= 0.0 ) {
|
197
|
+
// knock out condition
|
198
|
+
double temp_payoff_dash = sim_dash[leg] * ( *( Ns_array + leg ) );
|
199
|
+
|
200
|
+
if( temp_payoff_dash > ko_so_far_dash ) {
|
201
|
+
sim_dash_pos[leg] = ko_so_far_dash;
|
202
|
+
ko_so_far_dash = 0.0;
|
203
|
+
} else if( temp_payoff_dash <= ko_so_far_dash ) {
|
204
|
+
sim_dash_pos[leg] = temp_payoff_dash; // take the payoff in +ve's
|
205
|
+
ko_so_far_dash -= temp_payoff_dash; // update the knock out
|
206
|
+
}
|
207
|
+
} else {
|
208
|
+
sim_dash_neg[leg] = sim_dash[leg] * ( *( Ls_array + leg ) ) * ( *( Ns_array + leg ) );
|
209
|
+
}
|
210
|
+
}
|
211
|
+
}
|
212
|
+
} else if( KType == POINTS ) {
|
213
|
+
for( leg = 0; leg < NL; ++leg ) {
|
214
|
+
// simulation normal
|
215
|
+
if ( ko_so_far > 0.0 ) {
|
216
|
+
if( sim[leg] >= 0.0 ) {
|
217
|
+
// knock out condition
|
218
|
+
if( sim[leg] > ko_so_far ) {
|
219
|
+
sim_pos[leg] = ko_so_far * ( *( Ns_array + leg ) ); // discuss this
|
220
|
+
ko_so_far = 0.0;
|
221
|
+
} else if( sim[leg] <= ko_so_far ) {
|
222
|
+
sim_pos[leg] = sim[leg] * ( *( Ns_array + leg ) );
|
223
|
+
ko_so_far -= sim[leg];
|
224
|
+
}
|
225
|
+
} else {
|
226
|
+
sim_neg[leg] = sim[leg] * ( *( Ls_array + leg ) ) * ( *( Ns_array + leg ) );
|
227
|
+
}
|
228
|
+
}
|
229
|
+
|
230
|
+
// simulation reverse dash
|
231
|
+
if ( ko_so_far_dash > 0.0 ) {
|
232
|
+
if( sim_dash[leg] >= 0.0 ) {
|
233
|
+
// knock out condition
|
234
|
+
if( sim_dash[leg] > ko_so_far_dash ) {
|
235
|
+
sim_dash_pos[leg] = ko_so_far_dash * ( *( Ns_array + leg ) ); // discuss this
|
236
|
+
ko_so_far_dash = 0.0;
|
237
|
+
} else if( sim_dash[leg] <= ko_so_far_dash ) {
|
238
|
+
sim_dash_pos[leg] = sim_dash[leg] * ( *( Ns_array + leg ) );
|
239
|
+
ko_so_far_dash -= sim_dash[leg];
|
240
|
+
}
|
241
|
+
} else {
|
242
|
+
sim_dash_neg[leg] = sim_dash[leg] * ( *( Ls_array + leg ) ) * ( *( Ns_array + leg ) );
|
243
|
+
}
|
244
|
+
}
|
245
|
+
}
|
246
|
+
} else if( KType == LEGS ) {
|
247
|
+
for( leg = 0; leg < NL; ++leg ) {
|
248
|
+
// simulation normal
|
249
|
+
if ( ko_so_far > 0.0 ) {
|
250
|
+
if( sim[leg] >= 0.0 ) {
|
251
|
+
// knock out condition
|
252
|
+
sim_pos[leg] = sim[leg] * ( *( Ns_array + leg ) );
|
253
|
+
ko_so_far -= 1;
|
254
|
+
} else {
|
255
|
+
sim_neg[leg] = sim[leg] * ( *( Ls_array + leg ) ) * ( *( Ns_array + leg ) );
|
256
|
+
}
|
257
|
+
}
|
258
|
+
|
259
|
+
// simulation reverse dash
|
260
|
+
if ( ko_so_far_dash > 0.0 ) {
|
261
|
+
if( sim_dash[leg] >= 0.0 ) {
|
262
|
+
// knock out condition
|
263
|
+
sim_dash_pos[leg] = sim_dash[leg] * ( *( Ns_array + leg ) );
|
264
|
+
ko_so_far_dash -= 1;
|
265
|
+
} else {
|
266
|
+
sim_dash_neg[leg] = sim_dash[leg] * ( *( Ls_array + leg ) ) * ( *( Ns_array + leg ) );
|
267
|
+
}
|
268
|
+
}
|
269
|
+
}
|
270
|
+
}
|
271
|
+
|
272
|
+
// take sum of positive and negative payoffs and calculate pvs
|
273
|
+
double sim_pos_sum = 0.0, sim_neg_sum = 0.0, sim_dash_pos_sum = 0.0, sim_dash_neg_sum = 0.0;
|
274
|
+
for( leg = 0; leg < NL; ++leg ) {
|
275
|
+
sim_pos_sum += sim_pos[leg] * ( *( DFs_array + leg ) );
|
276
|
+
sim_neg_sum += sim_neg[leg] * ( *( DFs_array + leg ) );
|
277
|
+
sim_dash_pos_sum += sim_dash_pos[leg] * ( *( DFs_array + leg ) );
|
278
|
+
sim_dash_neg_sum += sim_dash_neg[leg] * ( *( DFs_array + leg ) );
|
279
|
+
}
|
280
|
+
|
281
|
+
// calculate +pv and -ve pv for individual leg
|
282
|
+
pvs_pos[sim_count] = sim_pos_sum;
|
283
|
+
pvs_pos[sim_count + 1] = sim_dash_pos_sum;
|
284
|
+
pvs_neg[sim_count] = sim_neg_sum;
|
285
|
+
pvs_neg[sim_count + 1] = sim_dash_neg_sum;
|
286
|
+
}
|
287
|
+
|
288
|
+
// sum all final +ve and -ve payoffs
|
289
|
+
for(sim_count = 0; sim_count < SCount; ++sim_count) {
|
290
|
+
pvs_pos_sum += *(pvs_pos + sim_count);
|
291
|
+
pvs_neg_sum += *(pvs_neg + sim_count);
|
292
|
+
}
|
293
|
+
|
294
|
+
// free dynamically alloted heap memory
|
295
|
+
free(pvs_pos);
|
296
|
+
free(pvs_neg);
|
297
|
+
free(Ls_array);
|
298
|
+
free(Ts_array);
|
299
|
+
free(Ns_array);
|
300
|
+
free(Xs_array);
|
301
|
+
free(vs_array);
|
302
|
+
free(bs_array);
|
303
|
+
free(DFs_array);
|
304
|
+
|
305
|
+
// return both payoffs
|
306
|
+
VALUE final_pvs = rb_hash_new();
|
307
|
+
rb_hash_aset(final_pvs, rb_str_new2("positive_pv"), DBL2NUM( pvs_pos_sum / SCount ));
|
308
|
+
rb_hash_aset(final_pvs, rb_str_new2("negative_pv"), DBL2NUM( pvs_neg_sum / SCount ));
|
309
|
+
rb_hash_aset(final_pvs, rb_str_new2("total_pv"), DBL2NUM( (pvs_neg_sum + pvs_pos_sum) / SCount ));
|
310
|
+
return final_pvs;
|
311
|
+
}
|
312
|
+
|
313
|
+
// The initialization method for this module
|
314
|
+
void Init_tarf_monte_carlo() {
|
315
|
+
// rb_define_module - defines a module
|
316
|
+
mTarfMonteCarlo = rb_define_module("TarfMonteCarlo");
|
317
|
+
|
318
|
+
// rb_define_class_under - defines a class under passed module
|
319
|
+
cTarfMonteCarlo = rb_define_class_under(mTarfMonteCarlo, "MC", rb_cObject);
|
320
|
+
|
321
|
+
// TarfMonteCarlo::MC
|
322
|
+
|
323
|
+
// void rb_define_singleton_method(VALUE klass, const char *name, VALUE (*func)(), int argc)
|
324
|
+
rb_define_singleton_method(cTarfMonteCarlo, "box_muller", method_box_muller, 0);
|
325
|
+
rb_define_singleton_method(cTarfMonteCarlo, "run_monte_carlo", method_run_monte_carlo, -2);
|
326
|
+
}
|
327
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "tarf_monte_carlo/version"
|
2
|
+
|
3
|
+
# must reuired- for requiring tarf_monte_carlo.so file
|
4
|
+
# my_malloc/my_malloc will be the shared object built when the gem is
|
5
|
+
# installed then copied into lib/my_malloc/.
|
6
|
+
|
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
|
+
|
13
|
+
module TarfMonteCarlo
|
14
|
+
# Your code goes here...
|
15
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# files = [
|
2
|
+
# "README.md",
|
3
|
+
# "Rakefile",
|
4
|
+
# "ext/tarf_monte_carlo/extconf.rb",
|
5
|
+
# "ext/tarf_monte_carlo/tarf_monte_carlo.c",
|
6
|
+
# "lib/tarf_monte_carlo.rb",
|
7
|
+
# "lib/tarf_monte_carlo/version.rb"
|
8
|
+
# ]
|
9
|
+
|
10
|
+
# coding: utf-8
|
11
|
+
lib = File.expand_path("../lib", __FILE__)
|
12
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
13
|
+
require "tarf_monte_carlo/version"
|
14
|
+
|
15
|
+
Gem::Specification.new do |spec|
|
16
|
+
spec.name = "tarf_monte_carlo"
|
17
|
+
spec.version = TarfMonteCarlo::VERSION
|
18
|
+
spec.authors = ["Vivek Routh"]
|
19
|
+
spec.email = ["vvkrth2@gmail.com"]
|
20
|
+
|
21
|
+
spec.summary = %q{Monte Carlo Simulation.}
|
22
|
+
spec.description = %q{Monte Carlo Simulation for TARF Contract.}
|
23
|
+
spec.homepage = %q{https://github.com/vivekapex/tarf_monte_carlo}
|
24
|
+
spec.license = "MIT"
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
|
+
# spec.files = `git ls-files -z`.split("\x0") + files
|
28
|
+
|
29
|
+
spec.bindir = "exe"
|
30
|
+
|
31
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
32
|
+
# spec.executables = ["tarf_monte_carlo"]
|
33
|
+
|
34
|
+
spec.require_paths = ["lib"]
|
35
|
+
spec.extensions = ["ext/tarf_monte_carlo/extconf.rb"]
|
36
|
+
spec.required_ruby_version = "~> 2.0"
|
37
|
+
|
38
|
+
spec.add_development_dependency "bundler", "~> 1.0"
|
39
|
+
spec.add_development_dependency "rake", "~> 10.4"
|
40
|
+
spec.add_development_dependency "rake-compiler", "~> 1.0"
|
41
|
+
spec.add_development_dependency "rake-compiler-dock", "~> 0.6.0"
|
42
|
+
spec.add_development_dependency "rspec", "~> 3.0", ">= 3.0.0"
|
43
|
+
spec.add_development_dependency "yard", "~> 0.9.11"
|
44
|
+
|
45
|
+
# spec.add_development_dependency "mini_portile2", "~> 2.3.0"
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tarf_monte_carlo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '2.2'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vivek Routh
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-04-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.4'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.4'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake-compiler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake-compiler-dock
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.6.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.6.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.0.0
|
76
|
+
- - "~>"
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '3.0'
|
79
|
+
type: :development
|
80
|
+
prerelease: false
|
81
|
+
version_requirements: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 3.0.0
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '3.0'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: yard
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: 0.9.11
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - "~>"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: 0.9.11
|
103
|
+
description: Monte Carlo Simulation for TARF Contract.
|
104
|
+
email:
|
105
|
+
- vvkrth2@gmail.com
|
106
|
+
executables: []
|
107
|
+
extensions:
|
108
|
+
- ext/tarf_monte_carlo/extconf.rb
|
109
|
+
extra_rdoc_files: []
|
110
|
+
files:
|
111
|
+
- ".gitignore"
|
112
|
+
- Gemfile
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- bin/console
|
116
|
+
- bin/setup
|
117
|
+
- ext/tarf_monte_carlo/extconf.rb
|
118
|
+
- ext/tarf_monte_carlo/tarf_monte_carlo.c
|
119
|
+
- lib/tarf_monte_carlo.rb
|
120
|
+
- lib/tarf_monte_carlo/version.rb
|
121
|
+
- tarf_monte_carlo.gemspec
|
122
|
+
homepage: https://github.com/vivekapex/tarf_monte_carlo
|
123
|
+
licenses:
|
124
|
+
- MIT
|
125
|
+
metadata: {}
|
126
|
+
post_install_message:
|
127
|
+
rdoc_options: []
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - "~>"
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '2.0'
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
requirements: []
|
141
|
+
rubygems_version: 3.0.6
|
142
|
+
signing_key:
|
143
|
+
specification_version: 4
|
144
|
+
summary: Monte Carlo Simulation.
|
145
|
+
test_files: []
|