version  0.0.1
Defines the C++ API for MsPASS
Loading...
Searching...
No Matches
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)
57 std::string("VectorStatistics constructor: ") +
58 "input vector has insufficient data to compute statistics",
59 mspass::utility::ErrorSeverity::Invalid);
60 d = din;
61 std::sort(d.begin(), d.end());
62}
Base class for error object thrown by MsPASS library routines.
Definition MsPASSError.h:38

◆ 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.

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

Member Function Documentation

◆ interquartile()

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

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

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

◆ lower_bound()

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

Return smallest value in the data set.

174{ return (d[0]); }

◆ mad()

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

Return the median absolute distance robust measure of spread.

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

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

◆ mean()

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

Return the mean

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

◆ median()

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

Return median

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

◆ 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 int nover4 = (n - 1) / 4;
97 switch (n % 4) {
98 case (0):
99 result = static_cast<double>(d[nover4]);
100 break;
101 case (1):
102 result = 0.75 * static_cast<double>(d[nover4]) +
103 0.25 * static_cast<double>(d[nover4 + 1]);
104 break;
105 case (2):
106 result =
107 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]) +
112 0.75 * static_cast<double>(d[nover4 + 1]);
113 }
114 }
115 return (static_cast<T>(result));
116}

◆ 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 int n3_4 = 3 * (n - 1) / 4;
124 switch (n % 4) {
125 case (0):
126 result = static_cast<double>(d[n3_4]);
127 break;
128 case (1):
129 result = 0.75 * static_cast<double>(d[n3_4]) +
130 0.25 * static_cast<double>(d[n3_4 + 1]);
131 break;
132 case (2):
133 result = static_cast<double>(d[n3_4]) + static_cast<double>(d[n3_4 + 1]);
134 result /= 2.0;
135 break;
136 case (3):
137 result = 0.25 * static_cast<double>(d[n3_4]) +
138 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.

181 {
182 if (n >= d.size()) {
183 std::stringstream ss;
184 ss << "VectorStatistics::quantile method: asked for 1-quantile number "
185 << n << " but data vecotor length is only " << d.size() << std::endl;
186 throw mspass::utility::MsPASSError(ss.str(),
187 mspass::utility::ErrorSeverity::Invalid);
188 }
189}

◆ range()

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

Return full range of data (largest - smallest)

175 {
176 T result;
177 result = this->upper_bound();
178 result -= this->lower_bound();
179 return (result);
180}
T upper_bound()
Definition VectorStatistics.h:171
T lower_bound()
Definition VectorStatistics.h:174

◆ ssq()

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

Return sum of squares.

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

◆ upper_bound()

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

Return largest value in the data set.

171 {
172 return (d[d.size() - 1]);
173}

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