1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Shared\Trend; |
4
|
|
|
|
5
|
|
|
class PowerBestFit extends BestFit |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Algorithm type to use for best-fit |
9
|
|
|
* (Name of this Trend class). |
10
|
|
|
*/ |
11
|
|
|
protected string $bestFitType = 'power'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Return the Y-Value for a specified value of X. |
15
|
|
|
* |
16
|
|
|
* @param float $xValue X-Value |
17
|
|
|
* |
18
|
|
|
* @return float Y-Value |
19
|
|
|
*/ |
20
|
1 |
|
public function getValueOfYForX(float $xValue): float |
21
|
|
|
{ |
22
|
1 |
|
return $this->getIntersect() * ($xValue - $this->xOffset) ** $this->getSlope(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Return the X-Value for a specified value of Y. |
27
|
|
|
* |
28
|
|
|
* @param float $yValue Y-Value |
29
|
|
|
* |
30
|
|
|
* @return float X-Value |
31
|
|
|
*/ |
32
|
|
|
public function getValueOfXForY(float $yValue): float |
33
|
|
|
{ |
34
|
|
|
return (($yValue + $this->yOffset) / $this->getIntersect()) ** (1 / $this->getSlope()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Return the Equation of the best-fit line. |
39
|
|
|
* |
40
|
|
|
* @param int $dp Number of places of decimal precision to display |
41
|
|
|
*/ |
42
|
|
|
public function getEquation(int $dp = 0): string |
43
|
|
|
{ |
44
|
|
|
$slope = $this->getSlope($dp); |
45
|
|
|
$intersect = $this->getIntersect($dp); |
46
|
|
|
|
47
|
|
|
return 'Y = ' . $intersect . ' * X^' . $slope; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Return the Value of X where it intersects Y = 0. |
52
|
|
|
* |
53
|
|
|
* @param int $dp Number of places of decimal precision to display |
54
|
|
|
*/ |
55
|
1 |
|
public function getIntersect(int $dp = 0): float |
56
|
|
|
{ |
57
|
1 |
|
if ($dp != 0) { |
58
|
|
|
return round(exp($this->intersect), $dp); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
return exp($this->intersect); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Execute the regression and calculate the goodness of fit for a set of X and Y data values. |
66
|
|
|
* |
67
|
|
|
* @param float[] $yValues The set of Y-values for this regression |
68
|
|
|
* @param float[] $xValues The set of X-values for this regression |
69
|
|
|
*/ |
70
|
1 |
|
private function powerRegression(array $yValues, array $xValues, bool $const): void |
71
|
|
|
{ |
72
|
1 |
|
$adjustedYValues = array_map( |
73
|
1 |
|
fn ($value): float => ($value < 0.0) ? 0 - log(abs($value)) : log($value), |
74
|
1 |
|
$yValues |
75
|
1 |
|
); |
76
|
1 |
|
$adjustedXValues = array_map( |
77
|
1 |
|
fn ($value): float => ($value < 0.0) ? 0 - log(abs($value)) : log($value), |
78
|
1 |
|
$xValues |
79
|
1 |
|
); |
80
|
|
|
|
81
|
1 |
|
$this->leastSquareFit($adjustedYValues, $adjustedXValues, $const); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Define the regression and calculate the goodness of fit for a set of X and Y data values. |
86
|
|
|
* |
87
|
|
|
* @param float[] $yValues The set of Y-values for this regression |
88
|
|
|
* @param float[] $xValues The set of X-values for this regression |
89
|
|
|
*/ |
90
|
1 |
|
public function __construct(array $yValues, array $xValues = [], bool $const = true) |
91
|
|
|
{ |
92
|
1 |
|
parent::__construct($yValues, $xValues); |
93
|
|
|
|
94
|
1 |
|
if (!$this->error) { |
95
|
1 |
|
$this->powerRegression($yValues, $xValues, (bool) $const); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|