Background: I am conducting some research on equity return characteristics in relation to firms' ESG scores. Specifically, I am trying to test if investing in firms that increased their ESG scores during the past 12 months yields a higher return. Question: Why do I get two columns in “effort(i,k)”? Code is below. Thanks in advance! esg=rand(100,100); meq=rand(100,100); ret=rand(100,100); T=length(esg); nAss=size(esg,2); numPort=3; k=1:(numPort-1); prctlVals=100*(k*(1/numPort)); breakeffort=NaN(T,numPort-1); % loop for ESG effort for i=12:T; effort(i,k)=esg(i,k)-esg(i-11,k); end
% loop for breakpoints for i=1:T; breakeffort(i,:)= prctile(effort(i,:),prctlVals); end;
bestret=NaN(T,nAss); bestmeq=NaN(T,nAss); worstret=NaN(T,nAss); worstmeq=NaN(T,nAss); besg=NaN(T,nAss); wesg=NaN(T,nAss);
% allocating according to best/worst-in-class 12m holding period for i=1:11:T-11; for k=1:nAss; if esg(i,k)>=breakeffort(i,numPort-1); bestret(i:i+11,k)=ret(i:i+11,k); bestmeq(i:i+11,k)=meq(i:i+11,k); besg(i:i+11,k)=esg(i:i+11,k); end if esg(i,k)<=breakeffort(i,1); worstret(i:i+11,k)=ret(i:i+11,k); worstmeq(i:i+11,k)=meq(i:i+11,k); wesg(i:i+11,k)=esg(i:i+11,k); end end end
% Weights wbest=NaN(T,nAss); wworst=NaN(T,nAss); for i=1:T; for k=1:nAss; wbest(i,k)=bestmeq(i,k)/nansum(bestmeq(i,:)); wworst(i,k)=worstmeq(i,k)/nansum(worstmeq(i,:)); end end
% Calculating weighted and equal portfolio return bestwret=NaN(T,nAss); worstwret=NaN(T,nAss); bestVW=NaN(T,1); bestEW=NaN(T,1); worstVW=NaN(T,1); worstEW=NaN(T,1); for i=1:T; for k=1:nAss; bestwret(i,k)=bestret(i,k)*wbest(i,k); worstwret(i,k)=worstret(i,k)*wworst(i,k); end bestVW(i)=nansum(bestwret(i,:)); worstVW(i)=nansum(worstwret(i,:)); bestEW(i)=nansum(bestret(i,:)/nnz(~isnan(bestret(i,:)))); worstEW(i)=nansum(worstret(i,:)/nnz(~isnan(worstret(i,:)))); end

댓글 수: 1

Rik
Rik 2021년 9월 29일
편집: Rik 2021년 9월 29일
@Oliver If the answer solves your question, why not hit the 'accept' button?
Flags should be used to attract the attention of admins.
Also, can you try to fix the formatting of your code? Something like what I did below:
esg=rand(100,100); meq=rand(100,100); ret=rand(100,100); T=length(esg); nAss=size(esg,2);
numPort=3; k=1:(numPort-1);
prctlVals=100*(k*(1/numPort));
breakeffort=NaN(T,numPort-1);
% loop for ESG effort
for i=12:T; effort(i,k)=esg(i,k)-esg(i-11,k); end
% loop for breakpoints
for i=1:T; breakeffort(i,:)= prctile(effort(i,:),prctlVals); end;
bestret=NaN(T,nAss); bestmeq=NaN(T,nAss); worstret=NaN(T,nAss); worstmeq=NaN(T,nAss); besg=NaN(T,nAss); wesg=NaN(T,nAss);
% allocating according to best/worst-in-class 12m holding period
for i=1:11:T-11;
for k=1:nAss;
if esg(i,k)>=breakeffort(i,numPort-1);
bestret(i:i+11,k)=ret(i:i+11,k);
bestmeq(i:i+11,k)=meq(i:i+11,k);
besg(i:i+11,k)=esg(i:i+11,k);
end
if esg(i,k)<=breakeffort(i,1);
worstret(i:i+11,k)=ret(i:i+11,k);
worstmeq(i:i+11,k)=meq(i:i+11,k);
wesg(i:i+11,k)=esg(i:i+11,k);
end
end
end
% Weights
wbest=NaN(T,nAss); wworst=NaN(T,nAss);
for i=1:T;
for k=1:nAss;
wbest(i,k)=bestmeq(i,k)/nansum(bestmeq(i,:));
wworst(i,k)=worstmeq(i,k)/nansum(worstmeq(i,:));
end
end
% Calculating weighted and equal portfolio return
bestwret=NaN(T,nAss); worstwret=NaN(T,nAss);
bestVW=NaN(T,1); bestEW=NaN(T,1);
worstVW=NaN(T,1); worstEW=NaN(T,1);
for i=1:T;
for k=1:nAss;
bestwret(i,k)=bestret(i,k)*wbest(i,k);
worstwret(i,k)=worstret(i,k)*wworst(i,k);
end
bestVW(i)=nansum(bestwret(i,:));
worstVW(i)=nansum(worstwret(i,:));
bestEW(i)=nansum(bestret(i,:)/nnz(~isnan(bestret(i,:))));
worstEW(i)=nansum(worstret(i,:)/nnz(~isnan(worstret(i,:))));
end

댓글을 달려면 로그인하십시오.

 채택된 답변

Cris LaPierre
Cris LaPierre 2021년 9월 27일

1 개 추천

Because k=1:(numPort-1) and numPort=3

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Financial Toolbox에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2021년 9월 26일

편집:

Rik
2021년 9월 29일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by