laplace_distribution.hpp
Go to the documentation of this file.
1 /*
2  * @file core/dists/laplace_distribution.hpp
3  * @author Zhihao Lou
4  * @author Rohan Raj
5  *
6  * Laplace (double exponential) distribution used in SA.
7  *
8  * mlpack is free software; you may redistribute it and/or modify it under the
9  * terms of the 3-clause BSD license. You should have received a copy of the
10  * 3-clause BSD license along with mlpack. If not, see
11  * http://www.opensource.org/licenses/BSD-3-Clause for more information.
12  */
13 
14 #ifndef MLPACK_CORE_DISTRIBUTIONS_LAPLACE_DISTRIBUTION_HPP
15 #define MLPACK_CORE_DISTRIBUTIONS_LAPLACE_DISTRIBUTION_HPP
16 
17 namespace mlpack {
18 namespace distribution {
19 
51 {
52  public:
57  LaplaceDistribution() : scale(0) { }
58 
66  LaplaceDistribution(const size_t dimensionality, const double scale) :
67  mean(arma::zeros<arma::vec>(dimensionality)), scale(scale) { }
68 
76  LaplaceDistribution(const arma::vec& mean, const double scale) :
77  mean(mean), scale(scale) { }
78 
80  size_t Dimensionality() const { return mean.n_elem; }
81 
87  double Probability(const arma::vec& observation) const
88  {
89  return exp(LogProbability(observation));
90  }
91 
98  void Probability(const arma::mat& x, arma::vec& probabilities) const;
99 
105  double LogProbability(const arma::vec& observation) const;
106 
113  void LogProbability(const arma::mat& x, arma::vec& logProbabilities) const
114  {
115  logProbabilities.set_size(x.n_cols);
116  for (size_t i = 0; i < x.n_cols; ++i)
117  {
118  logProbabilities(i) = LogProbability(x.unsafe_col(i));
119  }
120  }
121 
128  arma::vec Random() const
129  {
130  arma::vec result(mean.n_elem);
131  result.randu();
132 
133  // Convert from uniform distribution to Laplace distribution.
134  // arma::sign() does not exist in Armadillo < 3.920 so we have to do this
135  // elementwise.
136  for (size_t i = 0; i < result.n_elem; ++i)
137  {
138  if (result[i] < 0.5)
139  result[i] = mean[i] + scale * std::log(1 + 2.0 * (result[i] - 0.5));
140  else
141  result[i] = mean[i] - scale * std::log(1 - 2.0 * (result[i] - 0.5));
142  }
143 
144  return result;
145  }
146 
152  void Estimate(const arma::mat& observations);
153 
159  void Estimate(const arma::mat& observations,
160  const arma::vec& probabilities);
161 
163  const arma::vec& Mean() const { return mean; }
165  arma::vec& Mean() { return mean; }
166 
168  double Scale() const { return scale; }
170  double& Scale() { return scale; }
171 
175  template<typename Archive>
176  void serialize(Archive& ar, const uint32_t /* version */)
177  {
178  ar(CEREAL_NVP(mean));
179  ar(CEREAL_NVP(scale));
180  }
181 
182  private:
184  arma::vec mean;
186  double scale;
187 };
188 
189 } // namespace distribution
190 } // namespace mlpack
191 
192 #endif
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.
Linear algebra utility functions, generally performed on matrices or vectors.
The multivariate Laplace distribution centered at 0 has pdf.
LaplaceDistribution()
Default constructor, which creates a Laplace distribution with zero dimension and zero scale paramete...
size_t Dimensionality() const
Return the dimensionality of this distribution.
double LogProbability(const arma::vec &observation) const
Return the log probability of the given observation.
double Scale() const
Return the scale parameter.
void Estimate(const arma::mat &observations)
Estimate the Laplace distribution directly from the given observations.
double Probability(const arma::vec &observation) const
Return the probability of the given observation.
double & Scale()
Modify the scale parameter.
arma::vec Random() const
Return a randomly generated observation according to the probability distribution defined by this obj...
void serialize(Archive &ar, const uint32_t)
Serialize the distribution.
LaplaceDistribution(const size_t dimensionality, const double scale)
Construct the Laplace distribution with the given scale and dimensionality.
void LogProbability(const arma::mat &x, arma::vec &logProbabilities) const
Evaluate log probability density function of given observation.