StatisticsTest.h 9.14 KB
Newer Older
1
2
3
#ifndef STATISTICSTEST_H_
#define STATISTICSTEST_H_

4
#include "MantidKernel/Statistics.h"
5
#include <cxxtest/TestSuite.h>
6
#include <algorithm>
7
#include <cmath>
8
9
10
11
12
13
14
#include <vector>
#include <string>

using namespace Mantid::Kernel;
using std::string;
using std::vector;

15
class StatisticsTest : public CxxTest::TestSuite {
16
public:
17
  void test_Doubles_And_Default_Flags_Calculates_All_Stats() {
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    vector<double> data;
    data.push_back(17.2);
    data.push_back(18.1);
    data.push_back(16.5);
    data.push_back(18.3);
    data.push_back(12.6);

    Statistics stats = getStatistics(data);

    TS_ASSERT_EQUALS(stats.mean, 16.54);
    TS_ASSERT_DELTA(stats.standard_deviation, 2.0732, 0.0001);
    TS_ASSERT_EQUALS(stats.minimum, 12.6);
    TS_ASSERT_EQUALS(stats.maximum, 18.3);
    TS_ASSERT_EQUALS(stats.median, 17.2);
32
33
  }

34
  void test_Doubles_With_Sorted_Data() {
35
36
37
38
39
40
41
42
    vector<double> data;
    data.push_back(17.2);
    data.push_back(18.1);
    data.push_back(16.5);
    data.push_back(18.3);
    data.push_back(12.6);
    sort(data.begin(), data.end());

43
44
    Statistics stats =
        getStatistics(data, (StatOptions::Median | StatOptions::SortedData));
45

46
47
48
49
    TS_ASSERT(std::isnan(stats.mean));
    TS_ASSERT(std::isnan(stats.standard_deviation));
    TS_ASSERT(std::isnan(stats.minimum));
    TS_ASSERT(std::isnan(stats.maximum));
50
51
52
    TS_ASSERT_EQUALS(stats.median, 17.2);
  }

53
54
  void
  test_Unsorted_Data_With_Sorted_Flag_Gives_Expected_Incorrect_Result_For_Median() {
55
56
57
58
59
60
61
    vector<double> data;
    data.push_back(17.2);
    data.push_back(18.1);
    data.push_back(16.5);
    data.push_back(18.3);
    data.push_back(12.6);

62
63
    Statistics stats =
        getStatistics(data, (StatOptions::Median | StatOptions::SortedData));
64

65
66
67
68
    TS_ASSERT(std::isnan(stats.mean));
    TS_ASSERT(std::isnan(stats.standard_deviation));
    TS_ASSERT(std::isnan(stats.minimum));
    TS_ASSERT(std::isnan(stats.maximum));
69
70
    TS_ASSERT_EQUALS(stats.median, 16.5);
  }
71
72

  void test_Doubles_With_Corrected_StdDev_Calculates_Mean() {
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    vector<double> data;
    data.push_back(17.2);
    data.push_back(18.1);
    data.push_back(16.5);
    data.push_back(18.3);
    data.push_back(12.6);
    sort(data.begin(), data.end());

    Statistics stats = getStatistics(data, StatOptions::CorrectedStdDev);

    TS_ASSERT_EQUALS(stats.mean, 16.54);
    TS_ASSERT_DELTA(stats.standard_deviation, 2.3179, 0.0001);
    TS_ASSERT_EQUALS(stats.minimum, 12.6);
    TS_ASSERT_EQUALS(stats.maximum, 18.3);
87
    TS_ASSERT(std::isnan(stats.median));
88
89
90
91
92
93
94
95
96
  }

  void test_Types_Can_Be_Disabled_With_Flags() {
    vector<double> data;
    data.push_back(17.2);
    data.push_back(18.1);
    data.push_back(16.5);
    data.push_back(18.3);
    data.push_back(12.6);
Lynch, Vickie's avatar
Lynch, Vickie committed
97

98
99
    Statistics justMean = getStatistics(data, StatOptions::Mean);
    TS_ASSERT_EQUALS(justMean.mean, 16.54);
100
101
102
103
    TS_ASSERT(std::isnan(justMean.standard_deviation));
    TS_ASSERT(std::isnan(justMean.minimum));
    TS_ASSERT(std::isnan(justMean.maximum));
    TS_ASSERT(std::isnan(justMean.median));
Lynch, Vickie's avatar
Lynch, Vickie committed
104
  }
105
106

