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

Source for file bestFitClass.php

Documentation is available at bestFitClass.php

  1. <?php
  2.  
  3. {
  4.     protected $_error                = False;
  5.  
  6.     protected $_bestFitType            = 'undetermined';
  7.  
  8.     protected $_valueCount            = 0;
  9.  
  10.     protected $_xValues                = array();
  11.  
  12.     protected $_yValues                = array();
  13.  
  14.     protected $_adjustToZero        = False;
  15.  
  16.     protected $_yBestFitValues        = array();
  17.  
  18.     protected $_goodnessOfFit         = 1;
  19.  
  20.     protected $_stdevOfResiduals    = 0;
  21.  
  22.     protected $_covariance            = 0;
  23.  
  24.     protected $_correlation            = 0;
  25.  
  26.     protected $_SSRegression        = 0;
  27.  
  28.     protected $_SSResiduals            = 0;
  29.  
  30.     protected $_DFResiduals            = 0;
  31.  
  32.     protected $_F                    = 0;
  33.  
  34.     protected $_slope                = 0;
  35.  
  36.     protected $_slopeSE                = 0;
  37.  
  38.     protected $_intersect            = 0;
  39.  
  40.     protected $_intersectSE            = 0;
  41.  
  42.     protected $_Xoffset                = 0;
  43.  
  44.     protected $_Yoffset                = 0;
  45.  
  46.  
  47.     public function getError({
  48.         return $this->_error;
  49.     }    //    function getBestFitType()
  50.  
  51.  
  52.     public function getBestFitType({
  53.         return $this->_bestFitType;
  54.     }    //    function getBestFitType()
  55.  
  56.  
  57.     public function getValueOfYForX($xValue{
  58.         return False;
  59.     }    //    function getValueOfYForX()
  60.  
  61.  
  62.     public function getValueOfXForY($yValue{
  63.         return False;
  64.     }    //    function getValueOfXForY()
  65.  
  66.  
  67.     public function getXValues({
  68.         return $this->_xValues;
  69.     }    //    function getValueOfXForY()
  70.  
  71.  
  72.     public function getEquation($dp=0{
  73.         return False;
  74.     }    //    function getEquation()
  75.  
  76.  
  77.     public function getSlope($dp=0{
  78.         if ($dp != 0{
  79.             return round($this->_slope,$dp);
  80.         }
  81.         return $this->_slope;
  82.     }    //    function getSlope()
  83.  
  84.  
  85.     public function getSlopeSE($dp=0{
  86.         if ($dp != 0{
  87.             return round($this->_slopeSE,$dp);
  88.         }
  89.         return $this->_slopeSE;
  90.     }    //    function getSlopeSE()
  91.  
  92.  
  93.     public function getIntersect($dp=0{
  94.         if ($dp != 0{
  95.             return round($this->_intersect,$dp);
  96.         }
  97.         return $this->_intersect;
  98.     }    //    function getIntersect()
  99.  
  100.  
  101.     public function getIntersectSE($dp=0{
  102.         if ($dp != 0{
  103.             return round($this->_intersectSE,$dp);
  104.         }
  105.         return $this->_intersectSE;
  106.     }    //    function getIntersectSE()
  107.  
  108.  
  109.     public function getGoodnessOfFit($dp=0{
  110.         if ($dp != 0{
  111.             return round($this->_goodnessOfFit,$dp);
  112.         }
  113.         return $this->_goodnessOfFit;
  114.     }    //    function getGoodnessOfFit()
  115.  
  116.  
  117.     public function getGoodnessOfFitPercent($dp=0{
  118.         if ($dp != 0{
  119.             return round($this->_goodnessOfFit * 100,$dp);
  120.         }
  121.         return $this->_goodnessOfFit * 100;
  122.     }    //    function getGoodnessOfFitPercent()
  123.  
  124.  
  125.     public function getStdevOfResiduals($dp=0{
  126.         if ($dp != 0{
  127.             return round($this->_stdevOfResiduals,$dp);
  128.         }
  129.         return $this->_stdevOfResiduals;
  130.     }    //    function getStdevOfResiduals()
  131.  
  132.  
  133.     public function getSSRegression($dp=0{
  134.         if ($dp != 0{
  135.             return round($this->_SSRegression,$dp);
  136.         }
  137.         return $this->_SSRegression;
  138.     }    //    function getSSRegression()
  139.  
  140.  
  141.     public function getSSResiduals($dp=0{
  142.         if ($dp != 0{
  143.             return round($this->_SSResiduals,$dp);
  144.         }
  145.         return $this->_SSResiduals;
  146.     }    //    function getSSResiduals()
  147.  
  148.  
  149.     public function getDFResiduals($dp=0{
  150.         if ($dp != 0{
  151.             return round($this->_DFResiduals,$dp);
  152.         }
  153.         return $this->_DFResiduals;
  154.     }    //    function getDFResiduals()
  155.  
  156.  
  157.     public function getF($dp=0{
  158.         if ($dp != 0{
  159.             return round($this->_F,$dp);
  160.         }
  161.         return $this->_F;
  162.     }    //    function getF()
  163.  
  164.  
  165.     public function getCovariance($dp=0{
  166.         if ($dp != 0{
  167.             return round($this->_covariance,$dp);
  168.         }
  169.         return $this->_covariance;
  170.     }    //    function getCovariance()
  171.  
  172.  
  173.     public function getCorrelation($dp=0{
  174.         if ($dp != 0{
  175.             return round($this->_correlation,$dp);
  176.         }
  177.         return $this->_correlation;
  178.     }    //    function getCorrelation()
  179.  
  180.  
  181.     public function getYBestFitValues({
  182.         return $this->_yBestFitValues;
  183.     }    //    function getYBestFitValues()
  184.  
  185.  
  186.     protected function _calculateGoodnessOfFit($sumX,$sumY,$sumX2,$sumY2,$sumXY,$meanX,$meanY$const{
  187.         $SSres $SScov $SScor $SStot $SSsex 0.0;
  188.         foreach($this->_xValues as $xKey => $xValue{
  189.             $bestFitY $this->_yBestFitValues[$xKey$this->getValueOfYForX($xValue);
  190.  
  191.             $SSres += ($this->_yValues[$xKey$bestFitY($this->_yValues[$xKey$bestFitY);
  192.             if ($const{
  193.                 $SStot += ($this->_yValues[$xKey$meanY($this->_yValues[$xKey$meanY);
  194.             else {
  195.                 $SStot += $this->_yValues[$xKey$this->_yValues[$xKey];
  196.             }
  197.             $SScov += ($this->_xValues[$xKey$meanX($this->_yValues[$xKey$meanY);
  198.             if ($const{
  199.                 $SSsex += ($this->_xValues[$xKey$meanX($this->_xValues[$xKey$meanX);
  200.             else {
  201.                 $SSsex += $this->_xValues[$xKey$this->_xValues[$xKey];
  202.             }
  203.         }
  204.  
  205.         $this->_SSResiduals = $SSres;
  206.         $this->_DFResiduals = $this->_valueCount - $const;
  207.  
  208.         $this->_stdevOfResiduals = sqrt($SSres $this->_DFResiduals);
  209.         if (($SStot == 0.0|| ($SSres == $SStot)) {
  210.             $this->_goodnessOfFit = 1;
  211.         else {
  212.             $this->_goodnessOfFit = ($SSres $SStot);
  213.         }
  214.  
  215.         $this->_SSRegression = $this->_goodnessOfFit * $SStot;
  216.         $this->_covariance = $SScov $this->_valueCount;
  217.         $this->_correlation = ($this->_valueCount * $sumXY $sumX $sumYsqrt(($this->_valueCount * $sumX2 pow($sumX,2)) ($this->_valueCount * $sumY2 pow($sumY,2)));
  218.         $this->_slopeSE = $this->_stdevOfResiduals / sqrt($SSsex);
  219.         $this->_intersectSE = $this->_stdevOfResiduals * sqrt(($this->_valueCount - ($sumX $sumX$sumX2));
  220.         if ($this->_SSResiduals != 0.0{
  221.             $this->_F = $this->_SSRegression / ($this->_SSResiduals / $this->_DFResiduals);
  222.         else {
  223.             $this->_F = $this->_SSRegression / $this->_DFResiduals;
  224.         }
  225.     }    //    function _calculateGoodnessOfFit()
  226.  
  227.  
  228.     protected function _leastSquareFit($yValues$xValues$const{
  229.         // calculate sums
  230.         $x_sum array_sum($xValues);
  231.         $y_sum array_sum($yValues);
  232.         $meanX $x_sum $this->_valueCount;
  233.         $meanY $y_sum $this->_valueCount;
  234.         $mBase $mDivisor $xx_sum $xy_sum $yy_sum 0.0;
  235.         for($i 0$i $this->_valueCount++$i{
  236.             $xy_sum += $xValues[$i$yValues[$i];
  237.             $xx_sum += $xValues[$i$xValues[$i];
  238.             $yy_sum += $yValues[$i$yValues[$i];
  239.  
  240.             if ($const{
  241.                 $mBase += ($xValues[$i$meanX($yValues[$i$meanY);
  242.                 $mDivisor += ($xValues[$i$meanX($xValues[$i$meanX);
  243.             else {
  244.                 $mBase += $xValues[$i$yValues[$i];
  245.                 $mDivisor += $xValues[$i$xValues[$i];
  246.             }
  247.         }
  248.  
  249.         // calculate slope
  250. //        $this->_slope = (($this->_valueCount * $xy_sum) - ($x_sum * $y_sum)) / (($this->_valueCount * $xx_sum) - ($x_sum * $x_sum));
  251.         $this->_slope = $mBase $mDivisor;
  252.  
  253.         // calculate intersect
  254. //        $this->_intersect = ($y_sum - ($this->_slope * $x_sum)) / $this->_valueCount;
  255.         if ($const{
  256.             $this->_intersect = $meanY ($this->_slope * $meanX);
  257.         else {
  258.             $this->_intersect = 0;
  259.         }
  260.  
  261.         $this->_calculateGoodnessOfFit($x_sum,$y_sum,$xx_sum,$yy_sum,$xy_sum,$meanX,$meanY,$const);
  262.     }    //    function _leastSquareFit()
  263.  
  264.  
  265.     function __construct($yValues$xValues=array()$const=True{
  266.         //    Calculate number of points
  267.         $nY count($yValues);
  268.         $nX count($xValues);
  269.  
  270.         //    Define X Values if necessary
  271.         if ($nX == 0{
  272.             $xValues range(1,$nY);
  273.             $nX $nY;
  274.         elseif ($nY != $nX{
  275.             //    Ensure both arrays of points are the same size
  276.             $this->_error = True;
  277.             return False;
  278.         }
  279.  
  280.         $this->_valueCount = $nY;
  281.         $this->_xValues = $xValues;
  282.         $this->_yValues = $yValues;
  283.     }    //    function __construct()
  284.  
  285. }    //    class bestFit

Documentation generated on Wed, 22 Apr 2009 08:56:03 +0200 by phpDocumentor 1.4.1