2

Any thoughts on how I can compute critical value in a t-distribution using Math.NET?

More concretely, I am looking for how to compute the following excel function using Math.NET.

=T.INV.2T(0.05, 8)
Dr. Strangelove
  • 2,725
  • 3
  • 34
  • 61

1 Answers1

2

How about

var x = MathNet.Numerics.Distributions.StudentT.InvCDF(location:0.0, scale:1.0, freedom: (double)8, p: 0.05);

You should check against T.INV.2T whether two-tailed value is returned, if not just adjust the probability

You could also compare with critical values here

UPDATE

C# code below (.NET Core 3.1, Math.NET v4.9, Win10 x64)

using System;
using MathNet.Numerics.Distributions;

namespace Tinv
{
    class Program
    {
        public static double T_INV_2T(double p, double nu)
        {
            return StudentT.InvCDF(location:0.0, scale:1.0, freedom: nu, p: 1.0 - p/2.0);
            // same as             return Math.Abs(StudentT.InvCDF(location:0.0, scale:1.0, freedom: nu, p: p/2.0));

        }

        static void Main(string[] args)
        {
            var x = T_INV_2T(0.05, (double)8);
            Console.WriteLine($"Output = {x}");
        }
    }
}

produced output

2.306004135204172

where Excel produced value of =T.INV.2T(0.05, 8) equal to 2.306004, and in the NIST table by the link value is listed as 2.306

Seems quite consistent

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64
  • Thank you. Any thoughts on how I can make this two-tailed? – Dr. Strangelove Dec 25 '19 at 18:29
  • Thank you; I guess the trick to make this two-tailed was `p: 1.0 - p/2.0` – Dr. Strangelove Dec 26 '19 at 01:55
  • 1
    @Hamed or absolute value (from negative left tail) with probability `p/2.0`. You're welcome. I would only like to point out that in docs on `StudentT.InvCDF` there is a warning - `WARNING: currently not an explicit implementation, hence slow and unreliable.` Not sure how much to make out of it, there is no explicit implementation anywhere (not in Excel), inverse CDF for student T cannot be computed as closed expression. – Severin Pappadeux Dec 26 '19 at 02:19
  • Thanks for the heads-up, I will use the output value with caution then. Could you please elaborate a bit on "inverse CDF for student T cannot be computed as closed expression"? – Dr. Strangelove Dec 26 '19 at 03:57
  • There is a paper http://www.homepages.ucl.ac.uk/~ucahwts/lgsnotes/JCF_Student.pdf, where inverse CDF is computed for sampling. If you read it, it works for even number degrees of freedom up to n=4, where you can get closed expression. Everything else (odd number d.o.f., even number d.o.f. starting with n=6) requires some numerical scheme, series expansions, Newton-Raphson, iterations or all of above cobbled together. – Severin Pappadeux Dec 26 '19 at 04:18