You are here

private function PHPExcel_Polynomial_Best_Fit::_polynomial_regression in Loft Data Grids 7.2

Same name and namespace in other branches
  1. 6.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Shared/trend/polynomialBestFitClass.php \PHPExcel_Polynomial_Best_Fit::_polynomial_regression()

* Execute the regression and calculate the goodness of fit for a set of X and Y data values * *

Parameters

int $order Order of Polynomial for this regression: * @param float[] $yValues The set of Y-values for this regression * @param float[] $xValues The set of X-values for this regression * @param boolean $const

1 call to PHPExcel_Polynomial_Best_Fit::_polynomial_regression()
PHPExcel_Polynomial_Best_Fit::__construct in vendor/phpoffice/phpexcel/Classes/PHPExcel/Shared/trend/polynomialBestFitClass.php
* Define the regression and calculate the goodness of fit for a set of X and Y data values * *

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Shared/trend/polynomialBestFitClass.php, line 152

Class

PHPExcel_Polynomial_Best_Fit
PHPExcel_Polynomial_Best_Fit

Code

private function _polynomial_regression($order, $yValues, $xValues, $const) {

  // calculate sums
  $x_sum = array_sum($xValues);
  $y_sum = array_sum($yValues);
  $xx_sum = $xy_sum = 0;
  for ($i = 0; $i < $this->_valueCount; ++$i) {
    $xy_sum += $xValues[$i] * $yValues[$i];
    $xx_sum += $xValues[$i] * $xValues[$i];
    $yy_sum += $yValues[$i] * $yValues[$i];
  }

  /*
   *	This routine uses logic from the PHP port of polyfit version 0.1
   *	written by Michael Bommarito and Paul Meagher
   *
   *	The function fits a polynomial function of order $order through
   *	a series of x-y data points using least squares.
   *
   */
  for ($i = 0; $i < $this->_valueCount; ++$i) {
    for ($j = 0; $j <= $order; ++$j) {
      $A[$i][$j] = pow($xValues[$i], $j);
    }
  }
  for ($i = 0; $i < $this->_valueCount; ++$i) {
    $B[$i] = array(
      $yValues[$i],
    );
  }
  $matrixA = new Matrix($A);
  $matrixB = new Matrix($B);
  $C = $matrixA
    ->solve($matrixB);
  $coefficients = array();
  for ($i = 0; $i < $C->m; ++$i) {
    $r = $C
      ->get($i, 0);
    if (abs($r) <= pow(10, -9)) {
      $r = 0;
    }
    $coefficients[] = $r;
  }
  $this->_intersect = array_shift($coefficients);
  $this->_slope = $coefficients;
  $this
    ->_calculateGoodnessOfFit($x_sum, $y_sum, $xx_sum, $yy_sum, $xy_sum);
  foreach ($this->_xValues as $xKey => $xValue) {
    $this->_yBestFitValues[$xKey] = $this
      ->getValueOfYForX($xValue);
  }
}