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


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:



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?

  • $\begingroup$ Perhaps this question is better suited for Stack Overflow: stackoverflow.com $\endgroup$
    – Eric Naslund
    Commented Mar 8, 2011 at 18:30
  • 2
    $\begingroup$ Why would you be squaring the variances in the second code snippet? Or are your variables just oddly named? $\endgroup$
    – cardinal
    Commented Mar 8, 2011 at 18:58
  • $\begingroup$ @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. $\endgroup$
    – mpiktas
    Commented Mar 8, 2011 at 21:06
  • $\begingroup$ @Darren Young, this question and the answers might be of interest. $\endgroup$
    – mpiktas
    Commented Mar 8, 2011 at 21:08
  • $\begingroup$ @Cardinal - I think I have mis-interpreted the formula. @mpiktas Thanks for the info. I'll take a look when in work tomorrow. $\endgroup$ Commented Mar 8, 2011 at 21:40

2 Answers 2


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.

  • $\begingroup$ 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. $\endgroup$ Commented Mar 8, 2011 at 20:44
  • $\begingroup$ @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. $\endgroup$
    – Henry
    Commented Mar 8, 2011 at 22:43
  • $\begingroup$ @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. $\endgroup$
    – Henry
    Commented Mar 8, 2011 at 22:44
  • $\begingroup$ @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. $\endgroup$ Commented Mar 9, 2011 at 11:45
  • $\begingroup$ 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. $\endgroup$
    – Henry
    Commented Mar 9, 2011 at 12:28

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


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");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.