note 89378 added to function.stats-stat-correlation
| From: | umar dot anjum at ymail dot com | Date: | Thu, 05 Mar 2009 06:05:51 +0000 |
| Subject: | note 89378 added to function.stats-stat-correlation | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-151503@lists.php.net to get a copy of this message | ||
I tried to use this function, but got a not-defined error. Anyway, I have created a set of functions
to replace this:
<?php
//Since Correlation needs two arrays, I am hardcoding them
$array1[0] = 59.3;
$array1[1] = 61.2;
$array1[2] = 56.8
$array1[3] = 97.55;
$array2[0] = 565.82;
$array2[1] = 54.568;
$array2[2] = 84.22;
$array2[3] = 483.55;
//To find the correlation of the two arrays, simply call the
//function Correlation that takes two arrays:
$correlation = Correlation($array1, $array2);
//Displaying the calculated Correlation:
print $correlation;
//The functions that work behind the scene to calculate the
//correlation
function Correlation($arr1, $arr2)
{
$correlation = 0;
$k = SumProductMeanDeviation($arr1, $arr2);
$ssmd1 = SumSquareMeanDeviation($arr1);
$ssmd2 = SumSquareMeanDeviation($arr2);
$product = $ssmd1 * $ssmd2;
$res = sqrt($product);
$correlation = $k / $res;
return $correlation;
}
function SumProductMeanDeviation($arr1, $arr2)
{
$sum = 0;
$num = count($arr1);
for($i=0; $i<$num; $i++)
{
$sum = $sum + ProductMeanDeviation($arr1, $arr2, $i);
}
return $sum;
}
function ProductMeanDeviation($arr1, $arr2, $item)
{
return (MeanDeviation($arr1, $item) * MeanDeviation($arr2, $item));
}
function SumSquareMeanDeviation($arr)
{
$sum = 0;
$num = count($arr);
for($i=0; $i<$num; $i++)
{
$sum = $sum + SquareMeanDeviation($arr, $i);
}
return $sum;
}
function SquareMeanDeviation($arr, $item)
{
return MeanDeviation($arr, $item) * MeanDeviation($arr, $item);
}
function SumMeanDeviation($arr)
{
$sum = 0;
$num = count($arr);
for($i=0; $i<$num; $i++)
{
$sum = $sum + MeanDeviation($arr, $i);
}
return $sum;
}
function MeanDeviation($arr, $item)
{
$average = Average($arr);
return $arr[$item] - $average;
}
function Average($arr)
{
$sum = Sum($arr);
$num = count($arr);
return $sum/$num;
}
function Sum($arr)
{
return array_sum($arr);
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 116.71.182.97
----
Manual Page -- http://www.php.net/manual/en/function.stats-stat-correlation.php
Edit -- https://master.php.net/note/edit/89378
Del: integrated -- https://master.php.net/note/delete/89378/integrated
Del: useless -- https://master.php.net/note/delete/89378/useless
Del: bad code -- https://master.php.net/note/delete/89378/bad+code
Del: spam -- https://master.php.net/note/delete/89378/spam
Del: non-english -- https://master.php.net/note/delete/89378/non-english
Del: in docs -- https://master.php.net/note/delete/89378/in+docs
Del: other reasons-- https://master.php.net/note/delete/89378
Reject -- https://master.php.net/note/reject/89378
Search -- https://master.php.net/manage/user-notes.php