Factor Model-Empty answer
3 views (last 30 days)
Show older comments
Hi everyone, I'm trying to run a multivariate regression in order to estimate a factor model. Everything is running but when I look at the results my variables are empty. For example beta in the Workspace is: []. The same for the other results. Any hint?
close all clear all clc
%Data Import data=importdata('PortfolioData.xlsx');
%% Naming Variables
Dates=data.data.Return(1,181:end);
Dates=Dates';
Identities=data.data.Return(:,1);
Returns=data.data.Return(2:end,181:end);
Returns2=[Identities(2:end,1) Returns];
Returns2=Returns2';
FReturns=fints(Dates, Returns2(1:end-1,1));
%% Define Factor list
FactorList = {'MktmRf'; 'SMB'; 'HML'; 'MOM' };
MKTMRF=data.data.Factors(:,3);
SMB=data.data.Factors(:,4);
HML=data.data.Factors(:,5);
MOM=data.data.Factors(:,6);
FactorData = [MKTMRF SMB HML MOM];
%% Get Information about the data
SeriesList=fieldnames(FReturns,1);
AssetList=setdiff(SeriesList, FactorList);
NumSeries = numel(SeriesList);
NumAssets = numel(AssetList);
%% Primary controls
TMonth = 9; % terminal month for each historical analysis period
Window = 5; % historical analysis period in years
MaxNaNs = 4*260; % max number of daily NaNs in an analysis period
%% Set up date math
StartYear = year(Dates(1));
StartMonth = month(Dates(1));
EndYear = year(Dates(end));
EndMonth = month(Dates(end));
NumPeriods = EndYear - StartYear - Window + 1;
%% AnalysisPeriod = NaN(NumPeriods,1);
Alpha = NaN(NumAssets, NumPeriods);
Beta = NaN(NumAssets, NumPeriods);
SMB = NaN(NumAssets, NumPeriods);
HML = NaN(NumAssets, NumPeriods);
Sigma = NaN(NumAssets, NumPeriods);
StdAlpha = NaN(NumAssets, NumPeriods);
StdBeta = NaN(NumAssets, NumPeriods);
StdSMB = NaN(NumAssets, NumPeriods);
StdHML = NaN(NumAssets, NumPeriods);
FFLLF = NaN(NumPeriods,1);
FFDoF = NaN(NumPeriods,1);
FFXLLF = NaN(NumPeriods,1);
FFXDoF = NaN(NumPeriods,1);
%%Step 8 - main loop
TYear = StartYear + Window; % initial terminal year
for K = 1:NumPeriods % Step 8a - get start and end dates for current analysis period StartDate = datenum(TYear - Window,TMonth,eomday(TYear,TMonth)) + 1; EndDate = datenum(TYear,TMonth,eomday(TYear,TMonth)); % Step 8b - locate actual start and end dates in the data StartIndex = find(Dates >= StartDate,1,'first'); EndIndex = find(Dates <= EndDate,1,'last');
AnalysisPeriod(K) = Dates(EndIndex); % Step 8c - determine active assets for current analysis period %%%%OK
Active = true(NumAssets,1); for i = 1:NumAssets TestActive = sum(~isfinite(fts2mat(FReturns.(AssetList{i})(StartIndex:EndIndex)))); if TestActive > MaxNaNs; Active(i) = false; end end NumActive = sum(Active);
% Step 8d - set up regression with active assets over current date range %%%OK
Dates = Dates(StartIndex:EndIndex);
AssetData = fts2mat(FReturns.(AssetList));
AssetData = AssetData(StartIndex:EndIndex,Active);
FactorData = FactorData(StartIndex:EndIndex,:);
AssetData = AssetData - repmat(FactorData(:,1),1,size(AssetData,2));
NumSamples = size(AssetData,1);
Design = cell(NumSamples,1); for t = 1:NumSamples Design{t} = repmat([ 1, FactorData(t,2:4)],NumActive,1); end
XDesign = cell(NumSamples,1); for t = 1:NumSamples XDesign{t} = repmat(FactorData(t,2:4),NumActive,1); end % Step 8e - set up seemingly-unrelated regression
FactorDesign = convert2sur(Design,1:NumActive); XFactorDesign = convert2sur(XDesign,1:NumActive);
% Step 8f - multivariate normal regression
MaxIter = 1000;
TolObj = 1.0e-10;
TolParam = 1.0e-8;
[Param, Covar] = ecmmvnrmle(AssetData, FactorDesign, MaxIter, TolParam, TolObj);
StdParam = ecmmvnrstd(AssetData, FactorDesign, Covar, 'fisher');
% estimate model with Alpha = 0 restriction
[XParam, XCovar] = ecmmvnrmle(AssetData, XFactorDesign, MaxIter, TolParam, TolObj);
% Step 8g - compute log-likelihood functions
FFLLF(K) = ecmmvnrobj(AssetData, FactorDesign, Param, Covar); FFXLLF(K) = ecmmvnrobj(AssetData, XFactorDesign, XParam, XCovar); FFDoF(K) = NumActive; FFXDoF(K) = NumActive;
% Step 8h - pack estimates into analysis variables Param = reshape(Param,4,NumActive)'; StdParam = reshape(StdParam,4,NumActive)'; Alpha(Active,K) = Param(:,1); Beta(Active,K) = Param(:,2); SMB(Active,K) = Param(:,3); HML(Active,K) = Param(:,4);
Sigma(Active,K) = sqrt(diag(Covar)); StdAlpha(Active,K) = StdParam(:,1); StdBeta(Active,K) = StdParam(:,2); StdSMB(Active,K) = StdParam(:,3); StdHML(Active,K) = StdParam(:,4); % Step 8i - increment year by 1
TYear = TYear + 1; end %%
0 Comments
Answers (0)
See Also
Categories
Find more on Portfolio Optimization and Asset Allocation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!