SynAPSE  0.6.0
Synergistic Analysis of Phase Space Evolution
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
Statistics-inl.hh
1 template<typename T> void Trim(std::vector<T>& vx,
2  const double& frac) {
3 
4  // If the sample is empty, throw
5  Assert::IsNotEmpty("Math::Trim", "Input sample", vx);
6 
7  // Handle the special values of n
8  if ( !frac ) {
9  return;
10  } else if ( frac == .5 ) {
11  vx = {Median(vx)};
12  return;
13  } else if ( frac < 0. || frac > .5 ) {
14  throw(Exceptions::Exception(Exceptions::nonRecoverable,
15  "Invalid trimming fraction (0 < frac < .5)",
16  "Math::Trim"));
17  }
18 
19  // Sort the points and discard the smallest and largest values
20  std::vector<double> vnew;
21  std::sort(vx.begin(), vx.end());
22  size_t ntrim = frac*vx.size();
23  size_t nfinal = vx.size()-2*ntrim;
24  size_t i;
25  for (i = ntrim; i < ntrim+nfinal; i++)
26  vnew.push_back(vx[i]);
27 
28  // Randomly shuffle the elements to not return a sorted vector
29  std::srand(std::time(NULL));
30  std::random_shuffle(vnew.begin(), vnew.end());
31  vx = vnew;
32 }
33 
34 template<typename T> std::vector<T> Trimmed(std::vector<T> vx,
35  const double& frac) {
36 
37  Trim(vx, frac);
38  return vx;
39 }
40 
41 template<typename T> void Winsorize(std::vector<T>& vx,
42  const double& frac) {
43 
44  // If the sample is empty, throw
45  Assert::IsNotEmpty("Math::Winsorize", "Input sample", vx);
46 
47  // Handle the special values of n
48  if ( !frac ) {
49  return;
50  } else if ( frac == .5 ) {
51  double med = Median(vx);
52  for (size_t i = 0; i < vx.size(); i++)
53  vx[i] = med;
54  return;
55  } else if ( frac < 0. || frac > .5 ) {
56  throw(Exceptions::Exception(Exceptions::nonRecoverable,
57  "Invalid fraction (0 < frac < .5)",
58  "Math::Winsorize"));
59  }
60 
61  // Sort the points and replace the smallest and largest values
62  std::sort(vx.begin(), vx.end());
63  size_t id = frac*vx.size();
64  double lbound(vx[id]), ubound(vx[vx.size()-1-id]);
65  size_t i;
66  for (i = 0; i < id; i++) {
67  vx[i] = lbound;
68  vx[vx.size()-1-i] = ubound;
69  }
70 
71  // Randomly shuffle the elements to not return a sorted vector
72  std::srand(std::time(NULL));
73  std::random_shuffle(vx.begin(), vx.end());
74 }
75 
76 template<typename T> std::vector<T> Winsorized(std::vector<T> vx,
77  const double& frac) {
78 
79  Winsorize(vx, frac);
80  return vx;
81 }
82 
83 template<typename I> I RandomElement(I begin, I end) {
84 
85  const size_t n = std::distance(begin, end);
86  const size_t divisor = RAND_MAX / n;
87 
88  unsigned long k;
89  do { k = std::rand() / divisor; } while (k >= n);
90 
91  std::advance(begin, k);
92  return begin;
93 }
94 
95 template<typename T> std::vector<T> Resample(const std::vector<T>& vx,
96  size_t m,
97  const bool rep) {
98 
99  // If the sample is empty, throw
100  Assert::IsNotEmpty("Math::Resample", "Input sample", vx);
101 
102  // If m = 0, default it to the sample size
103  if ( !m )
104  m = vx.size();
105 
106  // If no replacement, m must be smaller than n. If not, throw.
107  if ( !rep && m >= vx.size() )
108  throw(Exceptions::Exception(Exceptions::nonRecoverable,
109  "Not enough points to resample without replacement",
110  "Math::Resample"));
111 
112  // Randomly pick m samples from the input sample. If no replacement, ditch (n-m) points randomly
113  std::vector<T> subsample;
114  size_t i, pos, divisor;
115  std::srand(time(NULL));
116  if ( rep ) {
117  subsample.resize(m);
118  divisor = RAND_MAX/vx.size();
119  for (i = 0; i < m; i++) {
120  do {
121  pos = std::rand()/divisor;
122  } while ( pos > vx.size() );
123  subsample[i] = vx[pos];
124  }
125  } else {
126  subsample = vx;
127  std::random_shuffle(subsample.begin(), subsample.end());
128  subsample.resize(m);
129  }
130 
131  return subsample;
132 }
double Median(const std::vector< double > &vx)
Computes the median of a variable sample.
Definition: Statistics.cc:404
I RandomElement(I begin, I end)
Randomly select a single element from an array.
Definition: Statistics.hh:84
std::vector< T > Resample(const std::vector< T > &vx, size_t m=0, const bool rep=true)
Randomly resample n data points into m data points with or without replacement.
Definition: Statistics.hh:96
void Trim(std::vector< T > &vx, const double &frac)
Trim the n% lowest and n% largest values.
Definition: Statistics.hh:2
Global exception object for all non-standard exceptions thrown by the code.
Definition: Exception.hh:30
std::vector< T > Trimmed(std::vector< T > vx, const double &frac)
Trim the n% lowest and n% largest values.
Definition: Statistics.hh:35
std::vector< T > Winsorized(std::vector< T > vx, const double &frac)
Winsorize to keep only a fraction frac of the original sample.
Definition: Statistics.hh:77
void Winsorize(std::vector< T > &vx, const double &frac)
Winsorize to keep only a fraction frac of the original sample.
Definition: Statistics.hh:42
void IsNotEmpty(const std::string &location, const std::string &what, const std::vector< T > &vector)
Asserts that a vector is not empty.
Definition: Assert.hh:53