| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Shared\Trend;
|
|
|
4 |
|
|
|
5 |
abstract class BestFit
|
|
|
6 |
{
|
|
|
7 |
/**
|
|
|
8 |
* Indicator flag for a calculation error.
|
|
|
9 |
*/
|
|
|
10 |
protected bool $error = false;
|
|
|
11 |
|
|
|
12 |
/**
|
|
|
13 |
* Algorithm type to use for best-fit.
|
|
|
14 |
*/
|
|
|
15 |
protected string $bestFitType = 'undetermined';
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Number of entries in the sets of x- and y-value arrays.
|
|
|
19 |
*/
|
|
|
20 |
protected int $valueCount;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* X-value dataseries of values.
|
|
|
24 |
*
|
|
|
25 |
* @var float[]
|
|
|
26 |
*/
|
|
|
27 |
protected array $xValues = [];
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Y-value dataseries of values.
|
|
|
31 |
*
|
|
|
32 |
* @var float[]
|
|
|
33 |
*/
|
|
|
34 |
protected array $yValues = [];
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Flag indicating whether values should be adjusted to Y=0.
|
|
|
38 |
*/
|
|
|
39 |
protected bool $adjustToZero = false;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Y-value series of best-fit values.
|
|
|
43 |
*
|
|
|
44 |
* @var float[]
|
|
|
45 |
*/
|
|
|
46 |
protected array $yBestFitValues = [];
|
|
|
47 |
|
|
|
48 |
protected float $goodnessOfFit = 1;
|
|
|
49 |
|
|
|
50 |
protected float $stdevOfResiduals = 0;
|
|
|
51 |
|
|
|
52 |
protected float $covariance = 0;
|
|
|
53 |
|
|
|
54 |
protected float $correlation = 0;
|
|
|
55 |
|
|
|
56 |
protected float $SSRegression = 0;
|
|
|
57 |
|
|
|
58 |
protected float $SSResiduals = 0;
|
|
|
59 |
|
|
|
60 |
protected float $DFResiduals = 0;
|
|
|
61 |
|
|
|
62 |
protected float $f = 0;
|
|
|
63 |
|
|
|
64 |
protected float $slope = 0;
|
|
|
65 |
|
|
|
66 |
protected float $slopeSE = 0;
|
|
|
67 |
|
|
|
68 |
protected float $intersect = 0;
|
|
|
69 |
|
|
|
70 |
protected float $intersectSE = 0;
|
|
|
71 |
|
|
|
72 |
protected float $xOffset = 0;
|
|
|
73 |
|
|
|
74 |
protected float $yOffset = 0;
|
|
|
75 |
|
|
|
76 |
public function getError(): bool
|
|
|
77 |
{
|
|
|
78 |
return $this->error;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
public function getBestFitType(): string
|
|
|
82 |
{
|
|
|
83 |
return $this->bestFitType;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* Return the Y-Value for a specified value of X.
|
|
|
88 |
*
|
|
|
89 |
* @param float $xValue X-Value
|
|
|
90 |
*
|
|
|
91 |
* @return float Y-Value
|
|
|
92 |
*/
|
|
|
93 |
abstract public function getValueOfYForX(float $xValue): float;
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Return the X-Value for a specified value of Y.
|
|
|
97 |
*
|
|
|
98 |
* @param float $yValue Y-Value
|
|
|
99 |
*
|
|
|
100 |
* @return float X-Value
|
|
|
101 |
*/
|
|
|
102 |
abstract public function getValueOfXForY(float $yValue): float;
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Return the original set of X-Values.
|
|
|
106 |
*
|
|
|
107 |
* @return float[] X-Values
|
|
|
108 |
*/
|
|
|
109 |
public function getXValues(): array
|
|
|
110 |
{
|
|
|
111 |
return $this->xValues;
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
/**
|
|
|
115 |
* Return the Equation of the best-fit line.
|
|
|
116 |
*
|
|
|
117 |
* @param int $dp Number of places of decimal precision to display
|
|
|
118 |
*/
|
|
|
119 |
abstract public function getEquation(int $dp = 0): string;
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Return the Slope of the line.
|
|
|
123 |
*
|
|
|
124 |
* @param int $dp Number of places of decimal precision to display
|
|
|
125 |
*/
|
|
|
126 |
public function getSlope(int $dp = 0): float
|
|
|
127 |
{
|
|
|
128 |
if ($dp != 0) {
|
|
|
129 |
return round($this->slope, $dp);
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
return $this->slope;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
/**
|
|
|
136 |
* Return the standard error of the Slope.
|
|
|
137 |
*
|
|
|
138 |
* @param int $dp Number of places of decimal precision to display
|
|
|
139 |
*/
|
|
|
140 |
public function getSlopeSE(int $dp = 0): float
|
|
|
141 |
{
|
|
|
142 |
if ($dp != 0) {
|
|
|
143 |
return round($this->slopeSE, $dp);
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
return $this->slopeSE;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* Return the Value of X where it intersects Y = 0.
|
|
|
151 |
*
|
|
|
152 |
* @param int $dp Number of places of decimal precision to display
|
|
|
153 |
*/
|
|
|
154 |
public function getIntersect(int $dp = 0): float
|
|
|
155 |
{
|
|
|
156 |
if ($dp != 0) {
|
|
|
157 |
return round($this->intersect, $dp);
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
return $this->intersect;
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
/**
|
|
|
164 |
* Return the standard error of the Intersect.
|
|
|
165 |
*
|
|
|
166 |
* @param int $dp Number of places of decimal precision to display
|
|
|
167 |
*/
|
|
|
168 |
public function getIntersectSE(int $dp = 0): float
|
|
|
169 |
{
|
|
|
170 |
if ($dp != 0) {
|
|
|
171 |
return round($this->intersectSE, $dp);
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
return $this->intersectSE;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* Return the goodness of fit for this regression.
|
|
|
179 |
*
|
|
|
180 |
* @param int $dp Number of places of decimal precision to return
|
|
|
181 |
*/
|
|
|
182 |
public function getGoodnessOfFit(int $dp = 0): float
|
|
|
183 |
{
|
|
|
184 |
if ($dp != 0) {
|
|
|
185 |
return round($this->goodnessOfFit, $dp);
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
return $this->goodnessOfFit;
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
/**
|
|
|
192 |
* Return the goodness of fit for this regression.
|
|
|
193 |
*
|
|
|
194 |
* @param int $dp Number of places of decimal precision to return
|
|
|
195 |
*/
|
|
|
196 |
public function getGoodnessOfFitPercent(int $dp = 0): float
|
|
|
197 |
{
|
|
|
198 |
if ($dp != 0) {
|
|
|
199 |
return round($this->goodnessOfFit * 100, $dp);
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
return $this->goodnessOfFit * 100;
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
/**
|
|
|
206 |
* Return the standard deviation of the residuals for this regression.
|
|
|
207 |
*
|
|
|
208 |
* @param int $dp Number of places of decimal precision to return
|
|
|
209 |
*/
|
|
|
210 |
public function getStdevOfResiduals(int $dp = 0): float
|
|
|
211 |
{
|
|
|
212 |
if ($dp != 0) {
|
|
|
213 |
return round($this->stdevOfResiduals, $dp);
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
return $this->stdevOfResiduals;
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
/**
|
|
|
220 |
* @param int $dp Number of places of decimal precision to return
|
|
|
221 |
*/
|
|
|
222 |
public function getSSRegression(int $dp = 0): float
|
|
|
223 |
{
|
|
|
224 |
if ($dp != 0) {
|
|
|
225 |
return round($this->SSRegression, $dp);
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
return $this->SSRegression;
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
/**
|
|
|
232 |
* @param int $dp Number of places of decimal precision to return
|
|
|
233 |
*/
|
|
|
234 |
public function getSSResiduals(int $dp = 0): float
|
|
|
235 |
{
|
|
|
236 |
if ($dp != 0) {
|
|
|
237 |
return round($this->SSResiduals, $dp);
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
return $this->SSResiduals;
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
/**
|
|
|
244 |
* @param int $dp Number of places of decimal precision to return
|
|
|
245 |
*/
|
|
|
246 |
public function getDFResiduals(int $dp = 0): float
|
|
|
247 |
{
|
|
|
248 |
if ($dp != 0) {
|
|
|
249 |
return round($this->DFResiduals, $dp);
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
return $this->DFResiduals;
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
/**
|
|
|
256 |
* @param int $dp Number of places of decimal precision to return
|
|
|
257 |
*/
|
|
|
258 |
public function getF(int $dp = 0): float
|
|
|
259 |
{
|
|
|
260 |
if ($dp != 0) {
|
|
|
261 |
return round($this->f, $dp);
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
return $this->f;
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
/**
|
|
|
268 |
* @param int $dp Number of places of decimal precision to return
|
|
|
269 |
*/
|
|
|
270 |
public function getCovariance(int $dp = 0): float
|
|
|
271 |
{
|
|
|
272 |
if ($dp != 0) {
|
|
|
273 |
return round($this->covariance, $dp);
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
return $this->covariance;
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
/**
|
|
|
280 |
* @param int $dp Number of places of decimal precision to return
|
|
|
281 |
*/
|
|
|
282 |
public function getCorrelation(int $dp = 0): float
|
|
|
283 |
{
|
|
|
284 |
if ($dp != 0) {
|
|
|
285 |
return round($this->correlation, $dp);
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
return $this->correlation;
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
/**
|
|
|
292 |
* @return float[]
|
|
|
293 |
*/
|
|
|
294 |
public function getYBestFitValues(): array
|
|
|
295 |
{
|
|
|
296 |
return $this->yBestFitValues;
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
protected function calculateGoodnessOfFit(float $sumX, float $sumY, float $sumX2, float $sumY2, float $sumXY, float $meanX, float $meanY, bool|int $const): void
|
|
|
300 |
{
|
|
|
301 |
$SSres = $SScov = $SStot = $SSsex = 0.0;
|
|
|
302 |
foreach ($this->xValues as $xKey => $xValue) {
|
|
|
303 |
$bestFitY = $this->yBestFitValues[$xKey] = $this->getValueOfYForX($xValue);
|
|
|
304 |
|
|
|
305 |
$SSres += ($this->yValues[$xKey] - $bestFitY) * ($this->yValues[$xKey] - $bestFitY);
|
|
|
306 |
if ($const === true) {
|
|
|
307 |
$SStot += ($this->yValues[$xKey] - $meanY) * ($this->yValues[$xKey] - $meanY);
|
|
|
308 |
} else {
|
|
|
309 |
$SStot += $this->yValues[$xKey] * $this->yValues[$xKey];
|
|
|
310 |
}
|
|
|
311 |
$SScov += ($this->xValues[$xKey] - $meanX) * ($this->yValues[$xKey] - $meanY);
|
|
|
312 |
if ($const === true) {
|
|
|
313 |
$SSsex += ($this->xValues[$xKey] - $meanX) * ($this->xValues[$xKey] - $meanX);
|
|
|
314 |
} else {
|
|
|
315 |
$SSsex += $this->xValues[$xKey] * $this->xValues[$xKey];
|
|
|
316 |
}
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
$this->SSResiduals = $SSres;
|
|
|
320 |
$this->DFResiduals = $this->valueCount - 1 - ($const === true ? 1 : 0);
|
|
|
321 |
|
|
|
322 |
if ($this->DFResiduals == 0.0) {
|
|
|
323 |
$this->stdevOfResiduals = 0.0;
|
|
|
324 |
} else {
|
|
|
325 |
$this->stdevOfResiduals = sqrt($SSres / $this->DFResiduals);
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
if ($SStot == 0.0 || $SSres == $SStot) {
|
|
|
329 |
$this->goodnessOfFit = 1;
|
|
|
330 |
} else {
|
|
|
331 |
$this->goodnessOfFit = 1 - ($SSres / $SStot);
|
|
|
332 |
}
|
|
|
333 |
|
|
|
334 |
$this->SSRegression = $this->goodnessOfFit * $SStot;
|
|
|
335 |
$this->covariance = $SScov / $this->valueCount;
|
|
|
336 |
$this->correlation = ($this->valueCount * $sumXY - $sumX * $sumY) / sqrt(($this->valueCount * $sumX2 - $sumX ** 2) * ($this->valueCount * $sumY2 - $sumY ** 2));
|
|
|
337 |
$this->slopeSE = $this->stdevOfResiduals / sqrt($SSsex);
|
|
|
338 |
$this->intersectSE = $this->stdevOfResiduals * sqrt(1 / ($this->valueCount - ($sumX * $sumX) / $sumX2));
|
|
|
339 |
if ($this->SSResiduals != 0.0) {
|
|
|
340 |
if ($this->DFResiduals == 0.0) {
|
|
|
341 |
$this->f = 0.0;
|
|
|
342 |
} else {
|
|
|
343 |
$this->f = $this->SSRegression / ($this->SSResiduals / $this->DFResiduals);
|
|
|
344 |
}
|
|
|
345 |
} else {
|
|
|
346 |
if ($this->DFResiduals == 0.0) {
|
|
|
347 |
$this->f = 0.0;
|
|
|
348 |
} else {
|
|
|
349 |
$this->f = $this->SSRegression / $this->DFResiduals;
|
|
|
350 |
}
|
|
|
351 |
}
|
|
|
352 |
}
|
|
|
353 |
|
|
|
354 |
/** @return float|int */
|
|
|
355 |
private function sumSquares(array $values)
|
|
|
356 |
{
|
|
|
357 |
return array_sum(
|
|
|
358 |
array_map(
|
|
|
359 |
fn ($value): float|int => $value ** 2,
|
|
|
360 |
$values
|
|
|
361 |
)
|
|
|
362 |
);
|
|
|
363 |
}
|
|
|
364 |
|
|
|
365 |
/**
|
|
|
366 |
* @param float[] $yValues
|
|
|
367 |
* @param float[] $xValues
|
|
|
368 |
*/
|
|
|
369 |
protected function leastSquareFit(array $yValues, array $xValues, bool $const): void
|
|
|
370 |
{
|
|
|
371 |
// calculate sums
|
|
|
372 |
$sumValuesX = array_sum($xValues);
|
|
|
373 |
$sumValuesY = array_sum($yValues);
|
|
|
374 |
$meanValueX = $sumValuesX / $this->valueCount;
|
|
|
375 |
$meanValueY = $sumValuesY / $this->valueCount;
|
|
|
376 |
$sumSquaresX = $this->sumSquares($xValues);
|
|
|
377 |
$sumSquaresY = $this->sumSquares($yValues);
|
|
|
378 |
$mBase = $mDivisor = 0.0;
|
|
|
379 |
$xy_sum = 0.0;
|
|
|
380 |
for ($i = 0; $i < $this->valueCount; ++$i) {
|
|
|
381 |
$xy_sum += $xValues[$i] * $yValues[$i];
|
|
|
382 |
|
|
|
383 |
if ($const === true) {
|
|
|
384 |
$mBase += ($xValues[$i] - $meanValueX) * ($yValues[$i] - $meanValueY);
|
|
|
385 |
$mDivisor += ($xValues[$i] - $meanValueX) * ($xValues[$i] - $meanValueX);
|
|
|
386 |
} else {
|
|
|
387 |
$mBase += $xValues[$i] * $yValues[$i];
|
|
|
388 |
$mDivisor += $xValues[$i] * $xValues[$i];
|
|
|
389 |
}
|
|
|
390 |
}
|
|
|
391 |
|
|
|
392 |
// calculate slope
|
|
|
393 |
$this->slope = $mBase / $mDivisor;
|
|
|
394 |
|
|
|
395 |
// calculate intersect
|
|
|
396 |
$this->intersect = ($const === true) ? $meanValueY - ($this->slope * $meanValueX) : 0.0;
|
|
|
397 |
|
|
|
398 |
$this->calculateGoodnessOfFit($sumValuesX, $sumValuesY, $sumSquaresX, $sumSquaresY, $xy_sum, $meanValueX, $meanValueY, $const);
|
|
|
399 |
}
|
|
|
400 |
|
|
|
401 |
/**
|
|
|
402 |
* Define the regression.
|
|
|
403 |
*
|
|
|
404 |
* @param float[] $yValues The set of Y-values for this regression
|
|
|
405 |
* @param float[] $xValues The set of X-values for this regression
|
|
|
406 |
*/
|
|
|
407 |
public function __construct(array $yValues, array $xValues = [])
|
|
|
408 |
{
|
|
|
409 |
// Calculate number of points
|
|
|
410 |
$yValueCount = count($yValues);
|
|
|
411 |
$xValueCount = count($xValues);
|
|
|
412 |
|
|
|
413 |
// Define X Values if necessary
|
|
|
414 |
if ($xValueCount === 0) {
|
|
|
415 |
$xValues = range(1, $yValueCount);
|
|
|
416 |
} elseif ($yValueCount !== $xValueCount) {
|
|
|
417 |
// Ensure both arrays of points are the same size
|
|
|
418 |
$this->error = true;
|
|
|
419 |
}
|
|
|
420 |
|
|
|
421 |
$this->valueCount = $yValueCount;
|
|
|
422 |
$this->xValues = $xValues;
|
|
|
423 |
$this->yValues = $yValues;
|
|
|
424 |
}
|
|
|
425 |
}
|