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/range/algorithm/for_each.hpp>
47 
48 namespace shark {
49 
50 /// Trainer for normal distribution
52 {
53 public:
54 
55  /// The type of variance. The difference between them is:
56  /// When you have "N" data values that are:
57  /// By Population: divide by N when calculating Variance
58  /// By Sample: divide by N-1 when calculating Variance
60  {
63  };
64 
65  /// Constructor
66  NormalTrainer(VarianceType varianceType = VARIANCE_BY_SAMPLE) : m_varianceType(varianceType) {}
67 
68  /// Internal implementation for trainer of normal distribution
69  template <typename RngType>
70  void train(Normal<RngType>& normal, const std::vector<double>& input) const
71  {
72  SIZE_CHECK(input.size() > 1u);
73  namespace bae = boost::accumulators::extract;
74 
75  InternalAccumulatorType accu;
76  boost::range::for_each(input, boost::bind(boost::ref(accu), _1));
77  SIZE_CHECK(bae::count(accu) > 1u);
78 
79  normal.mean(bae::mean(accu));
80  normal.variance(
81  VARIANCE_BY_SAMPLE == m_varianceType
82  ? bae::variance(accu) * bae::count(accu) / (bae::count(accu) - 1)
83  : bae::variance(accu));
84  }
85 
86 private:
87 
88  /// The covariance type this trainer will use
89  VarianceType m_varianceType;
90 
91  /// Internal accumulator type
92  typedef boost::accumulators::accumulator_set<
93  double,
94  boost::accumulators::stats<
95  boost::accumulators::tag::count,
96  boost::accumulators::tag::variance> > InternalAccumulatorType;
97 };
98 
99 } // namespace shark {
100 
101 #endif // SHARK_ALGORITHMS_TRAINERS_DISTRIBUTION_NORMAL_H