MLPACK  1.0.11
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.11.
8  *
9  * MLPACK is free software: you can redistribute it and/or modify it under the
10  * terms of the GNU Lesser General Public License as published by the Free
11  * Software Foundation, either version 3 of the License, or (at your option) any
12  * later version.
13  *
14  * MLPACK is distributed in the hope that it will be useful, but WITHOUT ANY
15  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16  * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
17  * details (LICENSE.txt).
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * MLPACK. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef __MLPACK_CORE_OPTIMIZER_SA_LAPLACE_DISTRIBUTION_HPP
24 #define __MLPACK_CORE_OPTIMIZER_SA_LAPLACE_DISTRIBUTION_HPP
25 
26 namespace mlpack {
27 namespace distribution {
28 
60 {
61  public:
67 
75  LaplaceDistribution(const size_t dimensionality, const double scale) :
76  mean(arma::zeros<arma::vec>(dimensionality)), scale(scale) { }
77 
84  LaplaceDistribution(const arma::vec& mean, const double scale) :
85  mean(mean), scale(scale) { }
86 
88  size_t Dimensionality() const { return mean.n_elem; }
89 
93  double Probability(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)
112  result[i] = mean[i] + scale * result[i] * std::log(1 + 2.0 * (result[i]
113  - 0.5));
114  else
115  result[i] = mean[i] - scale * result[i] * std::log(1 - 2.0 * (result[i]
116  - 0.5));
117  }
118 
119  return result;
120  }
121 
127  void Estimate(const arma::mat& observations);
128 
134  void Estimate(const arma::mat& observations,
135  const arma::vec& probabilities);
136 
138  const arma::vec& Mean() const { return mean; }
140  arma::vec& Mean() { return mean; }
141 
143  double Scale() const { return scale; }
145  double& Scale() { return scale; }
146 
148  std::string ToString() const;
149 
150  private:
152  arma::vec mean;
154  double scale;
155 
156 };
157 
158 }; // namespace distribution
159 }; // namespace mlpack
160 
161 #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...
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.