Probability functions
In the previous article, I side-stepped the issue of probability functions in PHP by shelling out to R for a probability value. I was not entirely happy with this solution, so I began exploring the issue of what it would take to develop PHP-based probability functions.
I began trolling the Web for information and code. One source of both was the probability functions in the book Numerical Recipes in C. I re-implemented some probability function code in PHP (the gammln.c
and betai.c
functions), but, again, was not happy with the results. Compared with
some of the other implementations, it seemed to be a lot of code. Also,
I needed to have inverse probability functions.
Luckily, I stumbled upon John Pezzullo's Interactive Statistical Calculation. John's Web site on Probability Distribution Functions had all the probability functions I needed already implemented in JavaScript for easy learning.
I ported the Student T and Fisher F functions to PHP. I changed the
API a bit to conform to a Java naming style and embedded all the
functions in a class called Distribution
. One elegant feature of this implementation is the doCommonMath
method that is reused by all the functions in this library. The other
tests that I have not bothered to implement (Normal and ChiSquare) use
the doCommonMath
method.
One other aspect of the port is noteworthy. By using JavaScript, the user can assign a dynamically determined value to an instance variable, such as:
|
You cannot do this in PHP. The only values you can assign to an instance variable are simple constants. Hopefully this limitation will be addressed in PHP5.
Notice that the code in Listing 1 has no defined instance variables -- this is because in the JavaScript version, they were dynamically assigned values.
Listing 1. Implementing probability functions
|
View Simple linear regression with PHP: Part 2 Discussion
Page: 1 2 3 4 5 6 7 8 9 Next Page: Output methods