cash_flow_analysis 0.1.0
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 +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +85 -0
- data/Rakefile +2 -0
- data/bin/console +11 -0
- data/bin/setup +7 -0
- data/cash_flow_analysis.gemspec +25 -0
- data/lib/cash_flow_analysis/calculation_error.rb +8 -0
- data/lib/cash_flow_analysis/calculator/cagr.rb +15 -0
- data/lib/cash_flow_analysis/calculator/payment.rb +15 -0
- data/lib/cash_flow_analysis/calculator/xirr.rb +29 -0
- data/lib/cash_flow_analysis/calculator/xnpv.rb +19 -0
- data/lib/cash_flow_analysis/util/objective_function.rb +39 -0
- data/lib/cash_flow_analysis/version.rb +3 -0
- data/lib/cash_flow_analysis.rb +7 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2b3168369c2bf3f70bd53f6bb2aab55b40de177c
|
4
|
+
data.tar.gz: 00c14fbfd6c104881d33a3878938c7012cfdcb17
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aa8a365bcc767a0267aee8f2ce0f11a63d2c67bd47e6849bce0ee88f3abbdad32b0ceca1c67054b4ccbb3c43d0531a1eecb0eb93e61853e3dffa7ed4381a7387
|
7
|
+
data.tar.gz: 3668b7a5ae6292a4407d5c3a1d316a8e1febdc8c9bc6be96ea324e67287611792026ec7fc485556e429bca13990ec250c641f29b0c4064851209dd6be850befd
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Brent Wheeldon
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# CashFlowAnalysis
|
2
|
+
|
3
|
+
Ruby implementation of various financial analysis functions.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem "cash_flow_analysis"
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install cash_flow_analysis
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
The gem provides four calculators, CAGR, Payment, XIRR, and XNPV.
|
24
|
+
|
25
|
+
### CAGR
|
26
|
+
|
27
|
+
Calculates the [CAGR](http://en.wikipedia.org/wiki/Compound_annual_growth_rate) based on a start value, end value, and a term.
|
28
|
+
|
29
|
+
```
|
30
|
+
CashFlowAnalysis::Calculator::Cagr.calculate(25.5, 35.5, 5)
|
31
|
+
# 0.06840923807777366
|
32
|
+
```
|
33
|
+
|
34
|
+
### Payment
|
35
|
+
|
36
|
+
Calculates the [payment for a loan](http://en.wikipedia.org/wiki/Mortgage_calculator), assuming fixed payments and rates.
|
37
|
+
|
38
|
+
```
|
39
|
+
CashFlowAnalysis::Calculator::Payment.calculate(10000, 60, 0.025)
|
40
|
+
# 323.5339590005929
|
41
|
+
```
|
42
|
+
|
43
|
+
### XIRR
|
44
|
+
|
45
|
+
Calculates the [XIRR](http://en.wikipedia.org/wiki/Internal_rate_of_return), assuming irregularly timed cash flows.
|
46
|
+
|
47
|
+
```
|
48
|
+
items = []
|
49
|
+
items << OpenStruct.new(date: Date.new(2008, 1, 1), amount: -10000)
|
50
|
+
items << OpenStruct.new(date: Date.new(2008, 3, 1), amount: 2750)
|
51
|
+
items << OpenStruct.new(date: Date.new(2008, 10, 30), amount: 4250)
|
52
|
+
items << OpenStruct.new(date: Date.new(2009, 2, 15), amount: 3250)
|
53
|
+
items << OpenStruct.new(date: Date.new(2009, 4, 1), amount: 2750)
|
54
|
+
CashFlowAnalysis::Calculator::Xirr.calculate(items)
|
55
|
+
# 0.373362533517793504136316860945228...
|
56
|
+
```
|
57
|
+
|
58
|
+
### XNPV
|
59
|
+
|
60
|
+
Calculates the [XNPV](http://en.wikipedia.org/wiki/Net_present_value), assuming irregularly timed cash flows.
|
61
|
+
|
62
|
+
```
|
63
|
+
items = []
|
64
|
+
items << OpenStruct.new(date: Date.new(2008, 1, 1), amount: -10000)
|
65
|
+
items << OpenStruct.new(date: Date.new(2008, 3, 1), amount: 2750)
|
66
|
+
items << OpenStruct.new(date: Date.new(2008, 10, 30), amount: 4250)
|
67
|
+
items << OpenStruct.new(date: Date.new(2009, 2, 15), amount: 3250)
|
68
|
+
items << OpenStruct.new(date: Date.new(2009, 4, 1), amount: 2750)
|
69
|
+
CashFlowAnalysis::Calculator::Xnpv.calculate(0.09, items)
|
70
|
+
# 2086.647602031535
|
71
|
+
```
|
72
|
+
|
73
|
+
## Development
|
74
|
+
|
75
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
76
|
+
|
77
|
+
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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
78
|
+
|
79
|
+
## Contributing
|
80
|
+
|
81
|
+
1. Fork it ( https://github.com/[my-github-username]/cash_flow_analysis/fork )
|
82
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
83
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
84
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
85
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "cash_flow_analysis"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "cash_flow_analysis/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cash_flow_analysis"
|
8
|
+
spec.version = CashFlowAnalysis::VERSION
|
9
|
+
spec.authors = ["Hightower"]
|
10
|
+
spec.email = ["engineering@gethightower.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Ruby implementation of various financial analysis functions.}
|
13
|
+
spec.homepage = "https://github.com/hightower/cash_flow_analysis"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
22
|
+
spec.add_development_dependency "pry", "~> 0"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.2"
|
25
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module CashFlowAnalysis
|
2
|
+
module Calculator
|
3
|
+
module Cagr
|
4
|
+
# Calculates the {http://en.wikipedia.org/wiki/Compound_annual_growth_rate CAGR} based on a start value, end value, and a term.
|
5
|
+
#
|
6
|
+
# @param start_value [Numeric] the start value
|
7
|
+
# @param end_value [Numeric] the end value
|
8
|
+
# @param years [Fixnum] the number of years
|
9
|
+
# @return [Numeric] the year over year growth rate
|
10
|
+
def self.calculate(start_value, end_value, years)
|
11
|
+
((end_value.to_f / start_value) ** (1.0 / (years))) - 1
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module CashFlowAnalysis
|
2
|
+
module Calculator
|
3
|
+
module Payment
|
4
|
+
# Calculates the {http://en.wikipedia.org/wiki/Mortgage_calculator payment for a loan}, assuming fixed payments and rates.
|
5
|
+
#
|
6
|
+
# @param principal [Numeric] the present value of the loan
|
7
|
+
# @param term [Fixnum] the term of the load
|
8
|
+
# @param per_period_rate [Numeric] the interest rate per period
|
9
|
+
# @return [Numeric] the payment required per period
|
10
|
+
def self.calculate(principal, term, per_period_rate)
|
11
|
+
(per_period_rate * principal) / (1 - ((1 + per_period_rate) ** (-1 * term)))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "bigdecimal/newton"
|
2
|
+
|
3
|
+
module CashFlowAnalysis
|
4
|
+
module Calculator
|
5
|
+
module Xirr
|
6
|
+
extend Newton
|
7
|
+
|
8
|
+
# Calculates the {http://en.wikipedia.org/wiki/Internal_rate_of_return XIRR}, assuming irregularly timed cash flows.
|
9
|
+
#
|
10
|
+
# @param cash_flow_items [Array] items which have `#date` ([Date]) and `#amount` ([Numeric]) properties
|
11
|
+
# @return [Numeric] the XIRR for the cash flow
|
12
|
+
def self.calculate(cash_flow_items)
|
13
|
+
return nil if cash_flow_items.map(&:amount).none? { |amount| amount >= 0 }
|
14
|
+
return nil if cash_flow_items.map(&:amount).none? { |amount| amount < 0 }
|
15
|
+
|
16
|
+
objective_function = Util::ObjectiveFunction.new(Xnpv, cash_flow_items)
|
17
|
+
discount_rate_vector = [objective_function.one]
|
18
|
+
|
19
|
+
begin
|
20
|
+
nlsolve(objective_function, discount_rate_vector)
|
21
|
+
rescue RuntimeError => ex
|
22
|
+
raise CalculationError.new(ex)
|
23
|
+
end
|
24
|
+
|
25
|
+
discount_rate_vector.first
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module CashFlowAnalysis
|
2
|
+
module Calculator
|
3
|
+
module Xnpv
|
4
|
+
# Calculates the {http://en.wikipedia.org/wiki/Net_present_value XNPV}, assuming irregularly timed cash flows.
|
5
|
+
#
|
6
|
+
# @param discount_rate [Numeric] the discount rate for the calculation
|
7
|
+
# @param cash_flow_items [Array] items which have `#date` ([Date]) and `#amount` ([Numeric]) properties
|
8
|
+
# @return [Numeric] the XNPV for the cash flow
|
9
|
+
def self.calculate(discount_rate, cash_flow_items)
|
10
|
+
start_date = cash_flow_items.first.date
|
11
|
+
|
12
|
+
cash_flow_items.inject(0) do |sum, item|
|
13
|
+
days_since_start = (item.date - start_date).to_i
|
14
|
+
sum += item.amount / ((1 + discount_rate) ** (days_since_start / 365.0))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module CashFlowAnalysis
|
2
|
+
module Util
|
3
|
+
class ObjectiveFunction
|
4
|
+
def eps
|
5
|
+
BigDecimal.new("1.0e-16")
|
6
|
+
end
|
7
|
+
|
8
|
+
def zero
|
9
|
+
BigDecimal.new("0.0")
|
10
|
+
end
|
11
|
+
|
12
|
+
def one
|
13
|
+
BigDecimal.new("1.0")
|
14
|
+
end
|
15
|
+
|
16
|
+
def two
|
17
|
+
BigDecimal.new("2.0")
|
18
|
+
end
|
19
|
+
|
20
|
+
def ten
|
21
|
+
BigDecimal.new("10.0")
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize(calculator, cash_flow_items)
|
25
|
+
@calculator = calculator
|
26
|
+
@cash_flow_items = cash_flow_items
|
27
|
+
end
|
28
|
+
|
29
|
+
def values(discount_rates)
|
30
|
+
[calculator.calculate(discount_rates.first.round(15), cash_flow_items)]
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
attr_reader :calculator
|
36
|
+
attr_reader :cash_flow_items
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
require "cash_flow_analysis/version"
|
2
|
+
require "cash_flow_analysis/calculation_error"
|
3
|
+
require "cash_flow_analysis/calculator/cagr"
|
4
|
+
require "cash_flow_analysis/calculator/payment"
|
5
|
+
require "cash_flow_analysis/calculator/xirr"
|
6
|
+
require "cash_flow_analysis/calculator/xnpv"
|
7
|
+
require "cash_flow_analysis/util/objective_function"
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cash_flow_analysis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hightower
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-06 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.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.2'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.2'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- engineering@gethightower.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- cash_flow_analysis.gemspec
|
87
|
+
- lib/cash_flow_analysis.rb
|
88
|
+
- lib/cash_flow_analysis/calculation_error.rb
|
89
|
+
- lib/cash_flow_analysis/calculator/cagr.rb
|
90
|
+
- lib/cash_flow_analysis/calculator/payment.rb
|
91
|
+
- lib/cash_flow_analysis/calculator/xirr.rb
|
92
|
+
- lib/cash_flow_analysis/calculator/xnpv.rb
|
93
|
+
- lib/cash_flow_analysis/util/objective_function.rb
|
94
|
+
- lib/cash_flow_analysis/version.rb
|
95
|
+
homepage: https://github.com/hightower/cash_flow_analysis
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
metadata: {}
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.4.6
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: Ruby implementation of various financial analysis functions.
|
119
|
+
test_files: []
|