CMS 3D CMS Logo

lms_1d.h
Go to the documentation of this file.
1 #ifndef CommonTools_Statistics_lms_1d_h
2 #define CommonTools_Statistics_lms_1d_h
3 
5 
6 #include <vector>
7 #include <algorithm>
8 #include <cmath>
9 
14 template <class T>
15 T lms_1d(std::vector<T> values) {
16  switch (values.size()) {
17  case 0:
18  throw StatisticsException("Lms of empty vector undefined");
19  case 1:
20  return (T) * (values.begin());
21  case 2:
22  return (T)((*(values.begin()) + *(values.end() - 1)) / 2);
23  };
24  const int size = values.size();
25  const int half = size / 2;
26  const int n_steps = (size + 1) / 2;
27 
28  sort(values.begin(), values.end());
29 
30  T i_begin = *(values.begin());
31  T i_end = *(values.begin() + half);
32  T div = (T)fabs(i_begin - i_end);
33  for (typename std::vector<T>::const_iterator i = values.begin(); i != (values.begin() + n_steps); ++i) {
34  if (fabs((*i) - (*(i + half))) < div) {
35  i_begin = (*i);
36  i_end = *(i + half);
37  div = (T)fabs(i_begin - i_end);
38  }
39  }
40 
41  return (T)((i_begin + i_end) / 2);
42 }
43 
44 #endif
size
Write out results.
T lms_1d(std::vector< T > values)
Definition: lms_1d.h:15
long double T