  void testZscores() {
Lynch, Vickie's avatar
Lynch, Vickie committed
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
    vector<double> data;
    data.push_back(12);
    data.push_back(13);
    data.push_back(9);
    data.push_back(18);
    data.push_back(7);
    data.push_back(9);
    data.push_back(14);
    data.push_back(16);
    data.push_back(10);
    data.push_back(12);
    data.push_back(7);
    data.push_back(13);
    data.push_back(14);
    data.push_back(19);
    data.push_back(10);
    data.push_back(16);
    data.push_back(12);
    data.push_back(16);
    data.push_back(19);
    data.push_back(11);

    std::vector<double> Zscore = getZscore(data);
    TS_ASSERT_DELTA(Zscore[4], 1.6397, 0.0001);
    TS_ASSERT_DELTA(Zscore[6], 0.3223, 0.0001);
    std::vector<double> ZModscore = getModifiedZscore(data);
    TS_ASSERT_DELTA(ZModscore[4], 1.2365, 0.0001);
    TS_ASSERT_DELTA(ZModscore[6], 0.3372, 0.0001);
135
136
  }

137
  void testDoubleSingle() {
138
139
140
141
142
143
144
145
146
147
148
149
    vector<double> data;
    data.push_back(42.);

    Statistics stats = getStatistics(data);

    TS_ASSERT_EQUALS(stats.mean, 42.);
    TS_ASSERT_EQUALS(stats.standard_deviation, 0.);
    TS_ASSERT_EQUALS(stats.minimum, 42.);
    TS_ASSERT_EQUALS(stats.maximum, 42.);
    TS_ASSERT_EQUALS(stats.median, 42.);
  }

150
  void testInt32Even() {
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    vector<int32_t> data;
    data.push_back(1);
    data.push_back(2);
    data.push_back(3);
    data.push_back(4);
    data.push_back(5);
    data.push_back(6);

    Statistics stats = getStatistics(data);

    TS_ASSERT_EQUALS(stats.mean, 3.5);
    TS_ASSERT_DELTA(stats.standard_deviation, 1.7078, 0.0001);
    TS_ASSERT_EQUALS(stats.minimum, 1.);
    TS_ASSERT_EQUALS(stats.maximum, 6.);
    TS_ASSERT_EQUALS(stats.median, 3.5);
  }

168
  bool my_isnan(const double number) { return number != number; }
169

170
  void testString() {
171
    vector<string> data{"hi there"};
172
173
174
175
176
177
178
179
180

    Statistics stats = getStatistics(data);

    TS_ASSERT(my_isnan(stats.mean));
    TS_ASSERT(my_isnan(stats.standard_deviation));
    TS_ASSERT(my_isnan(stats.minimum));
    TS_ASSERT(my_isnan(stats.maximum));
    TS_ASSERT(my_isnan(stats.median));
  }
181
182
183

  /** Test function to calculate Rwp
    */
184
  void testRwp() {
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
    vector<double> obsY(4);
    vector<double> calY(4);
    vector<double> obsE(4);

    obsY[0] = 1.0;
    calY[0] = 1.1;
    obsE[0] = 1.0;

    obsY[1] = 2.0;
    calY[1] = 2.1;
    obsE[1] = 1.2;

    obsY[2] = 3.0;
    calY[2] = 3.5;
    obsE[2] = 1.4;

    obsY[3] = 1.0;
    calY[3] = 1.3;
    obsE[3] = 1.0;

205
    Rfactor rfactor = getRFactor(obsY, calY, obsE);
206

207
    TS_ASSERT_DELTA(rfactor.Rwp, 0.1582, 0.0001);
208
209
210
211
  }

  /** Test throw exception
    */
212
  void testRwpException1() {
213
214
215
    vector<double> obsY{1.0, 2.0, 3.0, 1.0};
    vector<double> calY{1.1, 2.1, 3.5, 1.3};
    vector<double> obsE{1.0, 1.2, 1.4};
216
217

    TS_ASSERT_THROWS_ANYTHING(getRFactor(obsY, calY, obsE));
218
  }
219
220
221

