1 template<
typename T>
void Trim(std::vector<T>& vx,
10 }
else if ( frac == .5 ) {
13 }
else if ( frac < 0. || frac > .5 ) {
15 "Invalid trimming fraction (0 < frac < .5)",
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;
25 for (i = ntrim; i < ntrim+nfinal; i++)
26 vnew.push_back(vx[i]);
29 std::srand(std::time(NULL));
30 std::random_shuffle(vnew.begin(), vnew.end());
34 template<
typename T> std::vector<T>
Trimmed(std::vector<T> vx,
41 template<
typename T>
void Winsorize(std::vector<T>& vx,
50 }
else if ( frac == .5 ) {
52 for (
size_t i = 0; i < vx.size(); i++)
55 }
else if ( frac < 0. || frac > .5 ) {
57 "Invalid fraction (0 < frac < .5)",
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]);
66 for (i = 0; i < id; i++) {
68 vx[vx.size()-1-i] = ubound;
72 std::srand(std::time(NULL));
73 std::random_shuffle(vx.begin(), vx.end());
76 template<
typename T> std::vector<T>
Winsorized(std::vector<T> vx,
85 const size_t n = std::distance(begin, end);
86 const size_t divisor = RAND_MAX / n;
89 do { k = std::rand() / divisor; }
while (k >= n);
91 std::advance(begin, k);
95 template<
typename T> std::vector<T>
Resample(
const std::vector<T>& vx,
107 if ( !rep && m >= vx.size() )
109 "Not enough points to resample without replacement",
113 std::vector<T> subsample;
114 size_t i, pos, divisor;
115 std::srand(time(NULL));
118 divisor = RAND_MAX/vx.size();
119 for (i = 0; i < m; i++) {
121 pos = std::rand()/divisor;
122 }
while ( pos > vx.size() );
123 subsample[i] = vx[pos];
127 std::random_shuffle(subsample.begin(), subsample.end());
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