Multivariate Analysis Package  0.3
A multivariate distributions analysis package
 All Classes Namespaces Functions Pages
Public Member Functions | Friends | List of all members
Multivariate::tDistribution Class Reference

Multivariate Student's t Distribution More...

#include <tDist.h>

Inheritance diagram for Multivariate::tDistribution:
Multivariate::AbstarctDistribution

Public Member Functions

 tDistribution (unsigned int Dimension, unsigned int DegFreedom, const Eigen::VectorXd &locVect, const Eigen::MatrixXd &ScaleMatr)
 Construct a multivariate student's t with the given parameters. More...
 
 tDistribution (unsigned int Dimension=1U, unsigned int DegFreedom=1U)
 Construct a multivariate standardized student's t. More...
 
Eigen::MatrixXd ExtractSamples (unsigned int NumSamples) const
 Generates multiple simulations from the distribution. More...
 
Eigen::MatrixXd ExtractSamplesCDF (unsigned int NumSamples) const
 Extracts samples from the distribution and returns their marginal CDF. More...
 
double GetCumulativeDesity (const Eigen::VectorXd &Coordinates) const
 Computes the cumulative density function of the distribution in correspondence of the supplied coordinates. More...
 
unsigned int GetDegreesOfFreedom () const
 Get the degrees of freedom of the distribution. More...
 
double GetDensity (const Eigen::VectorXd &Coordinates) const
 Computes the probability density function of the distribution in correspondence of the supplied coordinates. More...
 
const Eigen::VectorXd & GetLocationVector () const
 Get the location vector of the distribution. More...
 
Eigen::VectorXd GetQuantile (double Prob) const
 Computes the inverse cumulative density function of the distribution in correspondence of the supplied probability. More...
 
const Eigen::MatrixXd & GetScaleMatrix () const
 Get the scale matrix of the distribution. More...
 
Eigen::MatrixXd GetVarMatrix () const
 Computes the variance matrix of the distribution. More...
 
bool SetDegreesOfFreedom (unsigned int a)
 Set the degrees of freedom of the distribution. More...
 
bool SetDimension (unsigned int Dimension)
 Set the dimensionality of the distribution. More...
 
bool SetLocationVector (const Eigen::VectorXd &mVect)
 Set the expected values vector. More...
 
bool SetLocationVector (const std::vector< double > &mVect)
 Set the location vector. More...
 
bool SetScaleMatrix (const Eigen::MatrixXd &SclMatr)
 Set the scale matrix of the distribution. More...
 
bool SetScaleMatrix (const std::vector< double > &mVect, bool RowWise=true)
 Set the scale matrix of the distribution. More...
 
- Public Member Functions inherited from Multivariate::AbstarctDistribution
virtual Eigen::RowVectorXd ExtractSample () const
 Generates a single simulation from the distribution. More...
 
virtual Eigen::RowVectorXd ExtractSampleCDF () const
 Generates a single simulation from the distribution and returns its marginal CDF. More...
 
virtual std::vector< double > ExtractSampleCDFVect () const
 Generates a single simulation from the distribution and returns its marginal CDF. More...
 
virtual std::map< unsigned int,
std::vector< double > > 
ExtractSamplesCDFMap (unsigned int NumSamples) const
 Extracts samples from the distribution and returns their marginal CDF. More...
 
virtual std::map< unsigned int,
std::vector< double > > 
ExtractSamplesMap (unsigned int NumSamples) const
 Generates multiple simulations from the distribution. More...
 
virtual std::vector< double > ExtractSampleVector () const
 Generates a single simulation from the distribution. More...
 
virtual double GetCumulativeDesity (const std::vector< double > &Coordinates) const
 Computes the cumulative density function of the distribution in correspondence of the supplied coordinates. More...
 
unsigned int GetCurrentSeed () const
 Get the random number generator seed. More...
 
virtual double GetDensity (const std::vector< double > &Coordinates) const
 Computes the probability density function of the distribution in correspondence of the supplied coordinates. More...
 
unsigned int GetDimension () const
 Get the dimensionality of the distribution. More...
 
virtual std::vector< double > GetQuantileVector (double Prob) const
 Computes the inverse cumulative density function of the distribution in correspondence of the supplied probability. More...
 
