File: mbl_sample_stats_1d.cxx

package info (click to toggle)
vxl 1.17.0.dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 153,280 kB
  • ctags: 105,123
  • sloc: cpp: 747,420; ansic: 209,130; fortran: 34,230; lisp: 14,915; sh: 6,187; python: 5,856; makefile: 340; perl: 294; xml: 160
file content (473 lines) | stat: -rw-r--r-- 11,385 bytes parent folder | download
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include "mbl_sample_stats_1d.h"
//:
// \file
#include <vsl/vsl_vector_io.h>
#include <vcl_cassert.h>
#include <vcl_cmath.h>
#include <vcl_limits.h>
#include <vcl_algorithm.h>


//=========================================================================
mbl_sample_stats_1d::mbl_sample_stats_1d(const vcl_vector<double> &samples)
{
  clear();
  for (unsigned i=0, n=samples.size(); i<n; ++i)
  {
    add_sample(samples[i]);
  }
}


//=========================================================================
mbl_sample_stats_1d::mbl_sample_stats_1d(const vnl_vector<double> &samples)
{
  clear();
  for (unsigned i=0, n=samples.size(); i<n; ++i)
  {
    add_sample(samples[i]);
  }
}


//=========================================================================
mbl_sample_stats_1d::mbl_sample_stats_1d()
{
  clear();
}


//=========================================================================
mbl_sample_stats_1d::~mbl_sample_stats_1d()
{
}


//=========================================================================
void mbl_sample_stats_1d::clear()
{
  samples_.resize(0);
  stats_1d_.clear();
  use_mvue_=true;
}


//=========================================================================
void mbl_sample_stats_1d::add_sample(double v)
{
  stats_1d_.obs(v);
  samples_.push_back(v);
  return;
}


//=========================================================================
unsigned mbl_sample_stats_1d::n_samples() const
{
  return samples_.size();
}


//=========================================================================
double mbl_sample_stats_1d::mean() const
{
  return stats_1d_.mean();
}


//=========================================================================
double mbl_sample_stats_1d::mean_of_absolutes() const
{
  double abs_sum = 0;
  for (unsigned i=0, n=samples_.size(); i<n; ++i)
    abs_sum+=vcl_fabs(samples_[i]);
  return abs_sum/samples_.size();
}


//=========================================================================
double mbl_sample_stats_1d::median() const
{
  double ret;

  if (samples_.size()>0)
  {
    if ( samples_.size() % 2 == 0 )
    {
      unsigned index = samples_.size() / 2 - 1;

      vcl_vector<double> tmp=samples_;

      vcl_vector<double>::iterator index_it0 = tmp.begin() + index;
      vcl_nth_element(tmp.begin(),index_it0,tmp.end(),vcl_less<double>());
      double v0 = *index_it0;

      vcl_vector<double>::iterator index_it1 = tmp.begin() + index + 1;
      vcl_nth_element(tmp.begin(),index_it1,tmp.end(),vcl_less<double>());
      double v1 = *index_it1;
      ret = v0 + v1;
      ret /= 2.0;
    }
    else
    {
      unsigned index = (samples_.size() - 1) / 2;

      vcl_vector<double> tmp=samples_;

      vcl_vector<double>::iterator index_it = tmp.begin() + index;
      vcl_nth_element(tmp.begin(),index_it,tmp.end(),vcl_less<double>());

      ret = *index_it;
    }
  }
  else // crazy value if  no samples
  {
    ret = vcl_numeric_limits<double>::max();
  }
  return ret;
}


