Direct Graphical Models
v.1.5.1
|
Multi - dimensional Gauss function class. More...
#include <NDGauss.h>
Public Member Functions | |
CNDGauss (dword k) | |
Constructor. More... | |
CNDGauss (const CNDGauss &rhs) | |
~CNDGauss (void) | |
CNDGauss & | operator= (const CNDGauss &rhs) |
CNDGauss & | operator+= (const CNDGauss &rhs) |
const CNDGauss | operator+ (const CNDGauss &rhs) const |
Merging operator. More... | |
void | clear (void) |
Clears class variables. More... | |
void | freeze (void) |
Freezes the state of the Gaussian function. More... | |
void | addPoint (Mat &point) |
Adds new n-dimensional point (sample) for Gaussian function approximation. More... | |
bool | empty (void) const |
Checks weather the Gaussian function is approximated. More... | |
void | setMu (Mat &mu) |
Sets \(\mu\). More... | |
Mat | getMu (void) const |
Returns \(\mu\). More... | |
void | setSigma (Mat &sigma) |
Sets \(\Sigma\). More... | |
Mat | getSigma (void) const |
Returns \(\Sigma\). More... | |
Mat | getSigmaInv (void) const |
Returns \(\Sigma^{-1}\). More... | |
long double | getAlpha (void) const |
Returns \(\alpha\). More... | |
double | getValue (Mat &x, Mat &aux1=Mat(), Mat &aux2=Mat(), Mat &aux3=Mat()) const |
Returns unscaled value of the Gaussian function. More... | |
void | setNumPoints (long nPoints) |
Sets the number of approximation points. More... | |
size_t | getNumPoints (void) const |
Returns the number of approximation points. More... | |
double | getEuclidianDistance (const Mat &x) const |
Returns the Euclidian distance between argument point x and the center of multivariate normal distribution \(\mu\). More... | |
double | getMahalanobisDistance (const Mat &x) const |
Returns the Mahalanobis distance between argument point x and the center of multivariate normal distribution \(\mathcal{N}(\mu,\Sigma)\). More... | |
double | getKullbackLeiberDivergence (CNDGauss &x) const |
Returns the Kullback-Leiber divergence from the multivariate normal distribution \(\mathcal{N}(\mu,\Sigma)\) to argument multivariate normal distribution \(\mathcal{N}_x(\mu_x,\Sigma_x)\). More... | |
Mat | getSample (void) const |
Returns a random vector (sample) from multivariate normal distribution. More... | |
Multi - dimensional Gauss function class.
This class allows to approximate the distribution of random variables (samples) \( \textbf{x} \), represented as k - dinemstional points \( [x_1,x_2,\dots,x_k] \). Under the assumption, that the random variables \( \textbf{x} \) are normally distributed, the approximation is done with multivariate normal distribution: \( \mathcal{N}_k(\mu,\Sigma)= \alpha\cdot\exp\big( -\frac{1}{2}(x-\mu)^T\Sigma^{-1}(x-\mu)\big) \) whith coefficient \( \alpha = \frac{1}{(2\pi)^{k/2}|\Sigma|^{1/2}} \). The parameters \( \mu\) and \(\Sigma \) of the Gaussian function \( \mathcal{N}(\mu,\Sigma) \) may be estimated sequentially with function addPoint() or operator operator+(); also they may be set directly with functions setMu() and setSigma().
The value of the Gaussian function \( \mathcal{N}(\mu,\Sigma) \) in point \( \textbf{x} \) is achieved with functions getAlpha() and getValue():
In order to generate a random sample from the distribution, given by Gaussian function \( \mathcal{N}(\mu,\Sigma) \), one uses function getSample().
DirectGraphicalModels::CNDGauss::CNDGauss | ( | dword | k | ) |
Constructor.
k | dimensions |
Definition at line 12 of file NDGauss.cpp.
DirectGraphicalModels::CNDGauss::CNDGauss | ( | const CNDGauss & | rhs | ) |
Definition at line 29 of file NDGauss.cpp.
DirectGraphicalModels::CNDGauss::~CNDGauss | ( | void | ) |
Definition at line 40 of file NDGauss.cpp.
void DirectGraphicalModels::CNDGauss::addPoint | ( | Mat & | point | ) |
Adds new n-dimensional point (sample) for Gaussian function approximation.
This function is a faster approximation of the merging operator (Ref. operator+()):
\( \hat{\mu} = \frac{n\mu + p}{n + 1} \); \( \hat{\Sigma} = \frac{n\Sigma + (p-\hat{\mu})(p-\hat{\mu})^\top}{n + 1}\)
point | k-dimensional point: Mat(size: k x 1; type: CV_64FC1) |
Definition at line 111 of file NDGauss.cpp.
void DirectGraphicalModels::CNDGauss::clear | ( | void | ) |
|
inline |
void DirectGraphicalModels::CNDGauss::freeze | ( | void | ) |
Freezes the state of the Gaussian function.
This is an optimization function, which calculates and fills internal variables need for major get- accessors of this class. If the function was not called, tese variable will be calculated in the get- accessors every tiime on call.
Definition at line 93 of file NDGauss.cpp.
long double DirectGraphicalModels::CNDGauss::getAlpha | ( | void | ) | const |
Returns \(\alpha\).
Definition at line 167 of file NDGauss.cpp.
double DirectGraphicalModels::CNDGauss::getEuclidianDistance | ( | const Mat & | x | ) | const |
Returns the Euclidian distance between argument point x and the center of multivariate normal distribution \(\mu\).
The Euclidian distance is calculated by the formula: \(D_E(\mathcal{N};x)=\sqrt{ (x-\mu)^\top(x-\mu) }\).
x | n-dimensional point (sample): Mat(size: k x 1; type: CV_64FC1) |
Definition at line 193 of file NDGauss.cpp.
double DirectGraphicalModels::CNDGauss::getKullbackLeiberDivergence | ( | CNDGauss & | x | ) | const |
Returns the Kullback-Leiber divergence from the multivariate normal distribution \(\mathcal{N}(\mu,\Sigma)\) to argument multivariate normal distribution \(\mathcal{N}_x(\mu_x,\Sigma_x)\).
The Kullback-Leiber divergence (or relative entropy) is calculated by the formula: \(D_{KL}(\mathcal{N};\mathcal{N}_x)=\frac{1}{2}\Big( tr(\Sigma^{-1}_{x}\Sigma) + D^{2}_{M}(\mathcal{N}_x;\mu) - k - \ln\big(\frac{\left|\Sigma\right|}{\left|\Sigma_x\right|}\big) \Big)\) and expressed in nats. Here \(D_M(\mathcal{N}_x;\mu)\) is the Mahalanobis distance beween the centers of multivariate normal distributions \(\mu_x\) and \(\mu\) with respect to \(\mathcal{N}_x\) (see getMahalanobisDistance() for more details). Please note, that it is not a symmetrical quantity, that is to say \( D_{KL}(\mathcal{N};\mathcal{N}_x) \neq D_{KL}(\mathcal{N}_x;\mathcal{N})\) and so could be hardly used as a "distance".
x | multivariate normal distribution \(\mathcal{N}_x(\mu_x,\Sigma_x)\) of the dimension k. |
Definition at line 217 of file NDGauss.cpp.
double DirectGraphicalModels::CNDGauss::getMahalanobisDistance | ( | const Mat & | x | ) | const |
Returns the Mahalanobis distance between argument point x and the center of multivariate normal distribution \(\mathcal{N}(\mu,\Sigma)\).
The Mahalanobis distance is calculated by the formula: \(D_M(\mathcal{N};x)=\sqrt{ (x-\mu)^\top\Sigma^{-1}(x-\mu) }\)
x | n-dimensional point (sample): Mat(size: k x 1; type: CV_64FC1) |
Definition at line 208 of file NDGauss.cpp.
|
inline |
|
inline |
Mat DirectGraphicalModels::CNDGauss::getSample | ( | void | ) | const |
Returns a random vector (sample) from multivariate normal distribution.
The implementation is based on the paper Generating Random Vectors from the Multivariate Normal Distribution
Definition at line 239 of file NDGauss.cpp.
|
inline |
Mat DirectGraphicalModels::CNDGauss::getSigmaInv | ( | void | ) | const |
Returns \(\Sigma^{-1}\).
Definition at line 157 of file NDGauss.cpp.
double DirectGraphicalModels::CNDGauss::getValue | ( | Mat & | x, |
Mat & | aux1 = Mat() , |
||
Mat & | aux2 = Mat() , |
||
Mat & | aux3 = Mat() |
||
) | const |
Returns unscaled value of the Gaussian function.
This function returns unscaled value of the Gaussian function, i.e. \( \exp\big( -\frac{1}{2}(x-\mu)^T\Sigma^{-1}(x-\mu)\big) \). In order to get the value of \( \mathcal{N}_k(\mu,\Sigma) \), the output of this function must be multiplied with \( \alpha \) from the getAlpha() function.
This function is PPL-safe function.
x | n-dimensional point (sample): Mat(size: k x 1; type: CV_64FC1) |
aux1 | Auxilary variable |
aux2 | Auxilary variable |
aux3 | Auxilary variable |
Definition at line 179 of file NDGauss.cpp.
Merging operator.
This operator merges two Gauss finctions into one:
\( \hat{\mu} = \frac{n_1\mu_1 + n_2\mu_2}{n_1 + n_2} \); \( \hat{\Sigma} = \frac{n_1(\Sigma_1 + \mu_1\mu^{\top}_{1}) + n_2(\Sigma_2 + \mu_2\mu^{\top}_{2})}{n_1 + n_2} - \hat{\mu}\hat{\mu}^\top\)
For the sequential Gauss function estimation one also may use this operator (see the code below) or addPoint() function:
In this case \(n_2 = 1\) and \(\Sigma_2 = 0\).
Definition at line 62 of file NDGauss.h.
Definition at line 67 of file NDGauss.cpp.
Definition at line 49 of file NDGauss.cpp.
void DirectGraphicalModels::CNDGauss::setMu | ( | Mat & | mu | ) |
Sets \(\mu\).
mu | the mean vector \(\mu\) : Mat(size: k x 1; type: CV_64FC1) |
Definition at line 135 of file NDGauss.cpp.
|
inline |
void DirectGraphicalModels::CNDGauss::setSigma | ( | Mat & | sigma | ) |
Sets \(\Sigma\).
sigma | the covariance matrix \(\Sigma\): Diagonal Mat(size: k x k; type: CV_64FC1) |
Definition at line 146 of file NDGauss.cpp.