yat  0.13.2pre
TukeyBiweightEstimator.h
1 #ifndef _theplu_yat_statistics_tukey_biweight_estimator
2 #define _theplu_yat_statistics_tukey_biweight_estimator
3 
4 // $Id: TukeyBiweightEstimator.h 3330 2014-10-14 08:03:25Z peter $
5 
6 /*
7  Copyright (C) 2011, 2012, 2014 Peter Johansson
8 
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10 
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15 
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23 */
24 
25 #include "AveragerWeighted.h"
26 #include "utility.h"
27 
28 #include "yat/regression/TukeyBiweight.h"
29 
30 #include "yat/utility/concept_check.h"
31 #include "yat/utility/Exception.h"
32 #include "yat/utility/iterator_traits.h"
33 
34 #include <boost/concept_check.hpp>
35 
36 #include <algorithm>
37 #include <iterator>
38 #include <limits>
39 #include <vector>
40 
41 namespace theplu {
42 namespace yat {
43 namespace statistics {
44 
64  {
65  public:
75  explicit TukeyBiweightEstimator(double cutoff=4.685, bool sorted=false)
76  : cutoff_(cutoff), sorted_(sorted) {}
77 
81  template<typename RandomAccessIterator>
82  double operator()(RandomAccessIterator first,
83  RandomAccessIterator last) const;
84 
85  private:
86  double cutoff_;
87  bool sorted_;
88 
89  template<typename RandomAccessIterator>
90  double estimate(RandomAccessIterator first,
91  RandomAccessIterator last) const;
92 
93  template<typename InputIterator>
94  double estimate(InputIterator first, InputIterator last,
95  double center, double spread) const;
96  };
97 
98  template<typename RandomAccessIterator>
99  double TukeyBiweightEstimator::operator()(RandomAccessIterator first,
100  RandomAccessIterator last) const
101  {
102  BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<RandomAccessIterator>));
104  BOOST_CONCEPT_ASSERT((DataIteratorConcept<RandomAccessIterator>));
105 
106  if (sorted_)
107  return estimate(first, last);
108 
109  // if not sorted, create a sorted copy
110  typedef typename std::iterator_traits<RandomAccessIterator> traits;
111  std::vector<typename traits::value_type> vec(first, last);
112  std::sort(vec.begin(), vec.end());
113  return estimate(vec.begin(), vec.end());
114  }
115 
116 
117  template<typename RandomAccessIterator>
118  double TukeyBiweightEstimator::estimate(RandomAccessIterator first,
119  RandomAccessIterator last) const
120  {
121  BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<RandomAccessIterator>));
123  BOOST_CONCEPT_ASSERT((DataIteratorConcept<RandomAccessIterator>));
124 
125  const double scale = mad(first, last, true);
126  double m = median(first, last, true);
127  // if mad is zero all (non-zero weight) data points are equal and
128  // median is the only sensible estimate. Also, calculations below
129  // would "divide by zero" so we need to interupt here.
130  if (scale==0)
131  return m;
132  double m0 = m+1.0;
133  size_t epoch = 0;
134  // FIXME: let user define convergence requirement
135  while (m!=m0) {
136  m0 = m;
137  m = estimate(first, last, m, scale);
138  ++epoch;
139  // FIXME: let user define maximal epochs
140  if (epoch>1000)
141  utility::runtime_error("TukeyBiweightIterator: too many epochs");
142  }
143  return m;
144  }
145 
146 
147  template<typename InputIterator>
148  double TukeyBiweightEstimator::estimate(InputIterator first,
149  InputIterator last,
150  double center, double spread) const
151  {
152  double scale = spread*cutoff_;
153  AveragerWeighted averager;
154  regression::TukeyBiweight biweight;
155  utility::iterator_traits<InputIterator> traits;
156  for ( ; first!=last; ++first) {
157  double x = traits.data(first);
158  double w = traits.weight(first) * biweight((x-center)/scale);
159  averager.add(x, w);
160  }
161  return averager.mean();
162  }
163 
164 }}} // end of namespace theplu yat statistics
165 # endif
TukeyBiweightEstimator(double cutoff=4.685, bool sorted=false)
Constructor.
Definition: TukeyBiweightEstimator.h:75
double mad(RandomAccessIterator first, RandomAccessIterator last, bool sorted=false)
Median absolute deviation from median.
Definition: utility.h:357
double operator()(RandomAccessIterator first, RandomAccessIterator last) const
Definition: TukeyBiweightEstimator.h:99
Concept check for Data Iterator.
Definition: concept_check.h:226
double median(RandomAccessIterator first, RandomAccessIterator last, bool sorted=false)
Definition: utility.h:380
Class used for all runtime error detected within yat library.
Definition: Exception.h:38
Tukey&#39;s Biweight Estimator.
Definition: TukeyBiweightEstimator.h:63

Generated on Wed Jan 4 2017 02:23:07 for yat by  doxygen 1.8.5