//=========================================================================
double mbl_sample_stats_1d::quantile(double q) const
{
  const unsigned n = samples_.size();

  // These checks are only asserts because client code is responsible for avoiding these errors.
  assert(q>=0.0 && q<=1.0);
  assert(n>0);

  // Map the specified quantile to a real-valued "index", i.e. a float lying between 2 integer indices
  double float_index = (n-1)*q;

  // Get the integer index immediately below (and enforce the bounds)
  double f0 = vcl_floor(float_index);
  f0 = f0<0.0 ? 0.0 : f0>n-1.0 ? n-1.0 : f0;
  unsigned i0 = static_cast<unsigned>(f0);

  // Get the integer index immediately above (and enforce the bounds)
  double f1 = vcl_ceil(float_index);
  f1 = f1<0.0 ? 0.0 : f1>n-1.0 ? n-1.0 : f1;
  unsigned i1 = static_cast<unsigned>(f1);

  // Get the 2 values bracketing the specified quantile position
  vcl_vector<double> tmp = samples_;

  vcl_vector<double>::iterator index_it0 = tmp.begin() + i0;
  vcl_nth_element(tmp.begin(), index_it0, tmp.end(), vcl_less<double>());
  double v0 = *index_it0;

  vcl_vector<double>::iterator index_it1 = tmp.begin() + i1;
  vcl_nth_element(tmp.begin(), index_it1, tmp.end(), vcl_less<double>());
  double v1 = *index_it1;

  // Linearly interpolate between the 2 values
  double f = float_index - f0;
  double ret = ((1.0-f)*v0) + (f*v1);
  return ret;
}


//=========================================================================
double mbl_sample_stats_1d::nth_percentile(int n) const
{
  if (samples_.size()==0)
    return vcl_numeric_limits<double>::max();

  double fact = double(n)/100.0;
  int index=int(fact*(samples_.size()-1));
  vcl_vector<double> tmp=samples_;

  vcl_vector<double>::iterator index_it = tmp.begin() + index;
  vcl_nth_element(tmp.begin(),index_it,tmp.end(),vcl_less<double>());
  double ret = *index_it;
  return ret;
}


//=========================================================================
double mbl_sample_stats_1d::variance() const
{
  double v=0;

  if (samples_.size()>1)
  {
    double mean_v = mean();
    double sum_sq = sum_squares();
    v = sum_sq - samples_.size()*(mean_v * mean_v);

    if (use_mvue_)
    {
      v /= (samples_.size()-1);
    }
    else
    {
      v /= samples_.size();
    }
  }

  return v;
}


//=========================================================================
double mbl_sample_stats_1d::sd() const
{
  return vcl_sqrt(variance());
}


//=========================================================================
double mbl_sample_stats_1d::stdError() const
{
  double se = variance();
  if (use_mvue_)
  {
    se /= samples_.size()-1;
  }
  else
  {
    se /= samples_.size();
  }

  return vcl_sqrt(se);
}


//=========================================================================
double mbl_sample_stats_1d::skewness() const
{
  double skew = 0;

  // skew
  // calculated as
  // ( Sum_i (Y_i-MEAN)^3 ) / ((N-1)*sigma^3)
  // where N is the number of samples
  // sigma is the standard deviation

  if (samples_.size()>1)
  {
    double s=sd();
    double m=mean();

    for (unsigned i=0, n=samples_.size(); i<n; ++i)
    {
      double tmp=samples_[i]-m;
      skew += (tmp*tmp*tmp) ;
    }

    skew /= ( (samples_.size()-1) * s * s * s );
  }

  return skew;
}


//=========================================================================
double mbl_sample_stats_1d::kurtosis() const
{
  double kurt = 0;

  // kurtosis
  // calculated as
  // -3 + ( Sum_i (Y_i-MEAN)^4 ) / ((N-1)*sigma^4)
  // where N is the number of samples
  // sigma is the standard deviation

  if (samples_.size()>1)
  {
    double s=sd();
    double m=mean();

    for (unsigned i=0, n=samples_.size(); i<n; ++i)
    {
      double tmp=samples_[i]-m;
      kurt += (tmp*tmp*tmp*tmp) ;
    }

    kurt /= ( (samples_.size()-1) * s * s * s *s);
    kurt -= 3;
  }
  return kurt;
}


//=========================================================================
double mbl_sample_stats_1d::min() const
{
  if (samples_.size()==0) return vcl_numeric_limits<double>::max();
  else return stats_1d_.min();
}


//=========================================================================
double mbl_sample_stats_1d::max() const
{
  if (samples_.size()==0) return vcl_numeric_limits<double>::min();
  else return stats_1d_.max();
}


