PHPExcel_Shared_Best_Fit
[ class tree: PHPExcel_Shared_Best_Fit ] [ index: PHPExcel_Shared_Best_Fit ] [ all elements ]

Source for file bestFitClass.php

Documentation is available at bestFitClass.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2011 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20.  *
  21.  * @category   PHPExcel
  22.  * @package    PHPExcel_Shared_Best_Fit
  23.  * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.6, 2011-02-27
  26.  */
  27.  
  28.  
  29. /**
  30.  * PHPExcel_Best_Fit
  31.  *
  32.  * @category   PHPExcel
  33.  * @package    PHPExcel_Shared_Best_Fit
  34.  * @copyright  Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  35.  */
  36. {
  37.     protected $_error                = False;
  38.  
  39.     protected $_bestFitType            = 'undetermined';
  40.  
  41.     protected $_valueCount            = 0;
  42.  
  43.     protected $_xValues                = array();
  44.  
  45.     protected $_yValues                = array();
  46.  
  47.     protected $_adjustToZero        = False;
  48.  
  49.     protected $_yBestFitValues        = array();
  50.  
  51.     protected $_goodnessOfFit         = 1;
  52.  
  53.     protected $_stdevOfResiduals    = 0;
  54.  
  55.     protected $_covariance            = 0;
  56.  
  57.     protected $_correlation            = 0;
  58.  
  59.     protected $_SSRegression        = 0;
  60.  
  61.     protected $_SSResiduals            = 0;
  62.  
  63.     protected $_DFResiduals            = 0;
  64.  
  65.     protected $_F                    = 0;
  66.  
  67.     protected $_slope                = 0;
  68.  
  69.     protected $_slopeSE                = 0;
  70.  
  71.     protected $_intersect            = 0;
  72.  
  73.     protected $_intersectSE            = 0;
  74.  
  75.     protected $_Xoffset                = 0;
  76.  
  77.     protected $_Yoffset                = 0;
  78.  
  79.  
  80.     public function getError({
  81.         return $this->_error;
  82.     }    //    function getBestFitType()
  83.  
  84.  
  85.     public function getBestFitType({
  86.         return $this->_bestFitType;
  87.     }    //    function getBestFitType()
  88.  
  89.  
  90.     public function getValueOfYForX($xValue{
  91.         return False;
  92.     }    //    function getValueOfYForX()
  93.  
  94.  
  95.     public function getValueOfXForY($yValue{
  96.         return False;
  97.     }    //    function getValueOfXForY()
  98.  
  99.  
  100.     public function getXValues({
  101.         return $this->_xValues;
  102.     }    //    function getValueOfXForY()
  103.  
  104.  
  105.     public function getEquation($dp=0{
  106.         return False;
  107.     }    //    function getEquation()
  108.  
  109.  
  110.     public function getSlope($dp=0{
  111.         if ($dp != 0{
  112.             return round($this->_slope,$dp);
  113.         }
  114.         return $this->_slope;
  115.     }    //    function getSlope()
  116.  
  117.  
  118.     public function getSlopeSE($dp=0{
  119.         if ($dp != 0{
  120.             return round($this->_slopeSE,$dp);
  121.         }
  122.         return $this->_slopeSE;
  123.     }    //    function getSlopeSE()
  124.  
  125.  
  126.     public function getIntersect($dp=0{
  127.         if ($dp != 0{
  128.             return round($this->_intersect,$dp);
  129.         }
  130.         return $this->_intersect;
  131.     }    //    function getIntersect()
  132.  
  133.  
  134.     public function getIntersectSE($dp=0{
  135.         if ($dp != 0{
  136.             return round($this->_intersectSE,$dp);
  137.         }
  138.         return $this->_intersectSE;
  139.     }    //    function getIntersectSE()
  140.  
  141.  
  142.     public function getGoodnessOfFit($dp=0{
  143.         if ($dp != 0{
  144.             return round($this->_goodnessOfFit,$dp);
  145.         }
  146.         return $this->_goodnessOfFit;
  147.     }    //    function getGoodnessOfFit()
  148.  
  149.  
  150.     public function getGoodnessOfFitPercent($dp=0{
  151.         if ($dp != 0{
  152.             return round($this->_goodnessOfFit * 100,$dp);
  153.         }
  154.         return $this->_goodnessOfFit * 100;
  155.     }    //    function getGoodnessOfFitPercent()
  156.  
  157.  
  158.     public function getStdevOfResiduals($dp=0{
  159.         if ($dp != 0{
  160.             return round($this->_stdevOfResiduals,$dp);
  161.         }
  162.         return $this->_stdevOfResiduals;
  163.     }    //    function getStdevOfResiduals()
  164.  
  165.  
  166.     public function getSSRegression($dp=0{
  167.         if ($dp != 0{
  168.             return round($this->_SSRegression,$dp);
  169.         }
  170.         return $this->_SSRegression;
  171.     }    //    function getSSRegression()
  172.  
  173.  
  174.     public function getSSResiduals($dp=0{
  175.         if ($dp != 0{
  176.             return round($this->_SSResiduals,$dp);
  177.         }
  178.         return $this->_SSResiduals;
  179.     }    //    function getSSResiduals()
  180.  
  181.  
  182.     public function getDFResiduals($dp=0{
  183.         if ($dp != 0{
  184.             return round($this->_DFResiduals,$dp);
  185.         }
  186.         return $this->_DFResiduals;
  187.     }    //    function getDFResiduals()
  188.  
  189.  
  190.     public function getF($dp=0{
  191.         if ($dp != 0{
  192.             return round($this->_F,$dp);
  193.         }
  194.         return $this->_F;
  195.     }    //    function getF()
  196.  
  197.  
  198.     public function getCovariance($dp=0{
  199.         if ($dp != 0{
  200.             return round($this->_covariance,$dp);
  201.         }
  202.         return $this->_covariance;
  203.     }    //    function getCovariance()
  204.  
  205.  
  206.     public function getCorrelation($dp=0{
  207.         if ($dp != 0{
  208.             return round($this->_correlation,$dp);
  209.         }
  210.         return $this->_correlation;
  211.     }    //    function getCorrelation()
  212.  
  213.  
  214.     public function getYBestFitValues({
  215.         return $this->_yBestFitValues;
  216.     }    //    function getYBestFitValues()
  217.  
  218.  
  219.     protected function _calculateGoodnessOfFit($sumX,$sumY,$sumX2,$sumY2,$sumXY,$meanX,$meanY$const{
  220.         $SSres $SScov $SScor $SStot $SSsex 0.0;
  221.         foreach($this->_xValues as $xKey => $xValue{
  222.             $bestFitY $this->_yBestFitValues[$xKey$this->getValueOfYForX($xValue);
  223.  
  224.             $SSres += ($this->_yValues[$xKey$bestFitY($this->_yValues[$xKey$bestFitY);
  225.             if ($const{
  226.                 $SStot += ($this->_yValues[$xKey$meanY($this->_yValues[$xKey$meanY);
  227.             else {
  228.                 $SStot += $this->_yValues[$xKey$this->_yValues[$xKey];
  229.             }
  230.             $SScov += ($this->_xValues[$xKey$meanX($this->_yValues[$xKey$meanY);
  231.             if ($const{
  232.                 $SSsex += ($this->_xValues[$xKey$meanX($this->_xValues[$xKey$meanX);
  233.             else {
  234.                 $SSsex += $this->_xValues[$xKey$this->_xValues[$xKey];
  235.             }
  236.         }
  237.  
  238.         $this->_SSResiduals = $SSres;
  239.         $this->_DFResiduals = $this->_valueCount - $const;
  240.  
  241.         if ($this->_DFResiduals == 0.0{
  242.             $this->_stdevOfResiduals = 0.0;
  243.         else {
  244.             $this->_stdevOfResiduals = sqrt($SSres $this->_DFResiduals);
  245.         }
  246.         if (($SStot == 0.0|| ($SSres == $SStot)) {
  247.             $this->_goodnessOfFit = 1;
  248.         else {
  249.             $this->_goodnessOfFit = ($SSres $SStot);
  250.         }
  251.  
  252.         $this->_SSRegression = $this->_goodnessOfFit * $SStot;
  253.         $this->_covariance = $SScov $this->_valueCount;
  254.         $this->_correlation = ($this->_valueCount * $sumXY $sumX $sumYsqrt(($this->_valueCount * $sumX2 pow($sumX,2)) ($this->_valueCount * $sumY2 pow($sumY,2)));
  255.         $this->_slopeSE = $this->_stdevOfResiduals / sqrt($SSsex);
  256.         $this->_intersectSE = $this->_stdevOfResiduals * sqrt(($this->_valueCount - ($sumX $sumX$sumX2));
  257.         if ($this->_SSResiduals != 0.0{
  258.             if ($this->_DFResiduals == 0.0{
  259.                 $this->_F = 0.0;
  260.             else {
  261.                 $this->_F = $this->_SSRegression / ($this->_SSResiduals / $this->_DFResiduals);
  262.             }
  263.         else {
  264.             if ($this->_DFResiduals == 0.0{
  265.                 $this->_F = 0.0;
  266.             else {
  267.                 $this->_F = $this->_SSRegression / $this->_DFResiduals;
  268.             }
  269.         }
  270.     }    //    function _calculateGoodnessOfFit()
  271.  
  272.  
  273.     protected function _leastSquareFit($yValues$xValues$const{
  274.         // calculate sums
  275.         $x_sum array_sum($xValues);
  276.         $y_sum array_sum($yValues);
  277.         $meanX $x_sum $this->_valueCount;
  278.         $meanY $y_sum $this->_valueCount;
  279.         $mBase $mDivisor $xx_sum $xy_sum $yy_sum 0.0;
  280.         for($i 0$i $this->_valueCount++$i{
  281.             $xy_sum += $xValues[$i$yValues[$i];
  282.             $xx_sum += $xValues[$i$xValues[$i];
  283.             $yy_sum += $yValues[$i$yValues[$i];
  284.  
  285.             if ($const{
  286.                 $mBase += ($xValues[$i$meanX($yValues[$i$meanY);
  287.                 $mDivisor += ($xValues[$i$meanX($xValues[$i$meanX);
  288.             else {
  289.                 $mBase += $xValues[$i$yValues[$i];
  290.                 $mDivisor += $xValues[$i$xValues[$i];
  291.             }
  292.         }
  293.  
  294.         // calculate slope
  295. //        $this->_slope = (($this->_valueCount * $xy_sum) - ($x_sum * $y_sum)) / (($this->_valueCount * $xx_sum) - ($x_sum * $x_sum));
  296.         $this->_slope = $mBase $mDivisor;
  297.  
  298.         // calculate intersect
  299. //        $this->_intersect = ($y_sum - ($this->_slope * $x_sum)) / $this->_valueCount;
  300.         if ($const{
  301.             $this->_intersect = $meanY ($this->_slope * $meanX);
  302.         else {
  303.             $this->_intersect = 0;
  304.         }
  305.  
  306.         $this->_calculateGoodnessOfFit($x_sum,$y_sum,$xx_sum,$yy_sum,$xy_sum,$meanX,$meanY,$const);
  307.     }    //    function _leastSquareFit()
  308.  
  309.  
  310.     function __construct($yValues$xValues=array()$const=True{
  311.         //    Calculate number of points
  312.         $nY count($yValues);
  313.         $nX count($xValues);
  314.  
  315.         //    Define X Values if necessary
  316.         if ($nX == 0{
  317.             $xValues range(1,$nY);
  318.             $nX $nY;
  319.         elseif ($nY != $nX{
  320.             //    Ensure both arrays of points are the same size
  321.             $this->_error = True;
  322.             return False;
  323.         }
  324.  
  325.         $this->_valueCount = $nY;
  326.         $this->_xValues = $xValues;
  327.         $this->_yValues = $yValues;
  328.     }    //    function __construct()
  329.  
  330. }    //    class bestFit

Documentation generated on Sun, 27 Feb 2011 16:28:30 -0800 by phpDocumentor 1.4.3