Changeset 511


Ignore:
Timestamp:
Feb 18, 2006, 3:46:46 PM (16 years ago)
Author:
Peter
Message:

added percentile function for gslapi::vector

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/statistics/utility.cc

    r500 r511  
    3232  }
    3333
     34  double percentile(const gslapi::vector& vec, const double p,
     35                    const bool sorted)
     36  {
     37    if (!sorted){
     38      gslapi::vector vec_c(vec);
     39      vec_c.sort();
     40      return gsl_stats_quantile_from_sorted_data(vec_c.gsl_vector_p()->data,
     41                                                 vec_c.gsl_vector_p()->stride,
     42                                                 vec_c.gsl_vector_p()->size,
     43                                                 p);
     44    }
     45    return gsl_stats_quantile_from_sorted_data (vec.gsl_vector_p()->data,
     46                                              vec.gsl_vector_p()->stride,
     47                                              vec.gsl_vector_p()->size,
     48                                              p);
     49  }
     50
    3451}} // of namespace statistics and namespace theplu
  • trunk/lib/statistics/utility.h

    r502 r511  
    1414namespace statistics { 
    1515
     16  //forward declarations
     17  template <class T>
     18  double percentile(const std::vector<T>& vec, const double p,
     19                    const bool sorted=false);
     20 
     21
    1622  ///
    1723  /// Calculates the probabilty to get \a k or smaller from a
     
    2632  double cdf_hypergeometric_P(u_int k, u_int n1, u_int n2, u_int t);
    2733
     34
     35  ///
     36  /// Median is defined to be value in the middle. If number of values
     37  /// is even median is the average of the two middle values.  the
     38  /// median value is given by p equal to 50. If @a sorted is false
     39  /// (default), the vector is copied, the copy is sorted, and then
     40  /// used to calculate the median.
     41  ///
     42  /// @return median
     43  ///
     44  /// @note interface will change
     45  ///
     46  template <class T>
     47  inline double median(const std::vector<T>& v, const bool sorted=false)
     48  { return percentile(v, 50.0, sorted); }
     49
     50  ///
     51  /// Median is defined to be value in the middle. If number of values
     52  /// is even median is the average of the two middle values. If @a
     53  /// sorted is true, the function assumes vector @a vec to be
     54  /// sorted. If @a sorted is false, the vector is copied, the copy is
     55  /// sorted (default), and then used to calculate the median.
     56  ///
     57  /// @return median
     58  ///
     59  double median(const gslapi::vector& vec, const bool sorted=false);
    2860
    2961  ///
     
    6395
    6496  ///
    65   /// Median is defined to be value in the middle. If number of values
    66   /// is even median is the average of the two middle values.  the
    67   /// median value is given by p equal to 50. If @a sorted is false
    68   /// (default), the vector is copied, the copy is sorted, and then
    69   /// used to calculate the median.
     97  /// The percentile is determined by the \a p, a number between 0 and
     98  /// 100. The percentile is found by interpolation, using the formula
     99  /// \f$ percentile = (1 - \delta) x_i + \delta x_{i+1} \f$ where \a
     100  /// p is floor\f$((n - 1)p/100)\f$ and \f$ \delta \f$ is \f$
     101  /// (n-1)p/100 - i \f$.Thus the minimum value of the vector is given
     102  /// by p equal to zero, the maximum is given by p equal to 100 and
     103  /// the median value is given by p equal to 50. If @a sorted
     104  /// is false (default), the vector is copied, the copy is sorted,
     105  /// and then used to calculate the median.
     106  ///
     107  /// @return \a p'th percentile
    70108  ///
    71   /// @return median
    72   ///
    73   /// @note interface will change
    74   ///
    75   template <class T>
    76   inline double median(const std::vector<T>& v, const bool sorted=false)
    77   { return percentile(v, 50.0, sorted); }
    78 
    79   ///
    80   /// Median is defined to be value in the middle. If number of values
    81   /// is even median is the average of the two middle values. If @a
    82   /// sorted is true, the function assumes vector @a vec to be
    83   /// sorted. If @a sorted is false, the vector is copied, the copy is
    84   /// sorted (default), and then used to calculate the median.
    85   ///
    86   /// @return median
    87   ///
    88   double median(const gslapi::vector& vec, const bool sorted=false);
     109  double median(const gslapi::vector& vec, const double,
     110                const bool sorted=false);
    89111
    90112}} // of namespace statistics and namespace theplu
  • trunk/test/statistics_test.cc

    r502 r511  
    22
    33#include <c++_tools/statistics/utility.h>
     4#include <c++_tools/gslapi/vector.h>
    45
    56#include <vector>
     
    89
    910
    10 using namespace std;
    11 
    1211int main()
    1312
     13  using namespace theplu;
     14  gslapi::vector gsl_vec(10);
    1415  std::vector<double> data;
    15   for (unsigned int i=0; i<10; i++)
     16  for (unsigned int i=0; i<10; i++){
    1617    data.push_back(static_cast<double>(i));
    17   double m=theplu::statistics::median(data);
    18   if (m!=4.5)
     18    gsl_vec(i)=i;
     19  }
     20  double m=statistics::median(data);
     21  double m_gsl=statistics::median(gsl_vec);
     22  if (m!=4.5 || m!=m_gsl)
    1923    return -1;
    2024  return 0;
Note: See TracChangeset for help on using the changeset viewer.