⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 teststudentttestsunit.cs

📁 数理统计Stutent s检验源代码
💻 CS
字号:

using System;

class teststudentttestsunit
{
    public static bool teststudentt(bool silent)
    {
        bool result = new bool();
        int pass = 0;
        int passcount = 0;
        int maxn = 0;
        int n = 0;
        int m = 0;
        int i = 0;
        int j = 0;
        int qcnt = 0;
        double[] x = new double[0];
        double[] y = new double[0];
        double[] qtbl = new double[0];
        double[] ptbl = new double[0];
        double[] lptbl = new double[0];
        double[] rptbl = new double[0];
        double bt = 0;
        double lt = 0;
        double rt = 0;
        double v = 0;
        bool waserrors = new bool();

        waserrors = false;
        maxn = 1000;
        passcount = 20000;
        x = new double[maxn-1+1];
        y = new double[maxn-1+1];
        qcnt = 8;
        ptbl = new double[qcnt-1+1];
        lptbl = new double[qcnt-1+1];
        rptbl = new double[qcnt-1+1];
        qtbl = new double[qcnt-1+1];
        qtbl[0] = 0.25;
        qtbl[1] = 0.15;
        qtbl[2] = 0.10;
        qtbl[3] = 0.05;
        qtbl[4] = 0.04;
        qtbl[5] = 0.03;
        qtbl[6] = 0.02;
        qtbl[7] = 0.01;
        if( !silent )
        {
            System.Console.Write("TESTING STUDENT T");
            System.Console.WriteLine();
        }
        
        //
        // 1-sample test
        //
        if( !silent )
        {
            System.Console.Write("Testing 1-sample test for 1-type errors");
            System.Console.WriteLine();
        }
        n = 15;
        for(i=0; i<=qcnt-1; i++)
        {
            ptbl[i] = 0;
            lptbl[i] = 0;
            rptbl[i] = 0;
        }
        for(pass=1; pass<=passcount; pass++)
        {
            
            //
            // Both tails
            //
            teststudenttgennormrnd(n, 0.0, 1+4*AP.Math.RandomReal(), ref x);
            studentttests.studentttest1(ref x, n, 0, ref bt, ref lt, ref rt);
            for(i=0; i<=qcnt-1; i++)
            {
                if( bt<=qtbl[i] )
                {
                    ptbl[i] = ptbl[i]+(double)(1)/(double)(passcount);
                }
            }
            
            //
            // Left tail
            //
            teststudenttgennormrnd(n, 0.5*AP.Math.RandomReal(), 1+4*AP.Math.RandomReal(), ref x);
            studentttests.studentttest1(ref x, n, 0, ref bt, ref lt, ref rt);
            for(i=0; i<=qcnt-1; i++)
            {
                if( lt<=qtbl[i] )
                {
                    lptbl[i] = lptbl[i]+(double)(1)/(double)(passcount);
                }
            }
            
            //
            // Right tail
            //
            teststudenttgennormrnd(n, -(0.5*AP.Math.RandomReal()), 1+4*AP.Math.RandomReal(), ref x);
            studentttests.studentttest1(ref x, n, 0, ref bt, ref lt, ref rt);
            for(i=0; i<=qcnt-1; i++)
            {
                if( rt<=qtbl[i] )
                {
                    rptbl[i] = rptbl[i]+(double)(1)/(double)(passcount);
                }
            }
        }
        if( !silent )
        {
            System.Console.Write("Expect. Both    Left    Right");
            System.Console.WriteLine();
            for(i=0; i<=qcnt-1; i++)
            {
                System.Console.Write("{0,4:F1}",qtbl[i]*100);
                System.Console.Write("%");
                System.Console.Write("   ");
                System.Console.Write("{0,4:F1}",ptbl[i]*100);
                System.Console.Write("%");
                System.Console.Write("   ");
                System.Console.Write("{0,4:F1}",lptbl[i]*100);
                System.Console.Write("%");
                System.Console.Write("   ");
                System.Console.Write("{0,4:F1}",rptbl[i]*100);
                System.Console.Write("%");
                System.Console.Write("   ");
                System.Console.WriteLine();
            }
        }
        for(i=0; i<=qcnt-1; i++)
        {
            waserrors = waserrors | ptbl[i]/qtbl[i]>1.3 | qtbl[i]/ptbl[i]>1.3;
            waserrors = waserrors | lptbl[i]>qtbl[i]*1.3;
            waserrors = waserrors | rptbl[i]>qtbl[i]*1.3;
        }
        
        //
        // 2-sample test
        //
        if( !silent )
        {
            System.Console.WriteLine();
            System.Console.WriteLine();
            System.Console.Write("Testing 2-sample test for 1-type errors");
            System.Console.WriteLine();
        }
        for(i=0; i<=qcnt-1; i++)
        {
            ptbl[i] = 0;
            lptbl[i] = 0;
            rptbl[i] = 0;
        }
        for(pass=1; pass<=passcount; pass++)
        {
            n = 5+AP.Math.RandomInteger(20);
            m = 5+AP.Math.RandomInteger(20);
            v = 1+4*AP.Math.RandomReal();
            
            //
            // Both tails
            //
            teststudenttgennormrnd(n, 0.0, v, ref x);
            teststudenttgennormrnd(m, 0.0, v, ref y);
            studentttests.studentttest2(ref x, n, ref y, m, ref bt, ref lt, ref rt);
            for(i=0; i<=qcnt-1; i++)
            {
                if( bt<=qtbl[i] )
                {
                    ptbl[i] = ptbl[i]+(double)(1)/(double)(passcount);
                }
            }
            
            //
            // Left tail
            //
            teststudenttgennormrnd(n, 0.5*AP.Math.RandomReal(), v, ref x);
            teststudenttgennormrnd(m, 0.0, v, ref y);
            studentttests.studentttest2(ref x, n, ref y, m, ref bt, ref lt, ref rt);
            for(i=0; i<=qcnt-1; i++)
            {
                if( lt<=qtbl[i] )
                {
                    lptbl[i] = lptbl[i]+(double)(1)/(double)(passcount);
                }
            }
            
            //
            // Right tail
            //
            teststudenttgennormrnd(n, -(0.5*AP.Math.RandomReal()), v, ref x);
            teststudenttgennormrnd(m, 0.0, v, ref y);
            studentttests.studentttest2(ref x, n, ref y, m, ref bt, ref lt, ref rt);
            for(i=0; i<=qcnt-1; i++)
            {
                if( rt<=qtbl[i] )
                {
                    rptbl[i] = rptbl[i]+(double)(1)/(double)(passcount);
                }
            }
        }
        if( !silent )
        {
            System.Console.Write("Expect. Both    Left    Right");
            System.Console.WriteLine();
            for(i=0; i<=qcnt-1; i++)
            {
                System.Console.Write("{0,4:F1}",qtbl[i]*100);
                System.Console.Write("%");
                System.Console.Write("   ");
                System.Console.Write("{0,4:F1}",ptbl[i]*100);
                System.Console.Write("%");
                System.Console.Write("   ");
                System.Console.Write("{0,4:F1}",lptbl[i]*100);
                System.Console.Write("%");
                System.Console.Write("   ");
                System.Console.Write("{0,4:F1}",rptbl[i]*100);
                System.Console.Write("%");
                System.Console.Write("   ");
                System.Console.WriteLine();
            }
        }
        for(i=0; i<=qcnt-1; i++)
        {
            waserrors = waserrors | ptbl[i]/qtbl[i]>1.3 | qtbl[i]/ptbl[i]>1.3;
            waserrors = waserrors | lptbl[i]>qtbl[i]*1.3;
            waserrors = waserrors | rptbl[i]>qtbl[i]*1.3;
        }
        
        //
        // Unequal variance test
        //
        if( !silent )
        {
            System.Console.WriteLine();
            System.Console.WriteLine();
            System.Console.Write("Testing unequal variance test for 1-type errors");
            System.Console.WriteLine();
        }
        for(i=0; i<=qcnt-1; i++)
        {
            ptbl[i] = 0;
            lptbl[i] = 0;
            rptbl[i] = 0;
        }
        for(pass=1; pass<=passcount; pass++)
        {
            n = 15+AP.Math.RandomInteger(20);
            m = 15+AP.Math.RandomInteger(20);
            
            //
            // Both tails
            //
            teststudenttgennormrnd(n, 0.0, 1+4*AP.Math.RandomReal(), ref x);
            teststudenttgennormrnd(m, 0.0, 1+4*AP.Math.RandomReal(), ref y);
            studentttests.unequalvariancettest(ref x, n, ref y, m, ref bt, ref lt, ref rt);
            for(i=0; i<=qcnt-1; i++)
            {
                if( bt<=qtbl[i] )
                {
                    ptbl[i] = ptbl[i]+(double)(1)/(double)(passcount);
                }
            }
            
            //
            // Left tail
            //
            teststudenttgennormrnd(n, 0.5*AP.Math.RandomReal(), 1+4*AP.Math.RandomReal(), ref x);
            teststudenttgennormrnd(m, 0.0, 1+4*AP.Math.RandomReal(), ref y);
            studentttests.unequalvariancettest(ref x, n, ref y, m, ref bt, ref lt, ref rt);
            for(i=0; i<=qcnt-1; i++)
            {
                if( lt<=qtbl[i] )
                {
                    lptbl[i] = lptbl[i]+(double)(1)/(double)(passcount);
                }
            }
            
            //
            // Right tail
            //
            teststudenttgennormrnd(n, -(0.5*AP.Math.RandomReal()), 1+4*AP.Math.RandomReal(), ref x);
            teststudenttgennormrnd(m, 0.0, 1+4*AP.Math.RandomReal(), ref y);
            studentttests.unequalvariancettest(ref x, n, ref y, m, ref bt, ref lt, ref rt);
            for(i=0; i<=qcnt-1; i++)
            {
                if( rt<=qtbl[i] )
                {
                    rptbl[i] = rptbl[i]+(double)(1)/(double)(passcount);
                }
            }
        }
        if( !silent )
        {
            System.Console.Write("Expect. Both    Left    Right");
            System.Console.WriteLine();
            for(i=0; i<=qcnt-1; i++)
            {
                System.Console.Write("{0,4:F1}",qtbl[i]*100);
                System.Console.Write("%");
                System.Console.Write("   ");
                System.Console.Write("{0,4:F1}",ptbl[i]*100);
                System.Console.Write("%");
                System.Console.Write("   ");
                System.Console.Write("{0,4:F1}",lptbl[i]*100);
                System.Console.Write("%");
                System.Console.Write("   ");
                System.Console.Write("{0,4:F1}",rptbl[i]*100);
                System.Console.Write("%");
                System.Console.Write("   ");
                System.Console.WriteLine();
            }
        }
        for(i=0; i<=qcnt-1; i++)
        {
            waserrors = waserrors | ptbl[i]/qtbl[i]>1.3 | qtbl[i]/ptbl[i]>1.3;
            waserrors = waserrors | lptbl[i]>qtbl[i]*1.3;
            waserrors = waserrors | rptbl[i]>qtbl[i]*1.3;
        }
        
        //
        //
        //
        if( !silent )
        {
            if( waserrors )
            {
                System.Console.Write("TEST FAILED");
                System.Console.WriteLine();
            }
            else
            {
                System.Console.Write("TEST PASSED");
                System.Console.WriteLine();
            }
        }
        result = !waserrors;
        return result;
    }


    private static void teststudenttgennormrnd(int n,
        double mean,
        double sigma,
        ref double[] r)
    {
        int i = 0;
        double u = 0;
        double v = 0;
        double s = 0;
        double sum = 0;

        i = 0;
        while( i<n )
        {
            u = (2*AP.Math.RandomInteger(2)-1)*AP.Math.RandomReal();
            v = (2*AP.Math.RandomInteger(2)-1)*AP.Math.RandomReal();
            sum = u*u+v*v;
            if( sum<1 & sum>0 )
            {
                sum = Math.Sqrt(-(2*Math.Log(sum)/sum));
                if( i<n )
                {
                    r[i] = sigma*u*sum+mean;
                }
                if( i+1<n )
                {
                    r[i+1] = sigma*v*sum+mean;
                }
                i = i+2;
            }
        }
    }


    /*************************************************************************
    Silent unit test
    *************************************************************************/
    public static bool teststudentttestsunit_test_silent()
    {
        bool result = new bool();

        result = teststudentt(true);
        return result;
    }


    /*************************************************************************
    Unit test
    *************************************************************************/
    public static bool teststudentttestsunit_test()
    {
        bool result = new bool();

        result = teststudentt(false);
        return result;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -