ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
PolynomialBestFit.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
8 {
15  protected $bestFitType = 'polynomial';
16 
22  protected $order = 0;
23 
29  public function getOrder()
30  {
31  return $this->order;
32  }
33 
41  public function getValueOfYForX($xValue)
42  {
43  $retVal = $this->getIntersect();
44  $slope = $this->getSlope();
45  // @phpstan-ignore-next-line
46  foreach ($slope as $key => $value) {
47  if ($value != 0.0) {
48  $retVal += $value * $xValue ** ($key + 1);
49  }
50  }
51 
52  return $retVal;
53  }
54 
62  public function getValueOfXForY($yValue)
63  {
64  return ($yValue - $this->getIntersect()) / $this->getSlope();
65  }
66 
74  public function getEquation($dp = 0)
75  {
76  $slope = $this->getSlope($dp);
77  $intersect = $this->getIntersect($dp);
78 
79  $equation = 'Y = ' . $intersect;
80  // @phpstan-ignore-next-line
81  foreach ($slope as $key => $value) {
82  if ($value != 0.0) {
83  $equation .= ' + ' . $value . ' * X';
84  if ($key > 0) {
85  $equation .= '^' . ($key + 1);
86  }
87  }
88  }
89 
90  return $equation;
91  }
92 
100  public function getSlope($dp = 0)
101  {
102  if ($dp != 0) {
103  $coefficients = [];
104  foreach ($this->slope as $coefficient) {
105  $coefficients[] = round($coefficient, $dp);
106  }
107 
108  // @phpstan-ignore-next-line
109  return $coefficients;
110  }
111 
112  return $this->slope;
113  }
114 
115  public function getCoefficients($dp = 0)
116  {
117  return array_merge([$this->getIntersect($dp)], $this->getSlope($dp));
118  }
119 
127  private function polynomialRegression($order, $yValues, $xValues): void
128  {
129  // calculate sums
130  $x_sum = array_sum($xValues);
131  $y_sum = array_sum($yValues);
132  $xx_sum = $xy_sum = $yy_sum = 0;
133  for ($i = 0; $i < $this->valueCount; ++$i) {
134  $xy_sum += $xValues[$i] * $yValues[$i];
135  $xx_sum += $xValues[$i] * $xValues[$i];
136  $yy_sum += $yValues[$i] * $yValues[$i];
137  }
138  /*
139  * This routine uses logic from the PHP port of polyfit version 0.1
140  * written by Michael Bommarito and Paul Meagher
141  *
142  * The function fits a polynomial function of order $order through
143  * a series of x-y data points using least squares.
144  *
145  */
146  $A = [];
147  $B = [];
148  for ($i = 0; $i < $this->valueCount; ++$i) {
149  for ($j = 0; $j <= $order; ++$j) {
150  $A[$i][$j] = $xValues[$i] ** $j;
151  }
152  }
153  for ($i = 0; $i < $this->valueCount; ++$i) {
154  $B[$i] = [$yValues[$i]];
155  }
156  $matrixA = new Matrix($A);
157  $matrixB = new Matrix($B);
158  $C = $matrixA->solve($matrixB);
159 
160  $coefficients = [];
161  for ($i = 0; $i < $C->getRowDimension(); ++$i) {
162  $r = $C->get($i, 0);
163  if (abs($r) <= 10 ** (-9)) {
164  $r = 0;
165  }
166  $coefficients[] = $r;
167  }
168 
169  $this->intersect = array_shift($coefficients);
170  $this->slope = $coefficients;
171 
172  $this->calculateGoodnessOfFit($x_sum, $y_sum, $xx_sum, $yy_sum, $xy_sum, 0, 0, 0);
173  foreach ($this->xValues as $xKey => $xValue) {
174  $this->yBestFitValues[$xKey] = $this->getValueOfYForX($xValue);
175  }
176  }
177 
185  public function __construct($order, $yValues, $xValues = [])
186  {
187  parent::__construct($yValues, $xValues);
188 
189  if (!$this->error) {
190  if ($order < $this->valueCount) {
191  $this->bestFitType .= '_' . $order;
192  $this->order = $order;
194  if (($this->getGoodnessOfFit() < 0.0) || ($this->getGoodnessOfFit() > 1.0)) {
195  $this->error = true;
196  }
197  } else {
198  $this->error = true;
199  }
200  }
201  }
202 }
calculateGoodnessOfFit($sumX, $sumY, $sumX2, $sumY2, $sumXY, $meanX, $meanY, $const)
Definition: BestFit.php:335
getGoodnessOfFit($dp=0)
Return the goodness of fit for this regression.
Definition: BestFit.php:202
getEquation($dp=0)
Return the Equation of the best-fit line.
__construct($order, $yValues, $xValues=[])
Define the regression and calculate the goodness of fit for a set of X and Y data values...
getIntersect($dp=0)
Return the Value of X where it intersects Y = 0.
Definition: BestFit.php:170
$r
Definition: example_031.php:79
getValueOfYForX($xValue)
Return the Y-Value for a specified value of X.
getValueOfXForY($yValue)
Return the X-Value for a specified value of Y.
getOrder()
Return the order of this polynomial.
$i
Definition: disco.tpl.php:19
Class for the creating "special" Matrices.
Definition: Builder.php:11
$key
Definition: croninfo.php:18
polynomialRegression($order, $yValues, $xValues)
Execute the regression and calculate the goodness of fit for a set of X and Y data values...