Optimizing code for HAC Weight matrix generation - How to be faster?
조회 수: 10 (최근 30일)
이전 댓글 표시
Hi everyone,
I am re-creating Newey-West procedure for a heteroskedasticity and autocorrelation consistent standard errors (HAC) from scratch.
In order to compute the sandwich matrix V(b), I need to generate the weight matrix W
Since I have to run that test thousands of times, I need to optimize the code. Right now, I have this:
%% Weight matrix generation for HAC
T = length(Dependant_Variable);
k = length(Coeff);
df = T-k;
Optimal_Lags = floor(4*(T/100)^(2/9));
Residual_correlation = Residual*Residual';
Weight_correlation = zeros(size(Residual_correlation));
for i = 1:size(Weight_correlation,1)
for j = 1:size(Weight_correlation,2)
if abs(i-j)>Optimal_Lags
Weight_correlation(i,j)=0;
else
Weight_correlation(i,j)=(T/df)*Residual_correlation(i,j)*(1-abs(j-i)/(Optimal_Lags+1));
end
end
end
Question:
How can I get the same result without using loops?
Thanks and best regards,
댓글 수: 0
채택된 답변
Alan Stevens
2024년 3월 18일
Try replacing the loops with something like this:
i = (1:size(Weight_correlation,1))';
j = 1:size(Weight_correlation,2);
c = ones(1,size(Weight_correlation,2));
r = ones(size(Weight_correlation,1),1);
lags = abs(i*c-r*j);
ind1 = lags>Optimal_Lags;
ind2 = lags<=Optimal_Lags;
Weight_correlation(ind1)=0;
Weight_correlation(ind2)=(T/df)*Residual_correlation(ind2).*(1-lags(ind2)/(Optimal_Lags+1));
Check carefully, as I can't really test it properly, not having your data.
댓글 수: 2
Alan Stevens
2024년 3월 20일
편집: Alan Stevens
2024년 3월 20일
Try this:
Variables = rand(10,10);
Companies = rand(10,1);
Coeff = (Variables'*Variables)\Variables'*Companies;
Expected = Variables*Coeff;
Residual = Companies-Expected;
T = length(Companies);
k = length(Coeff);
df = T-k;
Optimal_Lags = floor(4*(T/100)^(2/9));
Residual_correlation = Residual*Residual';
Weight_correlation = zeros(size(Residual_correlation));
for i = 1:size(Weight_correlation,1)
for j = 1:size(Weight_correlation,2)
if abs(i-j)>Optimal_Lags
Weight_correlation(i,j)=0;
else
Weight_correlation(i,j)=Residual_correlation(i,j)*(1-abs(j-i)/(Optimal_Lags+1));
end
end
end
i = (1:size(Weight_correlation,1))';
j = 1:size(Weight_correlation,2);
c = ones(1,size(Weight_correlation,2));
r = ones(size(Weight_correlation,1),1);
lags = abs(i*c-r*j);
ind2 = lags<=Optimal_Lags;
Weight_corr = ind2.*Residual_correlation.*(1-lags/(Optimal_Lags+1));
Check = Weight_correlation - Weight_corr;
disp(Check)
(I note that you removed the T/df term which gave the inf values!).
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Nonspherical Models에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!