PHP
  Home arrow PHP arrow Page 8 - Implementing Bayesian Inference Using PHP: Part 2
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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

Implementing Bayesian Inference Using PHP: Part 2
By: developerWorks
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2005-01-12


    Table of Contents:
  • Implementing Bayesian Inference Using PHP: Part 2
  • Defining simple surveys
  • What is parameter estimation?
  • Computing the MLE
  • Graphing the likelihood distribution
  • Algebraic cleverness
  • Bayes estimators
  • Beta distribution sampling model
  • Beta distribution source code
  • Conclusions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Implementing Bayesian Inference Using PHP: Part 2 - Beta distribution sampling model
    ( Page 8 of 10 )

    A random variable is said to have the standard beta distribution with parameters a and b if its probability density function is:

    f() =a - 1 * (1 -) b - 1 / B(a, b)

    Rather than explain the formula by resorting to more mathematics, I will discuss PHP code that you can use to compute f() for various values of a and b. Towards this end I created a class called BetaDistribution.phpand added it to a probability distributions package I developed for a previous article (see Resources). This class supplies methods that accept the a, b, andparameters.

    The class constructor is first called with the a and b parameters as follows:

    Listing 4. Instantiating the BetaDistribution class

      <?php

    // Demonstration of how to instantiate Beta Distribution
    // class.

    require_once "../BetaDistribution.php";

    $a = 1;  // num successes
    $b = 4;  // num failures

    $beta = new BetaDistribution($a, $b);

    ?>

     

    In this example, the number of success events (previously k) is denoted by a. The number of failure events is equal to n - k and is denoted by b. The a and b parameters jointly control the shape and location of the beta distribution curves.

    Graphically speaking, the beta distribution refers to a large family of plotting curves that can differ substantially from one another depending upon the a and b parameter values. As you shall see, the a and b parameters can be used to represent the prior probability distribution that you feel is most appropriate for representing P(i).

    Suppose you test your simple binary survey before going live. Select five people that you think are representative of the target sampling population and ask them to fill it out the survey online; then observe the following results:

    • One participant responds "yes" (a "success" event).
    • Four participants respond "no" ("failure" events).

    The code in Listing 5 invokes the BetaDistributionwith the appropriate parameter values of a=1 and b=4 to represent the results of this survey. Once you instantiate the beta distribution constructor with the appropriate a and b parameter values, you can then use other methods in this class (depicted in the following) to compute standard probability distribution functions:

    Listing 5. Using other methods to compute probability distribution functions

      <?php

    $a = 1;  // num successes
    $b = 4;  // num failures

    $beta = new BetaDistribution($a, $b)

    echo $beta->getMean() ."<br />";
    echo $beta->getStandardDeviation() ."<br />";
    echo $beta->PDF(.2) ."<br />";
    echo $beta->CDF(.50) ."<br />";
    echo $beta->inverseCDF(0.95);

    ?>

     

    Which produces the output displayed in this table:

    Table 4. Output of beta probability distribution methods


    BetaDistribution(1, 4)

    Methods Output
    getMean() 0.2
    getStandardDeviation() 0.16329931618555
    PDF(.2) 2.048
    CDF(.50) 0.9375
    inverseCDF(0.95) 0.52712919549841
     

    If the test has no glitches, you can go into your main experiment with BetaDistribution(1, 4) being used to represent your prior distribution P(). Note that the mean value (= p = k/n = a / a + b = .20) reported in the table is what you expect it to be from common-sense considerations (such as the expected value ofequal to the observed proportion of cases to date k/n).

    To visualize your prior probability distribution, you can use the following code below to obtain the x and y coordinates to plot. The probability density function PDF()returns a "probability" value associated with a particularvalue -- for instance, P[p = .20]. Given a contiguous range of p values, the PDF()method give you a corresponding range of probability values f(p) that you can use to graph the shape of the probability distribution for fixed a, b parameters and for a range of possiblevalues:

    Listing 6. Obtaining x and y coordinates to plot

      <?php

    require_once '../BetaDistribution.php';

    $a = 1;  // num successes
    $b = 4;  // num failures

    $beta = new BetaDistribution($a, $b);

    $i = 0; // counter
    for($p = 0.01; $p <= 0.99; $p = $p + 0.04 ) {
      $pdf_vals[$i]   = $beta->PDF($p);  // y coordinates
      $parameters[$i] = $p;              // x coordinates
      $i++;
    }

    $mean  = sprintf("%0.3f", $beta->getMean());
    $stdev = sprintf("%0.3f", $beta->getStandardDeviation());

    ?>

    In the following graph, p = $parameters[$i], and f(p) = $pdf_vals[$i].

    Figure 4. Prior distribution is not well defined; too few observations


    The exact values of f(p) are of less concern than the overall shape and center of gravity for the prior distribution. What this graph shows is that your prior distribution is still not very well defined because it does not peak around a particular parameter estimate. This is as it should be when you only have a few observations to work with.



     
     
    >>> More PHP Articles          >>> More By developerWorks
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT