Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PHP TUTORIALS > SIMPLE LINEAR REGRESSION WITH PHP: PART 2


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

Simple linear regression with PHP: Part 2
By Paul Meagher - 2004-05-21 Page:  1 2 3 4 5 6 7 8 9

Probability functions

In the previous article, I side-stepped the issue of probability functions in PHP by shelling out to R for a probability value. I was not entirely happy with this solution, so I began exploring the issue of what it would take to develop PHP-based probability functions.

I began trolling the Web for information and code. One source of both was the probability functions in the book Numerical Recipes in C. I re-implemented some probability function code in PHP (the gammln.c and betai.c functions), but, again, was not happy with the results. Compared with some of the other implementations, it seemed to be a lot of code. Also, I needed to have inverse probability functions.

Luckily, I stumbled upon John Pezzullo's Interactive Statistical Calculation. John's Web site on Probability Distribution Functions had all the probability functions I needed already implemented in JavaScript for easy learning.

I ported the Student T and Fisher F functions to PHP. I changed the API a bit to conform to a Java naming style and embedded all the functions in a class called Distribution. One elegant feature of this implementation is the doCommonMath method that is reused by all the functions in this library. The other tests that I have not bothered to implement (Normal and ChiSquare) use the doCommonMath method.

One other aspect of the port is noteworthy. By using JavaScript, the user can assign a dynamically determined value to an instance variable, such as:


var PiD2 = pi() / 2 

You cannot do this in PHP. The only values you can assign to an instance variable are simple constants. Hopefully this limitation will be addressed in PHP5.

Notice that the code in Listing 1 has no defined instance variables -- this is because in the JavaScript version, they were dynamically assigned values.

Listing 1. Implementing probability functions

<?php 

 // Distribution.php 

 // Copyright John Pezullo 
 // Released under same terms as PHP. 
 // PHP Port and OO'fying by Paul Meagher 

 class Distribution { 

  function doCommonMath($q, $i, $j, $b) { 
       
   $zz = 1;  
   $z  = $zz;  
   $k  = $i;  
       
       
   while($k <= $j) {  
        $zz = $zz * $q * $k / ($k - $b);  
        $z  = $z + $zz;  
        $k  = $k + 2;  
   } 
   return $z; 
  } 
       
  function getStudentT($t, $df) {   

   $t  = abs($t);  
   $w  = $t  / sqrt($df);  
   $th = atan($w); 
       
   if ($df == 1) {  
    return 1 - $th / (pi() / 2);  
   } 
     
   $sth = sin($th);  
   $cth = cos($th); 
     
   if( ($df % 2) ==1 ) {  
    return 
      1 - ($th + $sth * $cth * $this->doCommonMath($cth * $cth, 2, $df - 3, -1)) 
                         / (pi()/2); 
   } else { 
    return 1 - $sth * $this->doCommonMath($cth * $cth, 1, $df - 3, -1);  
   } 
     
  } 
     
  function getInverseStudentT($p, $df) {  
       
   $v =  0.5;  
   $dv = 0.5;  
   $t  = 0; 
       
   while($dv > 1e-6) {  
    $t = (1 / $v) - 1;  
    $dv = $dv / 2;  
    if ( $this->getStudentT($t, $df) > $p) {  
     $v = $v - $dv; 
    } else {  
     $v = $v + $dv; 
    }  
   } 
   return $t; 
  } 
     

  function getFisherF($f, $n1, $n2) { 
   // implemented but not shown     
  } 

  function getInverseFisherF($p, $n1, $n2) {  
   // implemented but not shown     
  } 

 } 
 ?> 



View Simple linear regression with PHP: Part 2 Discussion

Page:  1 2 3 4 5 6 7 8 9 Next Page: Output methods

First published by IBM developerWorks


Copyright 2004-2017 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.