bool IsValid () const
 Check if the distribution is valid. More...
 
void SetRandomSeed (unsigned int NewSeed)
 Set the random number generator seed. More...
 
virtual double * GetQuantileArray (double Prob)
 Computes the inverse cumulative density function of the distribution in correspondence of the supplied probability. More...
 
virtual double GetCumulativeDesity (double *Coordinates) const
 Computes the cumulative density function of the distribution in correspondence of the supplied coordinates. More...
 
virtual double GetDensity (double *Coordinates) const
 Computes the probability density function of the distribution in correspondence of the supplied coordinates. More...
 
virtual double * ExtractSampleArray () const
 Generates a single simulation from the distribution. More...
 
virtual double ** ExtractSamplesMatix (unsigned int NumSamples) const
 Generates a single simulation from the distribution. More...
 
virtual double * ExtractSampleCDFArray () const
 Generates a single simulation from the distribution and returns its marginal CDF. More...
 
virtual double ** ExtractSamplesCDFMatix (unsigned int NumSamples) const
 Generates a single simulation from the distribution and returns its marginal CDF. More...
 

Friends

template<class F , class T >
void boost::math::tools::detail::handle_zero_derivative (F f, T &last_f0, const T &f0, T &delta, T &result, T &guess, const T &min, const T &max)
 
template<class F , class T >
boost::math::tools::newton_raphson_iterate (F f, T guess, T min, T max, int digits)
 
template<class F , class T >
boost::math::tools::newton_raphson_iterate (F f, T guess, T min, T max, int digits, boost::uintmax_t &max_iter)
 

Detailed Description

Multivariate Student's t Distribution

This class provides the functionality of calculating the probability density value, cumulative probability density value and generate random samples from a multivariate Student's t distribution.

Defining:

The multivariate student's t distribution funtion is defined as: \( f(\textbf{x})= \frac{\Gamma(\frac{v+k}{2})}{\Gamma(\frac{v}{2}) v^{k/2} \pi^{k/2} |\boldsymbol{\Sigma}|^{1/2} (1+ \frac{1}{v} (\textbf{x}-\boldsymbol{\mu})' \boldsymbol{\Sigma}^{-1} (\textbf{x}-\boldsymbol{\mu}))^{\frac{v+k}{2}}} \)

The algorithm for cdf calculation is based on A. Genz & F. Bretz (2002)

If you construct multiple instances of this class, to avoid the generated samples to be the same, you should supply a different seed. To do so, for example, you can call MyDistribution.SetRandomSeed(MyDistribution.GetCurrentSeed()+1U);

Please refer to the Examples page for usage examples.

Remarks
This class is re-entrant
Date
November 2013
License
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

Here, you can find a copy of the GNU Lesser General Public License. Alternatively, see gnu.org.

Constructor & Destructor Documentation

tDistribution::tDistribution ( unsigned int  Dimension,
unsigned int  DegFreedom,
const Eigen::VectorXd &  locVect,
const Eigen::MatrixXd &  ScaleMatr 
)

Construct a multivariate student's t with the given parameters.

