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:
<?php
/**
* @package ProbabilityDistribution
*/
define("MAX_FLOAT", 3.40282346638528860e+305);
include "Pear.php";
/**
* The ProbabilityDistribution superclass provides an object
* for encapsulating probability distributions.
* @version 1.0
* @author Jaco van Kooten
* @author Paul Meagher
* @author Jesus Castagnetto
*/
class ProbabilityDistribution {
/**
* Constructs a probability distribution.
*/
function ProbabilityDistribution() {}
/**
* Probability density function.
* @return the probability that a stochastic variable x has the value X, i.e. P(x=X).
*/
function PDF($X) {}
/**
* Cumulative distribution function.
* @return the probability that a stochastic
* variable x is less then X, i.e. P(x<X).
*/
function CDF($X) {}
/**
* Inverse of the cumulative distribution function.
* @return the value X for which P(x<X).
*/
function InverseCDF($probability) {}
/**
* Inverse of the cumulative distribution function.
* @return the value X for which P(x<X).
*/
function RNG($num_vals) {}
/**
* Check if the range of the argument of the distribution
* method is between <code>lo</code> and <code>hi</code>.
* @exception OutOfRangeException If the argument is out of range.
*/
function checkRange($x, $lo=0.0, $hi=1.0) {
if (($x < $lo) || ($x > $hi)) {
return PEAR::raiseError("The argument should be between $lo and $hi.");
}
}
/**
* Get the factorial of the argument
* @return factorial of n.
*/
function getFactorial($n) {
return $n <= 1 ? 1 : $n * $this->getFactorial($n-1);
}
/**
* This method approximates the value of X for which P(x<X)=<I>prob</I>.
* It applies a combination of a Newton-Raphson procedure and bisection method
* with the value <I>guess</I> as a starting point. Furthermore, to ensure convergency
* and stability, one should supply an inverval [<I>xLo</I>,<I>xHi</I>] in which the probability
* distribution reaches the value <I>prob</I>. The method does no checking, it will produce
* bad results if wrong values for the parameters are supplied - use it with care.
*/
function findRoot($prob, $guess, $xLo, $xHi) {
$accuracy = 1.0e-10;
$maxIteration = 150;
$x = $guess;
$xNew = $guess;
$error = 0.0;
$pdf = 0.0;
$dx = 1000.0;
$i = 0;
while ( (abs($dx) > $accuracy) && ($i++ < $maxIteration) ) {
// Apply Newton-Raphson step
$error = $this->CDF($x) - $prob;
if($error < 0.0) {
$xLo = $x;
} else {
$xHi = $x;
}
$pdf = $this->PDF($x);
// Avoid division by zero
if ($pdf != 0.0) {
$dx = $error / $pdf;
$xNew = $x - $dx;
}
// If the NR fails to converge (which for example may be the
// case if the initial guess is to rough) we apply a bisection
// step to determine a more narrow interval around the root.
if ( ($xNew < $xLo) || ($xNew > $xHi) || ($pdf==0.0) ) {
$xNew = ($xLo + $xHi) / 2.0;
$dx = $xNew - $x;
}
$x = $xNew;
}
return $x;
}
}
?>