mlpack  1.0.12
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 1.0.12.
8  *
9  * mlpack is free software; you may redstribute 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_OPTIMIZER_SA_LAPLACE_DISTRIBUTION_HPP
16 #define __MLPACK_CORE_OPTIMIZER_SA_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 
93  arma::vec Random() const
94  {
95  arma::vec result(mean.n_elem);
96  result.randu();
97 
98  // Convert from uniform distribution to Laplace distribution.
99  // arma::sign() does not exist in Armadillo < 3.920 so we have to do this
100  // elementwise.
101  for (size_t i = 0; i < result.n_elem; ++i)
102  {
103  if (result[i] < 0)
104  result[i] = mean[i] + scale * result[i] * std::log(1 + 2.0 * (result[i]
105  - 0.5));
106  else
107  result[i] = mean[i] - scale * result[i] * std::log(1 - 2.0 * (result[i]
108  - 0.5));
109  }
110 
111  return result;
112  }
113 
119  void Estimate(const arma::mat& observations);
120 
126  void Estimate(const arma::mat& observations,
127  const arma::vec& probabilities);
128 
130  const arma::vec& Mean() const { return mean; }
132  arma::vec& Mean() { return mean; }
133 
135  double Scale() const { return scale; }
137  double& Scale() { return scale; }
138 
140  std::string ToString() const;
141 
142  private:
144  arma::vec mean;
146  double scale;
147 
148 };
149 
150 }; // namespace distribution
151 }; // namespace mlpack
152 
153 #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: load.hpp:23
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.
std::string ToString() const
Return a string representation of the object.
LaplaceDistribution()
Default constructor, which creates a Laplace distribution with zero dimension and zero scale paramete...
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.