Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1019
  • Last Modified:

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:


(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.
  • 2
1 Solution
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
bitt3nAuthor 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.
bitt3nAuthor Commented:
Thanks it works perfectly.

for the sake of anyone else who wants to use this


will return the relevant probability for t=4.541 and 3 degrees of freedom

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now