Documentation |
Covariance ignoring NaNs
c = nancov(X) c = nancov(..., 'pairwise')
nancov for financial times series objects is based on the Statistics Toolbox™ function nancov. See nancov in the Statistics Toolbox documentation.
c = nancov(X), if X is a financial time series object with one series and returns the sample variance of the values in X, treating NaNs as missing values. For a financial time series object containing more than one series, where each row is an observation and each series a variable, nancov(X) is the covariance matrix computing using rows of X that do not contain any NaN values. nancov(X,Y), where X and Y are financial time series objects with the same number of elements, is equivalent to nancov([X(:) Y(:)]).
nancov(X) or nancov(X,Y) normalizes by (N-1) if N >1, where N is the number of observations after removing missing values. This makes nancov the best unbiased estimate of the covariance matrix if the observations are from a normal distribution. For N = 1, cov normalizes by N.
nancov(X,1) or nancov(X,Y,1) normalizes by N and produces the second moment matrix of the observations about their mean. nancov(X,Y,0) is the same as nancov(X,Y), and nancov(X,0) is the same as nancov(X).
c = nancov(..., 'pairwise') computes c(i,j) using rows with no NaN values in columns ior j. The result may not be a positive definite matrix. c = nancov(..., 'complete') is the default, and it omits rows with any NaN values, even if they are not in column i or j. The mean is removed from each column before calculating the result.
To generate random data having nonzero covariance between column 4 and the other columns:
x = randn(30, 4); % uncorrelated data x(:, 4) = sum(x, 2); % introduce correlation x(2, 3) = NaN; % introduce one missing value f = fints((today:today+29)', x); % create a fints object using x c = nancov(f) % compute sample covariance
c = 1.6898 -0.0005 0.3612 1.9143 -0.0005 1.0833 -0.5513 0.6059 0.3612 -0.5513 1.0369 0.7570 1.9143 0.6059 0.7570 4.4895