Changeset 672


Ignore:
Timestamp:
Oct 7, 2006, 8:44:31 AM (15 years ago)
Author:
Peter
Message:

added MAD function in statistics

Location:
trunk/c++_tools/statistics
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/c++_tools/statistics/utility.cc

    r616 r672  
    1717    return p;
    1818  }
     19
     20  double mad(const utility::vector& vec, const bool sorted)
     21  {
     22    double m = median(vec, sorted);
     23    std::vector<double> ad;
     24    ad.reserve(vec.size());
     25    for (size_t i = 0; i<vec.size(); ++i)
     26      ad.push_back(fabs(vec[i]-m));
     27    std::sort(ad.begin(), ad.end());
     28    return median(ad,true);
     29  }
     30 
    1931
    2032  double median(const utility::vector& vec, const bool sorted)
  • trunk/c++_tools/statistics/utility.h

    r671 r672  
    1717
    1818  //forward declarations
     19  template <class T>
     20  inline double median(const std::vector<T>& v, const bool sorted=false);
     21
    1922  template <class T>
    2023  double percentile(const std::vector<T>& vec, const double p,
    2124                    const bool sorted=false);
    2225 
    23 
    2426  ///
    2527  /// Calculates the probability to get \a k or smaller from a
     
    4850  }
    4951
     52
     53  ///
     54  /// @brief Median absolute deviation from median
     55  ///
     56  template <class T>
     57  double mad(const std::vector<T>& vec, const bool sorted=false)
     58  {
     59    double m = median(vec, sorted);
     60    std::vector<double> ad;
     61    ad.reserve(vec.size());
     62    for (size_t i = 0; i<vec.size(); ++i)
     63      ad.push_back(fabs(vec[i]-m));
     64    std::sort(ad.begin(), ad.end());
     65    return median(ad,true);
     66  }
     67 
     68
     69  ///
     70  /// @brief Median absolute deviation from median
     71  ///
     72  double mad(const utility::vector& vec, const bool sorted=false);
     73 
    5074
    5175  ///
Note: See TracChangeset for help on using the changeset viewer.