need PHP code that returns t-distribution values

Posted on 2007-07-31
Last Modified: 2012-06-22
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: (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:
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.
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:

    there is a list of other statistics functions here:

    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.

    Author Comment

    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

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Both Easy and Powerful 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.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now