Implement Bayesian inference using PHP: Part 2, IBM developerWorks, April 13, 2004.

In this second article on Bayesian inference, Paul Meagher examines how you can use Bayes methods to solve parameter estimation problems. Relevant concepts are explained in the context of Web survey analysis using PHP and JPGraph.

Last updated: April 14, 2004 - 2:30 AM AST


Demos:

Source code listing:



<?php 
/** 
* @package ProbabilityDistribution 
*/ 

require_once "ProbabilityDistribution.php"
require_once 
"functions/SpecialMath.php"

/** 
* The BetaDistribution class provides an object for 
* encapsulating beta distributions. 
*
* @version 0.2 
* @author Mark Hale 
* @author Jaco van Kooten  
* @author Paul Meagher 
*/ 
class BetaDistribution extends ProbabilityDistribution 

  
/**
  * @int degrees of freedom p.
  */
  
var $p;
  
  
/**
  * @int degrees of freedom q.
  */
  
var $q;

  
/**
  * Constructs a beta distribution.
  * @param dgrP degrees of freedom p.
  * @param dgrQ degrees of freedom q.
  */
  
function BetaDistribution($dgrP$dgrQ) {
    if ((
$dgrP <= 0) || ($dgrQ <= 0) ) {
      return 
PEAR::raiseError("Paramters must be greater than zero.");
    }
    
$this->$dgrP;
    
$this->$dgrQ;
  }
  
  
/**
  * Returns the degrees of freedom p.
  */
  
function getDegreesOfFreedomP() {
    return 
$this->p;
  }
  
  
/**
  * Returns the degrees of freedom q.
  */
  
function getDegreesOfFreedomQ() {
    return 
$this->q;
  }

  
/**
  * Returns the distribution mean.
  */
  
function getMean() {
    return 
$this->p  / ( $this->$this->);
  }

  
/**
  * Returns the distribution standard deviation.
  */
  
function getStandardDeviation() {
    return 
sqrt( ( $this->$this->) / 
                 ( 
pow($this->$this->q,  2
                    * (
$this->$this->1
                 )
               );     
  }
  
  
/**
  * Probability density function of a beta distribution.
  * @return probability that x has the value X, i.e. P(x=X).
  */
  
function PDF($X) {     
    if (
is_array($X)) {       
      
$pdf_vals = array(); 
      
$num_vals count($X); 
      for(
$i=0$i $num_vals$i++) {                 
        
$this->checkRange($X[$i]);
        if (
$X[$i] == 0.0 || $X[$i] == 1.0) {
          
$pdf_vals[$i] = 0.0;
        } else {
          
$pdf_vals[$i] = exp( - logBeta($this->p$this->q
                               + (
$this->1.0
                               * 
log($X[$i]) 
                               + (
$this->1.0
                               * 
log(1.0 $X[$i]) 
                             );          
        }
      }     
      return 
$pdf_vals;     
    } else { 
      
$this->checkRange($X);
      if ( (
$X == 0.0) || ($X == 1.0) ) {
        return 
0.0;
      } else {
        return 
exp( - logBeta($this->p$this->q
                    + (
$this->1.0
                    * 
log($X
                    + (
$this->1.0
                    * 
log(1.0 $X
                  );
      }
    }
  } 
    
  
/**
  * Cumulative beta distribution function.
  * @return probability that x is less then X, i.e. P(x&lt;X).
  */
  
function CDF($X) {  
    if (
is_array($X)) { 
      
$cdf_vals = array(); 
      
$num_vals count($X); 
      for(
$i=0$i $num_vals$i++) {                 
        
$this->checkRange($X[$i]);        
        
$cdf_vals[$i] = incompleteBeta($X[$i], $this->p$this->q);
      } 
      return 
$cdf_vals
    } else {           
      
$this->checkRange($X);
      
$cdf_val incompleteBeta($X$this->p$this->q);
      return 
$cdf_val
    } 
  }   
  
  
/**
  * Inverse of the cumulative beta distribution function.
  * @return the value X for which P(x&lt;X).
  */
  
function inverseCDF($PROB) { 
    if (
is_array($PROB)) { 
      
$inv_vals = array(); 
      
$num_vals count($PROB); 
      for(
$i=0$i $num_vals$i++) {                 
        
$this->checkRange($PROB[$i]);
        if(
$PROB[$i] == 0.0) {
          
$inv_vals[$i] = 0.0;
          continue;
        }    
        if(
$PROB[$i] == 1.0) {
          
$inv_vals[$i] = 1.0;
          continue;
        }
        
$inv_vals[$i] = $this->findRoot($PROB[$i], 0.50.01.0);
      } 
      return 
$inv_vals
    } else { 
      
$this->checkRange($PROB);
      if(
$PROB == 0.0) {
        return 
0.0;
      }    
      if(
$PROB == 1.0) {
        return 
1.0;
      }
      return 
$this->findRoot($PROB0.50.01.0);
    }
  }
       
}

?>



Back to source code listing