I am trying to calculate a HC3 covariance matrix to adjust for unequal variances as add in and update parameters of a model. Here is how I am calculating it in matlab:
hat = X(1:t,:) * inv(Xprime(:,1:t) * X(1:t,:)) * Xprime(:,1:t); % Hat matrix
h_ii = diag(hat); % Leverage scores
sigma_sq = (Residual_MM(1:t,:)' * Residual_MM(1:t,:)) ./ ((1 - h_ii) * (1 - h_ii)' ); %HC2
Omega = sigma_sq * eye(t);
Covariance = (Xprime(:,1:t) * X(1:t,:)) \ ( Xprime(:,1:t) * Omega * X(1:t,:)) / (Xprime(:,1:t) * X(1:t,:)); %HC3 Sandwich
Am I approaching this appropriately? I eventually need to invert this matrix, but I get an error saying it is singular.