14 #ifndef MLPACK_CORE_DISTRIBUTIONS_DISCRETE_DISTRIBUTION_HPP 15 #define MLPACK_CORE_DISTRIBUTIONS_DISCRETE_DISTRIBUTION_HPP 22 namespace distribution {
53 probabilities(
std::vector<arma::vec>(1)){ }
64 probabilities(
std::vector<arma::vec>(1,
65 arma::ones<arma::vec>(numObservations) / numObservations))
78 for (
size_t i = 0; i < numObservations.n_elem; ++i)
80 const size_t numObs = size_t(numObservations[i]);
83 std::ostringstream oss;
84 oss <<
"number of observations for dimension " << i <<
" is 0, but " 85 <<
"must be greater than 0";
86 throw std::invalid_argument(oss.str());
88 probabilities.push_back(arma::ones<arma::vec>(numObs) / numObs);
100 for (
size_t i = 0; i < probabilities.size(); ++i)
102 arma::vec temp = probabilities[i];
103 double sum = accu(temp);
105 this->probabilities.push_back(temp / sum);
108 this->probabilities.push_back(arma::ones<arma::vec>(temp.n_elem)
129 double probability = 1.0;
131 if (observation.n_elem != probabilities.size())
133 Log::Fatal <<
"DiscreteDistribution::Probability(): observation has " 134 <<
"incorrect dimension " << observation.n_elem <<
" but should have" 135 <<
" dimension " << probabilities.size() <<
"!" << std::endl;
138 for (
size_t dimension = 0; dimension < observation.n_elem; dimension++)
142 const size_t obs = size_t(observation(dimension) + 0.5);
145 if (obs >= probabilities[dimension].n_elem)
147 Log::Fatal <<
"DiscreteDistribution::Probability(): received " 148 <<
"observation " << obs <<
"; observation must be in [0, " 149 << probabilities[dimension].n_elem <<
"] for this distribution." 152 probability *= probabilities[dimension][obs];
179 void Probability(
const arma::mat& x, arma::vec& probabilities)
const 181 probabilities.set_size(x.n_cols);
182 for (
size_t i = 0; i < x.n_cols; ++i)
196 logProbabilities.set_size(x.n_cols);
197 for (
size_t i = 0; i < x.n_cols; ++i)
198 logProbabilities(i) = log(
Probability(x.unsafe_col(i)));
217 void Train(
const arma::mat& observations);
228 void Train(
const arma::mat& observations,
229 const arma::vec& probabilities);
232 arma::vec&
Probabilities(
const size_t dim = 0) {
return probabilities[dim]; }
235 {
return probabilities[dim]; }
240 template<
typename Archive>
243 ar(CEREAL_NVP(probabilities));
249 std::vector<arma::vec> probabilities;
DiscreteDistribution()
Default constructor, which creates a distribution that has no observations.
Linear algebra utility functions, generally performed on matrices or vectors.
A discrete distribution where the only observations are discrete observations.
The core includes that mlpack expects; standard C++ includes and Armadillo.
arma::vec Random() const
Return a randomly generated observation (one-dimensional vector; one observation) according to the pr...
double LogProbability(const arma::vec &observation) const
Return the log probability of the given observation.
const arma::vec & Probabilities(const size_t dim=0) const
Modify the vector of probabilities for the given dimension.
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
size_t Dimensionality() const
Get the dimensionality of the distribution.
DiscreteDistribution(const arma::Col< size_t > &numObservations)
Define the multidimensional discrete distribution as having numObservations possible observations...
void Train(const arma::mat &observations)
Estimate the probability distribution directly from the given observations.
Miscellaneous math random-related routines.
double Probability(const arma::vec &observation) const
Return the probability of the given observation.
arma::vec & Probabilities(const size_t dim=0)
Return the vector of probabilities for the given dimension.
DiscreteDistribution(const size_t numObservations)
Define the discrete distribution as having numObservations possible observations. ...
void serialize(Archive &ar, const uint32_t)
Serialize the distribution.
DiscreteDistribution(const std::vector< arma::vec > &probabilities)
Define the multidimensional discrete distribution as having the given probabilities for each observat...
void LogProbability(const arma::mat &x, arma::vec &logProbabilities) const
Returns the Log probability of the given matrix.
void Probability(const arma::mat &x, arma::vec &probabilities) const
Calculates the Discrete probability density function for each data point (column) in the given matrix...