-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOLS.m
More file actions
42 lines (30 loc) · 1.19 KB
/
Copy pathOLS.m
File metadata and controls
42 lines (30 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function [mu, Q] = OLS(returns, factRet)
% Use this function to perform a basic OLS regression with all factors.
% You can modify this function (inputs, outputs and code) as much as
% you need to.
% *************** WRITE YOUR CODE HERE ***************
%----------------------------------------------------------------------
% Number of observations and factors
[T, p] = size(factRet);
% Data matrix
X = [ones(T,1) factRet];
% Regression coefficients
B = (X' * X) \ X' * returns;
% Separate B into alpha and betas
a = B(1,:)';
V = B(2:end,:);
% Residual variance
ep = returns - X * B;
sigma_ep = 1/(T - p - 1) .* sum(ep .^2, 1);
D = diag(sigma_ep);
% Factor expected returns and covariance matrix
f_bar = mean(factRet,1)';
F = cov(factRet);
% Calculate the asset expected returns and covariance matrix
mu = a + V' * f_bar;
Q = V' * F * V + D;
% Sometimes quadprog shows a warning if the covariance matrix is not
% perfectly symmetric.
Q = (Q + Q')/2;
%----------------------------------------------------------------------
end