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.fin.env;
22
23 import java.math.BigDecimal;
24
25 import com.nickokiss.investor.calc.MathCalc;
26
27
28
29
30
31 public class PeriodicCompoundingStrategy implements CompoundingStrategy {
32
33 private MathCalc mathCalc = new MathCalc();
34
35 private BigDecimal periodsPerYear;
36
37 public PeriodicCompoundingStrategy(BigDecimal periodsPerYear) {
38 this.periodsPerYear = periodsPerYear;
39 }
40
41 public PeriodicCompoundingStrategy(String string) {
42 this(new BigDecimal(string));
43 }
44
45 public BigDecimal getPeriodsPerYear() {
46 return periodsPerYear;
47 }
48
49 public void setPeriodsPerYear(BigDecimal periodsPerYear) {
50 this.periodsPerYear = periodsPerYear;
51 }
52
53
54
55
56
57
58
59
60
61
62
63
64 public BigDecimal getForwardRate(BigDecimal startTime, BigDecimal endTime, InterestRateStrategy interestRateStrategy) {
65
66 BigDecimal sjPart = mathCalc.pow(mathCalc.div(interestRateStrategy.getSpotRate(endTime), periodsPerYear).add(mathCalc.ONE), endTime);
67
68 BigDecimal siPart = mathCalc.pow(mathCalc.div(interestRateStrategy.getSpotRate(startTime), periodsPerYear).add(mathCalc.ONE), startTime);
69
70 BigDecimal base = mathCalc.div(sjPart, siPart);
71
72 BigDecimal exponent = mathCalc.div(mathCalc.ONE, endTime.subtract(startTime));
73
74 return periodsPerYear.multiply(mathCalc.pow(base, exponent)).subtract(periodsPerYear);
75 }
76
77
78
79
80 public BigDecimal getDiscountFactor(BigDecimal startTime, BigDecimal endTime, InterestRateStrategy interestRateStrategy) {
81
82 BigDecimal exponent = periodsPerYear.multiply(endTime.subtract(startTime));
83 if (exponent.compareTo(mathCalc.ZERO) == 0) {
84 return mathCalc.ONE;
85 }
86 BigDecimal forwardRate = getForwardRate(startTime, endTime, interestRateStrategy);
87
88 BigDecimal base = mathCalc.div(mathCalc.ONE, mathCalc.ONE.add(mathCalc.div(forwardRate, periodsPerYear)));
89
90 return mathCalc.pow(base, exponent);
91 }
92
93
94
95
96 public BigDecimal getDurationMultiplicand(BigDecimal time, BigDecimal spotRate) {
97 BigDecimal base = mathCalc.div(spotRate, periodsPerYear).add(mathCalc.ONE);
98 BigDecimal exponent = time.multiply(periodsPerYear).add(mathCalc.ONE).negate();
99 return mathCalc.pow(base, exponent);
100 }
101
102
103 }