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 - Nunavut

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 Toronto Office Locations
Addresses of Transparen Corporation offices in Toronto, Ontario.

 
 High Scalability - Large Systems Optimization
Transparen Corporation lends its expertise to clients experiencing rapid and sudden growth in traffic or server utilization, bottlenecks, systems instability, downtime during peak traffic, or which would like to plan to avoid such issues.

 
 Throughput (or Bandwidth) vs. Latency
This document uses the example of Bill Gates purchasing Google to explain the difference between bandwidth (or throughput) and latency.

 
 Emergency Management Services
The prototypical emergency involves a shutdown of essential services for a finite period of time. What will your organization do when a world-wide financial crisis strikes?

 
 Fast RAID Server Data Recovery Service
Transparen's Vancouver International Response Team provides the option in Canada and USA to get a raid server back running in hours - eliminating costly waiting associated with typical RAID recoveries.

 
 Data Recovery Service
Have you deleted a mission critical file? Accidentally dropped a computer, or formatted a hard drive? No recent backup? Mistakes can happen, but the data might still be there.

 
 About Transparen
Transparen is committed to serving its clients.

 
 Research Tools
Measure human resource allocation and collect data with the goal of determining patterns that will bring forward actionable insights which may lead to policy changes, saving money and improving quality of service.

 
 Process Evaluation Questions
Questions to help focus discussion about process improvement

 
 Operations Research
Operations Research (frequently called OR), is the methodical study of how to do things better. It is also called Optimization Theory.

 
 R. v. Ammaklak, 2008 NUCJ 27 (CanLII)

 
 Anawak v. Nunavut (Chief Electoral Officer), 2008 NUCJ 26 (CanLII)

 
 Anawak v. Nunavut (Chief Electoral Officer), 2008 NUCJ 24 (CanLII)

 
 B. (G.) v. K. (M.), 2008 NUCJ 23 (CanLII)

 
 D. (G.) v. D. (A.), 2008 NUCJ 21 (CanLII)

 
 Rogers, Re, 2008 NUCJ 20 (CanLII)

 
 Nunavut (Director of Child and Family Services) v. K. (H.), 2008 NUCJ 19 (CanLII)

 
 08/01/2009: How to Divorce and Not Wreck the Kids
For years, divorce has pitted couples against each other, fueling conflict and concerns about the children caught in the middle of it. Now, unhappy couples with children are looking for ways to end their marriage, but not end the family. Today on the podast, we'll hear from a couple trying to do that and the director of a CBC TV documentary called "How To Divorce and Not Wreck The Kids".

 
 07/01/2009: A Death in the Family - Documentary
Today on the podcast, the story of Paul Johnson and Bill Mullins-Johnson, two brothers from Sault Saint Marie, Ontario whose lives were torn apart after the murder of Paul's four-year-old daughter ... a crime that turned the two men against each other even though neither of them had committed it.

 
 06/01/2009: The Threatening Sea
Today on the podcast, we continue our Watershed series with a trip to Vanuatu, a nation of 83 islands in the South Pacific that is slowly but surely sinking into the sea.

 
 05/01/2009: Australia Drought
Dispatches from The Big Dry. Current producer Kathleen Goldhar brings us a report from Australia's enduring drought and the economy it's spawned, where rainless communities unravel, only the adaptable prosper and water is the new gold standard.

 
 02/02/2009: Economy Panel - 2009 Forecast
With the annus horibilis of 2008 in the rear view mirror, and 2009 lying in the wait, The Current organized an economy panel to give us their forecast for the new year.

 

Google
 
Web transparen.com

Contact Information

Related Information

 
   
 
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, Higher Grade Learning Centres, 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, Toronto Calgary and Vancouver IT staffing and talent search
Arctic Bay, Arviat, Baker Lake, Bathurst Inlet, Cambridge Bay, Cape Dorset, Chesterfield Inlet, Clyde River, Coral Harbour, Gjoa Haven, Grise Fiord, Hall Beach, Igloolik, Iqaluit, Frobisher Bay, Kimmirut, Lake Harbour, Kugaaruk, Pelly Bay, Kugluktuk, Coppermine, Pangnirtung, Pond Inlet, Qikiqtarjuaq, Rankin Inlet, Repulse Bay, Resolute, Sanikiluaq, Taloyoak, Spence Bay, Whale Cove, * Bernard Harbour (PIN-C), on the mainland * Bray Island (FOX-A) * Brevoort Island (BAF-3) * Broughton Island (FOX-5) * Byron Bay (PIN-4) * Cambridge Bay (CAM-MAIN) * Cape Dyer (DYE-MAIN), on Baffin Island * Cape Hooper (FOX-4) * Cape Mcloughlin (CAM-5A) * Cape Mercy (BAF-2) * Cape Peel West (PIN-EB) * Cape Young (PIN-2) * Clifton Point (PIN-B) * Clinton Point (PIN-1) * Croker River (PIN 1BG) * Dewar Lakes (FOX-3) * Durban Island (FOX-E) * Edinburgh Island (PIN-DA) * Ekalugad (FOX-C) * Gjoa Haven (CAM-CB) * Gladman Point (CAM-2) * Hall Beach (FOX.MAIN) * Harding River (PIN-2A) * Hat Island (CAM-B) * Jenny Lind Island (CAM-1) * Kangok Fjord (FOX-CA) * Keats Point (PIN-1BD) * Keith Bay (CAM-E) * Kivitoo, (FOX-D) * Lady Franklin Point (PIN-3) * Lailor River (CAM-FA) * Loks Land (BAF-4A) * Longstaff Bluff (FOX-2) * Mackar Inlet (CAM-5) * Matheson Point (CAM-C) * Nudluardjuk Lake (FOX-B) * Pelly Bay (CAM-4) * Resolution Island (BAF-5) * Ross Point (PIN-D) * Rowley Island (FOX-1) * Scarpa Lake (CAM-F) * Shepherd Bay (CAM-3) * Simpson Lake (CAM-D) * Sturt Point (CAM-A3A) * Alert * Ennadai, at Ennadai Lake * Eureka, on Ellesmere Island * Fox Five * Isachsen, on Ellef Ringnes Island * Jericho Diamond Mine * Little Cornwallis Island * Lupin Mine * Nanisivik (), on Baffin Island * Craig Harbour, on Ellesmere Island * Dundas Harbour, on Devon Island * Nuwata, on Baffin Island * Padlei, on the mainland * Tavani, on the mainland * Umingmaktok (Umingmaktuuq , formerly Bay Chimo), on the mainland * Wager Bay, (Ukkusiksalik ) on the mainland