version  0.0.1
Defines the C++ API for MsPASS
Public Member Functions | List of all members
mspass::utility::VectorStatistics< T > Class Template Reference

Generic object to compute common robust statistics from a vector container of data. More...

#include <VectorStatistics.h>

Public Member Functions

 VectorStatistics (std::vector< T > din)
 
 VectorStatistics (T *din, int n)
 
median ()
 
mean ()
 
q1_4 ()
 
q3_4 ()
 
interquartile ()
 
mad (T center)
 
ssq ()
 
upper_bound ()
 
lower_bound ()
 
range ()
 
quantile (size_t n)
 

Detailed Description

template<class T>
class mspass::utility::VectorStatistics< T >

Generic object to compute common robust statistics from a vector container of data.

Robust estimators commonly use statistics based on ranked data. This fits naturally with an STL vector container that is by definition sortable by a standard method. This object has methods that return common statistics derived from sorted vector data.

Constructor & Destructor Documentation

◆ VectorStatistics() [1/2]

template<class T >
mspass::utility::VectorStatistics< T >::VectorStatistics ( std::vector< T >  din)

Primary constructor.

Parameters
dinis vector container from which statistics are to be derived. Currently assume default sort is used. Probably should have an optional order template parameter for the container.
54 {
55  if(din.size()<=1) throw mspass::utility::MsPASSError(std::string("VectorStatistics constructor: ")
56  + "input vector has insufficient data to compute statistics",
57  mspass::utility::ErrorSeverity::Invalid);
58  d=din;
59  std::sort(d.begin(),d.end());
60 }
Base class for error object thrown by MsPASS library routines.
Definition: MsPASSError.h:40

◆ VectorStatistics() [2/2]

template<class T >
mspass::utility::VectorStatistics< T >::VectorStatistics ( T *  din,
int  n 
)

Construct from a C style pointer to an array of T.

62 {
63  if(n<=1) throw mspass::utility::MsPASSError(std::string("VectorStatistics constructor: ")
64  + "input vector has insufficient data to compute statistics",
65  mspass::utility::ErrorSeverity::Invalid);
66  d.reserve(n);
67  for(int i=0;i<n;++i) d.push_back(din[i]);
68  std::sort(d.begin(),d.end());
69 }

Member Function Documentation

◆ interquartile()

template<class T >
T mspass::utility::VectorStatistics< T >::interquartile

Return the interquartile (q3/4 - q1/4)

144 {
145  T result;
146  T d1_4,d3_4;
147  d1_4=this->q1_4();
148  d3_4=this->q3_4();
149  return(d3_4 - d1_4);
150 }
T q3_4()
Definition: VectorStatistics.h:116
T q1_4()
Definition: VectorStatistics.h:89

◆ lower_bound()

template<class T >
T mspass::utility::VectorStatistics< T >::lower_bound

Return smallest value in the data set.

178 {
179  return(d[0]);
180 }

◆ mad()

template<class T >
T mspass::utility::VectorStatistics< T >::mad ( center)

Return the median absolute distance robust measure of spread.

152 {
153  std::vector<T> absdiff;
154  int n=d.size();
155  int i;
156  for(i=0;i<n;++i)
157  {
158  T diff;
159  diff=d[i]-center;
160  if(diff<0) diff=-diff;
161  absdiff.push_back(diff);
162  }
163  VectorStatistics<T> result(absdiff);
164  return(result.median());
165 }

References mspass::utility::VectorStatistics< T >::median().

◆ mean()

template<class T >
T mspass::utility::VectorStatistics< T >::mean

Return the mean

80 {
81  T result;
82  result=0;
83  for(int i=0;i<d.size();++i)
84  {
85  result += d[i];
86  }
87  return (result/d.size());
88 }

◆ median()

template<class T >
T mspass::utility::VectorStatistics< T >::median

Return median

71 {
72  int count=d.size();
73  int medposition=count/2;
74  if(count%2)
75  return(d[medposition]);
76  else
77  return( (d[medposition]+d[medposition-1])/2 );
78 }

◆ q1_4()

template<class T >
T mspass::utility::VectorStatistics< T >::q1_4

Return the lower quartile.

90 {
91  int n=d.size();
92  double result;
93  if(n<4)
94  return(d[0]);
95  else
96  {
97  int nover4=(n-1)/4;
98  switch(n%4)
99  {
100  case(0):
101  result=static_cast<double>(d[nover4]);
102  break;
103  case(1):
104  result=0.75*static_cast<double>(d[nover4]) + 0.25*static_cast<double>(d[nover4+1]);
105  break;
106  case(2):
107  result=static_cast<double>(d[nover4]) + static_cast<double>(d[nover4+1]);
108  result /= 2.0;
109  break;
110  case(3):
111  result=0.25*static_cast<double>(d[nover4]) + 0.75*static_cast<double>(d[nover4+1]);
112  }
113  }
114  return(static_cast<T>(result));
115 }

◆ q3_4()

template<class T >
T mspass::utility::VectorStatistics< T >::q3_4

Return the upper (3/4) quartile.

117 {
118  int n=d.size();
119  double result;
120  if(n<4)
121  return(d[n-1]);
122  else
123  {
124  int n3_4=3*(n-1)/4;
125  switch(n%4)
126  {
127  case(0):
128  result=static_cast<double>(d[n3_4]);
129  break;
130  case(1):
131  result=0.75*static_cast<double>(d[n3_4]) + 0.25*static_cast<double>(d[n3_4+1]);
132  break;
133  case(2):
134  result=static_cast<double>(d[n3_4]) + static_cast<double>(d[n3_4+1]);
135  result /= 2.0;
136  break;
137  case(3):
138  result=0.25*static_cast<double>(d[n3_4]) + 0.75*static_cast<double>(d[n3_4+1]);
139  }
140  }
141  return(static_cast<T>(result));
142 }

◆ quantile()

template<class T >
T mspass::utility::VectorStatistics< T >::quantile ( size_t  n)

Return nth value from the sorted data that is the nth 1-quantile. Will throw a MsPASSError if n exceed the data length.

189 {
190  if(n>=d.size())
191  {
192  std::stringstream ss;
193  ss << "VectorStatistics::quantile method: asked for 1-quantile number "
194  << n<<" but data vecotor length is only "<<d.size()<<std::endl;
195  throw mspass::utility::MsPASSError(ss.str(),mspass::utility::ErrorSeverity::Invalid);
196  }
197 }

◆ range()

template<class T >
T mspass::utility::VectorStatistics< T >::range

Return full range of data (largest - smallest)

182 {
183  T result;
184  result=this->upper_bound();
185  result-=this->lower_bound();
186  return(result);
187 }
T upper_bound()
Definition: VectorStatistics.h:173
T lower_bound()
Definition: VectorStatistics.h:177

◆ ssq()

template<class T >
T mspass::utility::VectorStatistics< T >::ssq

Return sum of squares.

167 {
168  T result;
169  int i;
170  for(i=0;i<d.size();++i) result=d[i]*d[i];
171  return(result);
172 }

◆ upper_bound()

template<class T >
T mspass::utility::VectorStatistics< T >::upper_bound

Return largest value in the data set.

174 {
175  return(d[d.size()-1]);
176 }

The documentation for this class was generated from the following file: