Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ALGLIB/2.5.0/ALGLIB-2.5.0/variancetests.cs @ 4852

Last change on this file since 4852 was 3839, checked in by mkommend, 15 years ago

implemented first version of LR (ticket #1012)

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