1 | /*************************************************************************
|
---|
2 | Copyright (c) 2007, Sergey Bochkanov (ALGLIB project).
|
---|
3 |
|
---|
4 | >>> SOURCE LICENSE >>>
|
---|
5 | This program is free software; you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation (www.fsf.org); either version 2 of the
|
---|
8 | License, or (at your option) any later version.
|
---|
9 |
|
---|
10 | This program is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | GNU General Public License for more details.
|
---|
14 |
|
---|
15 | A copy of the GNU General Public License is available at
|
---|
16 | http://www.fsf.org/licensing/licenses
|
---|
17 |
|
---|
18 | >>> END OF LICENSE >>>
|
---|
19 | *************************************************************************/
|
---|
20 |
|
---|
21 | using System;
|
---|
22 |
|
---|
23 | namespace alglib
|
---|
24 | {
|
---|
25 | public class stest
|
---|
26 | {
|
---|
27 | /*************************************************************************
|
---|
28 | Sign test
|
---|
29 |
|
---|
30 | This test checks three hypotheses about the median of the given sample.
|
---|
31 | The following tests are performed:
|
---|
32 | * two-tailed test (null hypothesis - the median is equal to the given
|
---|
33 | value)
|
---|
34 | * left-tailed test (null hypothesis - the median is greater than or
|
---|
35 | equal to the given value)
|
---|
36 | * right-tailed test (null hypothesis - the median is less than or
|
---|
37 | equal to the given value)
|
---|
38 |
|
---|
39 | Requirements:
|
---|
40 | * the scale of measurement should be ordinal, interval or ratio (i.e.
|
---|
41 | the test could not be applied to nominal variables).
|
---|
42 |
|
---|
43 | The test is non-parametric and doesn't require distribution X to be normal
|
---|
44 |
|
---|
45 | Input parameters:
|
---|
46 | X - sample. Array whose index goes from 0 to N-1.
|
---|
47 | N - size of the sample.
|
---|
48 | Median - assumed median value.
|
---|
49 |
|
---|
50 | Output parameters:
|
---|
51 | BothTails - p-value for two-tailed test.
|
---|
52 | If BothTails is less than the given significance level
|
---|
53 | the null hypothesis is rejected.
|
---|
54 | LeftTail - p-value for left-tailed test.
|
---|
55 | If LeftTail is less than the given significance level,
|
---|
56 | the null hypothesis is rejected.
|
---|
57 | RightTail - p-value for right-tailed test.
|
---|
58 | If RightTail is less than the given significance level
|
---|
59 | the null hypothesis is rejected.
|
---|
60 |
|
---|
61 | While calculating p-values high-precision binomial distribution
|
---|
62 | approximation is used, so significance levels have about 15 exact digits.
|
---|
63 |
|
---|
64 | -- ALGLIB --
|
---|
65 | Copyright 08.09.2006 by Bochkanov Sergey
|
---|
66 | *************************************************************************/
|
---|
67 | public static void onesamplesigntest(ref double[] x,
|
---|
68 | int n,
|
---|
69 | double median,
|
---|
70 | ref double bothtails,
|
---|
71 | ref double lefttail,
|
---|
72 | ref double righttail)
|
---|
73 | {
|
---|
74 | int i = 0;
|
---|
75 | int gtcnt = 0;
|
---|
76 | int necnt = 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 | // Calculate:
|
---|
88 | // GTCnt - count of x[i]>Median
|
---|
89 | // NECnt - count of x[i]<>Median
|
---|
90 | //
|
---|
91 | gtcnt = 0;
|
---|
92 | necnt = 0;
|
---|
93 | for(i=0; i<=n-1; i++)
|
---|
94 | {
|
---|
95 | if( (double)(x[i])>(double)(median) )
|
---|
96 | {
|
---|
97 | gtcnt = gtcnt+1;
|
---|
98 | }
|
---|
99 | if( (double)(x[i])!=(double)(median) )
|
---|
100 | {
|
---|
101 | necnt = necnt+1;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | if( necnt==0 )
|
---|
105 | {
|
---|
106 |
|
---|
107 | //
|
---|
108 | // all x[i] are equal to Median.
|
---|
109 | // So we can conclude that Median is a true median :)
|
---|
110 | //
|
---|
111 | bothtails = 0.0;
|
---|
112 | lefttail = 0.0;
|
---|
113 | righttail = 0.0;
|
---|
114 | return;
|
---|
115 | }
|
---|
116 | bothtails = 2*binomialdistr.binomialdistribution(Math.Min(gtcnt, necnt-gtcnt), necnt, 0.5);
|
---|
117 | lefttail = binomialdistr.binomialdistribution(gtcnt, necnt, 0.5);
|
---|
118 | righttail = binomialdistr.binomialcdistribution(gtcnt-1, necnt, 0.5);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|