1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package com.nickokiss.investor.calc;
22
23 import java.math.BigDecimal;
24
25
26
27
28
29 public class ContCompInterestCalc {
30
31 private MathCalc mathCalc = new MathCalc();
32
33
34
35
36 public BigDecimal getGrowthFactor(BigDecimal nominalInterestRate, BigDecimal time) {
37 BigDecimal scaledNominalInterestRate = nominalInterestRate.multiply(time);
38 return mathCalc.exp(scaledNominalInterestRate);
39 }
40
41
42
43
44 public BigDecimal getFutureValue(BigDecimal presentValue, BigDecimal nominalInterestRate, BigDecimal time) {
45 BigDecimal growth = getGrowthFactor(nominalInterestRate, time);
46 BigDecimal futureValue = presentValue.multiply(growth);
47 return futureValue;
48 }
49
50
51
52
53
54
55
56
57
58 public BigDecimal getPresentValue(BigDecimal futureValue, BigDecimal nominalInterestRate, BigDecimal time) {
59 BigDecimal growth = getGrowthFactor(nominalInterestRate, time);
60 BigDecimal presentValue = mathCalc.div(futureValue, growth);
61 return presentValue;
62 }
63
64
65
66
67
68
69
70
71
72
73
74 public BigDecimal getNominalInterestRate(BigDecimal futureValue, BigDecimal presentValue, BigDecimal time) {
75 BigDecimal growth = mathCalc.div(futureValue, presentValue);
76 BigDecimal scaledNominalInterestRate = mathCalc.log(growth);
77 BigDecimal nominalInterestRate = mathCalc.div(scaledNominalInterestRate, time);
78 return nominalInterestRate;
79 }
80
81
82
83
84
85
86
87
88
89
90
91 public BigDecimal getTime(BigDecimal futureValue, BigDecimal presentValue, BigDecimal nominalInterestRate) {
92 BigDecimal growth = mathCalc.div(futureValue, presentValue);
93 BigDecimal scaledNominalInterestRate = mathCalc.log(growth);
94 BigDecimal time = mathCalc.div(scaledNominalInterestRate, nominalInterestRate);
95 return time;
96 }
97
98
99
100
101 public BigDecimal getEffectiveInterestRate(BigDecimal nominalInterestRate) {
102
103 BigDecimal yearlyGrowth = mathCalc.exp(nominalInterestRate);
104 BigDecimal effectiveInterestRate = yearlyGrowth.subtract(mathCalc.ONE);
105 return effectiveInterestRate;
106 }
107
108
109
110
111
112
113
114
115
116
117 public BigDecimal getNominalInterestRate(BigDecimal effectiveInterestRate) {
118 BigDecimal yearlyGrowth = effectiveInterestRate.add(mathCalc.ONE);
119 BigDecimal nominalInterestRate = mathCalc.log(yearlyGrowth);
120 return nominalInterestRate;
121 }
122
123 public BigDecimal getDiscountFactor(BigDecimal nominalInterestRate, BigDecimal investmentYears) {
124 return getGrowthFactor(nominalInterestRate, investmentYears.negate());
125 }
126
127
128
129
130
131
132
133
134
135
136 public BigDecimal getForwardRate(BigDecimal spotRate1, BigDecimal time1, BigDecimal spotRate2, BigDecimal time2) {
137 BigDecimal sjj = spotRate2.multiply(time2);
138 BigDecimal sii = spotRate1.multiply(time1);
139 return mathCalc.div(sjj.subtract(sii), time2.subtract(time1));
140 }
141
142 public BigDecimal getDiscountFactor(BigDecimal spotRate1, BigDecimal time1, BigDecimal spotRate2, BigDecimal time2) {
143 BigDecimal forwardRate = getForwardRate(spotRate1, time1, spotRate2, time2);
144 return getDiscountFactorFromForwardRate(forwardRate, time1, time2);
145 }
146
147
148
149
150 public BigDecimal getDiscountFactorFromForwardRate(BigDecimal forwardRate, BigDecimal time1, BigDecimal time2) {
151
152 return mathCalc.pow(mathCalc.invert(forwardRate.add(mathCalc.ONE)), time2.subtract(time1));
153 }
154
155 }