# How to implement formula for a independent groups t-test in C#?

I am writing a program in C# that requires me to use the Ttest formula. I have to effectively interpret the Excel formula:

=TTEST(range1,range2,1,3)


I am using the formula given here

and have interpreted into code as such:

 double TStatistic = (mean1 - mean2) / Math.Sqrt((Math.Pow(variance1, 2) /
count1) + (Math.Pow(variance2, 2) / count2));


However, I don't fully understand t-test and the values I am getting are completely different than those calculated within Excel.

I have been using the following ranges:

R1:
91.17462277,
118.3936425,
96.6746393,
102.488785,
91.26831043

R2:
17.20546254,
19.56969811,
19.2831241,
13.03360631,
13.86577314


The value I am getting using my attempt is 1.8248, however that from Excel is 1.74463E-05. Could somebody please point me in the right direction?

-

## migrated from math.stackexchange.comMar 8 '11 at 19:00

This question came from our site for people studying math at any level and professionals in related fields.

Perhaps this question is better suited for Stack Overflow: stackoverflow.com – Eric Naslund Mar 8 '11 at 18:30
Why would you be squaring the variances in the second code snippet? Or are your variables just oddly named? – cardinal Mar 8 '11 at 18:58
@Darren Young, Excel has well documented problems with its statistical questions, so take care validating your code with it. This is for the future reference, as others pointed out in this case there was an error in your code. – mpiktas Mar 8 '11 at 21:06
@Darren Young, this question and the answers might be of interest. – mpiktas Mar 8 '11 at 21:08
@Cardinal - I think I have mis-interpreted the formula. @mpiktas Thanks for the info. I'll take a look when in work tomorrow. – Darren Young Mar 8 '11 at 21:40

There are at least two problems with what you have done.

1. You have misinterpreted the formula $$t = \frac{\bar{x}_1-\bar{x}_2}{\sqrt{s_1^2 / n_1 + s_2^2 / n_2}}$$ since $s^2$ is already a variance (square of standard deviation) and does not need to be squared again.
2. You are comparing eggs and omelettes: you need compare your "calculated $t$-value, with $k$ degrees of freedom ... to the $t$ distribution table". Excel has already done this with TTEST().

There are other possible issues such as using a population variance or sample variance formula.

-
Thanks Henry. So, when I calculate the variance, then that is the figure I need for s2, rather than squaring the actual variance again? Also, I had a look at the distribution table, but I wasn't sure what (if any) calculation I needed to perform against the table - what does k degrees of freedom mean? Sorry for the questions - I am clearly not a statistician - more of a computer programmer. Thanks again. – Darren Young Mar 8 '11 at 20:44
@Darren Young: Correct, you don't need to square the variances again; but you do need make sure you are using unbiased estimates of the population variances. – Henry Mar 8 '11 at 22:43
@Darren Young: The degrees of freedom is a parameter in a Student's t-test - to complicate things, its simplistic explanation of how they come to be what they are does not apply when you are assuming unequal variances, so look especially at the section on unequal variances. – Henry Mar 8 '11 at 22:44
@I have re-calculated the functions, and for the original data above I get 17.84 as the t value, which has been confirmed using online ttest calculator. Why then in the Excel formula would it give such a small number as 1.74463E-05 instead? I have looked at the dist table, and with this input data the df = 8, and checked at 0.05 value, but I don't know what to do with that value? Any ideas please? Thanks again. – Darren Young Mar 9 '11 at 11:45
You should get a $t$ value of about $15.959 \approx \frac{100-16.5915}{\sqrt{127.427/5 + 9.1449/5}}$ as confirmed here. If you are assuming unequal variances as you did with TTEST(,,,3) you need Welch's t-test and you get 4.57 degrees of freedom here which you might round down to 4; if you assume equal variances you get 8 degrees of freedom. Either way, you next need to find the probability that if the means were equal then $t$ would be 15.959 or more extreme. The probability what Excel is reporting and the step you have missed. – Henry Mar 9 '11 at 12:28

Since version 4 the .NET libraries include the following methods:

StatisticFormula.TTestEqualVariances
StatisticFormula.TTestUnequalVariances


This class is available in the namespaces System.Web.UI.DataVisualization.Charting and System.Windows.Forms.DataVisualization.Charting

Link to documentation and usage:

TTestResult result = Chart1.DataManipulator.Statistics.TTestUnEqualVariances(0.2, 0.05, "Series1", "Series2");

-