Select your font size 
 
about us products & services consulting & support news & events contact us
The Bayes.php class implements the Bayes theorem calculation. An important aspect of Bayesian inference involves examining the effect of

Implementing the calculation with Bayes.php - Ontario

print this article 
 

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
  }
}
?>

Sensitivity analysis

An important aspect of Bayesian inference involves examining the effect of small changes to your prior and likelihood distributions. If the prior probability values you are using are viewed as best guesses, then you might want to see what happens when you adjust the prior probabilities of each hypothesis slightly. You may notice that this significantly changes the posterior distribution values or it might have little effect. It is good to know how sensitive your results are to the exact prior values (or likelihood values) used.

The final screen of the Bayes diagnosis wizard gives you the options to

  • Start again
  • Re-enter labels
  • Re-enter your priors
  • Re-enter your likelihoods

If you decide to re-enter your priors, the wizard remembers your previously entered likelihoods. After you re-enter your priors, you can click forward to Step 5 without having to re-enter your likelihood values (or you can modify the likelihoods as well). In other words, the design of the Bayes wizard encourages you to engage in sensitivity analysis prior to drawing any final conclusions.



Page:   1  2  3  4  5  6  7  8  9  10  11 Next Page: It's only a beginning

The content shown in this page was first published by IBM developerWorks and is reprinted with permission from Paul Meagher (www.datavore.com)


Most Recent Website and Regional Updates

 Transparen Corporation Canada Day Celebrations July 1, 2006
Transparen Corp. is proud of its Canadian heritage, and gladly celebrates Canada Day every year. Look back at previous Canada Days to learn about Transparen's history.

 
 Timing Upgrades - Factors Affecting Time Between Purchases for Tech Toys
It is possible to understand client purchase decisions by performing a regression analysis. By forming strategies based on the results, companies can optimize strategic programs to maximize profits.

 
 Federal Candidates Weigh In On Fairness
McGuinty government posts responses to Ontario?s call for fairness.

 
 Ontario Pays Tribute To Fallen Firefighters
McGuinty government honours families at Queen?s Park, fire fighter memorial.

 
 Ontario Celebrates Its Olympic Athletes
Premier McGuinty Passes The Torch With Pan Am Bid

 
 Ontario Supports Craft Brewers
The Government of Ontario is making an $8-million commitment to help ensure the prosperity of small brewers for many years to come.

 
 New Honda Engine Plant Drives Green Jobs
McGuinty Government Calls Plant A Vote Of Confidence In Ontario?s Skilled Workers

 
 Turning Innovation Into Ontario Jobs
Today, the government intends to introduce Ideas for the Future Act, 2008, that, if passed, would provide a 10-year corporate income tax exemption for new companies that turn home-grown ideas into Ontario jobs and products.

 
 Speaking Up For Ontario
Premier urges Ontarians to sign petition and stand up for fairness.

 
 Improving Care for Ontario?s Youngest Patients
McGuinty Government Improving Access To Care And Reducing Wait Times For Kids

 
 Moving Ontario Forward, Together
McGuinty Government?s Latest Progress Report Tells Stories Of Real People

 
 Making Ontario More Competitive At Home And Abroad
Cabinet Shuffle Focuses On Strengthening Economy

 

Google
 
Web transparen.com

Contact Information

Related Information

 
  TORONTO ON
LONDON ON
OTTAWA ON
HAMILTON ON
 
 
E C M | © 2003-2007 Transparen Corp.      

Standardized Services: Data Recovery Service / Creative Services / Premium Web Hosting Services / System Administration Tech Support Services
Recent Projects: Full-Service Mortgage and Financing Company / System to manage flights from Vancouver to Tofino / Photo exchange verification service
Our Vancouver BC Server Proudly Hosts: automated parking and revenue control systems, leafside lane at southlands, cost effective alternative power sources, the photo genie, pacific forage bag supply, sunburst medical, neosonic design, roger mahler photography - passionate, intriguing, desirable, the connection between east and west, affordable flights to victoria and tofino, low interest mortgage brokers in vancouver, richmond, surrey, toronto, mortgage brokers in calgary
Toronto, Ottawa, Kanata, Hamilton, London - view entire list