Online Shopping : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 19 people (37%) answered Yes
Recently 3 of 10 people (30%) answered Yes

Entry

How to perform simple linear regression analysis with PHP?
How to compute trend equation from an array of data?
How to forecast future value using existing data?

Mar 24th, 2008 20:38
ha mo, Son Nguyen,


<?php
/** 
* @author Son Nguyen
* @since 11/18/2005
* @package Framework.Data
* @subpackage Math
*/
class CRegressionLinear {
 private $mDatas;
 /** constructor */
 function __construct($pDatas) {
  $this->mDatas = $pDatas;
 }
 /** get the coefficients */
 function calculate() {
  $n = count($this->mDatas);
  $vSumXX = $vSumXY = $vSumX = $vSumY = 0;
  foreach ($this->mDatas AS $x=>$y) {
   $vSumXY += $x*$y;
   $vSumXX += $x*$x;
   $vSumX += $x;
   $vSumY += $y;
  } // rof
  $a = ($n*$vSumXY - $vSumX*$vSumY)/($n*$vSumXX - $vSumX*$vSumX);
  $b = ($vSumY - $a*$vSumX)/$n;
  return array($a,$b);
 }
 /** given x, return the prediction */
 function predict($x) {
  list($a,$b) = $this->calculate();
  $y = $a*$x+$b;
  return $y;
 }
}
?>
http://www.tantofa.com
http://www.fantofa.com
http://www.mantofa.com
http://www.tanpola.com
http://www.tampola.com
http://www.yamot.com
http://www.mozmar.com
http://www.templatestemp.com