DMP_BBO library
Public Member Functions | Friends | List of all members
DistributionGaussian Class Reference

A class for representing a Gaussian distribution. More...

#include <DistributionGaussian.hpp>

Public Member Functions

 DistributionGaussian (const Eigen::VectorXd &mean, const Eigen::MatrixXd &covar)
 Construct the Gaussian distribution with a mean and covariance matrix. More...
 
void generateSamples (int n_samples, Eigen::MatrixXd &samples) const
 Generate samples from the distribution. More...
 
double maxEigenValue (void) const
 Get the largest eigenvalue of the covariance matrix of this distribution. More...
 
DistributionGaussianclone (void) const
 Make a deep copy of the object. More...
 
const Eigen::VectorXd & mean (void) const
 Accessor get function for the mean. More...
 
const Eigen::MatrixXd & covar (void) const
 Accessor get function for the covariance matrix. More...
 
void set_mean (const Eigen::VectorXd &mean)
 Accessor set function for the mean. More...
 
void set_covar (const Eigen::MatrixXd &covar)
 Accessor set function for the covar. More...
 

Friends

class boost::serialization::access
 Give boost serialization access to private members. More...
 
std::ostream & operator<< (std::ostream &output, const DistributionGaussian &distribution)
 Print to output stream. More...
 

Detailed Description

A class for representing a Gaussian distribution.

This is mainly a wrapper around boost functionality The reason to make the wrapper is to provide functionality for serialization/deserialization.

Definition at line 44 of file DistributionGaussian.hpp.

Constructor & Destructor Documentation

DistributionGaussian ( const Eigen::VectorXd &  mean,
const Eigen::MatrixXd &  covar 
)

Construct the Gaussian distribution with a mean and covariance matrix.

Parameters
[in]meanMean of the distribution
[in]covarCovariance matrix of the distribution

Definition at line 55 of file DistributionGaussian.cpp.

56 {
57  mean_ = mean;
59 }
const Eigen::MatrixXd & covar(void) const
Accessor get function for the covariance matrix.
const Eigen::VectorXd & mean(void) const
Accessor get function for the mean.
void set_covar(const Eigen::MatrixXd &covar)
Accessor set function for the covar.

Member Function Documentation

void generateSamples ( int  n_samples,
Eigen::MatrixXd &  samples 
) const

Generate samples from the distribution.

Parameters
[in]n_samplesNumber of samples to sample
[in]samplesthe samples themselves (size n_samples X dim(mean)

Definition at line 97 of file DistributionGaussian.cpp.

98 {
99  if (covar_decomposed_.size()==0)
100  {
101  // Now perform the Cholesky decomposition, which makes it easier to generate samples.
102  MatrixXd A(covar_.llt().matrixL());
103  covar_decomposed_ = A;
104  // Remark: it would have been better to do this in the constructor and set_covar,
105  // but I couldn't get it to work with boost::serialization (I tried hard)
106  }
107 
108 
109  int n_dims = mean_.size();
110  samples.resize(n_samples,n_dims);
111 
112  // http://en.wikipedia.org/wiki/Multivariate_normal_distribution#Drawing_values_from_the_distribution
113  VectorXd z(n_dims);
114  for (int i_sample=0; i_sample<n_samples; i_sample++)
115  {
116  // Generate vector with samples from standard normal distribution N(0,1)
117  for (int i_dim=0; i_dim<n_dims; i_dim++)
118  z(i_dim) = (*normal_distribution_unit)();
119 
120  // Compute x = mu + Az
121  samples.row(i_sample) = mean_ + covar_decomposed_*z;
122  }
123 }
double maxEigenValue ( void  ) const

Get the largest eigenvalue of the covariance matrix of this distribution.

Returns
largest eigenvalue of the covariance matrix of this distribution.

Definition at line 72 of file DistributionGaussian.cpp.

73 {
74  if (max_eigen_value_<0.0)
75  {
76  SelfAdjointEigenSolver<MatrixXd> eigensolver(covar_);
77  if (eigensolver.info() == Success)
78  {
79  // Get the eigenvalues
80  VectorXd eigen_values = eigensolver.eigenvalues();
81  max_eigen_value_ = eigen_values.maxCoeff();
82  }
83  }
84  return max_eigen_value_;
85 }
DistributionGaussian * clone ( void  ) const

Make a deep copy of the object.

Returns
A deep copy of the object.

Definition at line 61 of file DistributionGaussian.cpp.

62 {
63  return new DistributionGaussian(mean(),covar());
64 }
const Eigen::MatrixXd & covar(void) const
Accessor get function for the covariance matrix.
const Eigen::VectorXd & mean(void) const
Accessor get function for the mean.
DistributionGaussian(const Eigen::VectorXd &mean, const Eigen::MatrixXd &covar)
Construct the Gaussian distribution with a mean and covariance matrix.
const Eigen::VectorXd& mean ( void  ) const
inline

Accessor get function for the mean.

Returns
The mean of the distribution

Definition at line 73 of file DistributionGaussian.hpp.

73 { return mean_; }
const Eigen::MatrixXd& covar ( void  ) const
inline

Accessor get function for the covariance matrix.

Returns
The covariance matrix of the distribution

Definition at line 79 of file DistributionGaussian.hpp.

79 { return covar_; }

Here is the call graph for this function:

void set_mean ( const Eigen::VectorXd &  mean)

Accessor set function for the mean.

Parameters
[in]meanThe new mean of the distribution

Definition at line 66 of file DistributionGaussian.cpp.

67 {
68  assert(mean.size()==mean_.size());
69  mean_ = mean;
70 }
const Eigen::VectorXd & mean(void) const
Accessor get function for the mean.
void set_covar ( const Eigen::MatrixXd &  covar)

Accessor set function for the covar.

Parameters
[in]covarThe new covariance matrix of the distribution

Definition at line 89 of file DistributionGaussian.cpp.

89  {
90  assert(covar.cols()==covar.rows());
91  assert(covar.rows()==mean_.size());
92  covar_ = covar;
93  covar_decomposed_ = MatrixXd::Zero(0,0);
94  max_eigen_value_ = -1.0;
95 }
const Eigen::MatrixXd & covar(void) const
Accessor get function for the covariance matrix.

Friends And Related Function Documentation

friend class boost::serialization::access
friend

Give boost serialization access to private members.

Definition at line 119 of file DistributionGaussian.hpp.

std::ostream& operator<< ( std::ostream &  output,
const DistributionGaussian distribution 
)
friend

Print to output stream.

Parameters
[in]outputOutput stream to which to write to
[in]distributionDistribution to write
Returns
Output stream

Definition at line 126 of file DistributionGaussian.cpp.

127 {
128  output << "N([" << toString(distribution.mean_) << "], ["<< toString(distribution.covar_) << "])";
129  return output;
130 }
std::string toString(const Eigen::Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > &matrix)
Convert an Eigen matrix to a string.

The documentation for this class was generated from the following files: