How can I make a dynamic portfolio allocation with the following code?
1 回表示 (過去 30 日間)
古いコメントを表示
Hello all,
Thanks to Semin, I obtained this code from file exchange:
function [Yiv, Eiv] = indivfc(X, y, m, rol, lag, distr)
%INDIVFC: Make individual forecasts
% [YIV, EIV] = INDIVFC(X, y, m, ...) fits multiple individual generalized
% linear models per factor using the predictors stored in matrix X and
% response Y with the in-sample ending at M by the means of the GLMFIT
% function, and makes multiple one-step ahead forecasts on the base of
% Y(t) = c1 + c2 X(t-lag,j).
%
% 'X' is a matrix with rows corresponding to observations, and columns to
% predictor variables. GLMFIT automatically includes a constant term
% in the model (do not enter a column of ones directly into 'X').
% 'y' is a vector of response values. 'm' refers to the start of the
% hold-out period and is assumed to be sufficiently larger than 1, to
% avoid for unnecessary large estimation errors.
%
% [YIV, EIV] = INDIVFC(X, Y, M, q0, 'val1', 'val2', 'val3') allows you to
% specify additional parameter values to control the model fit and
% forecasts. Parameter values are:
%
% 'rol' - length of the rolling window. In case 'rol' is set equal to
% zero, the corresponding individual forecasts and pooled regression
% are made through an expanding window framework. Important, it is
% obligated to set rol > m and it is advisably to set rol >> m.
%
% 'lag' - the number of lags in the regression Y(t) = c1 +
% c2*X(t-lag)
%
% 'distr' - Acceptable values for DISTR are 'normal', 'binomial',
% 'poisson', 'gamma', and 'inverse gaussian'. The distribution is
% fit using the canonical link corresponding to DISTR. See also the
% GLMFIT function for further explanation.
%
% GLMFIT treats NaNs in 'X' and 'y' as missing data, and removes the
% corresponding observations.
%
% [YIV, EIV] = INDIVFC(...) returns the 'n' by 'k' matrices:
% 'Yiv' individual forecasts of 'y' on the base of the
% factors 1,...,k
% 'Eiv' the realized prediction errors
% Semin Ibisevic (2011)
% $Date: 6/10/2011 19:31:03 $
if nargin < 3
error('stats:indivfc:TooFewInputs','At least three arguments are required');
end
if nargin < 4 || isempty(rol), rol = 0; end
if nargin < 5 || isempty(lag), lag = 1; end
if nargin < 6 || isempty(distr), distr = 'normal'; end
if lag < 0
error('stats:indivfc:InvalidInput','Lag cannot be negative');
end
if rol > m
error('stats:indivfc:InvalidInput','m should be always larger than rol');
end
% Initialization
n = size(y,1);
k = size(X,2);
if size(X,1) ~= size(y,1)
error('stats:indivfc:Dimension','The dimensions of X and y do not agree');
end
Yiv = zeros(n,k);
Eiv = zeros(n,k);
iter = rol+1;
for t=m:n
% forecasts are made individually per predictor j
for j=1:k
Xtemp = X( iter:t-lag , j);
Ytemp = y( iter+lag:t );
b = glmfit(Xtemp,Ytemp,distr);
Yiv(t+1,j) = [1, X(t, j)]*b;
if t < n
Eiv(t+1,j) = y(t+1) - Yiv(t+1,j);
end
end
% in case of a rolling window
if rol > 0
iter = iter+1;
end
end
I hope you can help me out since I am stuck at the moment.
This is my problem:
I have to figure out how to invest dynamically in a portfolio which consist of the following risky assets: a market factor (mkt), a value factor (hml) and a momentum factor (mom). To figure that out, I think I need to make an expanding window regression starting from the earliest of my dataset which goes back to 1926. I tried to do this in your code and plugged in the following values:
[YIV, EIV] = indivfc(A,C,dd,0,1,'normal')
Here ‘A’ is the matrix which contains the factor returns for mkt, hml and mom. ‘C’ is the matrix with the weights in it, ‘dd’ is the matrix which contains the dates. But here’s the problem. When I want to put in the window size of 30, I get the following error:
>> [YIV, EIV] = indivfc(A,C,dd,30,0,'normal')
Attempted to access R(1); index out of bounds
because numel(R)=0.
Error in glmfit (line 242)
rankx = sum(abs(diag(R)) >
abs(R(1))*max(n,ncolx)*eps(class(R)));
Error in indivfc (line 76)
b = glmfit(Xtemp,Ytemp,distr);
What would you suggest to change? After all, I want to make an in-sample and an out of sample test to see how to dynamically adjust my portfolio. I tried to do it with putting in Zero for the rolling window, and this yields a matrix. But how to interpret that matrix?
Thank you for your time, I would really appreciate your help!
Kind regards,
Kevin van Berkel
0 件のコメント
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Portfolio Optimization and Asset Allocation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!