annotate libs/commons-math-2.1/docs/userguide/stat.html @ 10:5f2c5fb36e93

commons-math-2.1 added
author dwinter
date Tue, 04 Jan 2011 10:00:53 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
2
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
3
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
4
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
5
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
6
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
7
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
8
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
9
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
10
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
11
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
12
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
13 <html xmlns="http://www.w3.org/1999/xhtml">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
14 <head>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
15 <title>Math - The Commons Math User Guide - Statistics</title>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
16 <style type="text/css" media="all">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
17 @import url("../css/maven-base.css");
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
18 @import url("../css/maven-theme.css");
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
19 @import url("../css/site.css");
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
20 </style>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
21 <link rel="stylesheet" href="../css/print.css" type="text/css" media="print" />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
22 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
23 </head>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
24 <body class="composite">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
25 <div id="banner">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
26 <span id="bannerLeft">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
27
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
28 Commons Math User Guide
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
29
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
30 </span>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
31 <div class="clear">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
32 <hr/>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
33 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
34 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
35 <div id="breadcrumbs">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
36
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
37
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
38
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
39
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
40
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
41
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
42
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
43
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
44 <div class="xright">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
45
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
46
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
47
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
48
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
49
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
50
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
51
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
52 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
53 <div class="clear">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
54 <hr/>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
55 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
56 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
57 <div id="leftColumn">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
58 <div id="navcolumn">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
59
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
60
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
61
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
62
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
63
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
64
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
65
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
66
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
67 <h5>User Guide</h5>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
68 <ul>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
69
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
70 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
71 <a href="../userguide/index.html">Contents</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
72 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
73
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
74 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
75 <a href="../userguide/overview.html">Overview</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
76 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
77
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
78 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
79 <strong>Statistics</strong>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
80 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
81
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
82 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
83 <a href="../userguide/random.html">Data Generation</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
84 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
85
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
86 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
87 <a href="../userguide/linear.html">Linear Algebra</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
88 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
89
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
90 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
91 <a href="../userguide/analysis.html">Numerical Analysis</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
92 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
93
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
94 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
95 <a href="../userguide/special.html">Special Functions</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
96 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
97
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
98 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
99 <a href="../userguide/utilities.html">Utilities</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
100 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
101
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
102 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
103 <a href="../userguide/complex.html">Complex Numbers</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
104 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
105
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
106 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
107 <a href="../userguide/distribution.html">Distributions</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
108 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
109
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
110 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
111 <a href="../userguide/fraction.html">Fractions</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
112 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
113
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
114 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
115 <a href="../userguide/transform.html">Transform Methods</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
116 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
117
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
118 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
119 <a href="../userguide/geometry.html">3D Geometry</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
120 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
121
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
122 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
123 <a href="../userguide/optimization.html">Optimization</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
124 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
125
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
126 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
127 <a href="../userguide/ode.html">Ordinary Differential Equations</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
128 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
129
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
130 <li class="none">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
131 <a href="../userguide/genetics.html">Genetic Algorithms</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
132 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
133 </ul>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
134 <a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
135 <img alt="Built by Maven" src="../images/logos/maven-feather.png"></img>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
136 </a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
137
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
138
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
139
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
140
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
141
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
142
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
143
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
144
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
145 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
146 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
147 <div id="bodyColumn">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
148 <div id="contentBox">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
149 <div class="section"><h2><a name="a1_Statistics"></a>1 Statistics</h2>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
150 <div class="section"><h3><a name="a1.1_Overview"></a>1.1 Overview</h3>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
151 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
152 The statistics package provides frameworks and implementations for
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
153 basic Descriptive statistics, frequency distributions, bivariate regression,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
154 and t-, chi-square and ANOVA test statistics.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
155 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
156 <p><a href="#a1.2_Descriptive_statistics">Descriptive statistics</a><br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
157 </br><a href="#a1.3_Frequency_distributions">Frequency distributions</a><br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
158 </br><a href="#a1.4_Simple_regression">Simple Regression</a><br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
159 </br><a href="#a1.5_Multiple_linear_regression">Multiple Regression</a><br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
160 </br><a href="#a1.6_Rank_transformations">Rank transformations</a><br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
161 </br><a href="#a1.7_Covariance_and_correlation">Covariance and correlation</a><br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
162 </br><a href="#a1.8_Statistical_tests">Statistical Tests</a><br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
163 </br></p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
164 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
165 <div class="section"><h3><a name="a1.2_Descriptive_statistics"></a>1.2 Descriptive statistics</h3>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
166 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
167 The stat package includes a framework and default implementations for
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
168 the following Descriptive statistics:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
169 <ul><li>arithmetic and geometric means</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
170 <li>variance and standard deviation</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
171 <li>sum, product, log sum, sum of squared values</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
172 <li>minimum, maximum, median, and percentiles</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
173 <li>skewness and kurtosis</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
174 <li>first, second, third and fourth moments</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
175 </ul>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
176 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
177 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
178 With the exception of percentiles and the median, all of these
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
179 statistics can be computed without maintaining the full list of input
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
180 data values in memory. The stat package provides interfaces and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
181 implementations that do not require value storage as well as
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
182 implementations that operate on arrays of stored values.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
183 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
184 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
185 The top level interface is
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
186 <a href="../apidocs/org/apache/commons/math/stat/descriptive/UnivariateStatistic.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
187 org.apache.commons.math.stat.descriptive.UnivariateStatistic.</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
188 This interface, implemented by all statistics, consists of
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
189 <code>evaluate()</code> methods that take double[] arrays as arguments
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
190 and return the value of the statistic. This interface is extended by
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
191 <a href="../apidocs/org/apache/commons/math/stat/descriptive/StorelessUnivariateStatistic.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
192 StorelessUnivariateStatistic</a>, which adds <code>increment(),</code><code>getResult()</code> and associated methods to support
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
193 &quot;storageless&quot; implementations that maintain counters, sums or other
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
194 state information as values are added using the <code>increment()</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
195 method.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
196 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
197 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
198 Abstract implementations of the top level interfaces are provided in
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
199 <a href="../apidocs/org/apache/commons/math/stat/descriptive/AbstractUnivariateStatistic.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
200 AbstractUnivariateStatistic</a> and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
201 <a href="../apidocs/org/apache/commons/math/stat/descriptive/AbstractStorelessUnivariateStatistic.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
202 AbstractStorelessUnivariateStatistic</a> respectively.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
203 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
204 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
205 Each statistic is implemented as a separate class, in one of the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
206 subpackages (moment, rank, summary) and each extends one of the abstract
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
207 classes above (depending on whether or not value storage is required to
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
208 compute the statistic). There are several ways to instantiate and use statistics.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
209 Statistics can be instantiated and used directly, but it is generally more convenient
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
210 (and efficient) to access them using the provided aggregates,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
211 <a href="../apidocs/org/apache/commons/math/stat/descriptive/DescriptiveStatistics.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
212 DescriptiveStatistics</a> and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
213 <a href="../apidocs/org/apache/commons/math/stat/descriptive/SummaryStatistics.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
214 SummaryStatistics.</a></p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
215 <p><code>DescriptiveStatistics</code> maintains the input data in memory
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
216 and has the capability of producing &quot;rolling&quot; statistics computed from a
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
217 &quot;window&quot; consisting of the most recently added values.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
218 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
219 <p><code>SummaryStatistics</code> does not store the input data values
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
220 in memory, so the statistics included in this aggregate are limited to those
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
221 that can be computed in one pass through the data without access to
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
222 the full array of values.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
223 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
224 <p><table class="bodyTable"><tr class="a"><th>Aggregate</th>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
225 <th>Statistics Included</th>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
226 <th>Values stored?</th>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
227 <th>&quot;Rolling&quot; capability?</th>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
228 </tr>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
229 <tr class="b"><td><a href="../apidocs/org/apache/commons/math/stat/descriptive/DescriptiveStatistics.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
230 DescriptiveStatistics</a></td>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
231 <td>min, max, mean, geometric mean, n,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
232 sum, sum of squares, standard deviation, variance, percentiles, skewness,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
233 kurtosis, median</td>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
234 <td>Yes</td>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
235 <td>Yes</td>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
236 </tr>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
237 <tr class="a"><td><a href="../apidocs/org/apache/commons/math/stat/descriptive/SummaryStatistics.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
238 SummaryStatistics</a></td>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
239 <td>min, max, mean, geometric mean, n,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
240 sum, sum of squares, standard deviation, variance</td>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
241 <td>No</td>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
242 <td>No</td>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
243 </tr>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
244 </table>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
245 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
246 <p><code>SummaryStatistics</code> can be aggregated using
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
247 <a href="../apidocs/org/apache/commons/math/stat/descriptive/AggregateSummaryStatistics.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
248 AggregateSummaryStatistics.</a> This class can be used to concurrently gather statistics for multiple
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
249 datasets as well as for a combined sample including all of the data.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
250 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
251 <p><code>MultivariateSummaryStatistics</code> is similar to <code>SummaryStatistics</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
252 but handles n-tuple values instead of scalar values. It can also compute the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
253 full covariance matrix for the input data.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
254 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
255 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
256 Neither <code>DescriptiveStatistics</code> nor <code>SummaryStatistics</code> is
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
257 thread-safe. <a href="../apidocs/org/apache/commons/math/stat/descriptive/SynchronizedDescriptiveStatistics.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
258 SynchronizedDescriptiveStatistics</a> and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
259 <a href="../apidocs/org/apache/commons/math/stat/descriptive/SynchronizedSummaryStatistics.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
260 SynchronizedSummaryStatistics</a>, respectively, provide thread-safe versions for applications that
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
261 require concurrent access to statistical aggregates by multiple threads.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
262 <a href="../apidocs/org/apache/commons/math/stat/descriptive/SynchronizedMultiVariateSummaryStatistics.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
263 SynchronizedMultivariateSummaryStatistics</a> provides threadsafe <code>MultivariateSummaryStatistics.</code></p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
264 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
265 There is also a utility class,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
266 <a href="../apidocs/org/apache/commons/math/stat/StatUtils.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
267 StatUtils</a>, that provides static methods for computing statistics
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
268 directly from double[] arrays.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
269 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
270 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
271 Here are some examples showing how to compute Descriptive statistics.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
272 <dl><dt>Compute summary statistics for a list of double values</dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
273 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
274 </br><dd>Using the <code>DescriptiveStatistics</code> aggregate
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
275 (values are stored in memory):
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
276 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
277 // Get a DescriptiveStatistics instance
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
278 DescriptiveStatistics stats = new DescriptiveStatistics();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
279
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
280 // Add the data from the array
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
281 for( int i = 0; i &lt; inputArray.length; i++) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
282 stats.addValue(inputArray[i]);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
283 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
284
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
285 // Compute some statistics
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
286 double mean = stats.getMean();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
287 double std = stats.getStandardDeviation();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
288 double median = stats.getMedian();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
289 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
290 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
291 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
292 <dd>Using the <code>SummaryStatistics</code> aggregate (values are
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
293 <strong>not</strong> stored in memory):
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
294 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
295 // Get a SummaryStatistics instance
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
296 SummaryStatistics stats = new SummaryStatistics();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
297
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
298 // Read data from an input stream,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
299 // adding values and updating sums, counters, etc.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
300 while (line != null) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
301 line = in.readLine();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
302 stats.addValue(Double.parseDouble(line.trim()));
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
303 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
304 in.close();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
305
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
306 // Compute the statistics
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
307 double mean = stats.getMean();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
308 double std = stats.getStandardDeviation();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
309 //double median = stats.getMedian(); &lt;-- NOT AVAILABLE
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
310 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
311 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
312 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
313 <dd>Using the <code>StatUtils</code> utility class:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
314 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
315 // Compute statistics directly from the array
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
316 // assume values is a double[] array
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
317 double mean = StatUtils.mean(values);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
318 double std = StatUtils.variance(values);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
319 double median = StatUtils.percentile(50);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
320
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
321 // Compute the mean of the first three values in the array
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
322 mean = StatUtils.mean(values, 0, 3);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
323 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
324 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
325 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
326 <dt>Maintain a &quot;rolling mean&quot; of the most recent 100 values from
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
327 an input stream</dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
328 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
329 </br><dd>Use a <code>DescriptiveStatistics</code> instance with
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
330 window size set to 100
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
331 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
332 // Create a DescriptiveStats instance and set the window size to 100
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
333 DescriptiveStatistics stats = new DescriptiveStatistics();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
334 stats.setWindowSize(100);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
335
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
336 // Read data from an input stream,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
337 // displaying the mean of the most recent 100 observations
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
338 // after every 100 observations
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
339 long nLines = 0;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
340 while (line != null) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
341 line = in.readLine();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
342 stats.addValue(Double.parseDouble(line.trim()));
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
343 if (nLines == 100) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
344 nLines = 0;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
345 System.out.println(stats.getMean());
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
346 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
347 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
348 in.close();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
349 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
350 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
351 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
352 <dt>Compute statistics in a thread-safe manner</dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
353 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
354 <dd>Use a <code>SynchronizedDescriptiveStatistics</code> instance
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
355 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
356 // Create a SynchronizedDescriptiveStatistics instance and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
357 // use as any other DescriptiveStatistics instance
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
358 DescriptiveStatistics stats = new SynchronizedDescriptiveStatistics();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
359 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
360 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
361 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
362 <dt>Compute statistics for multiple samples and overall statistics concurrently</dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
363 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
364 <dd>There are two ways to do this using <code>AggregateSummaryStatistics.</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
365 The first is to use an <code>AggregateSummaryStatistics</code> instance to accumulate
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
366 overall statistics contributed by <code>SummaryStatistics</code> instances created using
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
367 <a href="../apidocs/org/apache/commons/math/stat/descriptive/AggregateSummaryStatistics.html#createContributingStatistics()">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
368 AggregateSummaryStatistics.createContributingStatistics()</a>:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
369 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
370 // Create a AggregateSummaryStatistics instance to accumulate the overall statistics
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
371 // and AggregatingSummaryStatistics for the subsamples
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
372 AggregateSummaryStatistics aggregate = new AggregateSummaryStatistics();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
373 SummaryStatistics setOneStats = aggregate.createContributingStatistics();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
374 SummaryStatistics setTwoStats = aggregate.createContributingStatistics();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
375 // Add values to the subsample aggregates
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
376 setOneStats.addValue(2);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
377 setOneStats.addValue(3);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
378 setTwoStats.addValue(2);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
379 setTwoStats.addValue(4);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
380 ...
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
381 // Full sample data is reported by the aggregate
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
382 double totalSampleSum = aggregate.getSum();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
383 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
384 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
385
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
386 The above approach has the disadvantages that the <code>addValue</code> calls must be synchronized on the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
387 <code>SummaryStatistics</code> instance maintained by the aggregate and each value addition updates the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
388 aggregate as well as the subsample. For applications that can wait to do the aggregation until all values
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
389 have been added, a static
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
390 <a href="../apidocs/org/apache/commons/math/stat/descriptive/AggregateSummaryStatistics.html#aggregate(java.util.Collection)">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
391 aggregate</a> method is available, as shown in the following example.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
392 This method should be used when aggregation needs to be done across threads.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
393 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
394 // Create SummaryStatistics instances for the subsample data
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
395 SummaryStatistics setOneStats = new SummaryStatistics();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
396 SummaryStatistics setTwoStats = new SummaryStatistics();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
397 // Add values to the subsample SummaryStatistics instances
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
398 setOneStats.addValue(2);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
399 setOneStats.addValue(3);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
400 setTwoStats.addValue(2);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
401 setTwoStats.addValue(4);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
402 ...
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
403 // Aggregate the subsample statistics
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
404 Collection&lt;SummaryStatistics&gt; aggregate = new ArrayList&lt;SummaryStatistics&gt;();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
405 aggregate.add(setOneStats);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
406 aggregate.add(setTwoStats);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
407 StatisticalSummary aggregatedStats = AggregateSummaryStatistics.aggregate(aggregate);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
408
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
409 // Full sample data is reported by aggregatedStats
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
410 double totalSampleSum = aggregatedStats.getSum();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
411 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
412 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
413 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
414 </dl>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
415 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
416 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
417 <div class="section"><h3><a name="a1.3_Frequency_distributions"></a>1.3 Frequency distributions</h3>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
418 <p><a href="../apidocs/org/apache/commons/math/stat/Frequency.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
419 org.apache.commons.math.stat.descriptive.Frequency</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
420 provides a simple interface for maintaining counts and percentages of discrete
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
421 values.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
422 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
423 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
424 Strings, integers, longs and chars are all supported as value types,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
425 as well as instances of any class that implements <code>Comparable.</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
426 The ordering of values used in computing cumulative frequencies is by
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
427 default the <i>natural ordering,</i> but this can be overriden by supplying a
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
428 <code>Comparator</code> to the constructor. Adding values that are not
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
429 comparable to those that have already been added results in an
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
430 <code>IllegalArgumentException.</code></p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
431 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
432 Here are some examples.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
433 <dl><dt>Compute a frequency distribution based on integer values</dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
434 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
435 </br><dd>Mixing integers, longs, Integers and Longs:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
436 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
437 Frequency f = new Frequency();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
438 f.addValue(1);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
439 f.addValue(new Integer(1));
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
440 f.addValue(new Long(1));
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
441 f.addValue(2);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
442 f.addValue(new Integer(-1));
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
443 System.out.prinltn(f.getCount(1)); // displays 3
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
444 System.out.println(f.getCumPct(0)); // displays 0.2
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
445 System.out.println(f.getPct(new Integer(1))); // displays 0.6
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
446 System.out.println(f.getCumPct(-2)); // displays 0
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
447 System.out.println(f.getCumPct(10)); // displays 1
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
448 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
449 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
450 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
451 <dt>Count string frequencies</dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
452 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
453 </br><dd>Using case-sensitive comparison, alpha sort order (natural comparator):
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
454 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
455 Frequency f = new Frequency();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
456 f.addValue(&quot;one&quot;);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
457 f.addValue(&quot;One&quot;);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
458 f.addValue(&quot;oNe&quot;);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
459 f.addValue(&quot;Z&quot;);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
460 System.out.println(f.getCount(&quot;one&quot;)); // displays 1
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
461 System.out.println(f.getCumPct(&quot;Z&quot;)); // displays 0.5
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
462 System.out.println(f.getCumPct(&quot;Ot&quot;)); // displays 0.25
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
463 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
464 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
465 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
466 <dd>Using case-insensitive comparator:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
467 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
468 Frequency f = new Frequency(String.CASE_INSENSITIVE_ORDER);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
469 f.addValue(&quot;one&quot;);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
470 f.addValue(&quot;One&quot;);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
471 f.addValue(&quot;oNe&quot;);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
472 f.addValue(&quot;Z&quot;);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
473 System.out.println(f.getCount(&quot;one&quot;)); // displays 3
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
474 System.out.println(f.getCumPct(&quot;z&quot;)); // displays 1
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
475 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
476 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
477 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
478 </dl>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
479 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
480 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
481 <div class="section"><h3><a name="a1.4_Simple_regression"></a>1.4 Simple regression</h3>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
482 <p><a href="../apidocs/org/apache/commons/math/stat/regression/SimpleRegression.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
483 org.apache.commons.math.stat.regression.SimpleRegression</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
484 provides ordinary least squares regression with one independent variable,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
485 estimating the linear model:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
486 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
487 <p><code> y = intercept + slope * x </code></p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
488 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
489 Standard errors for <code>intercept</code> and <code>slope</code> are
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
490 available as well as ANOVA, r-square and Pearson's r statistics.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
491 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
492 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
493 Observations (x,y pairs) can be added to the model one at a time or they
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
494 can be provided in a 2-dimensional array. The observations are not stored
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
495 in memory, so there is no limit to the number of observations that can be
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
496 added to the model.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
497 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
498 <p><strong>Usage Notes</strong>: <ul><li> When there are fewer than two observations in the model, or when
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
499 there is no variation in the x values (i.e. all x values are the same)
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
500 all statistics return <code>NaN</code>. At least two observations with
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
501 different x coordinates are requred to estimate a bivariate regression
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
502 model.</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
503 <li> getters for the statistics always compute values based on the current
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
504 set of observations -- i.e., you can get statistics, then add more data
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
505 and get updated statistics without using a new instance. There is no
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
506 &quot;compute&quot; method that updates all statistics. Each of the getters performs
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
507 the necessary computations to return the requested statistic.</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
508 </ul>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
509 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
510 <p><strong>Implementation Notes</strong>: <ul><li> As observations are added to the model, the sum of x values, y values,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
511 cross products (x times y), and squared deviations of x and y from their
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
512 respective means are updated using updating formulas defined in
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
513 &quot;Algorithms for Computing the Sample Variance: Analysis and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
514 Recommendations&quot;, Chan, T.F., Golub, G.H., and LeVeque, R.J.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
515 1983, American Statistician, vol. 37, pp. 242-247, referenced in
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
516 Weisberg, S. &quot;Applied Linear Regression&quot;. 2nd Ed. 1985. All regression
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
517 statistics are computed from these sums.</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
518 <li> Inference statistics (confidence intervals, parameter significance levels)
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
519 are based on on the assumption that the observations included in the model are
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
520 drawn from a <a href="http://mathworld.wolfram.com/BivariateNormalDistribution.html" class="externalLink">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
521 Bivariate Normal Distribution</a></li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
522 </ul>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
523 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
524 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
525 Here are some examples.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
526 <dl><dt>Estimate a model based on observations added one at a time</dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
527 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
528 </br><dd>Instantiate a regression instance and add data points
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
529 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
530 regression = new SimpleRegression();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
531 regression.addData(1d, 2d);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
532 // At this point, with only one observation,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
533 // all regression statistics will return NaN
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
534
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
535 regression.addData(3d, 3d);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
536 // With only two observations,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
537 // slope and intercept can be computed
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
538 // but inference statistics will return NaN
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
539
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
540 regression.addData(3d, 3d);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
541 // Now all statistics are defined.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
542 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
543 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
544 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
545 <dd>Compute some statistics based on observations added so far
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
546 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
547 System.out.println(regression.getIntercept());
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
548 // displays intercept of regression line
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
549
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
550 System.out.println(regression.getSlope());
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
551 // displays slope of regression line
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
552
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
553 System.out.println(regression.getSlopeStdErr());
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
554 // displays slope standard error
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
555 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
556 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
557 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
558 <dd>Use the regression model to predict the y value for a new x value
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
559 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
560 System.out.println(regression.predict(1.5d)
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
561 // displays predicted y value for x = 1.5
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
562 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
563 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
564
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
565 More data points can be added and subsequent getXxx calls will incorporate
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
566 additional data in statistics.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
567 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
568 <dt>Estimate a model from a double[][] array of data points</dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
569 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
570 </br><dd>Instantiate a regression object and load dataset
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
571 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
572 double[][] data = { { 1, 3 }, {2, 5 }, {3, 7 }, {4, 14 }, {5, 11 }};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
573 SimpleRegression regression = new SimpleRegression();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
574 regression.addData(data);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
575 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
576 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
577 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
578 <dd>Estimate regression model based on data
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
579 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
580 System.out.println(regression.getIntercept());
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
581 // displays intercept of regression line
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
582
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
583 System.out.println(regression.getSlope());
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
584 // displays slope of regression line
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
585
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
586 System.out.println(regression.getSlopeStdErr());
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
587 // displays slope standard error
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
588 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
589 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
590
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
591 More data points -- even another double[][] array -- can be added and subsequent
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
592 getXxx calls will incorporate additional data in statistics.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
593 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
594 </dl>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
595 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
596 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
597 <div class="section"><h3><a name="a1.5_Multiple_linear_regression"></a>1.5 Multiple linear regression</h3>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
598 <p><a href="../apidocs/org/apache/commons/math/stat/regression/MultipleLinearRegression.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
599 org.apache.commons.math.stat.regression.MultipleLinearRegression</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
600 provides ordinary least squares regression with a generic multiple variable linear model, which
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
601 in matrix notation can be expressed as:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
602 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
603 <p><code> y=X*b+u </code></p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
604 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
605 where y is an <code>n-vector</code><b>regressand</b>, X is a <code>[n,k]</code> matrix whose <code>k</code> columns are called
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
606 <b>regressors</b>, b is <code>k-vector</code> of <b>regression parameters</b> and <code>u</code> is an <code>n-vector</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
607 of <b>error terms</b> or <b>residuals</b>. The notation is quite standard in literature,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
608 cf eg <a href="http://www.econ.queensu.ca/ETM" class="externalLink">Davidson and MacKinnon, Econometrics Theory and Methods, 2004</a>.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
609 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
610 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
611 Two implementations are provided: <a href="../apidocs/org/apache/commons/math/stat/regression/OLSMultipleLinearRegression.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
612 org.apache.commons.math.stat.regression.OLSMultipleLinearRegression</a> and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
613 <a href="../apidocs/org/apache/commons/math/stat/regression/GLSMultipleLinearRegression.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
614 org.apache.commons.math.stat.regression.GLSMultipleLinearRegression</a></p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
615 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
616 Observations (x,y and covariance data matrices) can be added to the model via the <code>addData(double[] y, double[][] x, double[][] covariance)</code> method.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
617 The observations are stored in memory until the next time the addData method is invoked.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
618 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
619 <p><strong>Usage Notes</strong>: <ul><li> Data is validated when invoking the <code>addData(double[] y, double[][] x, double[][] covariance)</code> method and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
620 <code>IllegalArgumentException</code> is thrown when inappropriate.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
621 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
622 <li> Only the GLS regressions require the covariance matrix, so in the OLS regression it is ignored and can be safely
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
623 inputted as <code>null</code>.</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
624 </ul>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
625 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
626 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
627 Here are some examples.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
628 <dl><dt>OLS regression</dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
629 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
630 </br><dd>Instantiate an OLS regression object and load dataset
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
631 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
632 MultipleLinearRegression regression = new OLSMultipleLinearRegression();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
633 double[] y = new double[]{11.0, 12.0, 13.0, 14.0, 15.0, 16.0};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
634 double[] x = new double[6][];
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
635 x[0] = new double[]{1.0, 0, 0, 0, 0, 0};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
636 x[1] = new double[]{1.0, 2.0, 0, 0, 0, 0};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
637 x[2] = new double[]{1.0, 0, 3.0, 0, 0, 0};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
638 x[3] = new double[]{1.0, 0, 0, 4.0, 0, 0};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
639 x[4] = new double[]{1.0, 0, 0, 0, 5.0, 0};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
640 x[5] = new double[]{1.0, 0, 0, 0, 0, 6.0};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
641 regression.addData(y, x, null); // we don't need covariance
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
642 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
643 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
644 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
645 <dd>Estimate of regression values honours the <code>MultipleLinearRegression</code> interface:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
646 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
647 double[] beta = regression.estimateRegressionParameters();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
648
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
649 double[] residuals = regression.estimateResiduals();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
650
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
651 double[][] parametersVariance = regression.estimateRegressionParametersVariance();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
652
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
653 double regressandVariance = regression.estimateRegressandVariance();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
654 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
655 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
656 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
657 <dt>GLS regression</dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
658 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
659 </br><dd>Instantiate an GLS regression object and load dataset
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
660 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
661 MultipleLinearRegression regression = new GLSMultipleLinearRegression();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
662 double[] y = new double[]{11.0, 12.0, 13.0, 14.0, 15.0, 16.0};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
663 double[] x = new double[6][];
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
664 x[0] = new double[]{1.0, 0, 0, 0, 0, 0};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
665 x[1] = new double[]{1.0, 2.0, 0, 0, 0, 0};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
666 x[2] = new double[]{1.0, 0, 3.0, 0, 0, 0};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
667 x[3] = new double[]{1.0, 0, 0, 4.0, 0, 0};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
668 x[4] = new double[]{1.0, 0, 0, 0, 5.0, 0};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
669 x[5] = new double[]{1.0, 0, 0, 0, 0, 6.0};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
670 double[][] omega = new double[6][];
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
671 omega[0] = new double[]{1.1, 0, 0, 0, 0, 0};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
672 omega[1] = new double[]{0, 2.2, 0, 0, 0, 0};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
673 omega[2] = new double[]{0, 0, 3.3, 0, 0, 0};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
674 omega[3] = new double[]{0, 0, 0, 4.4, 0, 0};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
675 omega[4] = new double[]{0, 0, 0, 0, 5.5, 0};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
676 omega[5] = new double[]{0, 0, 0, 0, 0, 6.6};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
677 regression.addData(y, x, omega); // we do need covariance
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
678 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
679 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
680 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
681 <dd>Estimate of regression values honours the same <code>MultipleLinearRegression</code> interface as
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
682 the OLS regression.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
683 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
684 </dl>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
685 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
686 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
687 <div class="section"><h3><a name="a1.6_Rank_transformations"></a>1.6 Rank transformations</h3>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
688 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
689 Some statistical algorithms require that input data be replaced by ranks.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
690 The <a href="../apidocs/org/apache/commons/math/stat/ranking/package-summary.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
691 org.apache.commons.math.stat.ranking</a> package provides rank transformation.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
692 <a href="../apidocs/org/apache/commons/math/stat/ranking/RankingAlgorithm.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
693 RankingAlgorithm</a> defines the interface for ranking.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
694 <a href="../apidocs/org/apache/commons/math/stat/ranking/NaturalRanking.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
695 NaturalRanking</a> provides an implementation that has two configuration options.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
696 <ul><li><a href="../apidocs/org/apache/commons/math/stat/ranking/TiesStrategy.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
697 Ties strategy</a> deterimines how ties in the source data are handled by the ranking</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
698 <li><a href="../apidocs/org/apache/commons/math/stat/ranking/NaNStrategy.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
699 NaN strategy</a> determines how NaN values in the source data are handled.</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
700 </ul>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
701 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
702 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
703 Examples:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
704 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
705 NaturalRanking ranking = new NaturalRanking(NaNStrategy.MINIMAL,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
706 TiesStrategy.MAXIMUM);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
707 double[] data = { 20, 17, 30, 42.3, 17, 50,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
708 Double.NaN, Double.NEGATIVE_INFINITY, 17 };
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
709 double[] ranks = ranking.rank(exampleData);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
710 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
711 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
712
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
713 results in <code>ranks</code> containing <code>{6, 5, 7, 8, 5, 9, 2, 2, 5}.</code><div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
714 new NaturalRanking(NaNStrategy.REMOVED,TiesStrategy.SEQUENTIAL).rank(exampleData);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
715 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
716 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
717
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
718 returns <code>{5, 2, 6, 7, 3, 8, 1, 4}.</code></p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
719 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
720 The default <code>NaNStrategy</code> is NaNStrategy.MAXIMAL. This makes <code>NaN</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
721 values larger than any other value (including <code>Double.POSITIVE_INFINITY</code>). The
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
722 default <code>TiesStrategy</code> is <code>TiesStrategy.AVERAGE,</code> which assigns tied
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
723 values the average of the ranks applicable to the sequence of ties. See the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
724 <a href="../apidocs/org/apache/commons/math/stat/ranking/NaturalRanking.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
725 NaturalRanking</a> for more examples and <a href="../apidocs/org/apache/commons/math/stat/ranking/TiesStrategy.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
726 TiesStrategy</a> and <a href="../apidocs/org/apache/commons/math/stat/ranking/NaNStrategy.html">NaNStrategy</a>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
727 for details on these configuration options.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
728 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
729 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
730 <div class="section"><h3><a name="a1.7_Covariance_and_correlation"></a>1.7 Covariance and correlation</h3>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
731 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
732 The <a href="../apidocs/org/apache/commons/math/stat/correlation/package-summary.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
733 org.apache.commons.math.stat.correlation</a> package computes covariances
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
734 and correlations for pairs of arrays or columns of a matrix.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
735 <a href="../apidocs/org/apache/commons/math/stat/correlation/Covariance.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
736 Covariance</a> computes covariances,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
737 <a href="../apidocs/org/apache/commons/math/stat/correlation/PearsonsCorrelation.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
738 PearsonsCorrelation</a> provides Pearson's Product-Moment correlation coefficients and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
739 <a href="../apidocs/org/apache/commons/math/stat/correlation/SpearmansCorrelation.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
740 SpearmansCorrelation</a> computes Spearman's rank correlation.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
741 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
742 <p><strong>Implementation Notes</strong><ul><li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
743 Unbiased covariances are given by the formula <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
744 </br><code>cov(X, Y) = sum [(x<sub>i</sub> - E(X))(y<sub>i</sub> - E(Y))] / (n - 1)</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
745 where <code>E(X)</code> is the mean of <code>X</code> and <code>E(Y)</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
746 is the mean of the <code>Y</code> values. Non-bias-corrected estimates use
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
747 <code>n</code> in place of <code>n - 1.</code> Whether or not covariances are
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
748 bias-corrected is determined by the optional parameter, &quot;biasCorrected,&quot; which
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
749 defaults to <code>true.</code></li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
750 <li><a href="../apidocs/org/apache/commons/math/stat/correlation/PearsonsCorrelation.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
751 PearsonsCorrelation</a> computes correlations defined by the formula <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
752 </br><code>cor(X, Y) = sum[(x<sub>i</sub> - E(X))(y<sub>i</sub> - E(Y))] / [(n - 1)s(X)s(Y)]</code><br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
753
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
754 where <code>E(X)</code> and <code>E(Y)</code> are means of <code>X</code> and <code>Y</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
755 and <code>s(X)</code>, <code>s(Y)</code> are standard deviations.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
756 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
757 <li><a href="../apidocs/org/apache/commons/math/stat/correlation/SpearmansCorrelation.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
758 SpearmansCorrelation</a> applies a rank transformation to the input data and computes Pearson's
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
759 correlation on the ranked data. The ranking algorithm is configurable. By default,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
760 <a href="../apidocs/org/apache/commons/math/stat/ranking/NaturalRanking.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
761 NaturalRanking</a> with default strategies for handling ties and NaN values is used.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
762 </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
763 </ul>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
764 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
765 <p><strong>Examples:</strong><dl><dt><strong>Covariance of 2 arrays</strong></dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
766 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
767 </br><dd>To compute the unbiased covariance between 2 double arrays,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
768 <code>x</code> and <code>y</code>, use:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
769 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
770 new Covariance().covariance(x, y)
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
771 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
772 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
773
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
774 For non-bias-corrected covariances, use
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
775 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
776 covariance(x, y, false)
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
777 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
778 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
779 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
780 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
781 </br><dt><strong>Covariance matrix</strong></dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
782 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
783 </br><dd> A covariance matrix over the columns of a source matrix <code>data</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
784 can be computed using
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
785 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
786 new Covariance().computeCovarianceMatrix(data)
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
787 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
788 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
789
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
790 The i-jth entry of the returned matrix is the unbiased covariance of the ith and jth
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
791 columns of <code>data.</code> As above, to get non-bias-corrected covariances,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
792 use
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
793 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
794 computeCovarianceMatrix(data, false)
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
795 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
796 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
797 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
798 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
799 </br><dt><strong>Pearson's correlation of 2 arrays</strong></dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
800 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
801 </br><dd>To compute the Pearson's product-moment correlation between two double arrays
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
802 <code>x</code> and <code>y</code>, use:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
803 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
804 new PearsonsCorrelation().correlation(x, y)
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
805 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
806 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
807 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
808 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
809 </br><dt><strong>Pearson's correlation matrix</strong></dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
810 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
811 </br><dd> A (Pearson's) correlation matrix over the columns of a source matrix <code>data</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
812 can be computed using
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
813 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
814 new PearsonsCorrelation().computeCorrelationMatrix(data)
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
815 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
816 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
817
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
818 The i-jth entry of the returned matrix is the Pearson's product-moment correlation between the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
819 ith and jth columns of <code>data.</code></dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
820 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
821 </br><dt><strong>Pearson's correlation significance and standard errors</strong></dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
822 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
823 </br><dd> To compute standard errors and/or significances of correlation coefficients
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
824 associated with Pearson's correlation coefficients, start by creating a
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
825 <code>PearsonsCorrelation</code> instance
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
826 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
827 PearsonsCorrelation correlation = new PearsonsCorrelation(data);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
828 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
829 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
830
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
831 where <code>data</code> is either a rectangular array or a <code>RealMatrix.</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
832 Then the matrix of standard errors is
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
833 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
834 correlation.getCorrelationStandardErrors();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
835 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
836 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
837
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
838 The formula used to compute the standard error is <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
839 <code>SE<sub>r</sub> = ((1 - r<sup>2</sup>) / (n - 2))<sup>1/2</sup></code><br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
840
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
841 where <code>r</code> is the estimated correlation coefficient and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
842 <code>n</code> is the number of observations in the source dataset.<br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
843 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
844 <strong>p-values</strong> for the (2-sided) null hypotheses that elements of
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
845 a correlation matrix are zero populate the RealMatrix returned by
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
846 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
847 correlation.getCorrelationPValues()
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
848 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
849 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
850 <code>getCorrelationPValues().getEntry(i,j)</code> is the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
851 probability that a random variable distributed as <code>t<sub>n-2</sub></code> takes
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
852 a value with absolute value greater than or equal to <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
853 </br><code>|r<sub>ij</sub>|((n - 2) / (1 - r<sub>ij</sub><sup>2</sup>))<sup>1/2</sup></code>,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
854 where <code>r<sub>ij</sub></code> is the estimated correlation between the ith and jth
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
855 columns of the source array or RealMatrix. This is sometimes referred to as the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
856 <i>significance</i> of the coefficient.<br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
857 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
858
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
859 For example, if <code>data</code> is a RealMatrix with 2 columns and 10 rows, then
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
860 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
861 new PearsonsCorrelation(data).getCorrelationPValues().getEntry(0,1)
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
862 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
863 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
864
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
865 is the significance of the Pearson's correlation coefficient between the two columns
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
866 of <code>data</code>. If this value is less than .01, we can say that the correlation
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
867 between the two columns of data is significant at the 99% level.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
868 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
869 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
870 </br><dt><strong>Spearman's rank correlation coefficient</strong></dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
871 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
872 </br><dd>To compute the Spearman's rank-moment correlation between two double arrays
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
873 <code>x</code> and <code>y</code>:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
874 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
875 new SpearmansCorrelation().correlation(x, y)
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
876 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
877 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
878
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
879 This is equivalent to
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
880 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
881 RankingAlgorithm ranking = new NaturalRanking();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
882 new PearsonsCorrelation().correlation(ranking.rank(x), ranking.rank(y))
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
883 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
884 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
885 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
886 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
887 </br></dl>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
888 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
889 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
890 <div class="section"><h3><a name="a1.8_Statistical_tests"></a>1.8 Statistical tests</h3>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
891 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
892 The interfaces and implementations in the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
893 <a href="../apidocs/org/apache/commons/math/stat/inference/">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
894 org.apache.commons.math.stat.inference</a> package provide
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
895 <a href="http://www.itl.nist.gov/div898/handbook/prc/section2/prc22.htm" class="externalLink">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
896 Student's t</a>,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
897 <a href="http://www.itl.nist.gov/div898/handbook/eda/section3/eda35f.htm" class="externalLink">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
898 Chi-Square</a> and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
899 <a href="http://www.itl.nist.gov/div898/handbook/prc/section4/prc43.htm" class="externalLink">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
900 One-Way ANOVA</a> test statistics as well as
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
901 <a href="http://www.cas.lancs.ac.uk/glossary_v1.1/hyptest.html#pvalue" class="externalLink">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
902 p-values</a> associated with <code>t-</code>,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
903 <code>Chi-Square</code> and <code>One-Way ANOVA</code> tests. The
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
904 interfaces are
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
905 <a href="../apidocs/org/apache/commons/math/stat/inference/TTest.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
906 TTest</a>,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
907 <a href="../apidocs/org/apache/commons/math/stat/inference/ChiSquareTest.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
908 ChiSquareTest</a>, and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
909 <a href="../apidocs/org/apache/commons/math/stat/inference/OneWayAnova.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
910 OneWayAnova</a> with provided implementations
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
911 <a href="../apidocs/org/apache/commons/math/stat/inference/TTestImpl.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
912 TTestImpl</a>,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
913 <a href="../apidocs/org/apache/commons/math/stat/inference/ChiSquareTestImpl.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
914 ChiSquareTestImpl</a> and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
915 <a href="../apidocs/org/apache/commons/math/stat/inference/OneWayAnovaImpl.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
916 OneWayAnovaImpl</a>, respectively.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
917 The
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
918 <a href="../apidocs/org/apache/commons/math/stat/inference/TestUtils.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
919 TestUtils</a> class provides static methods to get test instances or
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
920 to compute test statistics directly. The examples below all use the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
921 static methods in <code>TestUtils</code> to execute tests. To get
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
922 test object instances, either use e.g.,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
923 <code>TestUtils.getTTest()</code> or use the implementation constructors
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
924 directly, e.g.,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
925 <code>new TTestImpl()</code>.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
926 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
927 <p><strong>Implementation Notes</strong><ul><li>Both one- and two-sample t-tests are supported. Two sample tests
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
928 can be either paired or unpaired and the unpaired two-sample tests can
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
929 be conducted under the assumption of equal subpopulation variances or
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
930 without this assumption. When equal variances is assumed, a pooled
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
931 variance estimate is used to compute the t-statistic and the degrees
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
932 of freedom used in the t-test equals the sum of the sample sizes minus 2.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
933 When equal variances is not assumed, the t-statistic uses both sample
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
934 variances and the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
935 <a href="http://www.itl.nist.gov/div898/handbook/prc/section3/gifs/nu3.gif" class="externalLink">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
936 Welch-Satterwaite approximation</a> is used to compute the degrees
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
937 of freedom. Methods to return t-statistics and p-values are provided in each
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
938 case, as well as boolean-valued methods to perform fixed significance
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
939 level tests. The names of methods or methods that assume equal
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
940 subpopulation variances always start with &quot;homoscedastic.&quot; Test or
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
941 test-statistic methods that just start with &quot;t&quot; do not assume equal
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
942 variances. See the examples below and the API documentation for
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
943 more details.</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
944 <li>The validity of the p-values returned by the t-test depends on the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
945 assumptions of the parametric t-test procedure, as discussed
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
946 <a href="http://www.basic.nwu.edu/statguidefiles/ttest_unpaired_ass_viol.html" class="externalLink">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
947 here</a></li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
948 <li>p-values returned by t-, chi-square and Anova tests are exact, based
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
949 on numerical approximations to the t-, chi-square and F distributions in the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
950 <code>distributions</code> package. </li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
951 <li>p-values returned by t-tests are for two-sided tests and the boolean-valued
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
952 methods supporting fixed significance level tests assume that the hypotheses
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
953 are two-sided. One sided tests can be performed by dividing returned p-values
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
954 (resp. critical values) by 2.</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
955 <li>Degrees of freedom for chi-square tests are integral values, based on the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
956 number of observed or expected counts (number of observed counts - 1)
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
957 for the goodness-of-fit tests and (number of columns -1) * (number of rows - 1)
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
958 for independence tests.</li>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
959 </ul>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
960 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
961 <p><strong>Examples:</strong><dl><dt><strong>One-sample <code>t</code> tests</strong></dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
962 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
963 </br><dd>To compare the mean of a double[] array to a fixed value:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
964 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
965 double[] observed = {1d, 2d, 3d};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
966 double mu = 2.5d;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
967 System.out.println(TestUtils.t(mu, observed));
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
968 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
969 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
970
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
971 The code above will display the t-statisitic associated with a one-sample
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
972 t-test comparing the mean of the <code>observed</code> values against
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
973 <code>mu.</code></dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
974 <dd>To compare the mean of a dataset described by a
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
975 <a href="../apidocs/org/apache/commons/math/stat/descriptive/StatisticalSummary.html">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
976 org.apache.commons.math.stat.descriptive.StatisticalSummary</a> to a fixed value:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
977 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
978 double[] observed ={1d, 2d, 3d};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
979 double mu = 2.5d;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
980 SummaryStatistics sampleStats = new SummaryStatistics();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
981 for (int i = 0; i &lt; observed.length; i++) {
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
982 sampleStats.addValue(observed[i]);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
983 }
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
984 System.out.println(TestUtils.t(mu, observed));
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
985 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
986 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
987 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
988 <dd>To compute the p-value associated with the null hypothesis that the mean
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
989 of a set of values equals a point estimate, against the two-sided alternative that
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
990 the mean is different from the target value:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
991 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
992 double[] observed = {1d, 2d, 3d};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
993 double mu = 2.5d;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
994 System.out.println(TestUtils.tTest(mu, observed));
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
995 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
996 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
997
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
998 The snippet above will display the p-value associated with the null
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
999 hypothesis that the mean of the population from which the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1000 <code>observed</code> values are drawn equals <code>mu.</code></dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1001 <dd>To perform the test using a fixed significance level, use:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1002 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1003 TestUtils.tTest(mu, observed, alpha);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1004 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1005 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1006
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1007 where <code>0 &lt; alpha &lt; 0.5</code> is the significance level of
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1008 the test. The boolean value returned will be <code>true</code> iff the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1009 null hypothesis can be rejected with confidence <code>1 - alpha</code>.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1010 To test, for example at the 95% level of confidence, use
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1011 <code>alpha = 0.05</code></dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1012 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1013 </br><dt><strong>Two-Sample t-tests</strong></dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1014 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1015 </br><dd><strong>Example 1:</strong> Paired test evaluating
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1016 the null hypothesis that the mean difference between corresponding
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1017 (paired) elements of the <code>double[]</code> arrays
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1018 <code>sample1</code> and <code>sample2</code> is zero.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1019
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1020 To compute the t-statistic:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1021 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1022 TestUtils.pairedT(sample1, sample2);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1023 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1024 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1025 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1026 To compute the p-value:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1027 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1028 TestUtils.pairedTTest(sample1, sample2);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1029 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1030 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1031 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1032 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1033 To perform a fixed significance level test with alpha = .05:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1034 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1035 TestUtils.pairedTTest(sample1, sample2, .05);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1036 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1037 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1038 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1039
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1040 The last example will return <code>true</code> iff the p-value
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1041 returned by <code>TestUtils.pairedTTest(sample1, sample2)</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1042 is less than <code>.05</code></dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1043 <dd><strong>Example 2: </strong> unpaired, two-sided, two-sample t-test using
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1044 <code>StatisticalSummary</code> instances, without assuming that
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1045 subpopulation variances are equal.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1046
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1047 First create the <code>StatisticalSummary</code> instances. Both
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1048 <code>DescriptiveStatistics</code> and <code>SummaryStatistics</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1049 implement this interface. Assume that <code>summary1</code> and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1050 <code>summary2</code> are <code>SummaryStatistics</code> instances,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1051 each of which has had at least 2 values added to the (virtual) dataset that
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1052 it describes. The sample sizes do not have to be the same -- all that is required
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1053 is that both samples have at least 2 elements.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1054 <p><strong>Note:</strong> The <code>SummaryStatistics</code> class does
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1055 not store the dataset that it describes in memory, but it does compute all
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1056 statistics necessary to perform t-tests, so this method can be used to
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1057 conduct t-tests with very large samples. One-sample tests can also be
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1058 performed this way.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1059 (See <a href="#1.2 Descriptive statistics">Descriptive statistics</a> for details
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1060 on the <code>SummaryStatistics</code> class.)
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1061 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1062 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1063 To compute the t-statistic:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1064 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1065 TestUtils.t(summary1, summary2);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1066 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1067 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1068 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1069 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1070 To compute the p-value:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1071 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1072 TestUtils.tTest(sample1, sample2);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1073 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1074 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1075 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1076 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1077 To perform a fixed significance level test with alpha = .05:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1078 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1079 TestUtils.tTest(sample1, sample2, .05);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1080 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1081 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1082 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1083 <p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1084 In each case above, the test does not assume that the subpopulation
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1085 variances are equal. To perform the tests under this assumption,
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1086 replace &quot;t&quot; at the beginning of the method name with &quot;homoscedasticT&quot;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1087 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1088 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1089 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1090 </br><dt><strong>Chi-square tests</strong></dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1091 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1092 </br><dd>To compute a chi-square statistic measuring the agreement between a
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1093 <code>long[]</code> array of observed counts and a <code>double[]</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1094 array of expected counts, use:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1095 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1096 long[] observed = {10, 9, 11};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1097 double[] expected = {10.1, 9.8, 10.3};
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1098 System.out.println(TestUtils.chiSquare(expected, observed));
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1099 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1100 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1101
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1102 the value displayed will be
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1103 <code>sum((expected[i] - observed[i])^2 / expected[i])</code></dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1104 <dd> To get the p-value associated with the null hypothesis that
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1105 <code>observed</code> conforms to <code>expected</code> use:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1106 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1107 TestUtils.chiSquareTest(expected, observed);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1108 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1109 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1110 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1111 <dd> To test the null hypothesis that <code>observed</code> conforms to
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1112 <code>expected</code> with <code>alpha</code> siginficance level
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1113 (equiv. <code>100 * (1-alpha)%</code> confidence) where <code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1114 0 &lt; alpha &lt; 1 </code> use:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1115 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1116 TestUtils.chiSquareTest(expected, observed, alpha);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1117 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1118 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1119
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1120 The boolean value returned will be <code>true</code> iff the null hypothesis
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1121 can be rejected with confidence <code>1 - alpha</code>.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1122 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1123 <dd>To compute a chi-square statistic statistic associated with a
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1124 <a href="http://www.itl.nist.gov/div898/handbook/prc/section4/prc45.htm" class="externalLink">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1125 chi-square test of independence</a> based on a two-dimensional (long[][])
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1126 <code>counts</code> array viewed as a two-way table, use:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1127 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1128 TestUtils.chiSquareTest(counts);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1129 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1130 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1131
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1132 The rows of the 2-way table are
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1133 <code>count[0], ... , count[count.length - 1]. </code><br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1134 </br>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1135 The chi-square statistic returned is
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1136 <code>sum((counts[i][j] - expected[i][j])^2/expected[i][j])</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1137 where the sum is taken over all table entries and
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1138 <code>expected[i][j]</code> is the product of the row and column sums at
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1139 row <code>i</code>, column <code>j</code> divided by the total count.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1140 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1141 <dd>To compute the p-value associated with the null hypothesis that
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1142 the classifications represented by the counts in the columns of the input 2-way
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1143 table are independent of the rows, use:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1144 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1145 TestUtils.chiSquareTest(counts);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1146 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1147 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1148 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1149 <dd>To perform a chi-square test of independence with <code>alpha</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1150 siginficance level (equiv. <code>100 * (1-alpha)%</code> confidence)
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1151 where <code>0 &lt; alpha &lt; 1 </code> use:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1152 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1153 TestUtils.chiSquareTest(counts, alpha);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1154 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1155 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1156
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1157 The boolean value returned will be <code>true</code> iff the null
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1158 hypothesis can be rejected with confidence <code>1 - alpha</code>.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1159 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1160 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1161 </br><dt><strong>One-Way Anova tests</strong></dt>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1162 <br />
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1163 </br><dd>To conduct a One-Way Analysis of Variance (ANOVA) to evaluate the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1164 null hypothesis that the means of a collection of univariate datasets
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1165 are the same, start by loading the datasets into a collection, e.g.
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1166 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1167 double[] classA =
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1168 {93.0, 103.0, 95.0, 101.0, 91.0, 105.0, 96.0, 94.0, 101.0 };
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1169 double[] classB =
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1170 {99.0, 92.0, 102.0, 100.0, 102.0, 89.0 };
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1171 double[] classC =
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1172 {110.0, 115.0, 111.0, 117.0, 128.0, 117.0 };
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1173 List classes = new ArrayList();
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1174 classes.add(classA);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1175 classes.add(classB);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1176 classes.add(classC);
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1177 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1178 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1179
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1180 Then you can compute ANOVA F- or p-values associated with the
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1181 null hypothesis that the class means are all the same
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1182 using a <code>OneWayAnova</code> instance or <code>TestUtils</code>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1183 methods:
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1184 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1185 double fStatistic = TestUtils.oneWayAnovaFValue(classes); // F-value
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1186 double pValue = TestUtils.oneWayAnovaPValue(classes); // P-value
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1187 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1188 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1189
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1190 To test perform a One-Way Anova test with signficance level set at 0.01
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1191 (so the test will, assuming assumptions are met, reject the null
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1192 hypothesis incorrectly only about one in 100 times), use
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1193 <div class="source"><pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1194 TestUtils.oneWayAnovaTest(classes, 0.01); // returns a boolean
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1195 // true means reject null hypothesis
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1196 </pre>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1197 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1198 </dd>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1199 </dl>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1200 </p>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1201 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1202 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1203
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1204 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1205 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1206 <div class="clear">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1207 <hr/>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1208 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1209 <div id="footer">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1210 <div class="xright">&#169;
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1211 2003-2010
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1212
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1213
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1214
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1215
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1216
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1217
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1218
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1219
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1220
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1221 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1222 <div class="clear">
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1223 <hr/>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1224 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1225 </div>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1226 </body>
5f2c5fb36e93 commons-math-2.1 added
dwinter
parents:
diff changeset
1227 </html>