mlpack: a scalable c++ machine learning library
mlpack  2.0.2
regression_distribution.hpp
Go to the documentation of this file.
1 
14 #ifndef mlpack_CORE_DISTRIBUTIONS_REGRESSION_DISTRIBUTION_HPP
15 #define mlpack_CORE_DISTRIBUTIONS_REGRESSION_DISTRIBUTION_HPP
16 
17 #include <mlpack/core.hpp>
20 
21 namespace mlpack {
22 namespace distribution {
23 
33 {
34  private:
39 
40  public:
44  RegressionDistribution() { /* nothing to do */ }
45 
53  RegressionDistribution(const arma::mat& predictors,
54  const arma::vec& responses) :
55  rf(regression::LinearRegression(predictors, responses))
56  {
57  err = GaussianDistribution(1);
58  arma::mat cov(1, 1);
59  cov(0, 0) = rf.ComputeError(predictors, responses);
60  err.Covariance(std::move(cov));
61  }
62 
66  template<typename Archive>
67  void Serialize(Archive& ar, const unsigned int /* version */)
68  {
69  ar & data::CreateNVP(rf, "rf");
70  ar & data::CreateNVP(err, "err");
71  }
72 
74  const regression::LinearRegression& Rf() const { return rf; }
77 
79  const GaussianDistribution& Err() const { return err; }
81  GaussianDistribution& Err() { return err; }
82 
88  void Train(const arma::mat& observations);
89 
95  void Train(const arma::mat& observations, const arma::vec& weights);
96 
102  double Probability(const arma::vec& observation) const;
103 
109  double LogProbability(const arma::vec& observation) const {
110  return log(Probability(observation));
111  }
112 
119  void Predict(const arma::mat& points, arma::vec& predictions) const;
120 
122  const arma::vec& Parameters() const { return rf.Parameters(); }
123 
125  size_t Dimensionality() const { return rf.Parameters().n_elem; }
126 };
127 
128 
129 } // namespace distribution
130 } // namespace mlpack
131 
132 #endif
A single multivariate Gaussian distribution.
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.
A simple linear regression algorithm using ordinary least squares.
size_t Dimensionality() const
Return the dimensionality.
RegressionDistribution()
Default constructor, which creates a Gaussian with zero dimension.
void Train(const arma::mat &observations)
Estimate the Gaussian distribution directly from the given observations.
RegressionDistribution(const arma::mat &predictors, const arma::vec &responses)
Create a Conditional Gaussian distribution with conditional mean function obtained by running Regress...
void Predict(const arma::mat &points, arma::vec &predictions) const
Calculate y_i for each data point in points.
const GaussianDistribution & Err() const
Return error distribution.
double ComputeError(const arma::mat &points, const arma::vec &responses) const
Calculate the L2 squared error on the given predictors and responses using this linear regression mod...
A class that represents a univariate conditionally Gaussian distribution.
const arma::vec & Parameters() const
Return the parameters (the b vector).
GaussianDistribution err
Error distribution.
regression::LinearRegression rf
Regression function for representing conditional mean.
Include all of the base components required to write mlpack methods, and the main mlpack Doxygen docu...
const arma::vec & Parameters() const
Return the parameters (the b vector).
const regression::LinearRegression & Rf() const
Return regression function.
void Serialize(Archive &ar, const unsigned int)
Serialize the distribution.
double Probability(const arma::vec &observation) const
Evaluate probability density function of given observation.
regression::LinearRegression & Rf()
Modify regression function.
GaussianDistribution & Err()
Modify error distribution.
double LogProbability(const arma::vec &observation) const
Evaluate log probability density function of given observation.
const arma::mat & Covariance() const
Return the covariance matrix.