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