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
/**
* Script to graph likelihood distribution of the binomial
* parameter p (i.e., probability of success per trial).
*
* Much of the plotting code below has been adapted from
* example code written by JPGraph author Johan Persson.
*
* To get this working locally, you must install JPGraph
* and set the JPGRAPH constant to the folder where the
* JPGraph source code resides.
*
* @see http://www.aditus.nu/jpgraph/index.php
*/
require_once "../config.php";
include_once JPGRAPH . "/src/jpgraph.php";
include_once JPGRAPH . "/src/jpgraph_line.php";
include_once JPGRAPH . "/src/jpgraph_scatter.php";
include_once JPGRAPH . "/src/jpgraph_regstat.php";
$n = 5; // num events
$k = 1; // num success events
$i = 0; // counter
for($p = 0.05; $p <= 1.00; $p += 0.05 ) {
$log_likelihoods[$i] = $k * log($p) + ($n - $k) * log(1 - $p);
$parameters[$i] = $p;
$i++;
}
$mle = max($log_likelihoods);
$p = $parameters[array_search($mle, $log_likelihoods)];
$spline = new Spline($parameters, $log_likelihoods);
list($newx,$newy) = $spline->Get(50);
$graph = new Graph(450, 350);
$graph->SetMargin(60, 20, 40, 30);
$graph->title->Set("Maximum Likelihood Estimate");
$graph->subtitle->Set(" MLE = P( $k/$n | $p ) = $mle ");
$graph->subtitle->SetColor('darkred');
$graph->SetMarginColor('lightblue');
$graph->SetScale('linlin');
$graph->xaxis->SetPos("min");
$graph->xaxis->SetLabelFormat('%1.2f');
$graph->yaxis->SetLabelFormat('%1.2f');
$graph->xaxis->SetTitle("p","center");
$graph->yaxis->SetTitleMargin(40);
$graph->yaxis->SetTitle("L(p)", "center");
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$splot = new ScatterPlot($log_likelihoods, $parameters);
$splot->mark->SetFillColor('red@0.3');
$splot->mark->SetColor('red@0.5');
$lplot = new LinePlot($newy,$newx);
$lplot->SetColor('navy');
$graph->Add($lplot);
$graph->Add($splot);
$graph->Stroke();
?>