PHPBuilder - Skewness, Kurtosis and average statistical function



RSS Twitter
Snippets Math Functions

Skewness, Kurtosis and average statistical function

by: dhk
|
April 19, 2001

Version: 1.0

Type: Function

Category: Math Functions

License: GNU General Public License

Description: Skewness, Kurtosis, and also average function by using array methodology



<?php
/* 
In this code fraction, there r 3 function that run on mean or average and skewness and kurtosis. 
Program by : dhiranuntk@hotmail.com 
Date : Feb.18,2001 
*/ 

function mean(&$array) { 
$average = 0; 
while (list($key, $val)=each($array)) $average += $val; 
reset($array); 
$average /= count($array);  
return $average; 
} 
function skewnessandkurtosis($array, &$skew, &$kurt) { 
$skew = "N/A"; 
$kurt = "N/A"; 
$amount = count($array); 
if ($amount > 2) { 
for ($i = 0, $m2 =0,$m3=0,$m4=0; $i < $amount; $i++) { 
$array [$i] -= mean($array); 
$m2 += pow($array[$i], 2); 
$m3 += pow($array[$i], 3); 
$m4 += pow($array[$i], 4); 
} 
$m2 /= $amount; 
$m3 /= $amount; 
$m4 /= $amount; 
$skew = $m3 / pow($m2, 1.5); 
$skew *= sqrt($amount*($amount-1))/ ($amount-2); 
if ($amount > 3) { 
$kurt = ($m4/ pow($m2, 2))-3; 
$kurt = (($amount+1)*$kurt)+6; 
$kurt *= ($amount-1)/(($amount-2)*($amount-3)); 
} 
} 
}
?> 




<? 
// and also example on how 2 use is here 

$samplearray = array(1.8,1.9,1.2,1.5,1.7); 
skewnessandkurtosis($samplearray, $skew, $kurt); 
echo "<p>Data average = ". mean($samplearray)."</p>"; 
echo "<p>skewness = $skew </p>"; 
echo "<p>kurtosis=$kurt</p>"; 
?>   

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
dhk

Comment:



Comment:

(Maximum characters: 1200). You have characters left.