//=========================================================================
double mbl_sample_stats_1d::sum() const
{
  return stats_1d_.sum();
}


//=========================================================================
double mbl_sample_stats_1d::sum_squares() const
{
  return stats_1d_.sumSq();
}


//=========================================================================
double mbl_sample_stats_1d::rms() const
{
  double ms=sum_squares()/stats_1d_.nObs();
  return vcl_sqrt( ms );
}


//=========================================================================
mbl_sample_stats_1d& mbl_sample_stats_1d::operator+=(const mbl_sample_stats_1d& s1)
{
  // add new samples
  for (unsigned i=0;i<s1.samples().size();++i)
  {
    add_sample(s1.samples()[i]);
  }

  return *this ;
}


//=========================================================================
// Test for equality
bool mbl_sample_stats_1d::operator==(const mbl_sample_stats_1d& s) const
{
  return samples_==s.samples_ && use_mvue_==s.use_mvue_;
}

// =============================================
short mbl_sample_stats_1d::version_no() const
{
  return 1;
}


//=========================================================================
void mbl_sample_stats_1d::b_write(vsl_b_ostream& bfs) const
{
  vsl_b_write(bfs,version_no());
  vsl_b_write(bfs,samples_);
  vsl_b_write(bfs,stats_1d_);
  vsl_b_write(bfs,use_mvue_);
}


//=========================================================================
void mbl_sample_stats_1d::b_read(vsl_b_istream& bfs)
{
  if (!bfs) return;

  short file_version_no;
  vsl_b_read(bfs,file_version_no);

  switch (file_version_no)
  {
  case 1:
    vsl_b_read(bfs,samples_);
    vsl_b_read(bfs,stats_1d_);
    vsl_b_read(bfs,use_mvue_);
    break;
  default :
    vcl_cerr << "I/O ERROR: mbl_sample_stats_1d::b_read(vsl_b_istream&)\n"
             << "           Unknown version number "<< file_version_no << '\n';
    bfs.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
    return;
  }
}


//=========================================================================
void mbl_sample_stats_1d::print_summary(vcl_ostream& os) const
{
  os << "mbl_sample_stats_1d: ";
  if (samples_.size()==0)
  {
    os << "No samples.";
  }
  else
  {
    os << "mean: "<< mean()
       << " use MVUE: "<< use_mvue_
       << " sd: "<< sd()
       << " ["<<stats_1d_.min()<<','<<stats_1d_.max()<<"] N:"<<samples_.size();
  }
}


//=========================================================================
// Print all data
void mbl_sample_stats_1d::print_all(vcl_ostream& os,
                                    const vcl_string& delim/*="\n"*/) const
{
  unsigned nSamples = samples_.size();
  for (unsigned i=0; i<nSamples; ++i)
  {
    os << samples_[i] << delim;
  }
}


//=========================================================================
vcl_ostream& operator<<(vcl_ostream& os, const mbl_sample_stats_1d& stats)
{
  stats.print_summary(os);
  return os;
}


//=========================================================================
// Stream output operator for class reference
void vsl_print_summary(vcl_ostream& os,const mbl_sample_stats_1d& stats)
{
  stats.print_summary(os);
}


//=========================================================================
// Print all data
void vsl_print_all(vcl_ostream& os, const mbl_sample_stats_1d& stats)
{
  stats.print_all(os);
}


//=========================================================================
mbl_sample_stats_1d operator+(const mbl_sample_stats_1d& s1, const mbl_sample_stats_1d& s2)
{
  mbl_sample_stats_1d r = s1;
  r+=s2;

  return r;
}


//=========================================================================
// Binary file stream output operator for class reference
void vsl_b_write(vsl_b_ostream& bfs, const mbl_sample_stats_1d& b)
{
  b.b_write(bfs);
}


//=========================================================================
// Binary file stream input operator for class reference
void vsl_b_read(vsl_b_istream& bfs, mbl_sample_stats_1d& b)
{
  b.b_read(bfs);
}