Example #1
0
std::vector<double> getModifiedZscore(const vector<TYPE> &data,
                                      const bool sorted) {
  if (data.size() < 3) {
    std::vector<double> Zscore(data.size(), 0.);
    return Zscore;
  }
  std::vector<double> MADvec;
  double tmp;
  size_t num_data = data.size(); // cache since it is frequently used
  double median = getMedian(data, num_data, sorted);
  typename vector<TYPE>::const_iterator it = data.begin();
  for (; it != data.end(); ++it) {
    tmp = static_cast<double>(*it);
    MADvec.push_back(fabs(tmp - median));
  }
  double MAD = getMedian(MADvec, num_data, sorted);
  if (MAD == 0.) {
    std::vector<double> Zscore(data.size(), 0.);
    return Zscore;
  }
  MADvec.clear();
  std::vector<double> Zscore;
  it = data.begin();
  for (; it != data.end(); ++it) {
    tmp = static_cast<double>(*it);
    Zscore.push_back(0.6745 * fabs((tmp - median) / MAD));
  }
  return Zscore;
}
std::vector<double> getZscore(const vector<TYPE> &data) {
  if (data.size() < 3) {
    std::vector<double> Zscore(data.size(), 0.);
    return Zscore;
  }
  std::vector<double> Zscore;
  Statistics stats = getStatistics(data);
  if (stats.standard_deviation == 0.) {
    std::vector<double> Zscore(data.size(), 0.);
    return Zscore;
  }
  for (auto it = data.cbegin(); it != data.cend(); ++it) {
    double tmp = static_cast<double>(*it);
    Zscore.push_back(fabs((tmp - stats.mean) / stats.standard_deviation));
  }
  return Zscore;
}