Parameters
DimensionThe dimensionality of the distribution (supports also univariate student's t distributions in case this is 1)
DegFreedomThe degrees of freedom of the distribution
locVectThe location vector
ScaleMatrThe scale matrix

Construct a multivariate student's t distribution.

In case:

  • The Dimension is 0
  • The Degrees of freedom are 0
  • The location vector has a number of elements different from the Dimension
  • The ScaleMatr is not square
  • The ScaleMatr is not symmetric
  • The ScaleMatr is not semi-positive definite
  • The ScaleMatr has a number of rows different from the Dimension

The class will be considered invalid (it can be checked using IsValid()) and won't produce any result until the problem is fixed.

tDistribution::tDistribution ( unsigned int  Dimension = 1U,
unsigned int  DegFreedom = 1U 
)

Construct a multivariate standardized student's t.

Parameters
DimensionThe dimensionality of the multivariate normal (supports also univariate Gaussian distributions in case this is 1)
DegFreedomThe degrees of freedom of the distribution

Construct a multivariate student's t distribution with all location parameters set to 0 and scale matrix set to the identity.

In case the Dimension or the DegFreedom are 0 the class will be considered invalid (it can be checked using IsValid()) and won't produce any result until the problem is fixed.

If no argument is specified, a univariate standardized student's t with 1 degree of freedom, is created

Member Function Documentation

Eigen::MatrixXd tDistribution::ExtractSamples ( unsigned int  NumSamples) const
virtual

Generates multiple simulations from the distribution.

Parameters
NumSamplesThe number of simulation to run
Returns
A matrix with columns equal to the dimensionality of the distribution and rows equal to the number of simulations

This function generates NumSamples simulation from the current distribution and returns them in matrix form.

If NumSamples is 0 or the distribution is invalid, a null matrix is returned

Implements Multivariate::AbstarctDistribution.

Eigen::MatrixXd tDistribution::ExtractSamplesCDF ( unsigned int  NumSamples) const
virtual

Extracts samples from the distribution and returns their marginal CDF.

Parameters
NumSamplesThe number of simulation to run
Returns
A matrix with columns equal to the dimensionality of the distribution and rows equal to the number of simulations

This function generates NumSamples simulation from the current distribution, computes the marginal cumulative density function for each of them and returns them in matrix form.

This function simulates extractions from a t copula

If NumSamples is 0 or the distribution is invalid, a null matrix is returned

Implements Multivariate::AbstarctDistribution.

double tDistribution::GetCumulativeDesity ( const Eigen::VectorXd &  Coordinates) const
virtual

Computes the cumulative density function of the distribution in correspondence of the supplied coordinates.

Parameters
CoordinatesA vector containing the coordinates of the point for which the cdf should be computed
Returns
The value of the cumulative density function

This function computes the cumulative density function of the current distribution associated with the given coordinates.

If the number of elements in Coordinates is different from the dimensionality of the distribution or the distribution is invalid, -1 is returned.

Todo:
Add the Genz algorithm.
The Gentz algorithm described in Genz & Bretz (2002) should be implemented here.
Prof Genz suggested to use equation 13 and 14 to calculate T then use the algorithm on page 9 and then switch to quasi monte carlo points for the algorithm at page 14.

Implements Multivariate::AbstarctDistribution.

unsigned int Multivariate::tDistribution::GetDegreesOfFreedom ( ) const
inline

Get the degrees of freedom of the distribution.

Returns
The current degrees of freedom of the distribution
See Also
SetDegreesOfFreedom()
double tDistribution::GetDensity ( const Eigen::VectorXd &  Coordinates) const
virtual

Computes the probability density function of the distribution in correspondence of the supplied coordinates.

Parameters
CoordinatesA vector containing the coordinates of the point for which the pdf should be computed
Returns
The value of the probability density function

This function computes the probability density function of the current distribution associated with the given coordinates.

If the number of elements in Coordinates is different from the dimensionality of the distribution or the distribution is invalid, -1 is returned

Implements Multivariate::AbstarctDistribution.

const Eigen::VectorXd& Multivariate::tDistribution::GetLocationVector ( ) const
inline

Get the location vector of the distribution.

Returns
The current location vector of the distribution
See Also
SetLocationVector(const Eigen::VectorXd&)
Eigen::VectorXd tDistribution::GetQuantile ( double  Prob) const
virtual

Computes the inverse cumulative density function of the distribution in correspondence of the supplied probability.

Parameters
ProbThe probability for which the corresponding quantile must be found
Returns
A vector containing the coordinates of the quantile

This function computes the inverse cumulative density function of the current distribution associated with the given probability.

The solution is unique only in the univariate case.
Generally the system of equations \( F^{-1}(Coordinates_1 \cdots Coordinates_k)=Prob \) has k-1 degrees of freedom, where k is the dimensionality of the distribution.
The additional restriction imposed to get to an unique solution is that each coordinate has equal distance from it's mean.

If the probability supplied is greater than 1, less than 0 or the distribution is invalid, an empty vector is returned.

Implements Multivariate::AbstarctDistribution.

const Eigen::MatrixXd& Multivariate::tDistribution::GetScaleMatrix ( ) const
inline

Get the scale matrix of the distribution.

Returns
The current scale matrix of the distribution
See Also
SetScaleMatrix(const Eigen::MatrixXd&)
Eigen::MatrixXd tDistribution::GetVarMatrix ( ) const

Computes the variance matrix of the distribution.

Returns
The variance matrix

This function computes the variance matrix of the current distribution.

Defining:

  • \( v \) as the number of degrees of freedom of the distribution
  • \( \boldsymbol{\Sigma}=\begin{bmatrix} \sigma^2_1 & \cdots & \sigma_{1,k}\\ \vdots & \ddots & \vdots \\ \sigma_{k,1} & \cdots & \sigma^2_k \end{bmatrix} \) as the scale matrix

The variance matrix is defined as \( \frac{v}{v-2} \boldsymbol{\Sigma} \forall v>2 \)

If the degrees of freedom are less than 3 or the distribution is invalid, a null matrix is returned

bool tDistribution::SetDegreesOfFreedom ( unsigned int  a)

Set the degrees of freedom of the distribution.

Parameters
aThe number of degrees of freedom of the distribution
Returns
A boolean determining if the degrees of freedom of the distribution were changed successfully

This function tries to set the degrees of freedom of the distribution to the new ones.

In case a is less than 1 the degrees of freedom of the distribution will not be changed and this function will return false

See Also
GetDegreesOfFreedom()
bool tDistribution::SetDimension ( unsigned int  Dimension)
virtual

Set the dimensionality of the distribution.

Parameters
Dimensionthe new dimensionality of the distribution
Returns
A boolean determining if the dimensionality was changed successfully

This function will try to change the dimensionality of the distribution (e.g. 2 for bivariate, 3 for trivariate, etc.)

All the components of the location vector will be reset to 0 and the scale matrix will default to an identity matrix, degrees of freedom wil not be changed

If the argument passed is 0 the dimensionality will not be changed and the function will return false

See Also
GetDimension()

Implements Multivariate::AbstarctDistribution.

bool Multivariate::tDistribution::SetLocationVector ( const Eigen::VectorXd &  mVect)
inline

Set the expected values vector.

Parameters
mVectthe vector of new values for the location vector
Returns
A boolean determining if the location vector was changed successfully

This function attempts to set the location vector to the new values supplied.

If the degrees of freedom are more than 1, this vector is the mean vector

If the dimension of the vector is different from the dimension of the distribution the location vector is not changed and false is returned

See Also
GetLocationVector()
bool tDistribution::SetLocationVector ( const std::vector< double > &  mVect)

Set the location vector.

Parameters
mVectthe vector of new values for the location vector

This is an overloaded version of SetLocationVector(const Eigen::VectorXd&)

bool tDistribution::SetScaleMatrix ( const Eigen::MatrixXd &  SclMatr)

Set the scale matrix of the distribution.

Parameters
SclMatrthe new scale matrix of the distribution
Returns
A boolean determining if the scale matrix of the distribution was changed successfully

This function tries to set the scale matrix of the distribution to the new one.

In case:

  • The scale matrix is not square
  • The scale matrix is not symmetric
  • The scale matrix is not semi-positive definite
  • The scale matrix has a number of rows different from the Dimension

The scale matrix of the distribution will not be changed and this function will return false

See Also
GetScaleMatrix()
bool tDistribution::SetScaleMatrix ( const std::vector< double > &  mVect,
bool  RowWise = true 
)

Set the scale matrix of the distribution.

Parameters
mVecta vector containing the elements of the new scale matrix of the distribution
RowWiseif it's set to true (the default) the matrix will be filled by row. If it's false it will be filled by columns
Returns
A boolean determining if the scale matrix of the distribution was changed successfully

This function tries to set the scale matrix of the distribution to the new one.

Constructs square a matrix with number of rows equal to the dimensionality of the distribution, it is then filled with the values supplied in order according to the RowWise parameter

In case:

  • The vector size is different from the square of the distribution dimensionality
  • The scale matrix is not symmetric
  • The scale matrix is not semi-positive definite
  • The scale matrix has a number of rows different from the Dimension

The scale matrix of the distribution will not be changed and this function will return false

See Also
GetScaleMatrix()