Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/ALGLIB/variancetests.cs @ 2578

Last change on this file since 2578 was 2563, checked in by gkronber, 15 years ago

Updated ALGLIB to latest version. #751 (Plugin for for data-modeling with ANN (integrated into CEDMA))

File size: 8.1 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 variancetests
26    {
27        /*************************************************************************
28        Two-sample F-test
29
30        This test checks three hypotheses about dispersions of the given  samples.
31        The following tests are performed:
32            * two-tailed test (null hypothesis - the dispersions are equal)
33            * left-tailed test (null hypothesis  -  the  dispersion  of  the first
34              sample is greater than or equal to  the  dispersion  of  the  second
35              sample).
36            * right-tailed test (null hypothesis - the  dispersion  of  the  first
37              sample is less than or equal to the dispersion of the second sample)
38
39        The test is based on the following assumptions:
40            * the given samples have normal distributions
41            * the samples are independent.
42
43        Input parameters:
44            X   -   sample 1. Array whose index goes from 0 to N-1.
45            N   -   sample size.
46            Y   -   sample 2. Array whose index goes from 0 to M-1.
47            M   -   sample size.
48
49        Output parameters:
50            BothTails   -   p-value for two-tailed test.
51                            If BothTails is less than the given significance level
52                            the null hypothesis is rejected.
53            LeftTail    -   p-value for left-tailed test.
54                            If LeftTail is less than the given significance level,
55                            the null hypothesis is rejected.
56            RightTail   -   p-value for right-tailed test.
57                            If RightTail is less than the given significance level
58                            the null hypothesis is rejected.
59
60          -- ALGLIB --
61             Copyright 19.09.2006 by Bochkanov Sergey
62        *************************************************************************/
63        public static void ftest(ref double[] x,
64            int n,
65            ref double[] y,
66            int m,
67            ref double bothtails,
68            ref double lefttail,
69            ref double righttail)
70        {
71            int i = 0;
72            double xmean = 0;
73            double ymean = 0;
74            double xvar = 0;
75            double yvar = 0;
76            double p = 0;
77            int df1 = 0;
78            int df2 = 0;
79            double stat = 0;
80
81            if( n<=2 | m<=2 )
82            {
83                bothtails = 1.0;
84                lefttail = 1.0;
85                righttail = 1.0;
86                return;
87            }
88           
89            //
90            // Mean
91            //
92            xmean = 0;
93            for(i=0; i<=n-1; i++)
94            {
95                xmean = xmean+x[i];
96            }
97            xmean = xmean/n;
98            ymean = 0;
99            for(i=0; i<=m-1; i++)
100            {
101                ymean = ymean+y[i];
102            }
103            ymean = ymean/m;
104           
105            //
106            // Variance (using corrected two-pass algorithm)
107            //
108            xvar = 0;
109            for(i=0; i<=n-1; i++)
110            {
111                xvar = xvar+AP.Math.Sqr(x[i]-xmean);
112            }
113            xvar = xvar/(n-1);
114            yvar = 0;
115            for(i=0; i<=m-1; i++)
116            {
117                yvar = yvar+AP.Math.Sqr(y[i]-ymean);
118            }
119            yvar = yvar/(m-1);
120            if( (double)(xvar)==(double)(0) | (double)(yvar)==(double)(0) )
121            {
122                bothtails = 1.0;
123                lefttail = 1.0;
124                righttail = 1.0;
125                return;
126            }
127           
128            //
129            // Statistic
130            //
131            df1 = n-1;
132            df2 = m-1;
133            stat = Math.Min(xvar/yvar, yvar/xvar);
134            bothtails = 1-(fdistr.fdistribution(df1, df2, 1/stat)-fdistr.fdistribution(df1, df2, stat));
135            lefttail = fdistr.fdistribution(df1, df2, xvar/yvar);
136            righttail = 1-lefttail;
137        }
138
139
140        /*************************************************************************
141        One-sample chi-square test
142
143        This test checks three hypotheses about the dispersion of the given sample
144        The following tests are performed:
145            * two-tailed test (null hypothesis - the dispersion equals  the  given
146              number)
147            * left-tailed test (null hypothesis - the dispersion is  greater  than
148              or equal to the given number)
149            * right-tailed test (null hypothesis  -  dispersion is  less  than  or
150              equal to the given number).
151
152        Test is based on the following assumptions:
153            * the given sample has a normal distribution.
154
155        Input parameters:
156            X           -   sample 1. Array whose index goes from 0 to N-1.
157            N           -   size of the sample.
158            Variance    -   dispersion value to compare with.
159
160        Output parameters:
161            BothTails   -   p-value for two-tailed test.
162                            If BothTails is less than the given significance level
163                            the null hypothesis is rejected.
164            LeftTail    -   p-value for left-tailed test.
165                            If LeftTail is less than the given significance level,
166                            the null hypothesis is rejected.
167            RightTail   -   p-value for right-tailed test.
168                            If RightTail is less than the given significance level
169                            the null hypothesis is rejected.
170
171          -- ALGLIB --
172             Copyright 19.09.2006 by Bochkanov Sergey
173        *************************************************************************/
174        public static void onesamplevariancetest(ref double[] x,
175            int n,
176            double variance,
177            ref double bothtails,
178            ref double lefttail,
179            ref double righttail)
180        {
181            int i = 0;
182            double xmean = 0;
183            double ymean = 0;
184            double xvar = 0;
185            double yvar = 0;
186            double p = 0;
187            double s = 0;
188            double stat = 0;
189
190            if( n<=1 )
191            {
192                bothtails = 1.0;
193                lefttail = 1.0;
194                righttail = 1.0;
195                return;
196            }
197           
198            //
199            // Mean
200            //
201            xmean = 0;
202            for(i=0; i<=n-1; i++)
203            {
204                xmean = xmean+x[i];
205            }
206            xmean = xmean/n;
207           
208            //
209            // Variance
210            //
211            xvar = 0;
212            for(i=0; i<=n-1; i++)
213            {
214                xvar = xvar+AP.Math.Sqr(x[i]-xmean);
215            }
216            xvar = xvar/(n-1);
217            if( (double)(xvar)==(double)(0) )
218            {
219                bothtails = 1.0;
220                lefttail = 1.0;
221                righttail = 1.0;
222                return;
223            }
224           
225            //
226            // Statistic
227            //
228            stat = (n-1)*xvar/variance;
229            s = chisquaredistr.chisquaredistribution(n-1, stat);
230            bothtails = 2*Math.Min(s, 1-s);
231            lefttail = s;
232            righttail = 1-lefttail;
233        }
234    }
235}
Note: See TracBrowser for help on using the repository browser.