NormalTrainer.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief Trainer for normal distribution
6  *
7  *
8  *
9  *
10  * \author B. Li
11  * \date 2012
12  *
13  *
14  * \par Copyright 1995-2015 Shark Development Team
15  *
16  * <BR><HR>
17  * This file is part of Shark.
18  * <http://image.diku.dk/shark/>
19  *
20  * Shark is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU Lesser General Public License as published
22  * by the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  *
25  * Shark is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  * GNU Lesser General Public License for more details.
29  *
30  * You should have received a copy of the GNU Lesser General Public License
31  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
32  *
33  */
34 //===========================================================================
35 #ifndef SHARK_ALGORITHMS_TRAINERS_DISTRIBUTION_NORMAL_H
36 #define SHARK_ALGORITHMS_TRAINERS_DISTRIBUTION_NORMAL_H
37 
38 #include "shark/Rng/Normal.h"
39 #include "shark/Rng/Rng.h"
40 
41 #include <boost/accumulators/framework/accumulator_set.hpp>
42 #include <boost/accumulators/statistics/count.hpp>
43 #include <boost/accumulators/statistics/stats.hpp>
44 #include <boost/accumulators/statistics/variance.hpp>
45 #include <boost/bind/bind.hpp>
46 #include <boost/bind/placeholders.hpp>
47 #include <boost/range/algorithm/for_each.hpp>
48 
49 namespace shark {
50 
51 /// Trainer for normal distribution
53 {
54 public:
55 
56  /// The type of variance. The difference between them is:
57  /// When you have "N" data values that are:
58  /// By Population: divide by N when calculating Variance
59  /// By Sample: divide by N-1 when calculating Variance
61  {
64  };
65 
66  /// Constructor
67  NormalTrainer(VarianceType varianceType = VARIANCE_BY_SAMPLE) : m_varianceType(varianceType) {}
68 
69  /// Internal implementation for trainer of normal distribution
70  template <typename RngType>
71  void train(Normal<RngType>& normal, const std::vector<double>& input) const
72  {
73  SIZE_CHECK(input.size() > 1u);
74  namespace bae = boost::accumulators::extract;
75  InternalAccumulatorType accu;
76 #if (BOOST_VERSION < 106000)
77  boost::range::for_each(input, boost::bind(boost::ref(accu), _1));
78 #else
79  boost::range::for_each(input, boost::bind(boost::ref(accu), boost::placeholders::_1));
80 #endif
81  SIZE_CHECK(bae::count(accu) > 1u);
82 
83  normal.mean(bae::mean(accu));
84  normal.variance(
85  VARIANCE_BY_SAMPLE == m_varianceType
86  ? bae::variance(accu) * bae::count(accu) / (bae::count(accu) - 1)
87  : bae::variance(accu));
88  }
89 
90 private:
91 
92  /// The covariance type this trainer will use
93  VarianceType m_varianceType;
94 
95  /// Internal accumulator type
96  typedef boost::accumulators::accumulator_set<
97  double,
98  boost::accumulators::stats<
99  boost::accumulators::tag::count,
100  boost::accumulators::tag::variance> > InternalAccumulatorType;
101 };
102 
103 } // namespace shark {
104 
105 #endif // SHARK_ALGORITHMS_TRAINERS_DISTRIBUTION_NORMAL_H