Search code examples
c#statisticsdistributionmath.nett-test

Compute critical value T-Score using Math.NET


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)

Solution

  • 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