Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/2.3.0/ALGLIB-2.3.0/studentttests.cs @ 5955

Last change on this file since 5955 was 2806, checked in by gkronber, 15 years ago

Added plugin for new version of ALGLIB. #875 (Update ALGLIB sources)

File size: 12.7 KB
Line 
1/*************************************************************************
2Copyright (c) 2007, Sergey Bochkanov (ALGLIB project).
3
4>>> SOURCE LICENSE >>>
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation (www.fsf.org); either version 2 of the
8License, or (at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15A copy of the GNU General Public License is available at
16http://www.fsf.org/licensing/licenses
17
18>>> END OF LICENSE >>>
19*************************************************************************/
20
21using System;
22
23namespace alglib
24{
25    public class studentttests
26    {
27        /*************************************************************************
28        One-sample t-test
29
30        This test checks three hypotheses about the mean of the given sample.  The
31        following tests are performed:
32            * two-tailed test (null hypothesis - the mean is equal  to  the  given
33              value)
34            * left-tailed test (null hypothesis - the  mean  is  greater  than  or
35              equal to the given value)
36            * right-tailed test (null hypothesis - the mean is less than or  equal
37              to the given value).
38
39        The test is based on the assumption that  a  given  sample  has  a  normal
40        distribution and  an  unknown  dispersion.  If  the  distribution  sharply
41        differs from normal, the test will work incorrectly.
42
43        Input parameters:
44            X       -   sample. Array whose index goes from 0 to N-1.
45            N       -   size of sample.
46            Mean    -   assumed value of the mean.
47
48        Output parameters:
49            BothTails   -   p-value for two-tailed test.
50                            If BothTails is less than the given significance level
51                            the null hypothesis is rejected.
52            LeftTail    -   p-value for left-tailed test.
53                            If LeftTail is less than the given significance level,
54                            the null hypothesis is rejected.
55            RightTail   -   p-value for right-tailed test.
56                            If RightTail is less than the given significance level
57                            the null hypothesis is rejected.
58
59          -- ALGLIB --
60             Copyright 08.09.2006 by Bochkanov Sergey
61        *************************************************************************/
62        public static void studentttest1(ref double[] x,
63            int n,
64            double mean,
65            ref double bothtails,
66            ref double lefttail,
67            ref double righttail)
68        {
69            int i = 0;
70            double xmean = 0;
71            double xvariance = 0;
72            double xstddev = 0;
73            double v1 = 0;
74            double v2 = 0;
75            double stat = 0;
76            double s = 0;
77
78            if( n<=1 )
79            {
80                bothtails = 1.0;
81                lefttail = 1.0;
82                righttail = 1.0;
83                return;
84            }
85           
86            //
87            // Mean
88            //
89            xmean = 0;
90            for(i=0; i<=n-1; i++)
91            {
92                xmean = xmean+x[i];
93            }
94            xmean = xmean/n;
95           
96            //
97            // Variance (using corrected two-pass algorithm)
98            //
99            xvariance = 0;
100            xstddev = 0;
101            if( n!=1 )
102            {
103                v1 = 0;
104                for(i=0; i<=n-1; i++)
105                {
106                    v1 = v1+AP.Math.Sqr(x[i]-xmean);
107                }
108                v2 = 0;
109                for(i=0; i<=n-1; i++)
110                {
111                    v2 = v2+(x[i]-xmean);
112                }
113                v2 = AP.Math.Sqr(v2)/n;
114                xvariance = (v1-v2)/(n-1);
115                if( (double)(xvariance)<(double)(0) )
116                {
117                    xvariance = 0;
118                }
119                xstddev = Math.Sqrt(xvariance);
120            }
121            if( (double)(xstddev)==(double)(0) )
122            {
123                bothtails = 1.0;
124                lefttail = 1.0;
125                righttail = 1.0;
126                return;
127            }
128           
129            //
130            // Statistic
131            //
132            stat = (xmean-mean)/(xstddev/Math.Sqrt(n));
133            s = studenttdistr.studenttdistribution(n-1, stat);
134            bothtails = 2*Math.Min(s, 1-s);
135            lefttail = s;
136            righttail = 1-s;
137        }
138
139
140        /*************************************************************************
141        Two-sample pooled test
142
143        This test checks three hypotheses about the mean of the given samples. The
144        following tests are performed:
145            * two-tailed test (null hypothesis - the means are equal)
146            * left-tailed test (null hypothesis - the mean of the first sample  is
147              greater than or equal to the mean of the second sample)
148            * right-tailed test (null hypothesis - the mean of the first sample is
149              less than or equal to the mean of the second sample).
150
151        Test is based on the following assumptions:
152            * given samples have normal distributions
153            * dispersions are equal
154            * samples are independent.
155
156        Input parameters:
157            X       -   sample 1. Array whose index goes from 0 to N-1.
158            N       -   size of sample.
159            Y       -   sample 2. Array whose index goes from 0 to M-1.
160            M       -   size of sample.
161
162        Output parameters:
163            BothTails   -   p-value for two-tailed test.
164                            If BothTails is less than the given significance level
165                            the null hypothesis is rejected.
166            LeftTail    -   p-value for left-tailed test.
167                            If LeftTail is less than the given significance level,
168                            the null hypothesis is rejected.
169            RightTail   -   p-value for right-tailed test.
170                            If RightTail is less than the given significance level
171                            the null hypothesis is rejected.
172
173          -- ALGLIB --
174             Copyright 18.09.2006 by Bochkanov Sergey
175        *************************************************************************/
176        public static void studentttest2(ref double[] x,
177            int n,
178            ref double[] y,
179            int m,
180            ref double bothtails,
181            ref double lefttail,
182            ref double righttail)
183        {
184            int i = 0;
185            double xmean = 0;
186            double ymean = 0;
187            double stat = 0;
188            double s = 0;
189            double p = 0;
190
191            if( n<=1 | m<=1 )
192            {
193                bothtails = 1.0;
194                lefttail = 1.0;
195                righttail = 1.0;
196                return;
197            }
198           
199            //
200            // Mean
201            //
202            xmean = 0;
203            for(i=0; i<=n-1; i++)
204            {
205                xmean = xmean+x[i];
206            }
207            xmean = xmean/n;
208            ymean = 0;
209            for(i=0; i<=m-1; i++)
210            {
211                ymean = ymean+y[i];
212            }
213            ymean = ymean/m;
214           
215            //
216            // S
217            //
218            s = 0;
219            for(i=0; i<=n-1; i++)
220            {
221                s = s+AP.Math.Sqr(x[i]-xmean);
222            }
223            for(i=0; i<=m-1; i++)
224            {
225                s = s+AP.Math.Sqr(y[i]-ymean);
226            }
227            s = Math.Sqrt(s*((double)(1)/(double)(n)+(double)(1)/(double)(m))/(n+m-2));
228            if( (double)(s)==(double)(0) )
229            {
230                bothtails = 1.0;
231                lefttail = 1.0;
232                righttail = 1.0;
233                return;
234            }
235           
236            //
237            // Statistic
238            //
239            stat = (xmean-ymean)/s;
240            p = studenttdistr.studenttdistribution(n+m-2, stat);
241            bothtails = 2*Math.Min(p, 1-p);
242            lefttail = p;
243            righttail = 1-p;
244        }
245
246
247        /*************************************************************************
248        Two-sample unpooled test
249
250        This test checks three hypotheses about the mean of the given samples. The
251        following tests are performed:
252            * two-tailed test (null hypothesis - the means are equal)
253            * left-tailed test (null hypothesis - the mean of the first sample  is
254              greater than or equal to the mean of the second sample)
255            * right-tailed test (null hypothesis - the mean of the first sample is
256              less than or equal to the mean of the second sample).
257
258        Test is based on the following assumptions:
259            * given samples have normal distributions
260            * samples are independent.
261        Dispersion equality is not required
262
263        Input parameters:
264            X - sample 1. Array whose index goes from 0 to N-1.
265            N - size of the sample.
266            Y - sample 2. Array whose index goes from 0 to M-1.
267            M - size of the sample.
268
269        Output parameters:
270            BothTails   -   p-value for two-tailed test.
271                            If BothTails is less than the given significance level
272                            the null hypothesis is rejected.
273            LeftTail    -   p-value for left-tailed test.
274                            If LeftTail is less than the given significance level,
275                            the null hypothesis is rejected.
276            RightTail   -   p-value for right-tailed test.
277                            If RightTail is less than the given significance level
278                            the null hypothesis is rejected.
279
280          -- ALGLIB --
281             Copyright 18.09.2006 by Bochkanov Sergey
282        *************************************************************************/
283        public static void unequalvariancettest(ref double[] x,
284            int n,
285            ref double[] y,
286            int m,
287            ref double bothtails,
288            ref double lefttail,
289            ref double righttail)
290        {
291            int i = 0;
292            double xmean = 0;
293            double ymean = 0;
294            double xvar = 0;
295            double yvar = 0;
296            double df = 0;
297            double p = 0;
298            double stat = 0;
299            double c = 0;
300
301            if( n<=1 | m<=1 )
302            {
303                bothtails = 1.0;
304                lefttail = 1.0;
305                righttail = 1.0;
306                return;
307            }
308           
309            //
310            // Mean
311            //
312            xmean = 0;
313            for(i=0; i<=n-1; i++)
314            {
315                xmean = xmean+x[i];
316            }
317            xmean = xmean/n;
318            ymean = 0;
319            for(i=0; i<=m-1; i++)
320            {
321                ymean = ymean+y[i];
322            }
323            ymean = ymean/m;
324           
325            //
326            // Variance (using corrected two-pass algorithm)
327            //
328            xvar = 0;
329            for(i=0; i<=n-1; i++)
330            {
331                xvar = xvar+AP.Math.Sqr(x[i]-xmean);
332            }
333            xvar = xvar/(n-1);
334            yvar = 0;
335            for(i=0; i<=m-1; i++)
336            {
337                yvar = yvar+AP.Math.Sqr(y[i]-ymean);
338            }
339            yvar = yvar/(m-1);
340            if( (double)(xvar)==(double)(0) | (double)(yvar)==(double)(0) )
341            {
342                bothtails = 1.0;
343                lefttail = 1.0;
344                righttail = 1.0;
345                return;
346            }
347           
348            //
349            // Statistic
350            //
351            stat = (xmean-ymean)/Math.Sqrt(xvar/n+yvar/m);
352            c = xvar/n/(xvar/n+yvar/m);
353            df = (n-1)*(m-1)/((m-1)*AP.Math.Sqr(c)+(n-1)*(1-AP.Math.Sqr(c)));
354            if( (double)(stat)>(double)(0) )
355            {
356                p = 1-0.5*ibetaf.incompletebeta(df/2, 0.5, df/(df+AP.Math.Sqr(stat)));
357            }
358            else
359            {
360                p = 0.5*ibetaf.incompletebeta(df/2, 0.5, df/(df+AP.Math.Sqr(stat)));
361            }
362            bothtails = 2*Math.Min(p, 1-p);
363            lefttail = p;
364            righttail = 1-p;
365        }
366    }
367}
Note: See TracBrowser for help on using the repository browser.