Solved

# need PHP code that returns t-distribution values

Posted on 2007-07-31
927 Views
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
Question by:bitt3n

LVL 11

Accepted 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
0

Author Comment

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 Comment

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

## Featured Post

### Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.