• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1045

# need PHP code that returns t-distribution values

I need to find PHP code that, given a probability (eg 80%), and a number of degrees of freedom (eg 9) returns the t-distribution, as in the chart at the bottom of this page: http://en.wikipedia.org/wiki/T-distribution. (eg, for p=80%, df=9, the code would therefore return 0.883.)

what I really want to do is slightly more involved, namely for a given probability, and given a group of n values, and a given max range R, return:

1) the mean m of the n values
2) the standard deviation s of the n values (which can be calculated using the formula following "Estimating population standard deviation from sample standard deviation" on this page: http://en.wikipedia.org/wiki/Standard_deviation
3) the number of degrees of freedom, d = n - 1
4) the standard error of the mean, sigma = s/((n)^(1/2))
5) the upper acceptable error bound b = m + Rm
6) the critical t value beyond which error is too high, T= (b - m)/sigma
7) the probability corresponding to this T given d

The seventh thing I want to return is the real problem, since I don't know how to calculate a t value from a probability and I've just been using the table.

I found a couple of bits of code that claim to do t-distributions, but since I don't know how object oriented PHP works, I'm not sure how to extract the part I need, or even how to use them. What I want to do is kind of backwards, because normally you calculate Rm for a known t, whereas I want to calculate t given a value for R.

Links to the t-distribution PHP I found:

http://www.multistat.de/source.php?file=t.php

(this one is an archive of a bunch of different probability classes, including one for t-distribution)

All I really need is a function that will return a t-value given a probability and number of degrees of freedom.
0
bitt3n
• 2
1 Solution

Commented:
I'd take a look at the stats_cdf_t() function.   There isn't much documentation for it, but the description says that it "Calculates any one parameter of the T distribution given values for the others."

The php documentation page is here: http://www.php.net/manual/en/function.stats-cdf-t.php

there is a list of other statistics functions here: http://www.php.net/manual/en/ref.stats.php
0

Author Commented:
ok cool I will ask my host to install the PECL stats library and I will see if I can figure out how that function works and report back. If anyone out there knows how it works, I would be interested to hear about it.
0

Author Commented:
Thanks it works perfectly.

for the sake of anyone else who wants to use this

stats_cdf_t(4.541,3,1)

will return the relevant probability for t=4.541 and 3 degrees of freedom
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.