This PHP 5 class accepts an array of data as input, performs linear regression analysis and returns the trend line. The result is the same as in Excel (Tools/Data Analysis/Regression)
/** compute the coeff, equation source: http://people.hofstra.edu/faculty/Stefan_Waner/RealWorld/calctopic1/regression.html */
function calculate() {
$n = count($this->mDatas);
$vSumXX = $vSumXY = $vSumX = $vSumY = 0;
//var_dump($this->mDatas);
$vCnt = 0; // for time-series, start at t=0
foreach ($this->mDatas AS $vOne) {
if (is_array($vOne)) { // x,y pair
list($x,$y) = $vOne;
} else { // time-series
$x = $vCnt; $y = $vOne;
} // fi
$vSumXY += $x*$y;
$vSumXX += $x*$x;
$vSumX += $x;
$vSumY += $y;
$vCnt++;
} // rof
$vTop = ($n*$vSumXY – $vSumX*$vSumY);
$vBottom = ($n*$vSumXX – $vSumX*$vSumX);
$a = $vBottom!=0?$vTop/$vBottom:0;
$b = ($vSumY – $a*$vSumX)/$n;
//var_dump($a,$b);
return array($a,$b);
}
/** given x, return the prediction y */
function predict($x) {
list($a,$b) = $this->calculate();
$y = $a*$x+$b;
return $y;
}
}
?>
Sample Usage
// sales data for the last 30 quarters
$vSales = array(
637381,700986,641305,660285,604474,565316,598734,688690,723406,697358,
669910,605636,526655,555165,625800,579405,588317,634433,628443,570597,
662584,763516,742150,703209,669883,586504,489240,648875,692212,586509
);
$vRegression = new CRegressionLinear($vSales);
$vNextQuarter = $vRegression->predict(); // return the forecast for next period
In this example, we assume it’s a very simple case. For a better forecasting method, there are many, including this multiplicative forecasting method that takes into account seasonality, irregularity and also the growth trend.
Leave a Reply