File: StatisticsUtilities.cxx

package info (click to toggle)
caret 5.6.4~dfsg.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 31,904 kB
  • ctags: 28,901
  • sloc: cpp: 378,050; python: 6,718; ansic: 5,507; makefile: 333; sh: 46
file content (294 lines) | stat: -rw-r--r-- 9,274 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*LICENSE_START*/
/*
 *  Copyright 1995-2002 Washington University School of Medicine
 *
 *  http://brainmap.wustl.edu
 *
 *  This file is part of CARET.
 *
 *  CARET is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  CARET is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with CARET; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
/*LICENSE_END*/

/*****************************************************************************
   Major portions of this software are copyrighted by the Medical College
   of Wisconsin, 1994-2000, and are released under the Gnu General Public
   License, Version 2.  See the file README.Copyright for details.
   
   The tStatisticToPValue method and related code is from AFNI
******************************************************************************/
#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <set>

#include "DebugControl.h"
#include "StatisticsUtilities.h"
#include "ValueIndexSort.h"

/**
 * Compute statistics for a set of values.
 * If "dataIsSampleFlag" is true, the data is assumed to be a sample where the true
 * mean is unknown and so the deviation is computed by dividing by "N - 1".  If 
 * "dataIsSampleFlag" is false, then the data is assumed to be the total population
 * so that the true mean is known and the deviation is computed by dividing by "N".
 */
void
StatisticsUtilities::computeStatistics(const std::vector<float>& valuesIn,
                                       const bool dataIsSampleFlag,
                                       DescriptiveStatistics& statisticsOut)
{
   statisticsOut.reset();
   
   const int numValues = static_cast<int>(valuesIn.size());
   if (numValues == 0) {
      return;
   }
   
   //
   // Sort input values
   //
   std::vector<float> valuesSorted = valuesIn;
   std::sort(valuesSorted.begin(), valuesSorted.end());
   
   //
   // Compute statistics for all values
   //
   computeStatisticsHelper(valuesSorted, false, dataIsSampleFlag, statisticsOut);
      
   //
   // Throw out top and bottom 2%
   //
   const int percent2  = std::min(static_cast<int>(valuesSorted.size() * 0.02), numValues);
   const int percent98 = std::min(static_cast<int>(valuesSorted.size() * 0.98), numValues);
   for (int i = percent2; i < percent98; i++) {
      statisticsOut.values96Percent.push_back(valuesSorted[i]);
   }
   
   //
   // Compute statistics with the top and bottom 2% excluded
   //
   computeStatisticsHelper(statisticsOut.values96Percent, true,
                           dataIsSampleFlag, statisticsOut);
}

/**
 * compute statistics helper
 */
