AbstractDistribution.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief Abstract class for statistical distributions
6  *
7  *
8  *
9  *
10  * \author B. Li
11  * \date 2012
12  *
13  *
14  * \par Copyright 1995-2017 Shark Development Team
15  *
16  * <BR><HR>
17  * This file is part of Shark.
18  * <http://shark-ml.org/>
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_RNG_ABSTRACT_DISTRIBUTION_H
36 #define SHARK_RNG_ABSTRACT_DISTRIBUTION_H
37 
38 #include "shark/Core/Exception.h"
39 #include "shark/Core/Math.h"
40 
41 namespace shark {
42 
43 /// Abstract class for distributions
45 {
46 public:
47  /// Dtor
48  virtual ~AbstractDistribution() {}
49 
50  /// Calculate probability for a given input
51  /// @param x the input for calculating probability
52  /// @return probability of input
53  virtual double p(double x) const = 0;
54 
55  /// Calculate log(p(x))
56  ///
57  /// std::log can get -inf before it returns NaN. shark::safeLog tries to save the day, however is not perfect.
58  /// The only real solution is to implement a function logP inside the distributions which returns the energy of the state
59  /// @note subclasses should implement their own version of this function instead of replying on the default
60  /// implementation unless you are pretty sure what you are doing.
61  ///
62  /// @param x the input for calculating log of probability
63  /// @return log of probability of input
64  virtual double logP(double x) const { return safeLog(p(x)); }
65 };
66 
67 } // namespace shark {
68 
69 #endif // SHARK_RNG_ABSTRACT_DISTRIBUTION_H