Changeset 3917


Ignore:
Timestamp:
May 31, 2020, 7:26:09 AM (3 years ago)
Author:
Peter
Message:

allow user to define stop condition

Location:
trunk/yat/statistics
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/statistics/Makefile.am

    r3792 r3917  
    4040  yat/statistics/SAMScore.cc yat/statistics/Score.cc \
    4141  yat/statistics/Smoother.cc yat/statistics/SNRScore.cc \
    42   yat/statistics/Spearman.cc yat/statistics/tScore.cc \
     42  yat/statistics/Spearman.cc \
     43  yat/statistics/TukeyBiweightEstimator.cc \
     44  yat/statistics/tScore.cc \
    4345  yat/statistics/tTest.cc \
    4446  yat/statistics/utility.cc yat/statistics/VectorFunction.cc \
  • trunk/yat/statistics/TukeyBiweightEstimator.h

    r3875 r3917  
    6565  {
    6666  public:
     67    /**
     68       \since New in yat 0.18
     69     */
     70    class Stopper
     71    {
     72    public:
     73      /**
     74         Default constructor sets max epochs to 1000
     75       */
     76      Stopper(size_t max_epochs=1000);
     77      /**
     78         \return true if \a current is sufficiently close to \a previous
     79       */
     80      virtual bool operator()(double current, double previous) const=0;
     81    protected:
     82      /// \return number of epochs
     83      size_t epochs(void) const;
     84      /// \return number of max epochs
     85      size_t max_epochs(void) const;
     86    private:
     87      size_t epochs_;
     88      size_t max_epochs_;
     89      friend TukeyBiweightEstimator;
     90      bool stop(double current, double previous);
     91    };
     92
    6793    /**
    6894       \brief Constructor
     
    91117                      RandomAccessIterator last) const;
    92118
     119    /**
     120       \since New in yat 0.18
     121     */
     122    template<typename RandomAccessIterator>
     123    double operator()(RandomAccessIterator first,
     124                      RandomAccessIterator last,
     125                      Stopper& stopper) const;
     126
    93127  private:
    94128    double cutoff_;
     
    97131    template<typename RandomAccessIterator>
    98132    double estimate(RandomAccessIterator first,
    99                     RandomAccessIterator last) const;
     133                    RandomAccessIterator last,
     134                    Stopper& stopper) const;
    100135
    101136    template<typename InputIterator>
    102137    double estimate(InputIterator first, InputIterator last,
    103138                    double center, double spread) const;
     139
     140    class DefaultStopper : public Stopper
     141    {
     142    public:
     143      bool operator()(double current, double previous) const;
     144    };
    104145  };
     146
    105147
    106148  template<typename RandomAccessIterator>
    107149  double TukeyBiweightEstimator::operator()(RandomAccessIterator first,
    108150                                            RandomAccessIterator last) const
     151  {
     152    TukeyBiweightEstimator::DefaultStopper stopper;
     153    return (*this)(first, last, stopper);
     154  }
     155
     156
     157  template<typename RandomAccessIterator>
     158  double
     159  TukeyBiweightEstimator::operator()(RandomAccessIterator first,
     160                                     RandomAccessIterator last,
     161                                     TukeyBiweightEstimator::Stopper& stopper) const
    109162  {
    110163    using boost_concepts::RandomAccessTraversal;
     
    114167
    115168    if (sorted_)
    116       return estimate(first, last);
     169      return estimate(first, last, stopper);
    117170
    118171    // if not sorted, create a sorted copy
     
    120173    std::vector<typename traits::value_type> vec(first, last);
    121174    std::sort(vec.begin(), vec.end());
    122     return estimate(vec.begin(), vec.end());
     175    return estimate(vec.begin(), vec.end(), stopper);
    123176  }
    124177
    125178
    126179  template<typename RandomAccessIterator>
    127   double TukeyBiweightEstimator::estimate(RandomAccessIterator first,
    128                                           RandomAccessIterator last) const
     180  double
     181  TukeyBiweightEstimator::estimate(RandomAccessIterator first,
     182                                   RandomAccessIterator last,
     183                                   TukeyBiweightEstimator::Stopper& stopper) const
    129184  {
    130185    const double scale = mad(first, last, true);
     
    136191      return m;
    137192    double m0 = m+1.0;
    138     size_t epoch = 0;
    139     // FIXME: let user define convergence requirement
    140     while (m!=m0) {
     193    while (!stopper.stop(m, m0)) {
    141194      m0 = m;
    142195      m = estimate(first, last, m, scale);
    143       ++epoch;
    144       // FIXME: let user define maximal epochs
    145       if (epoch>1000)
    146         utility::runtime_error("TukeyBiweightIterator: too many epochs");
    147196    }
    148197    return m;
Note: See TracChangeset for help on using the changeset viewer.