# Changeset 932 for trunk/yat

Ignore:
Timestamp:
Oct 5, 2007, 11:03:46 PM (15 years ago)
Message:

median and percentile functions now take iterators rather than vectors

Location:
trunk/yat
Files:
5 edited

Unmodified
Removed
• ## trunk/yat/statistics/VectorFunction.cc

 r865 { assert(vec.size()); return median(vec); return median(vec.begin(), vec.end()); }
• ## trunk/yat/statistics/WilcoxonFoldChange.cc

 r865 } if (absolute_) return fabs(median(distance)); return median(distance); return fabs(median(distance.begin(), distance.end())); return median(distance.begin(), distance.end()); }
• ## trunk/yat/statistics/utility.cc

 r865 double mad(const utility::vector& vec, const bool sorted) { double m = median(vec, sorted); double m = median(vec.begin(), vec.end(), sorted); std::vector ad; ad.reserve(vec.size()); ad.push_back(fabs(vec[i]-m)); std::sort(ad.begin(), ad.end()); return median(ad,true); return median(ad.begin(), ad.end(),true); } double median(const utility::vector& vec, const bool sorted) { if (!sorted){ utility::vector vec_copy(vec); utility::sort(vec_copy); return gsl_stats_median_from_sorted_data (vec_copy.gsl_vector_p()->data, vec_copy.gsl_vector_p()->stride, vec_copy.gsl_vector_p()->size); } return gsl_stats_median_from_sorted_data (vec.gsl_vector_p()->data, vec.gsl_vector_p()->stride, vec.gsl_vector_p()->size); } double percentile(const utility::vector& vec, const double p, const bool sorted) { if (!sorted){ utility::vector vec_c(vec); utility::sort(vec_c); return gsl_stats_quantile_from_sorted_data(vec_c.gsl_vector_p()->data, vec_c.gsl_vector_p()->stride, vec_c.gsl_vector_p()->size, p); } return gsl_stats_quantile_from_sorted_data (vec.gsl_vector_p()->data, vec.gsl_vector_p()->stride, vec.gsl_vector_p()->size, p); } double skewness(const utility::vector& v)
• ## trunk/yat/statistics/utility.h

 r865 #include "yat/classifier/Target.h" #include "yat/utility/vector.h" #include "yat/utility/yat_assert.h" #include //forward declarations template double median(const std::vector& v, const bool sorted=false); double median(T first, T last, const bool sorted=false); template double percentile(const std::vector& vec, const double p, const bool sorted=false); double percentile(T first, T last, double p, bool sorted=false); /** ad.push_back(fabs(vec[i]-m)); std::sort(ad.begin(), ad.end()); return median(ad,true); return median(ad.begin(), ad.end(),true); } /// Median is defined to be value in the middle. If number of values /// is even median is the average of the two middle values.  the /// median value is given by p equal to 50. If @a sorted is false /// (default), the vector is copied, the copy is sorted, and then /// median value is given by p equal to 50. If \a sorted is false /// (default), the range is copied, the copy is sorted, and then /// used to calculate the median. /// /// @return median /// Requirements: T should be an iterator over a range of doubles (or /// any type being convertable to double). If \a sorted is false /// iterator must be mutable, else read-only iterator is also ok. /// /// @note interface will change /// @return median of range /// template double median(const std::vector& v, const bool sorted=false) { return percentile(v, 50.0, sorted); } /// /// Median is defined to be value in the middle. If number of values /// is even median is the average of the two middle values. If @a /// sorted is true, the function assumes vector @a vec to be /// sorted. If @a sorted is false, the vector is copied, the copy is /// sorted (default), and then used to calculate the median. /// /// @return median /// double median(const utility::vector& vec, const bool sorted=false); double median(T first, T last, const bool sorted=false) { return percentile(first, last, 50.0, sorted); } /** is false (default), the vector is copied, the copy is sorted, and then used to calculate the median. Requirements: T should be an iterator over a range of doubles (or any type being convertable to double). If \a sorted is false iterator must be mutable, else read-only iterator is also ok. @return \a p'th percentile @return \a p'th percentile of range */ template double percentile(const std::vector& vec, const double p, const bool sorted=false) double percentile(T first, T last, double p, bool sorted=false) { yat_assert(first=0); yat_assert(p<=100); if (sorted){ if (p>=100) return vec.back(); double j = p/100 * (vec.size()-1); return *(--last); double j = p/100 * (std::distance(first,last)-1); int i = static_cast(j); return (1-j+floor(j))*vec[i] + (j-floor(j))*vec[i+1]; return (1-j+floor(j))*first[i] + (j-floor(j))*first[i+1]; } if (p==100) return  *std::max_element(vec.begin(),vec.end()); std::vector v_copy(vec); std::vector v_copy; v_copy.reserve(std::distance(first,last)); std::copy(first, last, std::back_inserter(v_copy)); double j = p/100 * (v_copy.size()-1); int i = static_cast(j); std::partial_sort(v_copy.begin(),v_copy.begin()+i+2 , v_copy.end()); return (1-j+floor(j))*v_copy[i] + (j-floor(j))*v_copy[i+1]; return percentile(v_copy.begin(), v_copy.end(), p, true); } /** The percentile is determined by the \a p, a number between 0 and 100. The percentile is found by interpolation, using the formula \f$percentile = (1 - \delta) x_i + \delta x_{i+1} \f$ where \a p is floor\f$((n - 1)p/100)\f$ and \f$\delta \f$ is \f$(n-1)p/100 - i \f$.Thus the minimum value of the vector is given by p equal to zero, the maximum is given by p equal to 100 and the median value is given by p equal to 50. If @a sorted is false (default), the vector is copied, the copy is sorted, and then used to calculate the median. @return \a p'th percentile */ double percentile(const utility::vector& vec, const double, const bool sorted=false); ///
• ## trunk/yat/utility/Iterator.h

 r916 /** \return element \a n steps forward */ return_type operator[](difference_type n) const { yat_assert(index_+n < container_->size()); return container_->operator()(index_+n); } /** \brief pre-increment
Note: See TracChangeset for help on using the changeset viewer.