cholcov
Cholesky-like covariance decomposition
Description
computes
the Cholesky factor T = cholcov(X)T of the matrix X such that
X = T'*T. The input matrix X must be symmetric
positive definite or symmetric positive semi-definite. Otherwise, T is
empty. For more information, see Symmetric Positive Definite Matrix.
If your input matrix X is positive definite, you can also use the
chol function to compute its Cholesky
factor.
[
returns T,num] = cholcov(X,AllowPosSemiDef)T as empty and num as an arbitrary positive
integer when AllowPosSemiDef is false and
X is positive semi-definite.
Examples
Create a symmetric positive definite 3-by-3 matrix of integers.
rng(0,"twister") % For reproducibility A = randi([0 2],3,3); X = A'*A
X = 3×3
8 6 2
6 5 1
2 1 5
Compute the Cholesky factor T such that X = T'*T.
T = cholcov(X)
T = 3×3
2.8284 2.1213 0.7071
0 0.7071 -0.7071
0 0 2.0000
Because X is positive definite, T is a square, upper triangular matrix.
Verify that X = T'*T.
all(isapprox(X,T'*T),"all")ans = logical
1
Create a symmetric positive semi-definite 3-by-3 matrix of integers.
rng(0,"twister") % For reproducibility v1 = randi([0 2],3,1); v2 = randi([0 2],3,1); X = v1*v1.' + v2*v2.'
X = 3×3
8 6 0
6 5 0
0 0 0
Compute the eigenvalues of X.
eig(X)
ans = 3×1
0
0.3153
12.6847
X is positive semi-definite because it has no negative eigenvalues and at least one zero eigenvalue.
Compute the Cholesky factor T such that X = T'*T. Also return the number of negative eigenvalues num in X.
[T,num]= cholcov(X)
T = 2×3
-0.3456 0.4426 0
2.8072 2.1918 0
num = 0
Because X is positive semi-definite, T is not a square matrix.
Verify that X = T'*T.
all(isapprox(X,T'*T),"all")ans = logical
1
Create a column vector of means mu and a positive definite covariance matrix Sigma.
mu = [-4; 0; 4]; Sigma = [2 1 0; 1 2 1; 0 1 2];
Calculate the Cholesky factor of the covariance matrix.
T = cholcov(Sigma)
T = 3×3
1.4142 0.7071 0
0 1.2247 0.8165
0 0 1.1547
Generate 200 random deviates from a standard normal distribution and transform them using the means mu and the Cholesky factor T.
rng(0,"twister") % For reproducibility n = 200; Z = randn(length(mu),n); % Standard normal deviates X = T*Z + mu; X = X';
X is a 200-by-3 matrix of correlated samples drawn from the set of means mu using the covariance matrix Sigma.
Create a 3-D scatter plot of the samples.
scatter3(X(:,1),X(:,2),X(:,3)); xlabel("X1") ylabel("X2") zlabel("X3") view([230 45])

Input Arguments
Input matrix, specified as a numeric matrix. The input matrix X
must be symmetric positive definite or symmetric positive semi-definite. Otherwise, the
Cholesky factor T is empty.
Data Types: single | double
Output Arguments
Cholesky factor, returned as a numeric matrix.
If
Xis positive definite, thenTis the square, upper triangular Cholesky factor matrix.If
Xis positive semi-definite, andAllowPosSemiDefistrue(the default), the function computesTfrom an eigenvalue decomposition ofX. In this case,Tis not necessarily triangular or square. Any eigenvectors whose corresponding eigenvalue is close to zero (within a small tolerance) are omitted. If any remaining eigenvalues are negative, thenTis empty.
T is empty if any of the following are true:
Xis not symmetric.Xis not positive definite or positive semi-definite.Xis not positive definite andAllowPosSemiDefisfalse.
Number of negative eigenvalues of X, returned as a nonnegative integer.
If
num=0, thenXis positive definite or positive semi-definite.If
num>0andAllowPosSemiDefistrue(the default), thenXis not positive definite or positive semi-definite, andnumis the number of negative eigenvalues ofX.If
num>0andAllowPosSemiDefisfalse, thenXis not positive definite, andnumis an arbitrary positive integer.If
numisNaN, thenXis not symmetric.
More About
A symmetric positive definite matrix is a symmetric matrix with all positive eigenvalues.
For any real invertible matrix A, you can construct a symmetric
positive definite matrix with the product X = A'*A. The Cholesky
factorization reverses this formula by saying that any symmetric positive definite matrix
X can be factored into the product T'*T.
A symmetric positive semi-definite matrix is defined in a similar manner, except that the eigenvalues must all be positive or zero.
The line between positive definite and positive semi-definite matrices is blurred in the
context of numeric computation. It is rare for eigenvalues to be exactly equal to zero, but
they can be numerically zero (on the order of machine precision). For this reason,
cholcov might be able to return a square, triangular Cholesky
factor for one positive semi-definite matrix, but could fail with another matrix that has
very similar eigenvalues.
Extended Capabilities
The cholcov function fully supports
thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced in R2007aStarting in R2024a, cholcov returns the
num output argument on the CPU when you specify
gpuArray input arguments. In previous releases, the
num output argument was returned on the GPU.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Seleziona un sito web
Seleziona un sito web per visualizzare contenuto tradotto dove disponibile e vedere eventi e offerte locali. In base alla tua area geografica, ti consigliamo di selezionare: .
Puoi anche selezionare un sito web dal seguente elenco:
Come ottenere le migliori prestazioni del sito
Per ottenere le migliori prestazioni del sito, seleziona il sito cinese (in cinese o in inglese). I siti MathWorks per gli altri paesi non sono ottimizzati per essere visitati dalla tua area geografica.
Americhe
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)