|
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
|
|
|
* |
|
17
|
|
|
* @param NewtonRaphson $newton |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct(NewtonRaphson $newton = null) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->newton = $newton ?? new NewtonRaphson(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Get the interest when you know all the payments and their dates. Use this function when you have |
|
27
|
|
|
* administration fees at the first payment and/or when payments are irregular. |
|
28
|
|
|
* |
|
29
|
|
|
* @param int $principal |
|
30
|
|
|
* @param string $startDate in format 'YYYY-mm-dd' |
|
31
|
|
|
* @param array $payments array with payment dates and values ['YYYY-mm-dd'=>int] |
|
32
|
|
|
* @param float $guess A guess what the interest may be. Between zero and one. Example 0.045 |
|
33
|
|
|
* |
|
34
|
|
|
* @return float |
|
35
|
|
|
*/ |
|
36
|
|
|
public function withSpecifiedPayments(int $principal, string $startDate, array $payments, float $guess): float |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
return 0.045; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get the effective interest when the monthly payments are exactly the same. |
|
43
|
|
|
* |
|
44
|
|
|
* @param int $a The total loan amount (Principal) |
|
45
|
|
|
* @param int $p The monthly payment |
|
46
|
|
|
* @param int $n The number of months |
|
47
|
|
|
* @param float $i A guess of what the interest might be. Interest as a number between zero and one. Example 0.045 |
|
48
|
|
|
* |
|
49
|
|
|
* @return float |
|
50
|
|
|
*/ |
|
51
|
|
|
public function withEqualPayments(int $a, int $p, int $n, float $i): float |
|
52
|
|
|
{ |
|
53
|
|
View Code Duplication |
$fx = function ($i) use ($a, $p, $n) { |
|
|
|
|
|
|
54
|
|
|
return $p - $p * pow(1 + $i, -1 * $n) - $i * $a; |
|
55
|
|
|
}; |
|
56
|
|
|
|
|
57
|
|
View Code Duplication |
$fdx = function ($i) use ($a, $p, $n) { |
|
|
|
|
|
|
58
|
|
|
return $n * $p * pow(1 + $i, -1 * $n - 1) - $a; |
|
59
|
|
|
}; |
|
60
|
|
|
|
|
61
|
|
|
return $this->newton->run($fx, $fdx, $i); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.