75 #ifndef SLIP_STATISTICS_HPP
76 #define SLIP_STATISTICS_HPP
106 template <
typename T>
212 template<
typename Integer,
typename>
215 template <
typename _II>
219 return static_cast<Integer
>(last-first);
224 template<
typename Integer>
225 struct __cardinal<Integer,std::random_access_iterator_tag>
227 template <
typename _II>
231 return static_cast<Integer
>(last-first);
235 template<
typename Integer>
236 struct __cardinal<Integer,std::random_access_iterator2d_tag>
238 template <
typename _II>
254 template<
typename Integer>
255 struct __cardinal<Integer,std::random_access_iterator3d_tag>
257 template <
typename _II>
283 template<
typename Integer,
typename InputIterator>
288 typedef typename std::iterator_traits<InputIterator>::iterator_category _Category;
312 template<
typename Value_T,
typename InputIterator>
314 Value_T
mean(InputIterator first,
317 assert(first != last);
318 Value_T sum = Value_T(0);
325 return Value_T(sum) / Value_T(count);
350 template<
typename Value_T,
typename InputIterator>
354 InputIterator w_first,
355 InputIterator w_last)
357 assert(first != last);
358 assert(slip::cardinal<std::size_t>(first,last) ==
359 slip::cardinal<std::size_t>(w_first,w_last));
361 Value_T proj = Value_T(0);
362 Value_T sum = Value_T(0);
363 for (; first != last; ++first, ++w_first)
365 proj = proj + (*first) * (*w_first);
366 sum = sum + *w_first;
368 return Value_T(proj) / Value_T(sum);
397 template<
typename T,
int N,
typename InputIterator>
401 assert(last != first);
404 for (; first != last; ++first)
406 __init = __init + slip::nth_power<N>(*first -
mean);
409 return __init / count;
441 template<
typename T,
typename InputIterator,
typename InputIterator2>
444 InputIterator2 first2,
447 assert(last != first);
450 for (; first != last; ++first, ++first2)
452 __init = __init + (*first - mean1) * (*first2 - mean2);
455 return __init / count;
486 template<
typename T,
typename InputIterator,
typename InputIterator2>
489 InputIterator2 first2,
492 assert(last != first);
495 for (; first != last; ++first, ++first2)
497 __init = __init + (*first - mean1) * (*first2 - mean2);
503 result = __init / (count - 1);
533 template<
typename T,
typename InputIterator>
564 template<
typename T,
typename InputIterator>
594 template<
typename T,
typename InputIterator>
624 template<
typename T,
typename InputIterator>
664 template<
typename T,
typename InputIterator>
668 assert(first != last);
672 for (; first != last; ++first)
674 T p2 = slip::nth_power<2>(*first -
mean);
675 __init = __init + p2;
676 __init2 = __init2 + (p2 * p2);
682 result = (__init2 * T(count)) / (__init * __init);
713 template<
typename T,
typename InputIterator>
717 assert(slip::cardinal<int>(first,last) > 3);
721 for (; first != last; ++first)
723 T p2 = slip::nth_power<2>(*first -
mean);
724 __init = __init + p2;
725 __init2 = __init2 + (p2 * p2);
728 T count_m1 = T(count - 1);
729 T count_m2 = T(count - 2);
730 T count_m3 = T(count - 3);
736 result = (((T(count+1)*T(count)* count_m1) * __init2 )
737 / ( (count_m2 * count_m3) * (__init * __init) ))
738 - ( (T(3)* (count_m1*count_m1)) / (count_m2 * count_m3)) + T(3);
768 template<
typename T,
typename InputIterator>
772 assert(first != last);
776 for (; first != last; ++first)
778 T p1 = (*first -
mean);
780 __init = __init + p2;
781 __init2 = __init2 + (p2 * p1);
787 result = __init2 / (T(count)*slip::nth_power<3>(
std::sqrt(__init/T(count))));
818 template<
typename T,
typename InputIterator>
822 assert(slip::cardinal<int>(first,last) > 2);
826 for (; first != last; ++first)
828 T p1 = (*first -
mean);
830 __init = __init + p2;
831 __init2 = __init2 + (p2 * p1);
837 result = (__init2 * T(count)) / (T(count-1)*T(count-2)*slip::nth_power<3>(
std::sqrt(__init/T(count-1))));
864 template<
typename T,
typename InputIterator>
866 T
rms(InputIterator first, InputIterator last)
888 template<
typename InputIterator>
890 typename std::iterator_traits<InputIterator>::value_type
893 return *(first + (last - first) / 2);
910 template<
typename RandomAccessIterator,
typename Size>
912 typename std::iterator_traits<RandomAccessIterator>::value_type
915 return *(first + n/2);
941 template<
typename RandomAccessIterator,
typename Size>
943 typename std::iterator_traits<RandomAccessIterator>::value_type
947 assert(last != first);
974 template<
typename RandomAccessIterator,
976 typename StrictWeakOrdering>
978 typename std::iterator_traits<RandomAccessIterator>::value_type
981 StrictWeakOrdering comp)
983 assert(last != first);
986 std::sort(tmp.
begin(),tmp.
end(),comp);
994 template <
typename _II>
995 static typename std::iterator_traits<_II>::value_type
996 median(_II first, _II last)
998 typedef typename std::iterator_traits<_II>::value_type value_type;
1001 template <
typename _II,
typename StrictWeakOrdering>
1002 static typename std::iterator_traits<_II>::value_type
1003 median(_II first, _II last, StrictWeakOrdering comp)
1005 typedef typename std::iterator_traits<_II>::value_type value_type;
1008 template <
typename _II,
typename SizeType>
1009 static typename std::iterator_traits<_II>::value_type
1010 median_n(_II first, _II last, SizeType n)
1012 typedef typename std::iterator_traits<_II>::value_type value_type;
1015 template <
typename _II,
typename SizeType,
typename StrictWeakOrdering>
1016 static typename std::iterator_traits<_II>::value_type
1017 median_n(_II first, _II last, SizeType n, StrictWeakOrdering comp)
1019 typedef typename std::iterator_traits<_II>::value_type value_type;
1026 struct __median<std::random_access_iterator_tag>
1028 template <
typename _II>
1029 static typename std::iterator_traits<_II>::value_type
1030 median(_II first, _II last)
1032 typedef typename std::iterator_traits<_II>::value_type value_type;
1035 template <
typename _II,
typename StrictWeakOrdering>
1036 static typename std::iterator_traits<_II>::value_type
1037 median(_II first, _II last, StrictWeakOrdering comp)
1039 typedef typename std::iterator_traits<_II>::value_type value_type;
1042 template <
typename _II,
typename SizeType>
1043 static typename std::iterator_traits<_II>::value_type
1044 median_n(_II first, _II last, SizeType n)
1046 typedef typename std::iterator_traits<_II>::value_type value_type;
1050 template <
typename _II,
typename SizeType,
typename StrictWeakOrdering>
1051 static typename std::iterator_traits<_II>::value_type
1052 median_n(_II first, _II last, SizeType n, StrictWeakOrdering comp)
1054 typedef typename std::iterator_traits<_II>::value_type value_type;
1077 template<
typename RandomAccessIterator>
1079 typename std::iterator_traits<RandomAccessIterator>::value_type
1080 median(RandomAccessIterator first, RandomAccessIterator last)
1082 typedef typename std::iterator_traits<RandomAccessIterator>::iterator_category _Category;
1107 template<
typename RandomAccessIterator,
typename StrictWeakOrdering>
1109 typename std::iterator_traits<RandomAccessIterator>::value_type
1110 median(RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp)
1112 typedef typename std::iterator_traits<RandomAccessIterator>::iterator_category _Category;
1137 template<
typename RandomAccessIterator,
typename SizeType>
1139 typename std::iterator_traits<RandomAccessIterator>::value_type
1140 median_n(RandomAccessIterator first, RandomAccessIterator last, SizeType n)
1142 typedef typename std::iterator_traits<RandomAccessIterator>::iterator_category _Category;
1167 template<
typename RandomAccessIterator,
typename SizeType,
typename StrictWeakOrdering>
1169 typename std::iterator_traits<RandomAccessIterator>::value_type
1170 median_n(RandomAccessIterator first, RandomAccessIterator last, SizeType n, StrictWeakOrdering comp)
1172 typedef typename std::iterator_traits<RandomAccessIterator>::iterator_category _Category;
1196 template<
typename RandomAccessIterator>
1198 typename std::iterator_traits<RandomAccessIterator>::value_type
1201 assert(last != first);
1202 std::size_t n = slip::cardinal<std::size_t>(first,last);
1205 std::sort(tmp.begin(),tmp.end());
1206 return *(tmp.begin()+n/4);
1227 template<
typename RandomAccessIterator>
1229 typename std::iterator_traits<RandomAccessIterator>::value_type
1232 assert(last != first);
1233 std::size_t n=slip::cardinal<std::size_t>(first,last);
1236 std::sort(tmp.begin(),tmp.end());
1237 return *((tmp.end()-1)-n/4);
1267 template<
typename ForwardIterator>
1269 std::vector<ForwardIterator>&
max,
1270 const std::size_t n = 1)
1273 assert(n <= slip::cardinal<std::size_t>(first,last));
1274 assert(max.size() == n);
1278 max[0] = std::max_element(first,last);
1282 std::multiset<ForwardIterator,slip::gt_it<ForwardIterator> > S;
1284 while(first != last)
1290 typename std::multiset<ForwardIterator,gt_it<ForwardIterator> >::iterator it = S.begin();
1291 for(std::size_t i = 0; i < n; ++i)
1325 template<
typename ForwardIterator>
1327 std::vector<ForwardIterator>&
min,
1328 const std::size_t n = 1)
1330 assert(n <= slip::cardinal<std::size_t>(first,last));
1331 assert(min.size() == n);
1335 min[0] = std::min_element(first,last);
1339 std::multiset<ForwardIterator,slip::lt_it<ForwardIterator> > S;
1341 while(first != last)
1347 typename std::multiset<ForwardIterator,lt_it<ForwardIterator> >::iterator it = S.begin();
1348 for(std::size_t i = 0; i < n; ++i)
1380 template<
typename RandomAccessIterator,
typename RandomAccessIterator2>
1381 void center(RandomAccessIterator first, RandomAccessIterator last,
1382 RandomAccessIterator2 out_first)
1384 typedef typename std::iterator_traits<RandomAccessIterator2>::value_type value_type;
1385 value_type
mean = slip::mean<value_type>(first,last);
1387 for (; first != last; ++first, ++out_first)
1389 *out_first = (*first -
mean);
1417 template<
typename RandomAccessIterator,
typename RandomAccessIterator2>
1418 void studentize(RandomAccessIterator first, RandomAccessIterator last,
1419 RandomAccessIterator2 out_first)
1421 typedef typename std::iterator_traits<RandomAccessIterator2>::value_type value_type;
1422 value_type
mean = slip::mean<value_type>(first,last);
1423 value_type
std_dev = slip::std_dev<value_type>(first,last,
mean);
1424 if(std_dev != value_type(0))
1426 for (; first != last; ++first, ++out_first)
1428 *out_first = (*first -
mean) / std_dev;
1433 for (; first != last; ++first, ++out_first)
1435 *out_first = (*first -
mean);
1475 template<
typename Value_T,
1476 typename InputIterator,
1477 typename MaskIterator>
1480 MaskIterator mask_first,
1481 typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
1483 assert(first != last);
1484 Value_T sum = Value_T(0);
1486 while(first != last)
1488 if(*mask_first == value)
1497 Value_T result = Value_T(0);
1500 result = Value_T(sum) / Value_T(count);
1538 template<
typename T,
int N,
typename InputIterator,
typename MaskIterator>
1540 T
nth_moment_mask(InputIterator first, InputIterator last, MaskIterator mask_first, T
mean,
typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
1544 for (; first != last; ++first,++mask_first)
1546 if(*mask_first == value)
1548 __init = __init + slip::nth_power<N>(*first -
mean);
1555 result = __init /
static_cast<T
>(count);
1599 template<
typename T,
typename InputIterator,
typename InputIterator2,
typename MaskIterator>
1602 InputIterator2 first2, MaskIterator mask_first,
1603 T mean1, T mean2,
typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
1605 assert(first != last);
1608 for (; first != last; ++first, ++first2,++mask_first)
1610 if(*mask_first == value)
1612 __init = __init + (*first - mean1) * (*first2 - mean2);
1619 result = __init /
static_cast<T
>(count);
1662 template<
typename T,
typename InputIterator,
typename InputIterator2,
typename MaskIterator>
1665 InputIterator2 first2, MaskIterator mask_first,
1666 T mean1, T mean2,
typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
1670 for (; first != last; ++first, ++first2,++mask_first)
1672 if(*mask_first == value)
1674 __init = __init + (*first - mean1) * (*first2 - mean2);
1682 result = __init /
static_cast<T
>(count-1);
1724 template<
typename T,
typename InputIterator,
typename MaskIterator>
1726 T
variance_mask(InputIterator first, InputIterator last, MaskIterator mask_first, T mean,
typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
1768 template<
typename T,
typename InputIterator,
typename MaskIterator>
1770 T
unbiased_variance_mask(InputIterator first, InputIterator last, MaskIterator mask_first, T mean,
typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
1810 template<
typename T,
typename InputIterator,
typename MaskIterator>
1812 T
std_dev_mask(InputIterator first, InputIterator last, MaskIterator mask_first, T mean,
typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
1853 template<
typename T,
typename InputIterator,
typename MaskIterator>
1855 T
unbiased_std_dev_mask(InputIterator first, InputIterator last, MaskIterator mask_first, T mean,
typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
1905 template<
typename T,
typename InputIterator,
typename MaskIterator>
1907 T
kurtosis_mask(InputIterator first, InputIterator last, MaskIterator mask_first, T mean,
typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
1912 for (; first != last; ++first,++mask_first)
1914 if (*mask_first == value)
1916 T p2 = slip::nth_power<2>(*first -
mean);
1917 __init = __init + p2;
1918 __init2 = __init2 + (p2 * p2);
1925 result = (__init2 * count) / (__init * __init);
1967 template<
typename T,
1968 typename InputIterator,
1969 typename MaskIterator>
1972 MaskIterator mask_first,
1974 typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
1979 for (; first != last; ++first,++mask_first)
1981 if (*mask_first == value)
1983 T p2 = slip::nth_power<2>(*first -
mean);
1984 __init = __init + p2;
1985 __init2 = __init2 + (p2 * p2);
1990 T count_m1 = T(count - 1);
1991 T count_m2 = T(count - 2);
1992 T count_m3 = T(count - 3);
1997 result = (((T(count+1)*T(count)* count_m1) * __init2 )
1998 / ( (count_m2 * count_m3) * (__init * __init) ))
1999 - ( (T(3)* (count_m1*count_m1)) / (count_m2 * count_m3)) + T(3);
2043 template<
typename T,
2044 typename InputIterator,
2045 typename MaskIterator>
2048 MaskIterator mask_first,
2050 typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
2055 for (; first != last; ++first,++mask_first)
2057 if (*mask_first == value)
2059 T p1 = (*first -
mean);
2061 __init = __init + p2;
2062 __init2 = __init2 + (p2 * p1);
2071 result = __init2 / (T(count)*slip::nth_power<3>(
std::sqrt(__init/T(count))));
2114 template<
typename T,
2115 typename InputIterator,
2116 typename MaskIterator>
2119 MaskIterator mask_first,
2121 typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
2126 for (; first != last; ++first,++mask_first)
2128 if (*mask_first == value)
2130 T p1 = (*first -
mean);
2132 __init = __init + p2;
2133 __init2 = __init2 + (p2 * p1);
2142 result = (__init2 * T(count)) / (T(count-1)*T(count-2)*slip::nth_power<3>(
std::sqrt(__init/T(count-1))));
2181 template<
typename T,
2182 typename InputIterator,
2183 typename MaskIterator>
2186 MaskIterator mask_first,
2187 typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
2219 template<
typename Integer,
typename MaskIterator>
2222 MaskIterator mask_last,
2223 typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
2226 while(mask_first != mask_last)
2228 if (*mask_first == value)
2254 template<
typename InputIterator,
2255 typename MaskIterator>
2257 typename std::iterator_traits<InputIterator>::value_type
2259 MaskIterator mask_first,
2260 typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
2262 MaskIterator mask_last=mask_first+(last - first);
2263 std::size_t mask_count=slip::cardinal_mask<std::size_t>(mask_first,mask_last,value);
2264 assert(mask_count != 0);
2265 std::size_t count = 0;
2266 while(first != last)
2268 if (*mask_first == value)
2272 if (count>mask_count/2)
2279 std::cerr <<
"Error computing median_from_sorted_data_mask"<<std::endl;
2314 template<
typename InputIterator,
typename MaskIterator>
2316 typename std::iterator_traits<InputIterator>::value_type
2318 MaskIterator mask_first,
2319 typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
2321 std::size_t mask_count =
2322 slip::cardinal_mask<std::size_t>(mask_first,mask_first+(last-first),
2325 assert(mask_count != 0);
2327 std::size_t count = 0;
2328 while(first != last)
2330 if (*mask_first == value)
2375 template<
typename InputIterator,
2376 typename MaskIterator>
2378 typename std::iterator_traits<InputIterator>::value_type
2380 MaskIterator mask_first,
2381 typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
2383 std::size_t mask_count = slip::cardinal_mask<std::size_t>(mask_first,mask_first+(last-first),value);
2384 assert(mask_count != 0);
2386 std::size_t count = 0;
2387 while(first != last)
2389 if (*mask_first == value)
2435 template<
typename InputIterator,
typename MaskIterator>
2437 typename std::iterator_traits<InputIterator>::value_type
2438 third_quartile_mask(InputIterator first, InputIterator last, MaskIterator mask_first,
typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
2440 std::size_t mask_count=slip::cardinal_mask<std::size_t>(mask_first,mask_first+(last-first),value);
2441 assert(mask_count != 0);
2444 std::size_t count = 0;
2445 while(first != last)
2447 if (*mask_first == value)
2488 template<
typename InputIterator,
typename MaskIterator,
typename OutputIterator>
2491 MaskIterator mask_first,
2492 OutputIterator out_first,
2493 typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
2495 assert(first != last);
2496 typedef typename std::iterator_traits<OutputIterator>::value_type value_type;
2498 value_type mean = slip::mean_mask<value_type>(first,last,mask_first,value);
2500 for (; first != last; ++first, ++out_first, ++mask_first)
2502 if(*mask_first == value)
2504 *out_first = (*first -
mean);
2538 template<
typename InputIterator,
typename MaskIterator,
typename OutputIterator>
2541 MaskIterator mask_first,
2542 OutputIterator out_first,
2543 typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
2545 assert(first != last);
2546 typedef typename std::iterator_traits<OutputIterator>::value_type value_type;
2548 value_type mean = slip::mean_mask<value_type>(first,last,mask_first,value);
2549 value_type
std_dev = slip::std_dev_mask<value_type>(first,last,mask_first,
mean,value);
2551 if(std_dev != value_type(0))
2553 for (; first != last; ++first, ++out_first, ++mask_first)
2555 if(*mask_first == value)
2557 *out_first = (*first -
mean) / std_dev;
2563 for (; first != last; ++first, ++out_first, ++mask_first)
2565 if(*mask_first == value)
2567 *out_first = (*first -
mean);
2603 template<
typename Integer,
typename InputIterator,
typename Predicate>
2610 while(first != last)
2650 template<
typename Value_T,
typename InputIterator,
typename Predicate>
2652 Value_T
mean_if(InputIterator first, InputIterator last, Predicate pred)
2654 assert(first != last);
2655 Value_T sum = Value_T(0);
2657 while(first != last)
2661 sum += *first; ++count;
2666 Value_T result = Value_T(0);
2669 result = Value_T(sum) / Value_T(count);
2705 template<
typename T,
int N,
typename InputIterator,
typename Predicate>
2707 T
nth_moment_if(InputIterator first, InputIterator last, T mean, Predicate pred)
2711 for (; first != last; ++first)
2715 __init = __init + slip::nth_power<N>(*first -
mean);
2722 result = __init / count;
2763 template<
typename T,
2764 typename InputIterator,
2765 typename InputIterator2,
2769 InputIterator2 first2,
2770 T mean1, T mean2, Predicate pred)
2774 for (; first != last; ++first, ++first2)
2778 __init = __init + (*first - mean1) * (*first2 - mean2);
2785 result = __init / count;
2825 template<
typename T,
typename InputIterator,
typename InputIterator2,
typename Predicate>
2828 InputIterator2 first2,
2829 T mean1, T mean2,Predicate pred)
2833 for (; first != last; ++first, ++first2)
2837 __init = __init + (*first - mean1) * (*first2 - mean2);
2844 result = __init /
static_cast<T
>(count - 1);
2884 template<
typename T,
typename InputIterator,
typename Predicate>
2886 T
variance_if(InputIterator first, InputIterator last, T mean, Predicate pred)
2926 template<
typename T,
typename InputIterator,
typename Predicate>
2967 template<
typename T,
typename InputIterator,
typename Predicate>
2969 T
std_dev_if(InputIterator first, InputIterator last, T mean, Predicate pred)
3009 template<
typename T,
typename InputIterator,
typename Predicate>
3059 template<
typename T,
typename InputIterator,
typename Predicate>
3061 T
kurtosis_if(InputIterator first, InputIterator last, T mean, Predicate pred)
3066 for (; first != last; ++first)
3070 T p2 = slip::nth_power<2>(*first -
mean);
3071 __init = __init + p2;
3072 __init2 = __init2 + (p2 * p2);
3079 result = (__init2 * T(count)) / (__init * __init);
3119 template<
typename T,
typename InputIterator,
typename Predicate>
3126 for (; first != last; ++first)
3129 T p2 = slip::nth_power<2>(*first -
mean);
3130 __init = __init + p2;
3131 __init2 = __init2 + (p2 * p2);
3136 T count_m1 = T(count - 1);
3137 T count_m2 = T(count - 2);
3138 T count_m3 = T(count - 3);
3143 result = (((T(count+1)*T(count)* count_m1) * __init2 )
3144 / ( (count_m2 * count_m3) * (__init * __init) ))
3145 - ( (T(3)* (count_m1*count_m1)) / (count_m2 * count_m3)) + T(3);
3186 template<
typename T,
typename InputIterator,
typename Predicate>
3188 T
skewness_if(InputIterator first, InputIterator last, T mean, Predicate pred)
3193 for (; first != last; ++first)
3197 T p1 = (*first -
mean);
3199 __init = __init + p2;
3200 __init2 = __init2 + (p2 * p1);
3208 result = __init2 / (T(count)*slip::nth_power<3>(
std::sqrt(__init/T(count))));
3248 template<
typename T,
typename InputIterator,
typename Predicate>
3255 for (; first != last; ++first)
3259 T p1 = (*first -
mean);
3261 __init = __init + p2;
3262 __init2 = __init2 + (p2 * p1);
3270 result = (__init2 * T(count)) / (T(count-1)*T(count-2)*slip::nth_power<3>(
std::sqrt(__init/T(count-1))));
3307 template<
typename T,
typename InputIterator,
typename Predicate>
3309 T
rms_if(InputIterator first, InputIterator last, Predicate pred)
3331 template<
typename InputIterator,
typename Predicate>
3333 typename std::iterator_traits<InputIterator>::value_type
3337 std::size_t mask_count=slip::cardinal_if<std::size_t>(first,last,pred);
3338 assert(mask_count != 0);
3339 std::size_t count = 0;
3340 while(first != last)
3346 if (count > mask_count/2)
3352 std::cerr <<
"Error computing median_from_sorted_data_if"<<std::endl;
3384 template<
typename InputIterator,
typename Predicate>
3386 typename std::iterator_traits<InputIterator>::value_type
3390 std::size_t mask_count=slip::cardinal_if<std::size_t>(first,last,pred);
3391 assert(mask_count != 0);
3393 std::size_t count = 0;
3395 while(first != last)
3437 template<
typename InputIterator,
typename Predicate>
3439 typename std::iterator_traits<InputIterator>::value_type
3442 std::size_t mask_count=slip::cardinal_if<std::size_t>(first,last,pred);
3443 assert(mask_count != 0);
3445 std::size_t count = 0;
3447 while(first != last)
3451 tmp[count] = *first;
3491 template<
typename InputIterator,
typename Predicate>
3493 typename std::iterator_traits<InputIterator>::value_type
3496 std::size_t mask_count=slip::cardinal_if<std::size_t>(first,last,pred);
3497 assert(mask_count != 0);
3499 std::size_t count = 0;
3501 while(first != last)
3505 tmp[count] = *first;
3546 template<
typename InputIterator,
typename Predicate,
typename OutputIterator>
3550 OutputIterator out_first)
3552 assert(first != last);
3554 typedef typename std::iterator_traits<OutputIterator>::value_type value_type;
3555 value_type mean = slip::mean_if<value_type>(first,last,pred);
3557 while(first != last)
3561 *out_first = (*first -
mean);
3598 template<
typename InputIterator,
typename Predicate,
typename OutputIterator>
3602 OutputIterator out_first)
3604 assert(first != last);
3606 typedef typename std::iterator_traits<OutputIterator>::value_type value_type;
3607 value_type mean = slip::mean_if<value_type>(first,last,pred);
3608 value_type
std_dev = slip::std_dev_if<value_type>(first,last,
mean,pred);
3610 if(std_dev != value_type(0))
3612 while(first != last)
3616 *out_first = (*first -
mean) / std_dev;
3624 while(first != last)
3628 *out_first = (*first -
mean);
3664 template <
typename InputIterator,
typename T>
3670 std::size_t n = slip::cardinal<std::size_t>(first,last);
3673 std::sort(tmp.begin(),tmp.end());
3676 statistic.
min() = *tmp.begin();
3678 statistic.
median() = *(tmp.begin()+n/2);
3680 statistic.
max() = *(tmp.end() - 1);
3681 statistic.
mean() = slip::mean<T>(first,last);
3713 template <
typename InputIterator,
typename T>
3719 std::size_t n = slip::cardinal<std::size_t>(first,last);
3722 std::sort(tmp.begin(),tmp.end());
3725 statistic.
min() = *tmp.begin();
3727 statistic.
median() = *(tmp.begin()+n/2);
3729 statistic.
max() = *(tmp.end() - 1);
3730 statistic.
mean() = slip::mean<T>(first,last);
3790 template <
typename InputIterator,
typename T,
typename MaskIterator>
3793 MaskIterator mask_first,
3795 typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
3799 std::size_t n = slip::cardinal_mask<std::size_t>(mask_first,mask_first+(last-first),value);
3802 std::size_t count = 0;
3803 while(first != last)
3805 if (*mask_first == value)
3807 tmp[count] = *first;
3823 statistic.
max() = *(tmp.
end() - 1);
3824 statistic.
mean() = slip::mean<T>(tmp.
begin(),tmp.
end());
3883 template <
typename InputIterator,
typename T,
typename MaskIterator>
3886 MaskIterator mask_first,
3888 typename std::iterator_traits<MaskIterator>::value_type value=
typename std::iterator_traits<MaskIterator>::value_type(1))
3892 std::size_t n = slip::cardinal_mask<std::size_t>(mask_first,mask_first+(last-first),value);
3895 std::size_t count = 0;
3896 while(first != last)
3898 if (*mask_first == value)
3900 tmp[count] = *first;
3916 statistic.
max() = *(tmp.
end() - 1);
3917 statistic.
mean() = slip::mean<T>(tmp.
begin(),tmp.
end());
3953 template <
typename InputIterator,
typename T,
typename Predicate>
3961 std::size_t n = slip::cardinal_if<std::size_t>(first,last,pred);
3964 std::size_t count = 0;
3965 while(first != last)
3984 statistic.
max() = *(tmp.
end() - 1);
3985 statistic.
mean() = slip::mean<T>(tmp.
begin(),tmp.
end());
4021 template <
typename InputIterator,
typename T,
typename Predicate>
4029 std::size_t n = slip::cardinal_if<std::size_t>(first,last,pred);
4032 std::size_t count = 0;
4033 while(first != last)
4052 statistic.
max() = *(tmp.
end() - 1);
4053 statistic.
mean() = slip::mean<T>(tmp.
begin(),tmp.
end());
4087 template<
typename DataT,
4090 const MeanT& prev_mean,
4093 MeanT an =
static_cast<MeanT
>(1)/static_cast<MeanT>(n+1);
4094 MeanT bn =
static_cast<MeanT
>(n)*an;
4095 return bn * prev_mean + an * xnp1;
4125 template<
typename DataT,
4128 const VarT& prev_mean,
4129 const VarT& prev_var,
4134 VarT an =
static_cast<VarT
>(1)/static_cast<VarT>(n+1);
4135 VarT bn =
static_cast<VarT
>(n)*an;
4137 VarT s = (
static_cast<VarT
>(xnp1) - prev_mean);
4138 next_mean = bn * prev_mean + an *
static_cast<VarT
>(xnp1);
4139 next_var = bn * prev_var + cn * (s * s);
4145 #endif //SLIP_STATISTICS_HPP
T unbiased_kurtosis_if(InputIterator first, InputIterator last, T mean, Predicate pred)
Computes the unbiased kurtosis of a range using a predicate A high kurtosis distribution has a sharp...
Integer cardinal_mask(MaskIterator mask_first, MaskIterator mask_last, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the cardinal or the number of elements of a mask range.
T unbiased_skewness_mask(InputIterator first, InputIterator last, MaskIterator mask_first, T mean, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the unbiased skewness of a range over a mask skewness is a measure of the asymmetry of the ...
T & kurtosis()
Returns the kurtosis.
T covariance_mask(InputIterator first, InputIterator last, InputIterator2 first2, MaskIterator mask_first, T mean1, T mean2, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the covariance of a two sequences over a mask .
T unbiased_skewness(InputIterator first, InputIterator last, T mean)
Computes the unbiased skewness of a range skewness is a measure of the asymmetry of the probability ...
T kurtosis_mask(InputIterator first, InputIterator last, MaskIterator mask_first, T mean, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the kurtosis of a range over a mask A high kurtosis distribution has a sharper peak and lon...
std::iterator_traits< InputIterator >::value_type third_quartile_if(InputIterator first, InputIterator last, Predicate pred)
Computes the third quartile value from a non sorted range using a predicate.
std::iterator_traits< InputIterator >::value_type median_mask(InputIterator first, InputIterator last, MaskIterator mask_first, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the median value from a non sorted range over a mask.
T skewness_mask(InputIterator first, InputIterator last, MaskIterator mask_first, T mean, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the skewness of a range over a mask skewness is a measure of the asymmetry of the probabili...
void center(RandomAccessIterator first, RandomAccessIterator last, RandomAccessIterator2 out_first)
Substracts its mean to a range.
T kurtosis_if(InputIterator first, InputIterator last, T mean, Predicate pred)
Computes the kurtosis of a range using a predicate A high kurtosis distribution has a sharper peak a...
T variance_mask(InputIterator first, InputIterator last, MaskIterator mask_first, T mean, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the variance of a range over a mask .
T & max()
Returns the first maximal value.
T & std_dev()
Returns the standard deviation.
T unbiased_std_dev(InputIterator first, InputIterator last, T mean)
Computes the unbiased standard deviation of a range .
std::iterator_traits< InputIterator >::value_type median_from_sorted_data(InputIterator first, InputIterator last)
Computes the median value from a sorted range: returns *(first + (last - first) / 2) ...
std::iterator_traits< InputIterator >::value_type first_quartile_mask(InputIterator first, InputIterator last, MaskIterator mask_first, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the first quartile value from a non sorted range over a mask.
T unbiased_covariance_if(InputIterator first, InputIterator last, InputIterator2 first2, T mean1, T mean2, Predicate pred)
Computes the unbiased covariance of a two sequences using a predicate on the first one ...
T & min(const GrayscaleImage< T > &M1)
Returns the min element of a GrayscaleImage.
std::iterator_traits< RandomAccessIterator >::value_type first_quartile(RandomAccessIterator first, RandomAccessIterator last)
Computes the first quartile value from a non sorted range.
Value_T mean_if(InputIterator first, InputIterator last, Predicate pred)
Computes the mean value of a range using a predicate .
T unbiased_kurtosis(InputIterator first, InputIterator last, T mean)
Computes the unbiased kurtosis of a range A high kurtosis distribution has a sharper peak and longer...
Provides a class to tag SLIP iterators.
T unbiased_variance_if(InputIterator first, InputIterator last, T mean, Predicate pred)
Computes the unbiased variance of a range using a predicate .
void statistics_if(InputIterator first, InputIterator last, slip::Statistics< T > &statistic, Predicate pred)
compute all statistics of a range according to a preidcate.
iterator begin()
Returns a read/write iterator that points to the first element in the Array. Iteration is done in ord...
std::iterator_traits< InputIterator >::value_type third_quartile_mask(InputIterator first, InputIterator last, MaskIterator mask_first, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the third quartile value from a non sorted range over a mask.
T unbiased_variance_mask(InputIterator first, InputIterator last, MaskIterator mask_first, T mean, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the unbiased variance of a range over a mask .
std::iterator_traits< RandomAccessIterator >::value_type median_from_data_n(RandomAccessIterator first, RandomAccessIterator last, Size n)
Computes the median value from a non sorted range.
void n_max_elements(ForwardIterator first, ForwardIterator last, std::vector< ForwardIterator > &max, const std::size_t n=1)
finds the n largest elements in the range [first, last).
void unbiased_statistics(InputIterator first, InputIterator last, slip::Statistics< T > &statistic)
compute all unbiased statistics of a range
std::iterator_traits< RandomAccessIterator >::value_type median_n(RandomAccessIterator first, RandomAccessIterator last, SizeType n, StrictWeakOrdering comp)
Computes the median value from a range of size n.
T nth_moment(InputIterator first, InputIterator last, T mean)
Computes the nth moment of a range .
std::iterator_traits< RandomAccessIterator >::value_type median(RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp)
Computes the median value from a range.
T std_dev_mask(InputIterator first, InputIterator last, MaskIterator mask_first, T mean, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the standard deviation of a range over a mask .
Integer cardinal_if(InputIterator first, InputIterator last, Predicate pred)
Computes the cardinal or the number of elements of a range using a predicate.
std::iterator_traits< InputIterator >::value_type first_quartile_if(InputIterator first, InputIterator last, Predicate pred)
Computes the first quartile value from a non sorted range using a predicate.
This is a structure to store descriptive statistics.
T rms(InputIterator first, InputIterator last)
Computes the root mean square (rms) value of a range .
std::iterator_traits< RandomAccessIterator >::value_type third_quartile(RandomAccessIterator first, RandomAccessIterator last)
Computes the third quartile value from a non sorted range.
T unbiased_std_dev_if(InputIterator first, InputIterator last, T mean, Predicate pred)
Computes the unbiased standard deviation of a range using a predicate .
slip::block< T, 10 > all()
Returns a slip::block<T,10> with all the statistics.
T & min()
Returns the minimum value.
Value_T weighted_mean(InputIterator first, InputIterator last, InputIterator w_first, InputIterator w_last)
Computes the weighted mean value of a range: .
std::iterator_traits< InputIterator >::value_type median_from_sorted_data_mask(InputIterator first, InputIterator last, MaskIterator mask_first, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the median value from a sorted range over a mask.
std::iterator_traits< RandomAccessIterator >::value_type median(RandomAccessIterator first, RandomAccessIterator last)
Computes the median value from a range.
void center_mask(InputIterator first, InputIterator last, MaskIterator mask_first, OutputIterator out_first, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Substracts its mean to a range according a mask.
T & first_quartile()
Returns the first quartile value.
std::iterator_traits< InputIterator >::value_type median_from_sorted_data_if(InputIterator first, InputIterator last, Predicate pred)
Computes the median value from a sorted range using a predicate.
Provides a class to manipulate 1d static and generic arrays.
T rms_if(InputIterator first, InputIterator last, Predicate pred)
Computes the root mean square (rms) value of a range using a predicate .
Provides a class to manipulate 1d dynamic and generic arrays.
slip::block< T, 10 > stat_
void statistics_mask(InputIterator first, InputIterator last, MaskIterator mask_first, slip::Statistics< T > &statistic, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
compute all statistics of a range according to a mask range.
void studentize_mask(InputIterator first, InputIterator last, MaskIterator mask_first, OutputIterator out_first, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Substracts its mean to a range and divide it by its standard deviation according a mask...
void center_if(InputIterator first, InputIterator last, Predicate pred, OutputIterator out_first)
Substracts its mean to a range using a predicate.
T & mean()
Returns the mean value.
void studentize_if(InputIterator first, InputIterator last, Predicate pred, OutputIterator out_first)
Substracts its mean to a range and divide it by its standard deviation using a predicate.
void studentize(RandomAccessIterator first, RandomAccessIterator last, RandomAccessIterator2 out_first)
Substracts its mean to a range and divide it by its standard deviation.
T variance(InputIterator first, InputIterator last, T mean)
Computes the variance of a range .
T unbiased_skewness_if(InputIterator first, InputIterator last, T mean, Predicate pred)
Computes the unbiased skewness of a range using a predicate skewness is a measure of the asymmetry o...
MeanT mean_next(const int n, const MeanT &prev_mean, const DataT &xnp1)
Recursive mean algorithm.
T & third_quartile()
Returns the third quartile value.
T variance_if(InputIterator first, InputIterator last, T mean, Predicate pred)
Computes the variance of a range using a predicate .
iterator end()
Returns a read/write iterator that points one past the last element in the Array. Iteration is done i...
T unbiased_kurtosis_mask(InputIterator first, InputIterator last, MaskIterator mask_first, T mean, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the unbiased kurtosis of a range over a mask A high kurtosis distribution has a sharper pea...
T unbiased_covariance_mask(InputIterator first, InputIterator last, InputIterator2 first2, MaskIterator mask_first, T mean1, T mean2, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the unbiased covariance of a two sequences over a mask .
T std_dev_if(InputIterator first, InputIterator last, T mean, Predicate pred)
Computes the standard deviation of a range using a predicate .
Value_T mean(InputIterator first, InputIterator last)
Computes the mean value of a range .
T rms_mask(InputIterator first, InputIterator last, MaskIterator mask_first, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the root mean square (rms) value of a range over a mask .
Provides some mathematical functors and constants.
HyperVolume< T > sqrt(const HyperVolume< T > &M)
T nth_moment_mask(InputIterator first, InputIterator last, MaskIterator mask_first, T mean, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the nth moment of a range over a mask .
std::iterator_traits< InputIterator >::value_type median_if(InputIterator first, InputIterator last, Predicate pred)
Computes the median value from a non sorted range using a predicate.
T & skewness()
Returns the skewness.
T unbiased_std_dev_mask(InputIterator first, InputIterator last, MaskIterator mask_first, T mean, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the unbiased standard deviation of a range over a mask .
Integer cardinal(InputIterator first, InputIterator last)
Computes the cardinal or the number of elements of a range.
void unbiased_statistics_if(InputIterator first, InputIterator last, slip::Statistics< T > &statistic, Predicate pred)
compute all unbiased statistics of a range according to a preidcate.
T nth_moment_if(InputIterator first, InputIterator last, T mean, Predicate pred)
Computes the nth moment of a range using a predicate .
T covariance_if(InputIterator first, InputIterator last, InputIterator2 first2, T mean1, T mean2, Predicate pred)
Computes the covariance of a two sequences using a predicate on the first one .
std::iterator_traits< RandomAccessIterator >::value_type median_from_sorted_data_n(RandomAccessIterator first, Size n)
Computes the median value from a sorted range: returns *(first + n/2).
void unbiased_statistics_mask(InputIterator first, InputIterator last, MaskIterator mask_first, slip::Statistics< T > &statistic, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
compute all unbiased statistics of a range according to a mask range.
Value_T mean_mask(InputIterator first, InputIterator last, MaskIterator mask_first, typename std::iterator_traits< MaskIterator >::value_type value=typename std::iterator_traits< MaskIterator >::value_type(1))
Computes the mean value of a range over a mask .
void n_min_elements(ForwardIterator first, ForwardIterator last, std::vector< ForwardIterator > &min, const std::size_t n=1)
finds the n smallest elements in the range [first, last).
T skewness(InputIterator first, InputIterator last, T mean)
Computes the skewness of a range skewness is a measure of the asymmetry of the probability distribut...
T covariance(InputIterator first, InputIterator last, InputIterator2 first2, T mean1, T mean2)
Computes the covariance of a two sequences .
T & cardinal()
Returns the cardinal.
void var_next(const int n, const VarT &prev_mean, const VarT &prev_var, const DataT &xnp1, VarT &next_mean, VarT &next_var)
Recursive variance algorithm.
T std_dev(InputIterator first, InputIterator last, T mean)
Computes the standard deviation of a range .
void statistics(InputIterator first, InputIterator last, slip::Statistics< T > &statistic)
compute all statistics of a range
T unbiased_variance(InputIterator first, InputIterator last, T mean)
Computes the unbiased variance of a range .
T & median()
Returns the median value.
This is a linear (one-dimensional) dynamic template container. This container statisfies the RandomAc...
std::iterator_traits< RandomAccessIterator >::value_type median_n(RandomAccessIterator first, RandomAccessIterator last, SizeType n)
Computes the median value from a range of size n.
T & max(const GrayscaleImage< T > &M1)
Returns the max element of a GrayscaleImage.
T kurtosis(InputIterator first, InputIterator last, T mean)
Computes the kurtosis of a range A high kurtosis distribution has a sharper peak and longer...
T unbiased_covariance(InputIterator first, InputIterator last, InputIterator2 first2, T mean1, T mean2)
Computes the unbiased covariance of a two sequences .
T skewness_if(InputIterator first, InputIterator last, T mean, Predicate pred)
Computes the skewness of a range using a predicate skewness is a measure of the asymmetry of the pro...