nearcorr
Compute nearest correlation matrix by minimizing Frobenius distance
Description
Examples
Compute the Nearest Correlation Matrix
Find the nearest correlation matrix in the Frobenius norm for a given nonpositive semidefinite matrix.
Specify anN
-by-N
symmetric matrix with all elements in the interval[-1, 1]
and unit diagonal.
A = [1.0000 0 0 0 -0.9360 0 1.0000 -0.5500 -0.3645 -0.5300 0 -0.5500 1.0000 -0.0351 0.0875 0 -0.3645 -0.0351 1.0000 0.4557 -0.9360 -0.5300 0.0875 0.4557 1.0000];
Compute the eigenvalues ofA
usingeig
.
eig(A)
ans =5×1-0.1244 0.3396 1.0284 1.4457 2.3107
The smallest eigenvalue is less than0
, which indicates thatA
is not a positive semidefinite matrix.
Compute the nearest correlation matrix usingnearcorr
with the default Newton algorithm.
B = nearcorr(A)
B =5×51.0000 0.0372 0.0100 -0.0219 -0.8478 0.0372 1.0000 -0.5449 -0.3757 -0.4849 0.0100 -0.5449 1.0000 -0.0381 0.0996 -0.0219 -0.3757 -0.0381 1.0000 0.4292 -0.8478 -0.4849 0.0996 0.4292 1.0000
Compute the eigenvalues ofB
.
eig(B)
ans =5×10.0000 0.3266 1.0146 1.4113 2.2475
All of the eigenvalues are greater than or equal to0
, which means thatB
is a positive semidefinite matrix.
When you usenearcorr
, you can specify the alternating projections algorithm by setting the name-value pair argument'method'
to'projection'
.
nearcorr(A,'method','projection')
ans =5×51.0000 0.0372 0.0100 -0.0219 -0.8478 0.0372 1.0000 -0.5449 -0.3757 -0.4849 0.0100 -0.5449 1.0000 -0.0381 0.0996 -0.0219 -0.3757 -0.0381 1.0000 0.4292 -0.8478 -0.4849 0.0996 0.4292 1.0000
You can also impose elementwise weights by specifying the“重量”
name-value pair argument. For more information on elementwise weights, seeWeights.
W = [0.0000 1.0000 0.1000 0.1500 0.2500 1.0000 0.0000 0.0500 0.0250 0.1500 0.1000 0.0500 0.0000 0.2500 1 0.1500 0.0250 0.2500 0.0000 0.2500 0.2500 0.1500 1 0.2500 0.0000]; nearcorr(A,“重量”,W)
ans =5×51.0000 0.0014 0.0287 -0.0222 -0.8777 0.0014 1.0000 -0.4980 -0.7268 -0.4567 0.0287 -0.4980 1.0000 -0.0358 0.0878 -0.0222 -0.7268 -0.0358 1.0000 0.4465 -0.8777 -0.4567 0.0878 0.4465 1.0000
In addition, you can imposeN
-by-1
vectorized weights by specifying the“重量”
name-value pair argument. For more information on vectorized weights, seeWeights.
W = linspace(0.1,0.01,5)'
W =5×10.1000 0.0775 0.0550 0.0325 0.0100
C = nearcorr(A,“重量”, W)
C =5×51.0000 0.0051 0.0021 -0.0056 -0.8490 0.0051 1.0000 -0.5486 -0.3684 -0.4691 0.0021 -0.5486 1.0000 -0.0367 0.1119 -0.0056 -0.3684 -0.0367 1.0000 0.3890 -0.8490 -0.4691 0.1119 0.3890 1.0000
Compute the eigenvalues ofC
.
eig(C)
ans =5×10.0000 0.3350 1.0272 1.4308 2.2070
All of the eigenvalues are greater than or equal to0
, which means thatC
is a positive semidefinite matrix.
Generate a Correlation Matrix for Stocks with Missing Values
Usenearcorr
to create a positive semidefinite matrix for a correlation matrix for stocks with missing values.
Assume that you have stock values with missing values.
Stock_Missing = [59.875 42.734 47.938 60.359 NaN 69.625 61.500 62.125 53.188 49.000 39.500 64.813 34.750 56.625 83.000 44.500 55.750 50.000 38.938 62.875 30.188 43.375 NaN 29.938 65.500 51.063 45.563 69.313 48.250 62.375 85.250 46.875 69.938 47.000 52.313 71.016 37.500 59.359 61.188 48.219 61.500 44.188 NaN 57.000 35.313 55.813 51.500 62.188 59.230 48.210 62.190 61.390 54.310 70.170 61.750 91.080 NaN 48.700 60.300 68.580 61.250 70.340 61.590 90.350 52.900 52.690 54.230 61.670 68.170 NaN 57.870 88.640 57.370 59.040 59.870 62.090 61.620 66.470 65.370 85.840];
Usecorr
to compute the correlation matrix and then useeig
to check if the correlation matrix is positive semidefinite.
A = corr(Stock_Missing,'Rows',“成对”); eig(A)
ans =8×1-0.1300 -0.0398 0.0473 0.2325 0.6278 1.6276 1.7409 3.8936
A
has eigenvalues that are less than0
, which indicates that the correlation matrix is not positive semidefinite.
Usenearcorr
with this correlation matrix to generate a positive semidefinite matrix where all eigenvalues are greater than or equal to0
.
B = nearcorr(A); eigenvalues = eig(B)
eigenvalues =8×10.0000 0.0000 0.0180 0.2205 0.5863 1.6026 1.7258 3.8469
Copyright 2019 The MathWorks, Inc.
Input Arguments
A
—Input correlation matrix
matrix
Input correlation matrix, specified as anN-by-Nsymmetric approximate correlation matrix with all elements in the interval[-1 1]
and unit diagonal. TheA
input may or may not be a positive semidefinite matrix.
Example:A = [1.0000 0 0 0 -0.9360 0 1.0000 -0.5500 -0.3645 -0.5300 0 -0.5500 1.0000 -0.0351 0.0875 0 -0.3645 -0.0351 1.0000 0.4557 -0.9360 -0.5300 0.0875 0.4557 1.0000]
数据类型:single
|double
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, whereName
is the argument name andValue
is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.
Before R2021a, use commas to separate each name and value, and encloseName
in quotes.
Example:nearcorr(A,'Tolerance',1e-7,'MaxIterations',500,'Method','newton','Weights',weight_vector)
returns a nearest correlation matrix by minimizing the Frobenius distance.
Tolerance
—Termination tolerance for algorithm
1e-6
(default) |positive scalar
Termination tolerance for the algorithm, specified as the comma-separated pair consisting of'Tolerance'
and a positive scalar.
Example:'Tolerance',1e-7
数据类型:single
|double
MaxIterations
—Maximum number of solver iterations
200
(default) |positive integer
Maximum number of solver iterations, specified as the comma-separated pair consisting of'MaxIterations'
and a positive integer.
Example:'MaxIterations',500
数据类型:single
|double
Method
—Method for solving nearest correlation matrix problem
'newton'
(default) |'projection'
Method for solving nearest correlation matrix problem, specified as the comma-separated pair consisting of'Method'
and one of the values in the following table.
Value | Description |
---|---|
'newton' |
The Newton algorithm is quadratically convergent. If you specify the |
'projection' |
The alternating projections algorithm can converge to the nearest correlation matrix with high accuracy, at best linearly. If you specify the |
Example:'Method','projection'
数据类型:char
|string
Weights
—Weights for confidence levels of entries in input matrix
[ ]
(default) |matrix
|vector
Weights for confidence levels of entries in the input matrix, specified as the comma-separated pair consisting of“重量”
and either a symmetric matrix or anN
-by-1
vector.
对称矩阵——当你指定
Weights
as a symmetric matrixW
with all elements >=0
to do elementwise weighting, the nearest correlation matrixY
is computed by minimizing the norm of (W⚬ (A-Y)). Larger weight values place greater importance on the corresponding elements inA
.N
-by-1
vector — When you specifyWeights
as anN
-by-1
vectorw
with positive numeric values, the nearest correlation matrixY
is computed by minimizing the norm of (diag
(w)0.5× (A-Y) ×diag
(w)0.5).
Note
Matrix weights put weight on individual entries of the correlation matrix. A full matrix must be specified, but you can control which entries are more important to match. Alternatively, vector weights put weight on a full column (and the corresponding row). Fewer weights need to be specified as compared to the matrix weights, but an entire column (and the corresponding row) is weighted by a single weight.
Example:“重量”,W
数据类型:single
|double
Output Arguments
Y
— Nearest correlation matrix to input A
positive semidefinite matrix
Nearest correlation matrix to the inputA
, returned as a positive semidefinite matrix.
References
[1] Higham, N. J. "Computing the Nearest Correlation Matrix — A Problem from Finance."IMA Journal of Numerical Analysis.Vol. 22, Issue 3, 2002.
[2] Qi, H. and D. Sun. "An Augmented Lagrangian Dual Approach for the H-Weighted Nearest Correlation Matrix Problem."IMA Journal of Numerical Analysis.Vol. 31, Issue 2, 2011.
[3] Pang, J. S., D. Sun, and J. Sun. "Semismooth Homeomorphisms and Strong Stability of Semidefinite and Lorentz Complementarity Problems."Mathematics of Operation Research.Vol. 28, Number 1, 2003.
Version History
See Also
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
俄文n the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- 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)