finfast 0.0.1
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.
- data/README.rdoc +49 -0
- data/ext/finfast/extconf.rb +2 -0
- data/ext/finfast/finfast.c +64 -0
- data/lib/finfast.rb +36 -0
- data/lib/finfast/version.rb +6 -0
- data/test/finfast/finfast_test.rb +9 -0
- metadata +81 -0
data/README.rdoc
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
= finfast
|
|
2
|
+
|
|
3
|
+
* TODO url
|
|
4
|
+
|
|
5
|
+
== SYNOPSIS
|
|
6
|
+
|
|
7
|
+
TODO description
|
|
8
|
+
|
|
9
|
+
=== Usage
|
|
10
|
+
|
|
11
|
+
TODO (code sample of usage)
|
|
12
|
+
|
|
13
|
+
== REQUIREMENTS
|
|
14
|
+
|
|
15
|
+
* TODO (list of requirements)
|
|
16
|
+
|
|
17
|
+
== INSTALLATION
|
|
18
|
+
|
|
19
|
+
sudo gem install finfast
|
|
20
|
+
|
|
21
|
+
== DEVELOPMENT
|
|
22
|
+
|
|
23
|
+
TODO developer advice
|
|
24
|
+
|
|
25
|
+
== LICENSE
|
|
26
|
+
|
|
27
|
+
(The MIT License)
|
|
28
|
+
|
|
29
|
+
Copyright (c) 2012 TODO
|
|
30
|
+
|
|
31
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
32
|
+
a copy of this software and associated documentation files (the
|
|
33
|
+
'Software'), to deal in the Software without restriction, including
|
|
34
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
35
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
36
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
37
|
+
the following conditions:
|
|
38
|
+
|
|
39
|
+
The above copyright notice and this permission notice shall be
|
|
40
|
+
included in all copies or substantial portions of the Software.
|
|
41
|
+
|
|
42
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
43
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
44
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
45
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
46
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
47
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
48
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
49
|
+
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#include "ruby.h"
|
|
2
|
+
#include "math.h"
|
|
3
|
+
#include "stdio.h"
|
|
4
|
+
|
|
5
|
+
#define MIN(a,b) (((a)<(b))?(a):(b))
|
|
6
|
+
#define MAX(a,b) (((a)>(b))?(a):(b))
|
|
7
|
+
|
|
8
|
+
void Init_finfast();
|
|
9
|
+
static VALUE method_newton(VALUE self, VALUE pmts, VALUE exps,
|
|
10
|
+
VALUE guess, VALUE tolerance, VALUE max_iter);
|
|
11
|
+
static long double fx(long double x, VALUE *pmts, VALUE *exps, long len);
|
|
12
|
+
static long double dfx(long double x, VALUE *pmts, VALUE *exps, long len);
|
|
13
|
+
static long double const lower_bound = -0.99;
|
|
14
|
+
|
|
15
|
+
void Init_finfast() {
|
|
16
|
+
VALUE module = rb_define_module("Finfast");
|
|
17
|
+
rb_define_method(module, "newton", method_newton, 5);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static long double fx(long double x, VALUE *pmts, VALUE *exps, long len)
|
|
21
|
+
{
|
|
22
|
+
long double result = 0.0;
|
|
23
|
+
int i;
|
|
24
|
+
for (i = 0; i < len; ++i) {
|
|
25
|
+
long double ex = -NUM2DBL(exps[i]);
|
|
26
|
+
long double pmt = NUM2DBL(pmts[i]);
|
|
27
|
+
//printf("Payment: %Lf, exponent: %Lf\n", pmt, ex);
|
|
28
|
+
result += (pmt * pow(1.0 + x, ex));
|
|
29
|
+
}
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static long double dfx(long double x, VALUE *pmts, VALUE *exps, long len)
|
|
34
|
+
{
|
|
35
|
+
long double result = 0.0;
|
|
36
|
+
int i;
|
|
37
|
+
for (i = 0; i < len; ++i) {
|
|
38
|
+
long double ex = -NUM2DBL(exps[i]);
|
|
39
|
+
long double pmt = NUM2DBL(pmts[i]);
|
|
40
|
+
result += (pmt * ex * pow(1.0 + x, ex - 1.0));
|
|
41
|
+
}
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static VALUE method_newton(VALUE self, VALUE pmts, VALUE exps,
|
|
46
|
+
VALUE guess, VALUE tolerance, VALUE max_iter) {
|
|
47
|
+
long len = RARRAY_LEN(pmts);
|
|
48
|
+
long max_i = NUM2INT(max_iter);
|
|
49
|
+
long double x0 = NUM2DBL(guess);
|
|
50
|
+
long double x1 = 0.0;
|
|
51
|
+
long double tol = NUM2DBL(tolerance);
|
|
52
|
+
long double err = 1e100;
|
|
53
|
+
VALUE *pmt_p = RARRAY_PTR(pmts);
|
|
54
|
+
VALUE *exp_p = RARRAY_PTR(exps);
|
|
55
|
+
long iter = 0;
|
|
56
|
+
|
|
57
|
+
while (err > tol && iter++ < max_i) {
|
|
58
|
+
x1 = x0 - (fx(x0, pmt_p, exp_p, len) / dfx(x0, pmt_p, exp_p, len));
|
|
59
|
+
err = fabs(x1 - x0);
|
|
60
|
+
x0 = MAX(x1, lower_bound);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return (err > tol) ? Qnil : rb_float_new(x0);
|
|
64
|
+
}
|
data/lib/finfast.rb
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
require 'finfast/version'
|
|
2
|
+
require 'finfast/finfast'
|
|
3
|
+
|
|
4
|
+
module Finfast
|
|
5
|
+
|
|
6
|
+
DAYS_PER_YEAR = 365.0
|
|
7
|
+
|
|
8
|
+
# Calculates internal rate of return on a series of irregular cash flows, similar
|
|
9
|
+
# to the OpenOffice.org XIRR function described here:
|
|
10
|
+
# http://wiki.openoffice.org/wiki/Documentation/How_Tos/Calc%3a_XIRR_function
|
|
11
|
+
#
|
|
12
|
+
# Params:
|
|
13
|
+
# +transactions+:: A hash of {date: d, pmt: p}
|
|
14
|
+
# +guess+:: An initial guess for the algorithm.
|
|
15
|
+
#
|
|
16
|
+
def xirr(transactions, guess=0.0, tolerance=1e-6, iterations=100)
|
|
17
|
+
date_array = transactions.keys
|
|
18
|
+
pmt_array = transactions.values
|
|
19
|
+
d_0 = date_array.min
|
|
20
|
+
exp_array = date_array.map { |d| exp(d, d_0) }
|
|
21
|
+
newton(pmt_array, exp_array, guess, tolerance, iterations)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def self.exp(d_i, d_0)
|
|
27
|
+
if (d_i).is_a?(Date)
|
|
28
|
+
((d_i - d_0).to_i / DAYS_PER_YEAR)
|
|
29
|
+
else
|
|
30
|
+
raise "Can't calculate exponents for #{d_i.class}"
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
module_function :xirr, :newton
|
|
35
|
+
|
|
36
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: finfast
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Michael Grant
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-11-05 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: rake
|
|
16
|
+
requirement: &70281528856020 !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '0'
|
|
22
|
+
type: :development
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: *70281528856020
|
|
25
|
+
- !ruby/object:Gem::Dependency
|
|
26
|
+
name: rspec
|
|
27
|
+
requirement: &70281528855500 !ruby/object:Gem::Requirement
|
|
28
|
+
none: false
|
|
29
|
+
requirements:
|
|
30
|
+
- - ! '>='
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: *70281528855500
|
|
36
|
+
description: Fast Ruby financial math operations, using native code
|
|
37
|
+
email:
|
|
38
|
+
- mike.grant@jhu.edu
|
|
39
|
+
executables: []
|
|
40
|
+
extensions:
|
|
41
|
+
- ext/finfast/extconf.rb
|
|
42
|
+
extra_rdoc_files:
|
|
43
|
+
- README.rdoc
|
|
44
|
+
files:
|
|
45
|
+
- lib/finfast/version.rb
|
|
46
|
+
- lib/finfast.rb
|
|
47
|
+
- ext/finfast/finfast.c
|
|
48
|
+
- ext/finfast/extconf.rb
|
|
49
|
+
- README.rdoc
|
|
50
|
+
- test/finfast/finfast_test.rb
|
|
51
|
+
homepage: http://79mg.com/
|
|
52
|
+
licenses: []
|
|
53
|
+
post_install_message:
|
|
54
|
+
rdoc_options:
|
|
55
|
+
- --main
|
|
56
|
+
- README.rdoc
|
|
57
|
+
- --title
|
|
58
|
+
- finfast-0.0.1 Documentation
|
|
59
|
+
require_paths:
|
|
60
|
+
- lib
|
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
62
|
+
none: false
|
|
63
|
+
requirements:
|
|
64
|
+
- - ! '>='
|
|
65
|
+
- !ruby/object:Gem::Version
|
|
66
|
+
version: '0'
|
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
|
+
none: false
|
|
69
|
+
requirements:
|
|
70
|
+
- - ! '>='
|
|
71
|
+
- !ruby/object:Gem::Version
|
|
72
|
+
version: '0'
|
|
73
|
+
requirements: []
|
|
74
|
+
rubyforge_project:
|
|
75
|
+
rubygems_version: 1.8.17
|
|
76
|
+
signing_key:
|
|
77
|
+
specification_version: 3
|
|
78
|
+
summary: Fast Ruby financial math operations
|
|
79
|
+
test_files:
|
|
80
|
+
- test/finfast/finfast_test.rb
|
|
81
|
+
has_rdoc:
|