ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 {
42  protected $_bestFitType = 'polynomial';
43 
44  protected $_order = 0;
45 
46 
47  public function getOrder() {
48  return $this->_order;
49  } // function getOrder()
50 
51 
52  public function getValueOfYForX($xValue) {
53  $retVal = $this->getIntersect();
54  $slope = $this->getSlope();
55  foreach($slope as $key => $value) {
56  if ($value != 0.0) {
57  $retVal += $value * pow($xValue, $key + 1);
58  }
59  }
60  return $retVal;
61  } // function getValueOfYForX()
62 
63 
64  public function getValueOfXForY($yValue) {
65  return ($yValue - $this->getIntersect()) / $this->getSlope();
66  } // function getValueOfXForY()
67 
68 
69  public function getEquation($dp=0) {
70  $slope = $this->getSlope($dp);
71  $intersect = $this->getIntersect($dp);
72 
73  $equation = 'Y = '.$intersect;
74  foreach($slope as $key => $value) {
75  if ($value != 0.0) {
76  $equation .= ' + '.$value.' * X';
77  if ($key > 0) {
78  $equation .= '^'.($key + 1);
79  }
80  }
81  }
82  return $equation;
83  } // function getEquation()
84 
85 
86  public function getSlope($dp=0) {
87  if ($dp != 0) {
88  $coefficients = array();
89  foreach($this->_slope as $coefficient) {
90  $coefficients[] = round($coefficient,$dp);
91  }
92  return $coefficients;
93  }
94  return $this->_slope;
95  } // function getSlope()
96 
97 
98  public function getCoefficients($dp=0) {
99  return array_merge(array($this->getIntersect($dp)),$this->getSlope($dp));
100  } // function getCoefficients()
101 
102 
103  private function _polynomial_regression($order, $yValues, $xValues, $const) {
104  // calculate sums
105  $x_sum = array_sum($xValues);
106  $y_sum = array_sum($yValues);
107  $xx_sum = $xy_sum = 0;
108  for($i = 0; $i < $this->_valueCount; ++$i) {
109  $xy_sum += $xValues[$i] * $yValues[$i];
110  $xx_sum += $xValues[$i] * $xValues[$i];
111  $yy_sum += $yValues[$i] * $yValues[$i];
112  }
113  /*
114  * This routine uses logic from the PHP port of polyfit version 0.1
115  * written by Michael Bommarito and Paul Meagher
116  *
117  * The function fits a polynomial function of order $order through
118  * a series of x-y data points using least squares.
119  *
120  */
121  for ($i = 0; $i < $this->_valueCount; ++$i) {
122  for ($j = 0; $j <= $order; ++$j) {
123  $A[$i][$j] = pow($xValues[$i], $j);
124  }
125  }
126  for ($i=0; $i < $this->_valueCount; ++$i) {
127  $B[$i] = array($yValues[$i]);
128  }
129  $matrixA = new Matrix($A);
130  $matrixB = new Matrix($B);
131  $C = $matrixA->solve($matrixB);
132 
133  $coefficients = array();
134  for($i = 0; $i < $C->m; ++$i) {
135  $r = $C->get($i, 0);
136  if (abs($r) <= pow(10, -9)) {
137  $r = 0;
138  }
139  $coefficients[] = $r;
140  }
141 
142  $this->_intersect = array_shift($coefficients);
143  $this->_slope = $coefficients;
144 
145  $this->_calculateGoodnessOfFit($x_sum,$y_sum,$xx_sum,$yy_sum,$xy_sum);
146  foreach($this->_xValues as $xKey => $xValue) {
147  $this->_yBestFitValues[$xKey] = $this->getValueOfYForX($xValue);
148  }
149  } // function _polynomial_regression()
150 
151 
152  function __construct($order, $yValues, $xValues=array(), $const=True) {
153  if (parent::__construct($yValues, $xValues) !== False) {
154  if ($order < $this->_valueCount) {
155  $this->_bestFitType .= '_'.$order;
156  $this->_order = $order;
157  $this->_polynomial_regression($order, $yValues, $xValues, $const);
158  if (($this->getGoodnessOfFit() < 0.0) || ($this->getGoodnessOfFit() > 1.0)) {
159  $this->_error = True;
160  }
161  } else {
162  $this->_error = True;
163  }
164  }
165  } // function __construct()
166 
167 } // class polynomialBestFit