mlpack  master
laplace_distribution.hpp
Go to the documentation of this file.
1 /*
2  * @file laplace.hpp
3  * @author Zhihao Lou
4  *
5  * Laplace (double exponential) distribution used in SA.
6  *
7  * mlpack is free software; you may redistribute it and/or modify it under the
8  * terms of the 3-clause BSD license. You should have received a copy of the
9  * 3-clause BSD license along with mlpack. If not, see
10  * http://www.opensource.org/licenses/BSD-3-Clause for more information.
11  */
12 
13 #ifndef MLPACK_CORE_DISTRIBUTIONS_LAPLACE_DISTRIBUTION_HPP
14 #define MLPACK_CORE_DISTRIBUTIONS_LAPLACE_DISTRIBUTION_HPP
15 
16 namespace mlpack {
17 namespace distribution {
18 
50 {
51  public:
57 
65  LaplaceDistribution(const size_t dimensionality, const double scale) :
66  mean(arma::zeros<arma::vec>(dimensionality)), scale(scale) { }
67 
74  LaplaceDistribution(const arma::vec& mean, const double scale) :
75  mean(mean), scale(scale) { }
76 
78  size_t Dimensionality() const { return mean.n_elem; }
79 
83  double Probability(const arma::vec& observation) const
84  {
85  return exp(LogProbability(observation));
86  }
87 
91  double LogProbability(const arma::vec& observation) const;
92 
99  arma::vec Random() const
100  {
101  arma::vec result(mean.n_elem);
102  result.randu();
103 
104  // Convert from uniform distribution to Laplace distribution.
105  // arma::sign() does not exist in Armadillo < 3.920 so we have to do this
106  // elementwise.
107  for (size_t i = 0; i < result.n_elem; ++i)
108  {
109  if (result[i] < 0.5)
110  result[i] = mean[i] + scale * std::log(1 + 2.0 * (result[i] - 0.5));
111  else
112  result[i] = mean[i] - scale * std::log(1 - 2.0 * (result[i] - 0.5));
113  }
114 
115  return result;
116  }
117 
123  void Estimate(const arma::mat& observations);
124 
130  void Estimate(const arma::mat& observations,
131  const arma::vec& probabilities);
132 
134  const arma::vec& Mean() const { return mean; }
136  arma::vec& Mean() { return mean; }
137 
139  double Scale() const { return scale; }
141  double& Scale() { return scale; }
142 
146  template<typename Archive>
147  void Serialize(Archive& ar, const unsigned int /* version */)
148  {
149  ar & data::CreateNVP(mean, "mean");
150  ar & data::CreateNVP(scale, "scale");
151  }
152 
153  private:
155  arma::vec mean;
157  double scale;
158 
159 };
160 
161 } // namespace distribution
162 } // namespace mlpack
163 
164 #endif
void Estimate(const arma::mat &observations)
Estimate the Laplace distribution directly from the given observations.
arma::vec Random() const
Return a randomly generated observation according to the probability distribution defined by this obj...
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
The multivariate Laplace distribution centered at 0 has pdf.
FirstShim< T > CreateNVP(T &t, const std::string &name, typename std::enable_if_t< HasSerialize< T >::value > *=0)
Call this function to produce a name-value pair; this is similar to BOOST_SERIALIZATION_NVP(), but should be used for types that have a Serialize() function (or contain a type that has a Serialize() function) instead of a serialize() function.
double scale
Scale parameter of the distribution.
LaplaceDistribution(const size_t dimensionality, const double scale)
Construct the Laplace distribution with the given scale and dimensionality.
double Probability(const arma::vec &observation) const
Return the probability of the given observation.
double LogProbability(const arma::vec &observation) const
Return the log probability of the given observation.
LaplaceDistribution()
Default constructor, which creates a Laplace distribution with zero dimension and zero scale paramete...
void Serialize(Archive &ar, const unsigned int)
Serialize the distribution.
const arma::vec & Mean() const
Return the mean.
LaplaceDistribution(const arma::vec &mean, const double scale)
Construct the Laplace distribution with the given mean and scale parameter.
double & Scale()
Modify the scale parameter.
size_t Dimensionality() const
Return the dimensionality of this distribution.
double Scale() const
Return the scale parameter.
arma::vec mean
Mean of the distribution.