Statistics.cpp 14 KB
Newer Older
1
2
3
// Includes
#include "MantidKernel/Statistics.h"

4
5
6
#include <algorithm>
#include <functional>
#include <limits>
7
#include <cmath>
8
9
#include <numeric>
#include <string>
10
#include <stdexcept>
11
#include <iostream>
12
#include <sstream>
13
#include <cfloat>
14
15
16

namespace Mantid
{
Campbell, Stuart's avatar
Campbell, Stuart committed
17
  namespace Kernel
18
19
  {

Campbell, Stuart's avatar
Campbell, Stuart committed
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
    using std::string;
    using std::vector;

    /**
     * Generate a Statistics object where all of the values are NaN. This is a good initial default.
     */
    Statistics getNanStatistics()
    {
      double nan = std::numeric_limits<double>::quiet_NaN();

      Statistics stats;
      stats.minimum = nan;
      stats.maximum = nan;
      stats.mean = nan;
      stats.median = nan;
      stats.standard_deviation = nan;

      return stats;
    }

    /**
     * There are enough special cases in determining the median where it useful to
     * put it in a single function.
     */
    template<typename TYPE>
    double getMedian(const vector<TYPE>& data, const size_t num_data, const bool sorted)
    {
      if (num_data == 1)
        return static_cast<double> (*(data.begin()));

      bool is_even = ((num_data % 2) == 0);
      if (is_even)
      {
53
54
55
        double left = 0.0;
        double right = 0.0;

Campbell, Stuart's avatar
Campbell, Stuart committed
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
        if (sorted)
        {
          // Just get the centre two elements.
          left = static_cast<double> (*(data.begin() + num_data / 2 - 1));
          right = static_cast<double> (*(data.begin() + num_data / 2));
        }
        else
        {
          // If the data is not sorted, make a copy we can mess with
          vector<TYPE> temp(data.begin(), data.end());
          // Get what the centre two elements should be...
          std::nth_element(temp.begin(), temp.begin() + num_data / 2 - 1, temp.end());
          left = static_cast<double> (*(temp.begin() + num_data / 2 - 1));
          std::nth_element(temp.begin(), temp.begin() + num_data / 2, temp.end());
          right = static_cast<double> (*(temp.begin() + num_data / 2));
        }
        // return the average
        return (left + right) / 2.;
      }
      else
      // Odd number
      {
        if (sorted)
        {
          // If sorted and odd, just return the centre value
          return static_cast<double> (*(data.begin() + num_data / 2));
        }
        else
        {
          // If the data is not sorted, make a copy we can mess with
          vector<TYPE> temp(data.begin(), data.end());
          // Make sure the centre value is in the correct position
          std::nth_element(temp.begin(), temp.begin() + num_data / 2, temp.end());
          // Now return the centre value
          return static_cast<double> (*(temp.begin() + num_data / 2));
        }
      }
    }
94
95
96
97
98
99
100
101
102
103
104
105
106
    /**
     * There are enough special cases in determining the Z score where it useful to
     * put it in a single function.
     */
    template<typename TYPE>
    std::vector<double> getZscore(const vector<TYPE>& data, const bool sorted)
    {
      if (data.size() < 3)
      {
    	  std::vector<double>Zscore(data.size(),0.);
    	  return Zscore;
      }
      std::vector<double> Zscore;
107

108
      Statistics stats = getStatistics(data, sorted);
109
110
111
112
113
      if(stats.standard_deviation == 0.)
      {
    	  std::vector<double>Zscore(data.size(),0.);
    	  return Zscore;
      }
114
115
116
      typename vector<TYPE>::const_iterator it = data.begin();
      for (; it != data.end(); ++it)
      {
117
    	  double tmp = static_cast<double> (*it);
118
119
120
121
122
123
124
125
126
127
128
        Zscore.push_back(fabs((tmp - stats.mean) / stats.standard_deviation));
      }
      return Zscore;
    }
    /**
     * There are enough special cases in determining the modified Z score where it useful to
     * put it in a single function.
     */
    template<typename TYPE>
    std::vector<double> getModifiedZscore(const vector<TYPE>& data, const bool sorted)
    {
129
130
131
132
133
      if (data.size() < 3)
      {
    	  std::vector<double>Zscore(data.size(),0.);
    	  return Zscore;
      }
134
      std::vector<double>MADvec;
135
136
137
138
139
140
      double tmp;
      size_t num_data = data.size(); // cache since it is frequently used
      double median = getMedian(data, num_data, sorted);
      typename vector<TYPE>::const_iterator it = data.begin();
      for (; it != data.end(); ++it)
      {
141
        tmp = static_cast<double> (*it);
142
143
144
        MADvec.push_back(fabs(tmp - median));
      }
      double MAD = getMedian(MADvec, num_data, sorted);
145
146
      if(MAD == 0.)
      {
147
148
        std::vector<double>Zscore(data.size(),0.);
        return Zscore;
149
      }
150
      MADvec.clear();
151
      std::vector<double> Zscore;
152
153
154
      it = data.begin();
      for (; it != data.end(); ++it)
      {
155
        tmp = static_cast<double> (*it);
156
157
158
159
        Zscore.push_back(0.6745*fabs((tmp - median) / MAD));
      }
      return Zscore;
    }
Campbell, Stuart's avatar
Campbell, Stuart committed
160
161
162
163
164
165
166
167
168

    /**
     * Determine the statistics for a vector of data. If it is sorted then let the
     * function know so it won't make a copy of the data for determining the median.
     */
    template<typename TYPE>
    Statistics getStatistics(const vector<TYPE>& data, const bool sorted)
    {
      Statistics stats = getNanStatistics();
169
      size_t num_data = data.size(); // cache since it is frequently used
Campbell, Stuart's avatar
Campbell, Stuart committed
170
171
172
173
174
175
176

      if (num_data == 0)
      { // don't do anything
        return stats;
      }

      // calculate the mean
177
178
      const TYPE sum = std::accumulate(data.begin(), data.end(), static_cast<TYPE>(0), std::plus<TYPE>());
      stats.mean = static_cast<double>(sum)/(static_cast<double>(num_data));
Campbell, Stuart's avatar
Campbell, Stuart committed
179
180
181
182
183
184
185
186

      // calculate the standard deviation, min, max
      stats.minimum = stats.mean;
      stats.maximum = stats.mean;
      double stddev = 0.;
      typename vector<TYPE>::const_iterator it = data.begin();
      for (; it != data.end(); ++it)
      {
187
        double temp = static_cast<double> (*it);
Campbell, Stuart's avatar
Campbell, Stuart committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
        stddev += ((temp - stats.mean) * (temp - stats.mean));
        if (temp > stats.maximum)
          stats.maximum = temp;
        if (temp < stats.minimum)
          stats.minimum = temp;
      }
      stats.standard_deviation = sqrt(stddev / (static_cast<double> (num_data)));

      // calculate the median
      stats.median = getMedian(data, num_data, sorted);

      return stats;
    }

    /// Getting statistics of a string array should just give a bunch of NaNs
    template<>
    DLLExport Statistics getStatistics<string> (const vector<string>& data, const bool sorted)
    {
206
207
      UNUSED_ARG(sorted);
      UNUSED_ARG(data);
Campbell, Stuart's avatar
Campbell, Stuart committed
208
209
210
      return getNanStatistics();
    }

211
212
213
214
215
216
217
218
    /// Getting statistics of a boolean array should just give a bunch of NaNs
    template<>
    DLLExport Statistics getStatistics<bool> (const vector<bool>& data, const bool sorted)
    {
      UNUSED_ARG(sorted);
      UNUSED_ARG(data);
      return getNanStatistics();
    }
219

220
    /** Return the Rwp of a diffraction pattern data
221
222
223
      * @param obsI :: array of observed intensity values
      * @param calI :: array of calculated intensity values;
      * @param obsE :: array of error of the observed data;
224
      * @return :: RFactor including Rp and Rwp
225
226
      *
      */
227
    Rfactor getRFactor(const std::vector<double>& obsI, const std::vector<double>& calI, const std::vector<double>& obsE)
228
229
230
231
232
    {
      // 1. Check
      if (obsI.size() != calI.size() || obsI.size() != obsE.size())
      {
        std::stringstream errss;
233
234
235
        errss << "GetRFactor() Input Error!  Observed Intensity (" << obsI.size()
              << "), Calculated Intensity (" << calI.size() << ") and Observed Error ("
              << obsE.size() << ") have different number of elements.";
236
237
        throw std::runtime_error(errss.str());
      }
238
239
240
241
      if (obsI.size() == 0)
      {
        throw std::runtime_error("getRFactor(): the input arrays are empty.");
      }
242
243
244

      double sumnom = 0;
      double sumdenom = 0;
245
246
      double sumrpnom = 0;
      double sumrpdenom = 0;
247
248
249
250
251
252
253
254
255
256

      size_t numpts = obsI.size();
      for (size_t i = 0; i < numpts; ++i)
      {
        double cal_i = calI[i];
        double obs_i = obsI[i];
        double sigma = obsE[i];
        double weight = 1.0/(sigma*sigma);
        double diff = obs_i - cal_i;

257
258
259
260
261
262
263
264
265
266
267
        if (weight == weight && weight <= DBL_MAX)
        {
          // If weight is not NaN.
          sumrpnom += fabs(diff);
          sumrpdenom += fabs(obs_i);

          double tempnom = weight*diff*diff;
          double tempden = weight*obs_i*obs_i;

          sumnom += tempnom;
          sumdenom += tempden;
268

269
270
271
272
273
274
275
          if (tempnom != tempnom || tempden != tempden)
          {
            std::cout << "***** Error! ****** Data indexed " << i << " is NaN. "
                      << "i = " << i << ": cal = " << calI[i] << ", obs = " << obs_i
                      << ", weight = " << weight << ". \n";
          }
        }
276
277
      }

278
      Rfactor rfactor(0., 0.);
279
      rfactor.Rp = (sumrpnom/sumrpdenom);
280
      rfactor.Rwp = std::sqrt(sumnom/sumdenom);
281

282
283
284
      if (rfactor.Rwp != rfactor.Rwp)
        std::cout << "Rwp is NaN.  Denominator = " << sumnom << "; Nominator = " << sumdenom << ". \n";

285
      return rfactor;
286
287
    }

288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
    /**
     * This will calculate the first n-moments (inclusive) about the origin. For example
     * if maxMoment=2 then this will return 3 values: 0th (total weight), 1st (mean), 2nd (deviation).
     *
     * @param x The independent values
     * @param y The dependent values
     * @param maxMoment The number of moments to calculate
     * @returns The first n-moments.
     */
    template<typename TYPE>
    std::vector<double> getMomentsAboutOrigin(const std::vector<TYPE>& x, const std::vector<TYPE>& y, const int maxMoment)
    {
      // densities have the same number of x and y
      bool isDensity(x.size() == y.size());

      // if it isn't a density then check for histogram
      if ((!isDensity) && (x.size() != y.size()+1))
      {
        std::stringstream msg;
        msg << "length of x (" << x.size() << ") and y (" << y.size() << ")do not match";
        throw std::out_of_range(msg.str());
      }

      // initialize a result vector with all zeros
      std::vector<double> result(maxMoment+1, 0.);

314
315
316
317
      // cache the maximum index
      size_t numPoints = y.size();
      if (isDensity)
        numPoints = x.size()-1;
318
319

      // densities are calculated using Newton's method for numerical integration
320
321
      // as backwards as it sounds, the outer loop should be the points rather than the moments
      for (size_t j = 0; j < numPoints; ++j)
322
      {
323
324
325
326
327
        // reduce item lookup - and central x for histogram
        const double xVal = .5*static_cast<double>(x[j]+x[j+1]);
        // this variable will be (x^n)*y
        double temp = static_cast<double>(y[j]); // correct for histogram
        if (isDensity)
328
329
        {
          const double xDelta = static_cast<double>(x[j+1]-x[j]);
330
          temp = .5*(temp + static_cast<double>(y[j+1]))*xDelta;
331
        }
332
333
334
335

        // accumulate the moments
        result[0] += temp;
        for (size_t i = 1; i < result.size(); ++i)
336
        {
337
338
          temp *= xVal;
          result[i] += temp;
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
        }
      }

      return result;
    }

    /**
     * This will calculate the first n-moments (inclusive) about the mean (1st moment). For example
     * if maxMoment=2 then this will return 3 values: 0th (total weight), 1st (mean), 2nd (deviation).
     *
     * @param x The independent values
     * @param y The dependent values
     * @param maxMoment The number of moments to calculate
     * @returns The first n-moments.
     */
    template<typename TYPE>
    std::vector<double> getMomentsAboutMean(const std::vector<TYPE>& x, const std::vector<TYPE>& y, const int maxMoment)
    {
      // get the zeroth (integrated value) and first moment (mean)
      std::vector<double> momentsAboutOrigin = getMomentsAboutOrigin(x, y, 1);
      const double mean = momentsAboutOrigin[1];

      // initialize a result vector with all zeros
      std::vector<double> result(maxMoment+1, 0.);
      result[0] = momentsAboutOrigin[0];

      // escape early if we need to
      if (maxMoment == 0)
        return result;

369
370
371
372
373
374
375
      // densities have the same number of x and y
      bool isDensity(x.size() == y.size());

      // cache the maximum index
      size_t numPoints = y.size();
      if (isDensity)
        numPoints = x.size()-1;
376
377

      // densities are calculated using Newton's method for numerical integration
378
379
      // as backwards as it sounds, the outer loop should be the points rather than the moments
      for (size_t j = 0; j < numPoints; ++j)
380
      {
381
382
383
384
385
386
        // central x in histogram with a change of variables - and just change for density
        const double xVal = .5*static_cast<double>(x[j]+x[j+1]) - mean; // change of variables

        // this variable will be (x^n)*y
        double temp;
        if (isDensity)
387
388
        {
          const double xDelta = static_cast<double>(x[j+1]-x[j]);
389
          temp = xVal * .5*static_cast<double>(y[j]+y[j+1])*xDelta;
390
        }
391
392
393
394
395
396
397
398
        else
        {
          temp = xVal * static_cast<double>(y[j]);
        }

        // accumulate the moment
        result[1] += temp;
        for (size_t i = 2; i < result.size(); ++i)
399
        {
400
401
          temp *= xVal;
          result[i] += temp;
402
403
404
405
406
        }
      }

      return result;
    }
407

408
409
410
411
    // -------------------------- Macro to instantiation concrete types --------------------------------
#define INSTANTIATE(TYPE) \
    template MANTID_KERNEL_DLL Statistics getStatistics<TYPE> (const vector<TYPE> &, const bool); \
    template MANTID_KERNEL_DLL std::vector<double> getZscore<TYPE> (const vector<TYPE> &, const bool); \
412
413
414
415
    template MANTID_KERNEL_DLL std::vector<double> getModifiedZscore<TYPE> (const vector<TYPE> &, const bool); \
    template MANTID_KERNEL_DLL std::vector<double> getMomentsAboutOrigin<TYPE> (const std::vector<TYPE>& x, const std::vector<TYPE>& y, const int maxMoment); \
    template MANTID_KERNEL_DLL std::vector<double> getMomentsAboutMean<TYPE> (const std::vector<TYPE>& x, const std::vector<TYPE>& y, const int maxMoment);

416
417
418
419

    // --------------------------- Concrete instantiations ---------------------------------------------
    INSTANTIATE(float);
    INSTANTIATE(double);
420
421
    INSTANTIATE(int);
    INSTANTIATE(long);
422
    INSTANTIATE(long long);
423
424
    INSTANTIATE(unsigned int);
    INSTANTIATE(unsigned long);
425
    INSTANTIATE(unsigned long long);
Campbell, Stuart's avatar
Campbell, Stuart committed
426
427

  } // namespace Kernel
428
} // namespace Mantid