rupee 0.2.7 → 0.2.8
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +2 -4
- data/.gitignore +1 -0
- data/ext/rupee/bond.c +77 -26
- data/ext/rupee/future.c +5 -0
- data/lib/rupee.rb +3 -2
- data/lib/rupee/curve.rb +27 -0
- data/lib/rupee/curve/libor_3m.rb +5 -0
- data/lib/rupee/rate.rb +9 -0
- data/lib/rupee/version.rb +1 -1
- data/rupee.gemspec +3 -0
- data/spec/native/statistics_spec.rb +4 -3
- data/test_rubies +6 -4
- metadata +44 -9
- data/lib/rupee/yield_curve.rb +0 -14
data/.autotest
CHANGED
data/.gitignore
CHANGED
data/ext/rupee/bond.c
CHANGED
@@ -110,7 +110,13 @@ bond_ytm(times, cflows, price, len, discrete)
|
|
110
110
|
return r;
|
111
111
|
};
|
112
112
|
|
113
|
-
|
113
|
+
/*
|
114
|
+
* Ruby singleton functions
|
115
|
+
*
|
116
|
+
* See helper functions below for description broken out by discrete and
|
117
|
+
* continuous compounding. +discrete+ is a boolean equal to +true+ for discrete
|
118
|
+
* compounding and +false+ for continuous compounding
|
119
|
+
*/
|
114
120
|
|
115
121
|
static VALUE
|
116
122
|
convexity(self, _times, _cflows, _r, discrete)
|
@@ -174,9 +180,28 @@ price(self, _times, _cflows, _r, discrete)
|
|
174
180
|
return rb_float_new(bond_price(times, cflows, r, len, discrete));
|
175
181
|
}
|
176
182
|
|
183
|
+
static VALUE
|
184
|
+
yield_to_maturity(self, _times, _cflows, _price, discrete)
|
185
|
+
VALUE self, _times, _cflows, _price;
|
186
|
+
bool discrete;
|
187
|
+
{
|
188
|
+
int len = RARRAY_LEN(_cflows);
|
189
|
+
double times[len], cflows[len], price;
|
190
|
+
|
191
|
+
rtofa(times, _times, len);
|
192
|
+
rtofa(cflows, _cflows, len);
|
193
|
+
price = NUM2DBL(_price);
|
194
|
+
|
195
|
+
return rb_float_new(bond_ytm(times, cflows, price, len, discrete));
|
196
|
+
};
|
197
|
+
|
177
198
|
// Helper functions
|
178
199
|
|
179
|
-
|
200
|
+
/* call-seq: convexity_continuous(times, cflows, rates)
|
201
|
+
*
|
202
|
+
* The bond's convexity based on the provided set of times (in years), cash
|
203
|
+
* flows and discount rates, assuming continuous compounding
|
204
|
+
*/
|
180
205
|
static VALUE
|
181
206
|
convexity_continuous(self, _times, _cflows, _r)
|
182
207
|
VALUE self, _times, _cflows, _r;
|
@@ -184,7 +209,11 @@ convexity_continuous(self, _times, _cflows, _r)
|
|
184
209
|
return convexity(self, _times, _cflows, _r, false);
|
185
210
|
}
|
186
211
|
|
187
|
-
|
212
|
+
/* call-seq: convexity_discrete(times, cflows, rates)
|
213
|
+
*
|
214
|
+
* The bond's convexity based on the provided set of times (in years), cash
|
215
|
+
* flows and discount rates, assuming discrete compounding
|
216
|
+
*/
|
188
217
|
static VALUE
|
189
218
|
convexity_discrete(self, _times, _cflows, _r)
|
190
219
|
VALUE self, _times, _cflows, _r;
|
@@ -192,7 +221,11 @@ convexity_discrete(self, _times, _cflows, _r)
|
|
192
221
|
return convexity(self, _times, _cflows, _r, true);
|
193
222
|
}
|
194
223
|
|
195
|
-
|
224
|
+
/* call-seq: duration_continuous(times, cflows, rates)
|
225
|
+
*
|
226
|
+
* The bond's duration based on the provided set of times (in years), cash
|
227
|
+
* flows and discount rates, assuming continuous compounding
|
228
|
+
*/
|
196
229
|
static VALUE
|
197
230
|
duration_continuous(self, _times, _cflows, _r)
|
198
231
|
VALUE self, _times, _cflows, _r;
|
@@ -200,7 +233,11 @@ duration_continuous(self, _times, _cflows, _r)
|
|
200
233
|
return duration(self, _times, _cflows, _r, false);
|
201
234
|
}
|
202
235
|
|
203
|
-
|
236
|
+
/* call-seq: duration_discrete(times, cflows, rates)
|
237
|
+
*
|
238
|
+
* The bond's duration based on the provided set of times (in years), cash
|
239
|
+
* flows and discount rates, assuming discrete compounding
|
240
|
+
*/
|
204
241
|
static VALUE
|
205
242
|
duration_discrete(self, _times, _cflows, _r)
|
206
243
|
VALUE self, _times, _cflows, _r;
|
@@ -208,7 +245,11 @@ duration_discrete(self, _times, _cflows, _r)
|
|
208
245
|
return duration(self, _times, _cflows, _r, true);
|
209
246
|
}
|
210
247
|
|
211
|
-
|
248
|
+
/* call-seq: macaulay_continuous(times, cflows, price)
|
249
|
+
*
|
250
|
+
* The bond's Macaulay duration based on the provided set of times (in years),
|
251
|
+
* cash flows and price, assuming continuous compounding
|
252
|
+
*/
|
212
253
|
static VALUE
|
213
254
|
macaulay_continuous(self, _times, _cflows, _price)
|
214
255
|
VALUE self, _times, _cflows, _price;
|
@@ -216,7 +257,11 @@ macaulay_continuous(self, _times, _cflows, _price)
|
|
216
257
|
return macaulay(self, _times, _cflows, _price, false);
|
217
258
|
}
|
218
259
|
|
219
|
-
|
260
|
+
/* call-seq: macaulay_discrete(times, cflows, price)
|
261
|
+
*
|
262
|
+
* The bond's Macaulay duration based on the provided set of times (in years),
|
263
|
+
* cash flows and price, assuming discrete compounding
|
264
|
+
*/
|
220
265
|
static VALUE
|
221
266
|
macaulay_discrete(self, _times, _cflows, _price)
|
222
267
|
VALUE self, _times, _cflows, _price;
|
@@ -224,7 +269,11 @@ macaulay_discrete(self, _times, _cflows, _price)
|
|
224
269
|
return macaulay(self, _times, _cflows, _price, true);
|
225
270
|
}
|
226
271
|
|
227
|
-
|
272
|
+
/* call-seq: modified_discrete(times, cflows, price)
|
273
|
+
*
|
274
|
+
* The bond's duration based on the provided set of times (in years), cash
|
275
|
+
* flows and price, assuming discrete compounding
|
276
|
+
*/
|
228
277
|
static VALUE
|
229
278
|
modified_discrete(self, _times, _cflows, _price)
|
230
279
|
VALUE self, _times, _cflows, _price;
|
@@ -242,7 +291,11 @@ modified_discrete(self, _times, _cflows, _price)
|
|
242
291
|
return rb_float_new(D / (1 + y));
|
243
292
|
};
|
244
293
|
|
245
|
-
|
294
|
+
/* call-seq: price_continuous(times, cflows, rates)
|
295
|
+
*
|
296
|
+
* The bond's price based on the provided set of times (in years), cash flows
|
297
|
+
* and discount rates, assuming continuous compounding
|
298
|
+
*/
|
246
299
|
static VALUE
|
247
300
|
price_continuous(self, _times, _cflows, _r)
|
248
301
|
VALUE self, _times, _cflows, _r;
|
@@ -250,7 +303,11 @@ price_continuous(self, _times, _cflows, _r)
|
|
250
303
|
return price(self, _times, _cflows, _r, false);
|
251
304
|
}
|
252
305
|
|
253
|
-
|
306
|
+
/* call-seq: price_discrete(times, cflows, rates)
|
307
|
+
*
|
308
|
+
* The bond's price based on the provided set of times (in years), cash flows
|
309
|
+
* and discount rates, assuming discrete compounding
|
310
|
+
*/
|
254
311
|
static VALUE
|
255
312
|
price_discrete(self, _times, _cflows, _r)
|
256
313
|
VALUE self, _times, _cflows, _r;
|
@@ -258,22 +315,12 @@ price_discrete(self, _times, _cflows, _r)
|
|
258
315
|
return price(self, _times, _cflows, _r, true);
|
259
316
|
}
|
260
317
|
|
261
|
-
static VALUE
|
262
|
-
yield_to_maturity(self, _times, _cflows, _price, discrete)
|
263
|
-
VALUE self, _times, _cflows, _price;
|
264
|
-
bool discrete;
|
265
|
-
{
|
266
|
-
int len = RARRAY_LEN(_cflows);
|
267
|
-
double times[len], cflows[len], price;
|
268
|
-
|
269
|
-
rtofa(times, _times, len);
|
270
|
-
rtofa(cflows, _cflows, len);
|
271
|
-
price = NUM2DBL(_price);
|
272
|
-
|
273
|
-
return rb_float_new(bond_ytm(times, cflows, price, len, discrete));
|
274
|
-
};
|
275
318
|
|
276
|
-
|
319
|
+
/* call-seq: yield_to_maturity_continuous(times, cflows, rates)
|
320
|
+
*
|
321
|
+
* The bond's yield to maturity based on the provided set of times (in years),
|
322
|
+
* cash flows and price, assuming continuous compounding
|
323
|
+
*/
|
277
324
|
static VALUE
|
278
325
|
yield_to_maturity_continuous(self, _times, _cflows, _price)
|
279
326
|
VALUE self, _times, _cflows, _price;
|
@@ -281,7 +328,11 @@ yield_to_maturity_continuous(self, _times, _cflows, _price)
|
|
281
328
|
return yield_to_maturity(self, _times, _cflows, _price, false);
|
282
329
|
}
|
283
330
|
|
284
|
-
|
331
|
+
/* call-seq: yield_to_maturity_discrete(times, cflows, rates)
|
332
|
+
*
|
333
|
+
* The bond's yield to maturity based on the provided set of times (in years),
|
334
|
+
* cash flows and price, assuming discrete compounding
|
335
|
+
*/
|
285
336
|
static VALUE
|
286
337
|
yield_to_maturity_discrete(self, _times, _cflows, _price)
|
287
338
|
VALUE self, _times, _cflows, _price;
|
data/ext/rupee/future.c
CHANGED
@@ -7,6 +7,11 @@ future_price(S, r, ttm)
|
|
7
7
|
return S * exp(r * ttm);
|
8
8
|
}
|
9
9
|
|
10
|
+
/* call-seq: price(underlying, rate, time_to_maturity)
|
11
|
+
*
|
12
|
+
* The future's price based on the provided underlying, risk-free rate and time
|
13
|
+
* to maturity
|
14
|
+
*/
|
10
15
|
static VALUE
|
11
16
|
price(self, _S, _r, _ttm)
|
12
17
|
VALUE self, _S, _r, _ttm;
|
data/lib/rupee.rb
CHANGED
@@ -22,14 +22,15 @@ module Rupee
|
|
22
22
|
autoload :Benchmark, "rupee/benchmark"
|
23
23
|
autoload :BusinessDay, "rupee/business_day"
|
24
24
|
autoload :Calendar, "rupee/calendar"
|
25
|
-
autoload :FixedIncome, "rupee/fixed_income"
|
26
25
|
autoload :Call, "rupee/option"
|
26
|
+
autoload :Curve, "rupee/curve"
|
27
|
+
autoload :FixedIncome, "rupee/fixed_income"
|
27
28
|
autoload :Currency, "rupee/currency"
|
28
29
|
autoload :DayCount, "rupee/day_count"
|
29
30
|
autoload :Frequency, "rupee/frequency"
|
30
31
|
autoload :Option, "rupee/option"
|
31
32
|
autoload :Put, "rupee/option"
|
32
33
|
autoload :Quote, "rupee/quote"
|
34
|
+
autoload :Rate, "rupee/rate"
|
33
35
|
autoload :Source, "rupee/source"
|
34
|
-
autoload :YieldCurve, "rupee/yield_curve"
|
35
36
|
end
|
data/lib/rupee/curve.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module Rupee
|
2
|
+
# A class representing a yield or basis curve
|
3
|
+
class Curve
|
4
|
+
include FindInstance
|
5
|
+
|
6
|
+
autoload :LIBOR_3M, "rupee/curve/libor_3m"
|
7
|
+
|
8
|
+
# The curve's currency
|
9
|
+
attr :currency
|
10
|
+
# A description for the curve
|
11
|
+
attr :description
|
12
|
+
# The interpolation method
|
13
|
+
attr :interpolation
|
14
|
+
|
15
|
+
# Create a new curve
|
16
|
+
def initialize(description = "", opts = {})
|
17
|
+
opts = {
|
18
|
+
:currency => :usd,
|
19
|
+
:interpolation => :cubic_spline
|
20
|
+
}.merge opts
|
21
|
+
|
22
|
+
@description = description
|
23
|
+
@currency = opts[:currency]
|
24
|
+
@interpolation = opts[:interpolation]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/rupee/rate.rb
ADDED
data/lib/rupee/version.rb
CHANGED
data/rupee.gemspec
CHANGED
@@ -27,4 +27,7 @@ Gem::Specification.new do |s|
|
|
27
27
|
s.add_development_dependency "bundler", "~> 1.0"
|
28
28
|
s.add_development_dependency "rspec", "~> 2.0"
|
29
29
|
s.add_development_dependency "sdoc", "~> 0.3"
|
30
|
+
s.add_development_dependency "autotest"
|
31
|
+
s.add_development_dependency "autotest-fsevent"
|
32
|
+
s.add_development_dependency "autotest-growl"
|
30
33
|
end
|
@@ -42,8 +42,9 @@ describe Statistics do
|
|
42
42
|
end
|
43
43
|
|
44
44
|
it "should return an accurate correlation" do
|
45
|
-
Statistics.correlation(@values, @more_values)
|
46
|
-
|
47
|
-
Statistics.
|
45
|
+
@result = Statistics.correlation(@values, @more_values)
|
46
|
+
@result.should be_within(@tolerance).of 0.95
|
47
|
+
Statistics.corr(@values, @more_values).should == @result
|
48
|
+
Statistics.correl(@values, @more_values).should == @result
|
48
49
|
end
|
49
50
|
end
|
data/test_rubies
CHANGED
@@ -6,16 +6,18 @@ CURRENT=`rvm current`
|
|
6
6
|
|
7
7
|
# Prints the name of the Ruby, sets it to use under RVM, installs, tests
|
8
8
|
function test_ruby {
|
9
|
-
echo -e "\n\033[1m$
|
9
|
+
echo -e "\n\033[1m$2\033[0m"
|
10
10
|
rvm use $1
|
11
11
|
rake install
|
12
12
|
rspec spec
|
13
13
|
}
|
14
14
|
|
15
15
|
# Run tests on these versions
|
16
|
-
test_ruby 1.8.7
|
17
|
-
test_ruby 1.9.2
|
18
|
-
test_ruby 1.9.3
|
16
|
+
test_ruby 1.8.7 "Ruby 1.8.7 (MRI)"
|
17
|
+
test_ruby 1.9.2 "Ruby 1.9.2 (MRI)"
|
18
|
+
test_ruby 1.9.3 "Ruby 1.9.3 (MRI)"
|
19
|
+
test_ruby jruby JRuby
|
20
|
+
test_ruby rbx Rubinius
|
19
21
|
|
20
22
|
# Restore original Ruby in use
|
21
23
|
rvm use $CURRENT
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rupee
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-10-
|
12
|
+
date: 2011-10-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
16
|
-
requirement: &
|
16
|
+
requirement: &70214885187220 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '1.0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70214885187220
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70214885201400 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '2.0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70214885201400
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: sdoc
|
38
|
-
requirement: &
|
38
|
+
requirement: &70214885198020 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,7 +43,40 @@ dependencies:
|
|
43
43
|
version: '0.3'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70214885198020
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: autotest
|
49
|
+
requirement: &70214885197200 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70214885197200
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: autotest-fsevent
|
60
|
+
requirement: &70214885196560 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70214885196560
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: autotest-growl
|
71
|
+
requirement: &70214885195960 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70214885195960
|
47
80
|
description: ! " rupee aims to provide user-friendly tools for
|
48
81
|
use in\n financial gems and applications.\n"
|
49
82
|
email:
|
@@ -83,6 +116,8 @@ files:
|
|
83
116
|
- lib/rupee/currency/gbp.rb
|
84
117
|
- lib/rupee/currency/jpy.rb
|
85
118
|
- lib/rupee/currency/usd.rb
|
119
|
+
- lib/rupee/curve.rb
|
120
|
+
- lib/rupee/curve/libor_3m.rb
|
86
121
|
- lib/rupee/day_count.rb
|
87
122
|
- lib/rupee/day_count/30_360.rb
|
88
123
|
- lib/rupee/day_count/30e+_360.rb
|
@@ -96,13 +131,13 @@ files:
|
|
96
131
|
- lib/rupee/mixins/find_instance.rb
|
97
132
|
- lib/rupee/option.rb
|
98
133
|
- lib/rupee/quote.rb
|
134
|
+
- lib/rupee/rate.rb
|
99
135
|
- lib/rupee/security.rb
|
100
136
|
- lib/rupee/source.rb
|
101
137
|
- lib/rupee/source/bloomberg.rb
|
102
138
|
- lib/rupee/source/google.rb
|
103
139
|
- lib/rupee/source/yahoo.rb
|
104
140
|
- lib/rupee/version.rb
|
105
|
-
- lib/rupee/yield_curve.rb
|
106
141
|
- rupee.gemspec
|
107
142
|
- spec/native/bond_spec.rb
|
108
143
|
- spec/native/future_spec.rb
|
data/lib/rupee/yield_curve.rb
DELETED