ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
polynomialBestFitClass.php
Go to the documentation of this file.
1 <?php
29 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/trend/bestFitClass.php';
30 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/JAMA/Matrix.php';
31 
32 
41 {
48  protected $_bestFitType = 'polynomial';
49 
56  protected $_order = 0;
57 
58 
64  public function getOrder() {
65  return $this->_order;
66  } // function getOrder()
67 
68 
75  public function getValueOfYForX($xValue) {
76  $retVal = $this->getIntersect();
77  $slope = $this->getSlope();
78  foreach($slope as $key => $value) {
79  if ($value != 0.0) {
80  $retVal += $value * pow($xValue, $key + 1);
81  }
82  }
83  return $retVal;
84  } // function getValueOfYForX()
85 
86 
93  public function getValueOfXForY($yValue) {
94  return ($yValue - $this->getIntersect()) / $this->getSlope();
95  } // function getValueOfXForY()
96 
97 
104  public function getEquation($dp=0) {
105  $slope = $this->getSlope($dp);
106  $intersect = $this->getIntersect($dp);
107 
108  $equation = 'Y = '.$intersect;
109  foreach($slope as $key => $value) {
110  if ($value != 0.0) {
111  $equation .= ' + '.$value.' * X';
112  if ($key > 0) {
113  $equation .= '^'.($key + 1);
114  }
115  }
116  }
117  return $equation;
118  } // function getEquation()
119 
120 
127  public function getSlope($dp=0) {
128  if ($dp != 0) {
129  $coefficients = array();
130  foreach($this->_slope as $coefficient) {
131  $coefficients[] = round($coefficient,$dp);
132  }
133  return $coefficients;
134  }
135  return $this->_slope;
136  } // function getSlope()
137 
138 
139  public function getCoefficients($dp=0) {
140  return array_merge(array($this->getIntersect($dp)),$this->getSlope($dp));
141  } // function getCoefficients()
142 
143 
152  private function _polynomial_regression($order, $yValues, $xValues, $const) {
153  // calculate sums
154  $x_sum = array_sum($xValues);
155  $y_sum = array_sum($yValues);
156  $xx_sum = $xy_sum = 0;
157  for($i = 0; $i < $this->_valueCount; ++$i) {
158  $xy_sum += $xValues[$i] * $yValues[$i];
159  $xx_sum += $xValues[$i] * $xValues[$i];
160  $yy_sum += $yValues[$i] * $yValues[$i];
161  }
162  /*
163  * This routine uses logic from the PHP port of polyfit version 0.1
164  * written by Michael Bommarito and Paul Meagher
165  *
166  * The function fits a polynomial function of order $order through
167  * a series of x-y data points using least squares.
168  *
169  */
170  for ($i = 0; $i < $this->_valueCount; ++$i) {
171  for ($j = 0; $j <= $order; ++$j) {
172  $A[$i][$j] = pow($xValues[$i], $j);
173  }
174  }
175  for ($i=0; $i < $this->_valueCount; ++$i) {
176  $B[$i] = array($yValues[$i]);
177  }
178  $matrixA = new Matrix($A);
179  $matrixB = new Matrix($B);
180  $C = $matrixA->solve($matrixB);
181 
182  $coefficients = array();
183  for($i = 0; $i < $C->m; ++$i) {
184  $r = $C->get($i, 0);
185  if (abs($r) <= pow(10, -9)) {
186  $r = 0;
187  }
188  $coefficients[] = $r;
189  }
190 
191  $this->_intersect = array_shift($coefficients);
192  $this->_slope = $coefficients;
193 
194  $this->_calculateGoodnessOfFit($x_sum,$y_sum,$xx_sum,$yy_sum,$xy_sum);
195  foreach($this->_xValues as $xKey => $xValue) {
196  $this->_yBestFitValues[$xKey] = $this->getValueOfYForX($xValue);
197  }
198  } // function _polynomial_regression()
199 
200 
209  function __construct($order, $yValues, $xValues=array(), $const=True) {
210  if (parent::__construct($yValues, $xValues) !== False) {
211  if ($order < $this->_valueCount) {
212  $this->_bestFitType .= '_'.$order;
213  $this->_order = $order;
214  $this->_polynomial_regression($order, $yValues, $xValues, $const);
215  if (($this->getGoodnessOfFit() < 0.0) || ($this->getGoodnessOfFit() > 1.0)) {
216  $this->_error = True;
217  }
218  } else {
219  $this->_error = True;
220  }
221  }
222  } // function __construct()
223 
224 } // class polynomialBestFit
getValueOfXForY($yValue)
Return the X-Value for a specified value of Y.
getOrder()
Return the order of this polynomial.
getIntersect($dp=0)
Return the Value of X where it intersects Y = 0.
getValueOfYForX($xValue)
Return the Y-Value for a specified value of X.
$r
Definition: example_031.php:79
_calculateGoodnessOfFit($sumX, $sumY, $sumX2, $sumY2, $sumXY, $meanX, $meanY, $const)
getGoodnessOfFit($dp=0)
Return the goodness of fit for this regression.
getEquation($dp=0)
Return the Equation of the best-fit line.
_polynomial_regression($order, $yValues, $xValues, $const)
Execute the regression and calculate the goodness of fit for a set of X and Y data values...
Create styles array
The data for the language used.
getSlope($dp=0)
Return the Slope of the line.
$i
Definition: disco.tpl.php:19
__construct($order, $yValues, $xValues=array(), $const=True)
Define the regression and calculate the goodness of fit for a set of X and Y data values...
$key
Definition: croninfo.php:18