mlpack: src/mlpack/core/dists/laplace_distribution.hpp Source File
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  * This file is part of mlpack 2.0.2.
8  *
9  * mlpack is free software; you may redistribute it and/or modify it under the
10  * terms of the 3-clause BSD license. You should have received a copy of the
11  * 3-clause BSD license along with mlpack. If not, see
12  * http://www.opensource.org/licenses/BSD-3-Clause for more information.
13  */
14 
15 #ifndef mlpack_CORE_DISTRIBUTIONS_LAPLACE_DISTRIBUTION_HPP
16 #define mlpack_CORE_DISTRIBUTIONS_LAPLACE_DISTRIBUTION_HPP
17 
18 namespace mlpack {
19 namespace distribution {
20 
52 {
53  public:
59 
67  LaplaceDistribution(const size_t dimensionality, const double scale) :
68  mean(arma::zeros<arma::vec>(dimensionality)), scale(scale) { }
69 
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 
85  double Probability(const arma::vec& observation) const
86  {
87  return exp(LogProbability(observation));
88  }
89 
93  double LogProbability(const arma::vec& observation) const;
94 
101  arma::vec Random() const
102  {
103  arma::vec result(mean.n_elem);
104  result.randu();
105 
106  // Convert from uniform distribution to Laplace distribution.
107  // arma::sign() does not exist in Armadillo < 3.920 so we have to do this
108  // elementwise.
109  for (size_t i = 0; i < result.n_elem; ++i)
110  {
111  if (result[i] < 0.5)
112  result[i] = mean[i] + scale * std::log(1 + 2.0 * (result[i] - 0.5));
113  else
114  result[i] = mean[i] - scale * std::log(1 - 2.0 * (result[i] - 0.5));
115  }
116 
117  return result;
118  }
119 
125  void Estimate(const arma::mat& observations);
126 
132  void Estimate(const arma::mat& observations,
133  const arma::vec& probabilities);
134 
136  const arma::vec& Mean() const { return mean; }
138  arma::vec& Mean() { return mean; }
139 
141  double Scale() const { return scale; }
143  double& Scale() { return scale; }
144 
148  template<typename Archive>
149  void Serialize(Archive& ar, const unsigned int /* version */)
150  {
151  ar & data::CreateNVP(mean, "mean");
152  ar & data::CreateNVP(scale, "scale");
153  }
154 
155  private:
157  arma::vec mean;
159  double scale;
160 
161 };
162 
163 } // namespace distribution
164 } // namespace mlpack
165 
166 #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.
FirstShim< T > CreateNVP(T &t, const std::string &name, typename boost::enable_if< HasSerialize< T >>::type *=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.
The multivariate Laplace distribution centered at 0 has pdf.
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.