-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_error_cov.m
50 lines (37 loc) · 1.57 KB
/
compute_error_cov.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function [cov,covt] = compute_error_cov(data)
% PURPOSE:
% This subroutine calculates the error covariance matrix for a lagged
% ensemble forecast of a user specified climate time series. The user must input
% the error matrix for the forecast time series. Specifcally, the error matrix
% should be written such that each column of the matrix
% is for a given target day and the rows enteries give error at different leads.
%
% REFERENCES:
%
% CALLING SEQUENCE:
% [cov,covt] = compute_error_cov(data);
%
% INPUTS:
% data: A matrix of the forecast error for a given target day as a function of lead time and year.
% Assumed dimensions are [lead (L), target day (ntime), year (nyr)].
%
% OUTPUTS:
% cov: The [L,L,nyr] lagged error covariance matrix for input data
% supplied by user. L == lead (or initalization frequency) and
% nyr == the number of years for which the forecasts exist.
%
% covt: The lagged error covariance matrix for input data
% supplied by user averaged across all forecast years. As before,
% L == lead (or initalization frequency) and
% nyr == the number of years for which the forecasts exist.
[L,ntime,nyr] = size(data);
for ny=1:nyr
% Calculate the lagged error covariance matrix allowing for NaN's in data.
% This calculation in matrix notation is equivalent to C =
% data*data'/ntime.
A = squeeze(data(:,:,ny));
cov(:,:,ny) =nancov(A'); %Lagged error covariance matrix per year
end
% Average the lagged error covariance matrix across all years
covt = mean(cov,3);
end