OpendTect  6.6
statgrubbs.h
Go to the documentation of this file.
1 #pragma once
2 /*+
3 ________________________________________________________________________
4 
5  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
6  Author: Bert/Bruno
7  Date: Feb 2011
8  RCS: $Id$
9 ________________________________________________________________________
10 
11 -*/
12 
13 #include "algomod.h"
14 #include "gendefs.h"
15 #include <math.h>
16 
17 namespace Stats
18 {
19 
25 {
26 public:
27 
28  template <class T,class SzTp>
29  static T getMax(T*,SzTp sz,SzTp& index_of_max);
30 
31 };
32 
33 
34 template <class T,class SzTp>
35 inline T Stats::Grubbs::getMax( T* arr, SzTp sz, SzTp& idxof )
36 {
37  if ( sz < 3 )
38  { idxof = 0; return sz > 0 ? 0 : mUdf(T); }
39  SzTp startidx = 0;
40  for ( ; startidx<sz && mIsUdf(arr[startidx]); startidx++ )
41  /* just skip undefs at start */;
42  if ( startidx+3 > sz )
43  { idxof = startidx; return 0; }
44 
45  T maxval = arr[startidx]; T minval = maxval, sum = maxval;
46  SzTp minidx = startidx, maxidx = startidx;
47  SzTp nonudfsz = 1;
48  for ( SzTp idx=startidx+1; idx<sz; idx++ )
49  {
50  const T val = arr[idx];
51  if ( mIsUdf(val) ) continue;
52 
53  if ( maxval < val ) { maxidx = idx; maxval = val; }
54  if ( minval > val ) { minidx = idx; minval = val; }
55  sum += val;
56  nonudfsz++;
57  }
58  if ( maxval == minval || nonudfsz < 3 )
59  { idxof = startidx; return 0; }
60 
61  const T avg = sum / nonudfsz;
62  sum = 0;
63  for ( SzTp idx=startidx; idx<sz; idx++ )
64  {
65  const T val = arr[idx];
66  if ( mIsUdf(val) ) continue;
67 
68  const T delta = avg - arr[idx];
69  sum += delta * delta;
70  }
71  const T stdev = Math::Sqrt( sum / nonudfsz );
72 
73  const T diffmin = avg - minval;
74  const T diffmax = maxval - avg;
75  idxof = diffmin > diffmax ? minidx : maxidx;
76  return (diffmin > diffmax ? diffmin : diffmax) / stdev;
77 }
78 
79 } // namespace Stats
80 
Stats::Grubbs
Grubbs' test to detect outliers in a univariate data set assumed to come from a normally distributed ...
Definition: statgrubbs.h:25
mIsUdf
#define mIsUdf(val)
Use mIsUdf to check for undefinedness of simple types.
Definition: undefval.h:289
mExpClass
#define mExpClass(module)
Definition: commondefs.h:177
Stats::Grubbs::getMax
static T getMax(T *, SzTp sz, SzTp &index_of_max)
Definition: statgrubbs.h:35
gendefs.h
mUdf
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:274
Stats
Statistics.
Definition: array2dinterpol.h:27
Math::Sqrt
float Sqrt(float)
getMax
T getMax(const ArrayND< T > &in)
returns the Maximum of all defined values in the ArrrayND. Returns UDF if empty or only udfs encounte...
Definition: arrayndalgo.h:201

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B.V. 1995-2021