0001 function Crs = slregcov(Cs, varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 [d1, d2, k] = size(Cs);
0036 if d1 ~= d2
0037 error('sltoolbox:notsquaremat', ...
0038 'The covariance matrices should be square');
0039 end
0040 d = d1;
0041 S.lambda = 0;
0042 S.gamma = 0;
0043 S.prior = [];
0044 S.poolcov = [];
0045 S = slparseprops(S, varargin{:});
0046 if isempty(S.prior)
0047 ispriorgiven = false;
0048 S.prior = ones(1, k) / k;
0049 else
0050 ispriorgiven = true;
0051 S.prior = S.prior(:)';
0052 if length(S.prior) ~= k
0053 error('sltoolbox:sizmismatch', ...
0054 'The length of prior is not consistent with the number of covariances');
0055 end
0056 S.prior = S.prior / sum(S.prior);
0057 end
0058 if S.lambda < 0 || S.lambda > 1
0059 error('sltoolbox:invalidarg', ...
0060 'lambda should be within [0, 1]');
0061 end
0062 if S.gamma < 0 || S.gamma > 1
0063 error('sltoolbox:invalidarg', ...
0064 'gamma should be within [0, 1]');
0065 end
0066 if ~isempty(S.poolcov)
0067 ispoolgiven = true;
0068 if ~isequal(size(S.poolcov), [d d]);
0069 error('sltoolbox:invalidarg', ...
0070 'The size of pooled covariance is illegal');
0071 end
0072 else
0073 ispoolgiven = false;
0074 end
0075
0076
0077
0078 if ~ispoolgiven
0079 if ~ispriorgiven
0080 S.poolcov = slpoolcov(Cs);
0081 else
0082 S.poolcov = slpoolcov(Cs, S.prior);
0083 end
0084 end
0085
0086
0087
0088 lambda = S.lambda;
0089 if lambda == 0
0090 Crs = Cs;
0091 elseif lambda == 1
0092 Crs = zeros(size(Cs));
0093 for i = 1 : k
0094 Crs(:, :, i) = S.poolcov;
0095 end
0096 else
0097 Crs = zeros(size(Cs));
0098 coeffsL = zeros(2, k);
0099 coeffsL(1, :) = (1 - lambda) * S.prior;
0100 coeffsL(2, :) = lambda;
0101 coeffsL = slnormalize(coeffsL, 1, 1);
0102 for i = 1 : k
0103 Crs(:,:,i) = coeffsL(1, i) * Cs(:,:,i) + coeffsL(2, i) * S.poolcov;
0104 end
0105 end
0106
0107
0108
0109 gamma = S.gamma;
0110 if gamma == 0
0111
0112 elseif gamma == 1
0113 for i = 1 : k
0114 avgtr = trace(Crs(:,:,i)) / d;
0115 Crs(:,:,i) = avgtr * eye(d, d);
0116 end
0117 else
0118 for i = 1 : k
0119 C0 = Crs(:,:,i);
0120 avgtr = trace(C0) / d;
0121 Crs(:,:,i) = (1-gamma) * C0 + gamma * avgtr * eye(d, d);
0122 end
0123 end
0124
0125