| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Shared\Trend; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException; |
| 6 |
|
| 7 |
class Trend |
| 8 |
{ |
| 9 |
const TREND_LINEAR = 'Linear'; |
| 10 |
const TREND_LOGARITHMIC = 'Logarithmic'; |
| 11 |
const TREND_EXPONENTIAL = 'Exponential'; |
| 12 |
const TREND_POWER = 'Power'; |
| 13 |
const TREND_POLYNOMIAL_2 = 'Polynomial_2'; |
| 14 |
const TREND_POLYNOMIAL_3 = 'Polynomial_3'; |
| 15 |
const TREND_POLYNOMIAL_4 = 'Polynomial_4'; |
| 16 |
const TREND_POLYNOMIAL_5 = 'Polynomial_5'; |
| 17 |
const TREND_POLYNOMIAL_6 = 'Polynomial_6'; |
| 18 |
const TREND_BEST_FIT = 'Bestfit'; |
| 19 |
const TREND_BEST_FIT_NO_POLY = 'Bestfit_no_Polynomials'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Names of the best-fit Trend analysis methods. |
| 23 |
*/ |
| 24 |
private const TREND_TYPES = [ |
| 25 |
self::TREND_LINEAR, |
| 26 |
self::TREND_LOGARITHMIC, |
| 27 |
self::TREND_EXPONENTIAL, |
| 28 |
self::TREND_POWER, |
| 29 |
]; |
| 30 |
|
| 31 |
/** |
| 32 |
* Names of the best-fit Trend polynomial orders. |
| 33 |
* |
| 34 |
* @var string[] |
| 35 |
*/ |
| 36 |
private static array $trendTypePolynomialOrders = [ |
| 37 |
self::TREND_POLYNOMIAL_2, |
| 38 |
self::TREND_POLYNOMIAL_3, |
| 39 |
self::TREND_POLYNOMIAL_4, |
| 40 |
self::TREND_POLYNOMIAL_5, |
| 41 |
self::TREND_POLYNOMIAL_6, |
| 42 |
]; |
| 43 |
|
| 44 |
/** |
| 45 |
* Cached results for each method when trying to identify which provides the best fit. |
| 46 |
* |
| 47 |
* @var BestFit[] |
| 48 |
*/ |
| 49 |
private static array $trendCache = []; |
| 50 |
|
| 51 |
/** |
| 52 |
* @param mixed[] $yValues |
| 53 |
* @param mixed[] $xValues |
| 54 |
*/ |
| 55 |
public static function calculate(string $trendType = self::TREND_BEST_FIT, array $yValues = [], array $xValues = [], bool $const = true): BestFit |
| 56 |
{ |
| 57 |
// Calculate number of points in each dataset |
| 58 |
/** @var float[] $xValues */ |
| 59 |
$nY = count($yValues); |
| 60 |
/** @var float[] $xValues */ |
| 61 |
$nX = count($xValues); |
| 62 |
|
| 63 |
// Define X Values if necessary |
| 64 |
if ($nX === 0) { |
| 65 |
$xValues = range(1, $nY); |
| 66 |
} elseif ($nY !== $nX) { |
| 67 |
// Ensure both arrays of points are the same size |
| 68 |
throw new SpreadsheetException('Trend(): Number of elements in coordinate arrays do not match.'); |
| 69 |
} |
| 70 |
|
| 71 |
$key = md5($trendType . $const . serialize($yValues) . serialize($xValues)); |
| 72 |
// Determine which Trend method has been requested |
| 73 |
switch ($trendType) { |
| 74 |
// Instantiate and return the class for the requested Trend method |
| 75 |
case self::TREND_LINEAR: |
| 76 |
case self::TREND_LOGARITHMIC: |
| 77 |
case self::TREND_EXPONENTIAL: |
| 78 |
case self::TREND_POWER: |
| 79 |
if (!isset(self::$trendCache[$key])) { |
| 80 |
/** @var float[] $yValues */ |
| 81 |
$className = '\TablePress\PhpOffice\PhpSpreadsheet\Shared\Trend\\' . $trendType . 'BestFit'; |
| 82 |
/** @var float[] $xValues */ |
| 83 |
self::$trendCache[$key] = new $className($yValues, $xValues, $const); |
| 84 |
} |
| 85 |
|
| 86 |
return self::$trendCache[$key]; |
| 87 |
case self::TREND_POLYNOMIAL_2: |
| 88 |
case self::TREND_POLYNOMIAL_3: |
| 89 |
case self::TREND_POLYNOMIAL_4: |
| 90 |
case self::TREND_POLYNOMIAL_5: |
| 91 |
case self::TREND_POLYNOMIAL_6: |
| 92 |
if (!isset(self::$trendCache[$key])) { |
| 93 |
$order = (int) substr($trendType, -1); |
| 94 |
/** @var float[] $yValues */ |
| 95 |
self::$trendCache[$key] = new PolynomialBestFit($order, $yValues, $xValues); |
| 96 |
} |
| 97 |
|
| 98 |
return self::$trendCache[$key]; |
| 99 |
case self::TREND_BEST_FIT: |
| 100 |
case self::TREND_BEST_FIT_NO_POLY: |
| 101 |
// If the request is to determine the best fit regression, then we test each Trend line in turn |
| 102 |
// Start by generating an instance of each available Trend method |
| 103 |
/** @var float[] $yValues */ |
| 104 |
$bestFit = []; |
| 105 |
/** @var float[] $xValues */ |
| 106 |
$bestFitValue = []; |
| 107 |
foreach (self::TREND_TYPES as $trendMethod) { |
| 108 |
$className = '\TablePress\PhpOffice\PhpSpreadsheet\Shared\Trend\\' . $trendMethod . 'BestFit'; |
| 109 |
$bestFit[$trendMethod] = new $className($yValues, $xValues, $const); |
| 110 |
$bestFitValue[$trendMethod] = $bestFit[$trendMethod]->getGoodnessOfFit(); |
| 111 |
} |
| 112 |
if ($trendType !== self::TREND_BEST_FIT_NO_POLY) { |
| 113 |
foreach (self::$trendTypePolynomialOrders as $trendMethod) { |
| 114 |
$order = (int) substr($trendMethod, -1); |
| 115 |
$bestFit[$trendMethod] = new PolynomialBestFit($order, $yValues, $xValues); |
| 116 |
if ($bestFit[$trendMethod]->getError()) { |
| 117 |
unset($bestFit[$trendMethod]); |
| 118 |
} else { |
| 119 |
$bestFitValue[$trendMethod] = $bestFit[$trendMethod]->getGoodnessOfFit(); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
// Determine which of our Trend lines is the best fit, and then we return the instance of that Trend class |
| 124 |
arsort($bestFitValue); |
| 125 |
$bestFitType = key($bestFitValue); |
| 126 |
|
| 127 |
return $bestFit[$bestFitType]; |
| 128 |
default: |
| 129 |
throw new SpreadsheetException("Unknown trend type $trendType"); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|