void
StatisticsUtilities::computeStatisticsHelper(const std::vector<float>& values,
                                       const bool do96Percent,
                                       const bool dataIsSampleFlag,
                                       DescriptiveStatistics& statisticsOut)
{
   const int num = static_cast<int>(values.size());
   if (num == 0) {
      return;
   }
   
   //
   // Compute average and averages of absolute values.
   //
   float smallNumber = 0.0000000001;
   double sum = 0.0;
   double absSum = 0.0;
   double mostPositiveValue  = 0;
   double leastPositiveValue = std::numeric_limits<float>::max();
   double mostNegativeValue  = 0;
   double leastNegativeValue = -std::numeric_limits<float>::max();
   bool hadPositiveValues = false;
   bool hadNegativeValues = false;
   std::vector<float> valuesForAbsMedian;
   for (int i = 0; i < num; i++) {
      float v = values[i];
      sum += v;
      
      if (v > smallNumber) {
         hadPositiveValues = true;
         if (v > mostPositiveValue) {
            mostPositiveValue = v;
         }
         if (v < leastPositiveValue) {
            leastPositiveValue = v;
         }
      }
      else if (v < -smallNumber) {
         hadNegativeValues = true;
         if (v < mostNegativeValue) {
            mostNegativeValue = v;
         }
         if (v > leastNegativeValue) {
            leastNegativeValue = v;
         }
      }
      
      if (v < 0.0) v = -v;
      absSum += v;
      valuesForAbsMedian.push_back(v);
   }   
   const double numValues = static_cast<double>(num);
   const float average = sum / static_cast<double>(numValues);
   const float absAverage = absSum / static_cast<double>(numValues);
   
   
   if (hadPositiveValues == false) {
      mostPositiveValue  = 0.0;
      leastPositiveValue = 0.0;
   }
   if (hadNegativeValues == false) {
      mostNegativeValue  = 0.0;
      leastNegativeValue = 0.0;
   }
   
   //
   // Compute Standard Deviation and standard deviation of absolute values.
   //
   sum = 0.0;
   absSum = 0.0;
   for (int i = 0; i < num; i++) {
      double v = values[i];
      double diff = v - average;
      sum += diff * diff;
      
      if (v < 0.0) v = -v;
      diff = v - absAverage;
      absSum += diff * diff;
   }
   
   double devNumValues = numValues;
   if (dataIsSampleFlag) {
      devNumValues = numValues - 1.0;
   }
   if (devNumValues <= 0.0) {
      devNumValues = 1.0;
   }
   
   const float standardDeviation = std::sqrt(sum / devNumValues);
   const float standardError     = standardDeviation / std::sqrt(numValues);
   const float absStandardDeviation = std::sqrt(absSum / devNumValues);
   const float absStandardError = absStandardDeviation / std::sqrt(numValues);
   
   const float minValue = values[0];
   const float maxValue = values[num-1];
   const float range = maxValue - minValue;
   
   const int medianIndex = num / 2;
   const float median = values[medianIndex];
   const float absMedian = valuesForAbsMedian[medianIndex];

   if (do96Percent) {
      statisticsOut.average96 = average;
      statisticsOut.absAverage96 = absAverage;
      statisticsOut.standardDeviation96 = standardDeviation;
      statisticsOut.standardError96 = standardError;
      statisticsOut.absStandardDeviation96 = absStandardDeviation;
      statisticsOut.absStandardError96 = absStandardError;
      
      statisticsOut.mostPositiveValue96 = mostPositiveValue;
      statisticsOut.leastPositiveValue96 = leastPositiveValue;
      statisticsOut.mostNegativeValue96 = mostNegativeValue;
      statisticsOut.leastNegativeValue96 = leastNegativeValue;
      
      statisticsOut.minValue96 = minValue;
      statisticsOut.maxValue96 = maxValue;
      statisticsOut.range96 = range;
      statisticsOut.median96  = median;
      statisticsOut.absMedian96 = absMedian;
   }
   else {
      statisticsOut.average = average;
      statisticsOut.absAverage = absAverage;
      statisticsOut.standardDeviation = standardDeviation;
      statisticsOut.standardError = standardError;
      statisticsOut.absStandardDeviation = absStandardDeviation;
      statisticsOut.absStandardError = absStandardError;
      
      statisticsOut.mostPositiveValue = mostPositiveValue;
      statisticsOut.leastPositiveValue = leastPositiveValue;
      statisticsOut.mostNegativeValue = mostNegativeValue;
      statisticsOut.leastNegativeValue = leastNegativeValue;
      
      statisticsOut.minValue = minValue;
      statisticsOut.maxValue = maxValue;
      statisticsOut.range = range;
      statisticsOut.median = median;
      statisticsOut.absMedian = absMedian;
   }
}

//****************************************************************************************
//
// DescriptiveStatistics methods
//
//****************************************************************************************

/**
 * Constructor.
 */
StatisticsUtilities::DescriptiveStatistics::DescriptiveStatistics()
{
   reset();
}

/**
 * Destructor.
 */
StatisticsUtilities::DescriptiveStatistics::~DescriptiveStatistics()
{
}

/**
 * reset all members.
 */
void 
StatisticsUtilities::DescriptiveStatistics::reset()
{
   average              = 0.0;
   standardDeviation    = 0.0;
   standardError        = 0.0;
   absAverage           = 0.0;
   absStandardDeviation = 0.0;
   absStandardError     = 0.0;
   minValue             = 0.0;
   maxValue             = 0.0;
   range                = 0.0;   
   mostPositiveValue    = 0.0;
   leastPositiveValue   = 0.0;
   mostNegativeValue    = 0.0;
   leastNegativeValue   = 0.0;
   median               = 0.0;
   absMedian            = 0.0;
   
   average96              = 0.0;
   standardDeviation96    = 0.0;
   standardError96        = 0.0;
   absAverage96           = 0.0;
   absStandardDeviation96 = 0.0;
   absStandardError96     = 0.0;
   minValue96             = 0.0;
   maxValue96             = 0.0;
   mostPositiveValue96    = 0.0;
   leastPositiveValue96   = 0.0;
   mostNegativeValue96    = 0.0;
   leastNegativeValue96   = 0.0;
   range96                = 0.0;   
   median96               = 0.0;
   absMedian96            = 0.0;
   
   values96Percent.clear();
}