Search

Useful Lists

Partners

Online Manuals

Implement Bayesian inference using PHP, Part 1
By Paul Meagher - 2004-04-21 Page:  1 2 3 4 5 6 7 8 9 10 11

## Implementing the calculation with Bayes.php

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
 ``````priors = \$priors; \$this->likelihoods = \$likelihoods; \$this->m = count(\$this->likelihoods); // num rows \$this->n = count(\$this->likelihoods); // 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") { ?>
column_labels[\$h] ?>
row_labels[\$e] ?> posterior[\$e][\$h]) ?>
m; \$h++) { ?> n; \$e++) { ?> m; \$h++) { ?> ``````

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

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.

View Implement Bayesian inference using PHP, Part 1 Discussion

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