IT.COM

[PHP] PMCC Calculator

Spaceship Spaceship
Watch
Impact
9
Possibly one of the most useless functions, but i thought i'd post it anyway, as it may be of use to someone. It calculates PMCC between to set's of data, i created it because excel was continously throwing up errors with all the data I entered.

pmcc.class.php
PHP:
  <?php
/*
    PMCC (Product Moment Correlation Coefficient) Class
    Takes 2 sets of data, and calculates their relationship using the PMCC formula
    and returns a value between -1 and 1
    


    Created by: Lee Findlow
    Contact:     [email protected]
    Website:     http://conceptsublime.com
*/ 
class PMCC{
    //Variables Used, for range x and range y
    var $DataX;
    var $DataY;

    //Function to create Sxx
    function Sxx(){
	//Disable Error Reporting
	error_reporting(0);
        $NumOfAll = count($this->DataX);
        $SumOfAll = array_sum($this->DataX);
        $MeanOfAll = $SumOfAll/$NumOfAll;
        //Calculate Σ(Xi-[Mean Of All X's])^2
        $RunningTotal = 0;
            foreach($this->DataX as $Sxx){
                $RunningTotal += pow(($Sxx-$MeanOfAll),2);
            }
        return $RunningTotal;
    }
    
    //Function to create Syy
    function Syy(){
	//Disable Error Reporting
	error_reporting(0);
        $NumOfAll = count($this->DataY);
        $SumOfAll = array_sum($this->DataY);
        $MeanOfAll = $SumOfAll/$NumOfAll;
        //Calculate Σ(Yi-[Mean Of All Y's])^2
        $RunningTotal = 0;
            foreach($this->DataY as $Syy){
                $RunningTotal += pow(($Syy-$MeanOfAll),2);
            }
        return $RunningTotal;
    }
    
    //Function to create Sxy
    function Sxy(){
	//Disable Error Reporting
	error_reporting(0);
        $NumOfAll_X = count($this->DataX);
        $SumOfAll_X = array_sum($this->DataX);
        $MeanOfAll_X = $SumOfAll_X/$NumOfAll_X;
        $NumOfAll_Y = count($this->DataX);
        $SumOfAll_Y = array_sum($this->DataX);
        $MeanOfAll_Y = $SumOfAll_Y/$NumOfAll_Y;
        $NumOfAll = min($NumOfAll_X,$NumOfAll_Y);
        //Loop Through and create Σ(Xi-[Mean Of All X's])(Yi-[Mean Of All Y's])
        $i = 0;
        $RunningTotal = 0;
        while($i < $NumOfAll){
            $X = $this->DataX[$i] - $MeanOfAll_X;
            $Y = ($this->DataY[$i]-$MeanOfAll_Y);
            $RunningTotal+= $X * $Y;
            $i++;
        }
        return $RunningTotal;
    }
    
    //Combine All Functions to Create PMCC Value
    function PMCC(){
	//Disable Error Reporting
	error_reporting(0);
        //Calculate Lower Part
        $SxxSyy = $this->Sxx()*$this->Syy();
        $SxxSyy = sqrt($SxxSyy);
        //Do Calculation
        $PMCC = $this->Sxy()/$SxxSyy;
        //Return Answer
        return $PMCC;
    }
};
?>
The data you want to compare is entered as an array, as displayed above


More Information - http://en.wikipedia.org/wiki/PMCC

Lee :)
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back