  /** Test throw exception on empty array
    */
222
  void testRwpException2() {
223
224
225
226
227
228
229
    vector<double> obsY;
    vector<double> calY;
    vector<double> obsE;

    TS_ASSERT_THROWS_ANYTHING(getRFactor(obsY, calY, obsE));
  }

230
  /// Test moment calculations about origin and mean
231
  void test_getMoments() {
232
233
234
235
236
237
238
    const double mean = 5.;
    const double sigma = 4.;
    const double deltaX = .2;
    const size_t numX = 200;
    // calculate to have same number of points left and right of function
    const double offsetX = mean - (.5 * deltaX * static_cast<double>(numX));
    // variance about origin
239
    double expVar = mean * mean + sigma * sigma;
240
    // skew about origin
241
    double expSkew = mean * mean * mean + 3. * mean * sigma * sigma;
242
243
244
245
246
247
248
249
250
251
252
253

    // x-values to try out
    vector<double> x;
    for (size_t i = 0; i < numX; ++i)
      x.push_back(static_cast<double>(i) * deltaX + offsetX);

    // just declare so we can have test of exception handling
    vector<double> y;

    TS_ASSERT_THROWS(getMomentsAboutOrigin(x, y), std::out_of_range);

    // now calculate the y-values
254
255
256
    for (size_t i = 0; i < numX; ++i) {
      double temp = (x[i] - mean) / sigma;
      y.push_back(exp(-.5 * temp * temp) / (sigma * sqrt(2. * M_PI)));
257
258
259
260
    }

    // Normal distribution values are taken from the wikipedia page
    {
261
      std::cout << "Normal distribution about origin\n";
262
263
264
265
      vector<double> aboutOrigin = getMomentsAboutOrigin(x, y);
      TS_ASSERT_EQUALS(aboutOrigin.size(), 4);
      TS_ASSERT_DELTA(aboutOrigin[0], 1., .0001);
      TS_ASSERT_DELTA(aboutOrigin[1], mean, .0001);
266
267
      TS_ASSERT_DELTA(aboutOrigin[2], expVar, .001 * expVar);
      TS_ASSERT_DELTA(aboutOrigin[3], expSkew, .001 * expSkew);
268

269
      std::cout << "Normal distribution about mean\n";
270
271
272
273
      vector<double> aboutMean = getMomentsAboutMean(x, y);
      TS_ASSERT_EQUALS(aboutMean.size(), 4);
      TS_ASSERT_DELTA(aboutMean[0], 1., .0001);
      TS_ASSERT_DELTA(aboutMean[1], 0., .0001);
274
275
      TS_ASSERT_DELTA(aboutMean[2], sigma * sigma, .001 * expVar);
      TS_ASSERT_DELTA(aboutMean[3], 0., .0001 * expSkew);
276
277
278
279
    }

    // Now a gaussian function as a histogram
    y.clear();
280
281
    for (size_t i = 0; i < numX - 1;
         ++i) // one less y than x makes it a histogram
282
    {
283
284
285
286
287
      double templeft = (x[i] - mean) / sigma;
      templeft = exp(-.5 * templeft * templeft) / (sigma * sqrt(2. * M_PI));
      double tempright = (x[i + 1] - mean) / sigma;
      tempright = exp(-.5 * tempright * tempright) / (sigma * sqrt(2. * M_PI));
      y.push_back(.5 * deltaX * (templeft + tempright));
288
      //      std::cout << i << ":\t" << x[i] << "\t" << y[i] << '\n';
289
290
291
292
    }

    // Normal distribution values are taken from the wikipedia page
    {
293
      std::cout << "Normal distribution about origin\n";
294
295
296
297
      vector<double> aboutOrigin = getMomentsAboutOrigin(x, y);
      TS_ASSERT_EQUALS(aboutOrigin.size(), 4);
      TS_ASSERT_DELTA(aboutOrigin[0], 1., .0001);
      TS_ASSERT_DELTA(aboutOrigin[1], mean, .0001);
298
299
      TS_ASSERT_DELTA(aboutOrigin[2], expVar, .001 * expVar);
      TS_ASSERT_DELTA(aboutOrigin[3], expSkew, .001 * expSkew);
300

301
      std::cout << "Normal distribution about mean\n";
302
303
304
305
      vector<double> aboutMean = getMomentsAboutMean(x, y);
      TS_ASSERT_EQUALS(aboutMean.size(), 4);
      TS_ASSERT_DELTA(aboutMean[0], 1., .0001);
      TS_ASSERT_DELTA(aboutMean[1], 0., .0001);
306
307
      TS_ASSERT_DELTA(aboutMean[2], sigma * sigma, .001 * expVar);
      TS_ASSERT_DELTA(aboutMean[3], 0., .0001 * expSkew);
308
309
    }
  }
310
311
312
};

#endif // STATISTICSTEST_H_