%% Estimate Capital Asset Pricing Model Using SUR
% This example shows how to implement the capital asset pricing model (CAPM) using the Econometrics Toolbox™ VAR model framework.
% The CAPM model characterizes comovements between asset and market prices. Under this framework, individual asset returns are linearly associated with the return of the whole market (for details, see [56], [80], and [105]). That is, given the return series of all stocks in a market (M_t) and the return of a riskless asset (C_t), the CAPM model for return series j (R_j) is
% R_{jt} - C_t = a_j + b_j(M_t - C_t) + \varepsilon_{jt}
% for all assets j = 1,...,n in the market.
% a = [a_1\;...\; a_n]^\prime is an n-by-1 vector of asset alphas that should be zero, and it is of interest to investigate assets whose asset alphas are significantly away from zero. b = [b_1\;...\;b_n]^\prime is a n-by-1 vector of asset betas that specify the degree of comovement between the asset being modeled and the market. An interpretation of element j of b is
% If b_j = 1, then asset j moves in the same direction and with the same volatility as the market, i.e., is positively correlated with the market .
% If b_j = -1, then asset j moves in the opposite direction, but with the same volatility as the market, i.e., is negatively correlated with the market.
% If b_j = 0, then asset j is uncorrelated with the market.
% In general:
% {\rm sign}(b_j) determines the direction that the asset is moving relative to the market as described in the previous bullets.
% |b_j| is the factor that determines how much more or less volatile asset j is relative to the market. For example, if |b_j| = 10, then asset j is 10 times as volatile as the market.
%% Load and Process Data
% Load the CAPM data set included in the Financial Toolbox™.

load CAPMuniverse
varWithNaNs = Assets(any(isnan(Data),1))
dateRange = datestr([Dates(1) Dates(end)])

% The variable Data is a 1471-by-14 numeric matrix containing the daily returns of a set of 12 stocks (columns 1 through 12), one riskless asset (column 13), and the return of the whole market (column 14). The returns were measured from 03Jan2000 through 07Nov2005.  AMZN and GOOG had their IPO during sampling, and so they have missing values.
%
% Assign variables for the response and predictor series.

Y = bsxfun(@minus,Data(:,1:12),Data(:,14));
X = Data(:,13) - Data(:,14);
[T,n] = size(Y)

% Y is a 1471-by-12 matrix of the returns adjusted by the riskless return.  X is a 1471-by-1 vector of the market return adjusted by the riskless return.
%
%% Create Multivariate Time Series Model
% Create a varm model object that characterizes the CAPM model. You must specify the number of response series and degree of the autoregressive polynomial.

Mdl = varm(n,0);

% Mdl is a varm model object that characterizes the desired CAPM model.
%
%% Estimate Multivariate Time Series Model
% Pass the CAPM model specification (Mdl), the response series (Y), and the predictor data (X) to estimate. Request to return the estimated multivariate time series model and the estimated coefficient standard errors.  estimate maximizes the likelihood using the expectation-conditional-maximization (ECM) algorithm.

[EstMdl,EstCoeffSEMdl] = estimate(Mdl,Y,'X',X);

% EstMdl has the same structure as Mdl, but EstMdl contains the parameter estimates. EstCoeffSEMdl is a structure array containing the estimated standard errors of the parameter estimates.  EstCoeffSEMdl:
% Contains the biased maximum likelihood standard errors.
% Does not include the estimated standard errors of the intra-period covariances.
%
%% Analyze Coefficient Estimates
% Display the regression estimates, their standard errors, their t statistics, and p-values. By default, the software estimates, stores, and displays standard errors from maximum likelihood.

results = summarize(EstMdl);
results.Table

%
% Response series 6 has a significant asset alpha.

sigASymbol = Assets(6)

% As a result, GOOG has exploitable economic properties.
% All asset betas are greater than 3. This indicates that all assets are significantly correlated with the market.
% However, GOOG has an asset beta of approximately 0.37, whereas all other asset betas are greater than or close to 1. This indicates that the magnitude of the volatility of GOOG is approximately 37% of the market volatility. The reason for this is that GOOG steadily and almost consistently appreciated in value while the market experienced volatile horizontal movements.
% For more details and an alternative analysis, see Capital Asset Pricing Model with Missing Data.
% Copyright 2012 The MathWorks, Inc.