PHP
  Home arrow PHP arrow Page 8 - Implement Bayesian inference using PHP...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Implement Bayesian inference using PHP, Part 1
By: developerWorks
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 23
    2005-01-05

    Table of Contents:
  • Implement Bayesian inference using PHP, Part 1
  • Conditional probability
  • Learning from experience
  • Conditional probability and SQL
  • Frequency versus probability format
  • Deriving Bayes Theorem
  • Medical diagnosis wizard
  • Implementing the calculation with Bayes.php
  • Sensitivity analysis
  • Resources

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Implement Bayesian inference using PHP, Part 1 - Implementing the calculation with Bayes.php


    (Page 8 of 10 )

    The Bayes.php class implements the Bayes theorem calculation. The getPosterior method is where most of the mathematically interesting code resides.

    Listing 5. Implementing the calculation with the Bayes.php class
    <?php

    /**
    * Bayes
    *
    * Calculates posterior probabilities for m hypotheses and n evidence
    * alternatives.  The code was inspired by a procedural TrueBasic version
    * (Bayes.tru) bundled with Grimstead and Snell's excellent online
    * textbook "Introduction to Probability".
    */
    class Bayes {

      /**
      * Number of evidence alternatives (that is, number of rows).
      */
      var $m;

      /**
      * Number of hypothesis alternatives (that is, number of columns).
      */
      var $n;

      /**
      * Output labels for evidence alternatives.
      */
      var $row_labels = array();
     
      /**
      * Output labels for hypothesis alternatives.
      */ 
      var $column_labels = array();

      /**
      * Vector container for prior probabilities.
      */
      var $priors = array();

      /**
      * Matrix container for likelihood of evidence e given hypothesis h.
      */
      var $likelihoods = array();

      /**
      * Matrix container for posterior probabilties.
      */
      var $posterior = array();

      /**
      * Vector container for evidence probabilties.
      */
      var $evidence = array();

      /**
      * Initialize the Bayes algorithm by setting the priors, likelihoods
      * and dimensions of the likelihood and posterior matrices.
      */
      function Bayes($priors, $likelihoods) {
        $this->priors = $priors;
        $this->likelihoods = $likelihoods;
        $this->m = count($this->likelihoods);  // num rows
        $this->n = count($this->likelihoods[0]); // num cols
        return true;
      }
     
      /**
      * Output method for setting row labels prior to display.
      */
      function setRowLabels($row_labels) {
        $this->row_labels = $row_labels;
        return true;
      }

      /**
      * Output method for setting column labels prior to display.
      */
      function setColumnLabels($column_labels) {
        $this->column_labels = $column_labels;
        return true;
      }

      /**
      * Compute the posterior probability matrix given the priors and
      * likelihoods.
      *
      * The first set of loops computes the denominator of the canonical
      * Bayes equation. The probability appearing in the denominator
      * serves a normalizing role in the computation - it ensures that
      * posterior probabilities sum to 1.
      *
      * The second set of loops:
      *
      *   1. multiplies the prior[$h] by the likelihood[$h][$e]
      *   2. divides the result by the denominator
      *   3. assigns the result to the posterior[$e][$h] probability matrix
      */
      function getPosterior() {
        // Find probability of evidence e
        for($e=0; $e < $this->n; $e++) {
          for ($h=0; $h < $this->m; $h++) {
            $this->evidence[$e] += $this->priors[$h]
               * $this->likelihoods[$h][$e];
          }
        }
        // Find probability of hypothesis given evidence
        for($e=0; $e < $this->n; $e++) {
          for ($h=0; $h < $this->m; $h++) {
            $this->posterior[$e][$h] = $this->priors[$h
               * $this->likelihoods[$h][$e] / $this->evidence[$e];
          }
        }
        return true;
      }
     
      /**
      * Output method for displaying posterior probability matrix
      */
      function toHTML($number_format="%01.3f") {
        ?>
        <table border='1' cellpadding='5' cellspacing='0'>
          <tr>
            <td> </td>
            <?php
            for ($h=0; $h < $this->m; $h++) {
              ?>
              <td align='center'>
                 <b><?php echo $this->column_labels[$h] ?></b>
              </td>
              <?php
            }
            ?>
          </tr>
          <?php
          for($e=0; $e < $this->n; $e++) {
            ?>
            <tr>
              <td><b><?php echo $this->row_labels[$e] ?></b></td>
              <?php
              for ($h=0; $h < $this->m; $h++) {
                ?>
                <td align='right'>
                   <?php printf($number_format, $this->posterior[$e][$h]) ?>
                </td>
                <?php
              }
              ?>
            </tr>
            <?php
          }
          ?>
        </table>
        <?php
      }
    }
    ?>

    More PHP Articles
    More By developerWorks


       · Definately worth reading.j.olson[url=http://www.linuxloggers.com/]Linux Help...
       · good article, for all science computer fields.
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT