Belle II Software  release-08-01-10
MultivariateNormalGenerator Class Reference

Class to generate normal distributed, correlated random numbers given the mean values and the covariance matrix of all dimensions. More...

#include <MultivariateNormalGenerator.h>

Collaboration diagram for MultivariateNormalGenerator:

Public Member Functions

 MultivariateNormalGenerator ()
 default constructor to allow later initialization
 
 MultivariateNormalGenerator (int n, const double *mean, const double *cov)
 constructor with array interface: mean and covariance are passed as double arrays where the covariance is expected to be an NxN matrix in row major layout More...
 
 MultivariateNormalGenerator (const Eigen::VectorXd &mean, const Eigen::MatrixXd &cov)
 constructor with Eigen matrix interface. More...
 
Eigen::VectorXd generate () const
 Generate a set of correlated random numbers with the previouly set mean and covariance. More...
 
void reset ()
 reset the generator setting the size to 0. More...
 
size_t size () const
 Return the number of elements to be generated on generate()
 
void generate (double *output) const
 Generate a set of correlated random numbers with the previouly set mean and covariance and store them in buffer output. More...
 
TVector3 generateVec3 () const
 Generate a set of correlated random numbers with the previouly set mean and covariance and return a TVector3. More...
 
TVectorD generateVecT () const
 Generate a set of correlated random numbers with the previouly set mean and covariance and return a TVectorT<double>
 
bool setMeanCov (int n, const double *mean, const double *cov)
 set the mean and covariance for the distribution with array interface: mean and covariance are passed as double arrays where the covariance is expected to be an NxN matrix in row major layout More...
 
bool setMeanCov (const Eigen::VectorXd &mean, const Eigen::MatrixXd &cov)
 set the mean and covariance for the distribution. More...
 
template<class value_type >
bool setMeanCov (const TVectorT< value_type > &mean, const TMatrixTBase< value_type > &cov)
 set mean and covariance matrix from ROOT vector/matrix objects, e.g. More...
 
template<class value_type >
bool setMeanCov (const ROOT::Math::XYZVector &mean, const TMatrixTBase< value_type > &cov)
 set the mean and covariance for the distribution. More...
 

Private Attributes

Eigen::VectorXd m_mean
 Member to store the mean values of the distribution.
 
Eigen::MatrixXd m_transform
 Member to store the transformation matrix for standard normal distributed random values.
 

Detailed Description

Class to generate normal distributed, correlated random numbers given the mean values and the covariance matrix of all dimensions.

This class can be used to generate normal distributed random values according to a given covariance matrix (assuming the covariance matrix is positive semi-definite).

To use it first set the desired mean values and covariance matrix using setMeanCov() and then call generate() to generate one set of values.

Warning
: setMeanCov() will not work for all matrices. Please check the return value when setting the covariance matrix.

To generate normal distributed random values according to a covariance matrix we need to decompose the covariance matrix $M$ into $M= A A^T$. Given the vector of mean values as $\mu$ and a vector of standard normal distributed random values $(\mu=0, \sigma=1)$ as n we can obtain a set of correlated random values $x = \mu + A * n$.

Usually the Cholesky decomposition is chosen as it computes $M = L L^T$. However the Cholesky composition only works for positive definite matrices so it does not work if for example one of the values is fixed and has no error.

To ease this restriction a little we use the LDLT decomposition given as $M = P^T L D L^T P$ where $P$ is a permutation matrix and D a diagonal matrix. We then can use $A = P^T L \sqrt(D)$ to caluclate the correlated values also for positive semi-definite covariance matrices if the elements of D are positive.

Definition at line 53 of file MultivariateNormalGenerator.h.

Constructor & Destructor Documentation

◆ MultivariateNormalGenerator() [1/2]

MultivariateNormalGenerator ( int  n,
const double *  mean,
const double *  cov 
)
inline

constructor with array interface: mean and covariance are passed as double arrays where the covariance is expected to be an NxN matrix in row major layout

Parameters
ndimensionality
meanpointer to the n mean values of the distribution
covpointer to the n*n covariance values in row major layout

Definition at line 64 of file MultivariateNormalGenerator.h.

◆ MultivariateNormalGenerator() [2/2]

MultivariateNormalGenerator ( const Eigen::VectorXd &  mean,
const Eigen::MatrixXd &  cov 
)
inline

constructor with Eigen matrix interface.

Parameters
meanVector of mean values
covMatrix containing the covariance values

Definition at line 72 of file MultivariateNormalGenerator.h.

Member Function Documentation

◆ generate() [1/2]

Eigen::VectorXd generate ( ) const
inline

Generate a set of correlated random numbers with the previouly set mean and covariance.

Returns
Vector containing the generated random numbers

Definition at line 80 of file MultivariateNormalGenerator.h.

◆ generate() [2/2]

void generate ( double *  output) const
inline

Generate a set of correlated random numbers with the previouly set mean and covariance and store them in buffer output.

Parameters
outputpointer to array where generated values will be stored.

Definition at line 100 of file MultivariateNormalGenerator.h.

◆ generateVec3()

TVector3 generateVec3 ( ) const
inline

Generate a set of correlated random numbers with the previouly set mean and covariance and return a TVector3.

Optimally, the set mean and covariance matrix should be of dimension three, otherwise just the first size() elements of the TVector3 are set and the remaining elements are zero. If size() is bigger than 3 the remaining values will be discarded.

Definition at line 112 of file MultivariateNormalGenerator.h.

◆ reset()

void reset ( void  )

reset the generator setting the size to 0.

Subsequent calls to generate will return 0-sized results until the generator is reinitialized using setMeanCov()

Definition at line 14 of file MultivariateNormalGenerator.cc.

15 {
16  m_mean.resize(0);
17  m_transform.resize(0, 0);
18 }
Eigen::VectorXd m_mean
Member to store the mean values of the distribution.
Eigen::MatrixXd m_transform
Member to store the transformation matrix for standard normal distributed random values.

◆ setMeanCov() [1/2]

bool setMeanCov ( const Eigen::VectorXd &  mean,
const Eigen::MatrixXd &  cov 
)

set the mean and covariance for the distribution.

Parameters
meanVector of mean values
covMatrix containing the covariance values
Returns
true if covariance could be decomposited, false otherwise

Definition at line 36 of file MultivariateNormalGenerator.cc.

◆ setMeanCov() [2/2]

bool setMeanCov ( int  n,
const double *  mean,
const double *  cov 
)

set the mean and covariance for the distribution with array interface: mean and covariance are passed as double arrays where the covariance is expected to be an NxN matrix in row major layout

Parameters
ndimensionality
meanpointer to the n mean values of the distribution
covpointer to the n*n covariance values in row major layout
Returns
true if covariance could be decomposited, false otherwise

Definition at line 20 of file MultivariateNormalGenerator.cc.


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