1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nyholm\EffectiveInterest; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @author Tobias Nyholm <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
class Calculator |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var NewtonRaphson |
12
|
|
|
*/ |
13
|
|
|
private $newton; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param NewtonRaphson $newton |
17
|
|
|
*/ |
18
|
|
|
public function __construct(NewtonRaphson $newton = null) |
19
|
|
|
{ |
20
|
|
|
$this->newton = $newton ?? new NewtonRaphson(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get the interest when you know all the payments and their dates. Use this function when you have |
25
|
|
|
* administration fees at the first payment and/or when payments are irregular. |
26
|
|
|
* |
27
|
|
|
* @param int $principal |
28
|
|
|
* @param string $startDate in format 'YYYY-mm-dd' |
29
|
|
|
* @param array $payments array with payment dates and values ['YYYY-mm-dd'=>int] |
30
|
|
|
* @param float $guess A guess what the interest may be. Between zero and one. Example 0.045 |
31
|
|
|
* |
32
|
|
|
* @return float |
33
|
|
|
*/ |
34
|
|
|
public function withSpecifiedPayments(int $principal, string $startDate, array $payments, float $guess): float |
35
|
|
|
{ |
36
|
|
|
$values = [-1 * $principal]; |
37
|
|
|
$dates = [1]; |
38
|
|
|
$startDate = new \DateTimeImmutable($startDate); |
39
|
|
|
|
40
|
|
|
foreach ($payments as $date => $payment) { |
41
|
|
|
$values[] = $payment; |
42
|
|
|
$dates[] = 1 + $startDate->diff(new \DateTime($date))->days; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$fx = function ($x) use ($dates, $values) { |
46
|
|
|
$sum = 0; |
47
|
|
|
foreach ($dates as $idx => $date) { |
48
|
|
|
$sum += $values[$idx] * pow(1 + $x, ($dates[0] - $date) / 365); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $sum; |
52
|
|
|
}; |
53
|
|
|
|
54
|
|
|
$fdx = function ($x) use ($dates, $values) { |
55
|
|
|
$sum = 0; |
56
|
|
|
foreach ($dates as $idx => $date) { |
57
|
|
|
$sum += (1 / 365) * ($dates[0] - $date) * $values[$idx] * pow(1 + $x, (($dates[0] - $date) / 365.0) - 1.0); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $sum; |
61
|
|
|
}; |
62
|
|
|
|
63
|
|
|
return $this->newton->run($fx, $fdx, $guess); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the effective interest when the monthly payments are exactly the same. |
68
|
|
|
* |
69
|
|
|
* @param int $a The total loan amount (Principal) |
70
|
|
|
* @param int $p The monthly payment |
71
|
|
|
* @param int $n The number of months |
72
|
|
|
* @param float $i A guess of what the interest might be. Interest as a number between zero and one. Example 0.045 |
73
|
|
|
* |
74
|
|
|
* @return float |
75
|
|
|
*/ |
76
|
|
|
public function withEqualPayments(int $a, int $p, int $n, float $i): float |
77
|
|
|
{ |
78
|
|
View Code Duplication |
$fx = function ($i) use ($a, $p, $n) { |
|
|
|
|
79
|
|
|
return $p - $p * pow(1 + $i, -1 * $n) - $i * $a; |
80
|
|
|
}; |
81
|
|
|
|
82
|
|
View Code Duplication |
$fdx = function ($i) use ($a, $p, $n) { |
|
|
|
|
83
|
|
|
return $n * $p * pow(1 + $i, -1 * $n - 1) - $a; |
84
|
|
|
}; |
85
|
|
|
|
86
|
|
|
return 12 * $this->newton->run($fx, $fdx, $i); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.