fincalc 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.
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env ruby
2
+
@@ -0,0 +1,73 @@
1
+ # FinCalc is a class with methods to perform basic financial operations
2
+ # It works inspired in the HP-12 financial calclulator. Feel free to
3
+ # add more financial operations as needed
4
+ # Author: Hernan Velasquez
5
+ class FinCalc
6
+ # Interest rate
7
+ attr_writer :i
8
+ # Number of periods
9
+ attr_writer :n
10
+ # Ammount of a constant payment in a cash flow
11
+ attr_writer :pmt
12
+ # Ammount of the i-esm paymanent in a no-constant cash flow
13
+ attr_writer :cfi
14
+ # To be used to calculate the NPV of a cash flow. The internalo period counter
15
+ attr_reader :periodCounter
16
+
17
+ # Class constructor. Clear the calculator
18
+ def initialize
19
+ clear
20
+ end
21
+
22
+ # Method to clear the calculator. Set all variables in their defaults
23
+ def clear
24
+ @i = 0
25
+ @n = 0
26
+ @pmt = 0
27
+ @periodCounter = 1
28
+ @cfi = Array.new
29
+ @cfi[0] = 0
30
+ end
31
+
32
+ # Set the cashflow value for period 0
33
+ def cfPeriod0 (pValue)
34
+ @cfi[0] = pValue
35
+ end
36
+
37
+ # Set the cashflow value for period i
38
+ def cfiPeriodi (pValue)
39
+ @cfi[@periodCounter] = pValue
40
+ @periodCounter = @periodCounter + 1
41
+ end
42
+
43
+ # Calculate the present value of an annuity based on the attributes values already set
44
+ def calculateAnnuity
45
+ rate = @i /100.0
46
+ @pmt*((1 - (1 / ((1 + rate)**@n))) / rate)
47
+ end
48
+
49
+ # Calculates the present value of a perpetuity taking the attribute's values
50
+ def calculatePerpetuityPV
51
+ return @pmt / (@i / 100.0)
52
+ end
53
+
54
+ # Static method to calculate the present value of a perpetuity taking the attribute's values
55
+ def self.calculatePerpetuityPV (pPaymentValue, pInterestRate)
56
+ i = (pInterestRate / 100.0)
57
+ return pPaymentValue / i
58
+ end
59
+
60
+ # Calculates the Net Present Value of a cashflow
61
+ def NPV
62
+ npv = 0;
63
+ rate = @i / 100.0;
64
+ j = 0
65
+ while j < @periodCounter
66
+ val = @cfi[j] / (1+rate)**j
67
+ npv = npv + val
68
+ j = j+1
69
+ end
70
+ return npv
71
+ end
72
+
73
+ end
@@ -0,0 +1,59 @@
1
+ require 'test/unit'
2
+ require 'fincalc'
3
+
4
+ class FinCalcTest < Test::Unit::TestCase
5
+
6
+ def test_perpetuity_static
7
+ assert_equal 10000.0, FinCalc.calculatePerpetuityPV(100, 1)
8
+ end
9
+
10
+ def test_perpetuity_nostatic
11
+ f = FinCalc.new
12
+ f.pmt = 100
13
+ f.i = 1
14
+ assert_equal 10000.0, f.calculatePerpetuityPV
15
+ end
16
+
17
+ def test_npv_2periods
18
+ f = FinCalc.new
19
+ f.i = 10
20
+ f.cfPeriod0 -50.0
21
+ f.cfiPeriodi 100
22
+ assert_equal 40.90909090909091, f.NPV
23
+ end
24
+
25
+ def test_npv_5periods
26
+ f = FinCalc.new
27
+ f.i = 10
28
+ f.cfPeriod0 -50.0
29
+ f.cfiPeriodi 60
30
+ f.cfiPeriodi -30
31
+ f.cfiPeriodi 10
32
+ f.cfiPeriodi 20
33
+ assert_equal 0.925483232019662, f.NPV
34
+ end
35
+
36
+ def test_clear
37
+ f = FinCalc.new
38
+ f.i = 10
39
+ f.cfPeriod0 -50.0
40
+ f.cfiPeriodi 100
41
+ assert_equal 40.90909090909091, f.NPV
42
+ f.clear
43
+ f.i = 10
44
+ f.cfPeriod0 -50.0
45
+ f.cfiPeriodi 60
46
+ f.cfiPeriodi -30
47
+ f.cfiPeriodi 10
48
+ f.cfiPeriodi 20
49
+ assert_equal 0.925483232019662, f.NPV
50
+ end
51
+
52
+ def test_annuity
53
+ f = FinCalc.new
54
+ f.i = 10.0
55
+ f.pmt = 500
56
+ f.n = 12
57
+ assert_equal 3406.8459114482175, f.calculateAnnuity
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fincalc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hernan Velasquez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-07 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple financial calculator
15
+ email: hernamvel@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Rakefile
21
+ - lib/fincalc.rb
22
+ - bin/fincalc
23
+ - test/test_fincalc.rb
24
+ homepage: http://rubygems.org/gems/fincalc
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.24
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: This is a small financial calculator. You can calculate the NPV of a cashflows
48
+ over the time!
49
+ test_files:
50
+ - test/test_fincalc.rb