Top Description Fields Constructors Methods
org.apache.commons.math3.random

public Class CorrelatedRandomVectorGenerator

extends Object
implements RandomVectorGenerator
Class Inheritance
All Implemented Interfaces
org.apache.commons.math3.random.RandomVectorGenerator
Imports
org.apache.commons.math3.exception.DimensionMismatchException, org.apache.commons.math3.linear.RealMatrix, .RectangularCholeskyDecomposition

A RandomVectorGenerator that generates vectors with with correlated components.

Random vectors with correlated components are built by combining the uncorrelated components of another random vector in such a way that the resulting correlations are the ones specified by a positive definite covariance matrix.

The main use for correlated random vector generation is for Monte-Carlo simulation of physical problems with several variables, for example to generate error vectors to be added to a nominal vector. A particularly interesting case is when the generated vector should be drawn from a Multivariate Normal Distribution. The approach using a Cholesky decomposition is quite usual in this case. However, it can be extended to other cases as long as the underlying random generator provides normalized values like GaussianRandomGenerator or UniformRandomGenerator.

Sometimes, the covariance matrix for a given simulation is not strictly positive definite. This means that the correlations are not all independent from each other. In this case, however, the non strictly positive elements found during the Cholesky decomposition of the covariance matrix should not be negative either, they should be null. Another non-conventional extension handling this case is used here. Rather than computing C = UT.U where C is the covariance matrix and U is an upper-triangular matrix, we compute C = B.BT where B is a rectangular matrix having more rows than columns. The number of columns of B is the rank of the covariance matrix, and it is the dimension of the uncorrelated random vector that is needed to compute the component of the correlated vector. This class handles this situation automatically.

Since
1.2

Field Summary

Modifier and TypeField and Description
private final NormalizedRandomGenerator
generator

Underlying generator.

private final double[]
mean

Mean vector.

private final double[]
normalized

Storage for the normalized vector.

private final RealMatrix
root

Root of the covariance matrix.

Constructor Summary

AccessConstructor and Description
public
CorrelatedRandomVectorGenerator(double[]
Expected mean values for all components.
mean
,
RealMatrix
Covariance matrix.
covariance
,
double
Diagonal elements threshold under which column are considered to be dependent on previous ones and are discarded
small
,
NormalizedRandomGenerator
underlying generator for uncorrelated normalized components.
generator
)

Builds a correlated random vector generator from its mean vector and covariance matrix.

public
CorrelatedRandomVectorGenerator(RealMatrix
Covariance matrix.
covariance
,
double
Diagonal elements threshold under which column are considered to be dependent on previous ones and are discarded.
small
,
NormalizedRandomGenerator
Underlying generator for uncorrelated normalized components.
generator
)

Builds a null mean random correlated vector generator from its covariance matrix.

Method Summary

Modifier and TypeMethod and Description
public NormalizedRandomGenerator

Returns:

underlying uncorrelated components generator
getGenerator
()

Get the underlying normalized components generator.

public int

Returns:

rank of the square matrix.
getRank
()

Get the rank of the covariance matrix.

public RealMatrix

Returns:

root of the square matrix
getRootMatrix
()

Get the root of the covariance matrix.

public double[]

Returns:

a random vector as an array of double. The returned array is created at each call, the caller can do what it wants with it.
nextVector
()

Implements org.apache.commons.math3.random.RandomVectorGenerator.nextVector.

Generate a correlated random vector.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

generatorback to summary
private final NormalizedRandomGenerator generator

Underlying generator.

meanback to summary
private final double[] mean

Mean vector.

normalizedback to summary
private final double[] normalized

Storage for the normalized vector.

rootback to summary
private final RealMatrix root

Root of the covariance matrix.

Constructor Detail

CorrelatedRandomVectorGeneratorback to summary
public CorrelatedRandomVectorGenerator(double[] mean, RealMatrix covariance, double small, NormalizedRandomGenerator generator)

Builds a correlated random vector generator from its mean vector and covariance matrix.

Parameters
mean:double[]

Expected mean values for all components.

covariance:RealMatrix

Covariance matrix.

small:double

Diagonal elements threshold under which column are considered to be dependent on previous ones and are discarded

generator:NormalizedRandomGenerator

underlying generator for uncorrelated normalized components.

Exceptions
NonPositiveDefiniteMatrixException:
if the covariance matrix is not strictly positive definite.
DimensionMismatchException:
if the mean and covariance arrays dimensions do not match.
CorrelatedRandomVectorGeneratorback to summary
public CorrelatedRandomVectorGenerator(RealMatrix covariance, double small, NormalizedRandomGenerator generator)

Builds a null mean random correlated vector generator from its covariance matrix.

Parameters
covariance:RealMatrix

Covariance matrix.

small:double

Diagonal elements threshold under which column are considered to be dependent on previous ones and are discarded.

generator:NormalizedRandomGenerator

Underlying generator for uncorrelated normalized components.

Exceptions
NonPositiveDefiniteMatrixException:
if the covariance matrix is not strictly positive definite.

Method Detail

getGeneratorback to summary
public NormalizedRandomGenerator getGenerator()

Get the underlying normalized components generator.

Returns:NormalizedRandomGenerator

underlying uncorrelated components generator

getRankback to summary
public int getRank()

Get the rank of the covariance matrix. The rank is the number of independent rows in the covariance matrix, it is also the number of columns of the root matrix.

Returns:int

rank of the square matrix.

See Also
getRootMatrix()
getRootMatrixback to summary
public RealMatrix getRootMatrix()

Get the root of the covariance matrix. The root is the rectangular matrix B such that the covariance matrix is equal to B.BT

Returns:RealMatrix

root of the square matrix

See Also
getRank()
nextVectorback to summary
public double[] nextVector()

Implements org.apache.commons.math3.random.RandomVectorGenerator.nextVector.

Generate a correlated random vector.

Returns:double[]

a random vector as an array of double. The returned array is created at each call, the caller can do what it wants with it.