%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Empirical Financial Econometrics Matlab Class Code part 1 % Updated Spring 2021 % This Matlab code facilitates to ASSN 1-2. % Copyright at Chaoyi Chen (BCE & MNB) % All rights reserved. For use by registered students only. % Please do not distribute without express written consent. % Email: chaoyi.chen@uni-corvinus.hu % Web: www.chenchaoyi.com %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% clear all % clean workspace %% %%%%%%%%%%%%%%%%%%%%%%%%% Data load and transformation %%%%%%%%%%%%%%%%%%%%%%%%% % Load data load YMOPD1901 % This is mat. file, which stores data that the MATLAB program uses %{ Remarks: 1). The mat file should be stored under the "Current Folder" 2). If your data stored as csv., you can use readtable('myfile.csv') function; 3). If your data stored as xlsx., you can use readtablexlsread('myfile.xlsx','Variable') function; you should save your data at the "Current Folder". Otherwise the Matlab cannot detect your data %} year = YMOPD(:,1); price = YMOPD(:,2); lagprice = lagmatrix(price,1); RiskFree = YMOPD(:,3); n=size(year,1); %{ Remarks: Suppose your dataset, say X, is of n rows by k columns size, where eacg column stores a variable 1). X_i = X(:,i) means you store the ith columnn to X_i, so the X_i will be of size n by one. 2). X_j = X(j,:) means you store the jth row to X_j, so the X_j will be of size one by k. 3). Similarly, X_ij = X(i,j) means you store the element at ith columnn and jth row of X. 4). size(X) function shows the size of the matrix. In addition, size(X,1) shows the row size of X (=n in our example) and size(X,2)shows the column size of X (=k in our example). 5). lagmatrix(X,Lags) creates a lagged (shifted) version of a time series matrix. Type lagmatrix and see examples from HELP for more references %} % Define log versions p = log(price); p = p(2:end); lp = log(lagprice); lp = lp(2:end); %{ Remarks: Since lagprice is the first-lag matrix of price, we lose one observation initially (You will see NA in lagprice). So we need to re-match the data by deleting the first observation for all data. p=p(2:end) means we restore data p with its two to end rows. %} % Define ratios and returns y = p - lp; riskfree = RiskFree(2:end); %{ Remarks: Remember Log(x_t)-Log(x_{t-1}) approx = return of x at period t by first order Taylor expansion. See lecture note "Notation and Concept" for details %} % mean, variance, skewness and kurtosis mean_y = mean(y); var_y = var(y); skew_y = skewness(y); kurtosis_y = kurtosis(y); % Print statistic descriptives %---------TABLES-----------% diary results; disp('---------------------------'); disp('Mean of SP500'); disp('---------------------------'); disp(mean_y); disp('---------------------------'); disp('Variance of SP500'); disp('---------------------------'); disp(var_y); disp('---------------------------'); disp('Skewness of SP500'); disp('---------------------------'); disp(skew_y); disp('---------------------------'); disp('Kurtosis of SP500'); disp('---------------------------'); disp(kurtosis_y); diary off; % Plot the series year = year(2:end,1); base = zeros(size(y,1),1); % break-even investment point set(0,'DefaulttextInterpreter','latex'); plot(year, y, 'LineWidth',4); hold on plot(year, base, '-.', 'LineWidth',4); hold on plot(year, riskfree,'--', 'LineWidth',4); legend({ 'SP 500','Break-even', 'Risk Free'},'Interpreter','latex','location','SW','FontSize',15); xlabel('Date','FontSize',10); ylabel('Return','FontSize',10); title('SP 500 and Risk Free Daily Return, 1901-2015','FontSize',15) grid on; %%%%%%%%%%%%%%%%%%%%%%%%% Static Portfolio with weight=1 or 0 %%%%%%%%%%%%%%%%%%%%%%%%% %% %{ Suppose you invested $100 at the end of year 1901 and put full weight on risky asset (sp500 in our example), your cumulative return until the end of 2015 will be 100*(1+y_{1902})*(1+y_{1903})*...*(1+y_{2015}). You can make use of a loop to compute for the number as below: %} principle = 100; years_invest = size(year,1); cumu = principle; % cumulative value / initial cumulative = principle cumu_yearly = zeros(years_invest,1); % store cumulative value for each investment year for i=1:years_invest cumu = cumu*(1+y(i)); % update by each period return cumu_yearly(i) = cumu; end Sharpe_ratio = mean(y-riskfree)/std(y); % Here we assume the variance of risk free =0 (not true however) %{ If put full weight on risk free asset %} cumu_rf = principle; cumu_yearly_rf = zeros(years_invest,1); for i=1:years_invest cumu_rf = cumu_rf*(1+riskfree(i)); % update by each period return cumu_yearly_rf(i) = cumu_rf; end % Print cumulative value %---------TABLES-----------% diary results; disp('---------------------------'); disp('Initial value at 1901'); disp('---------------------------'); disp(100); disp('---------------------------') disp('cumulative value of investing SP500, 1901-2015'); disp('---------------------------'); disp(cumu); disp('---------------------------'); disp('cumulative value of investing risk free, 1901-2015'); disp('---------------------------'); disp(cumu_rf ); disp('---------------------------'); diary off; % Plot the cumulative value series set(0,'DefaulttextInterpreter','latex'); subplot(2,1,1); plot(year, cumu_yearly, 'LineWidth',4); hold on plot(year, cumu_yearly_rf, '-.', 'LineWidth',4); hold on legend({ 'SP 500', 'Risk Free'},'Interpreter','latex','location','SW','FontSize',15); xlabel('Date','FontSize',10); ylabel('Cumulative value','FontSize',10); title('SP 500 and Risk Free Cumulative Value, 1901-2015','FontSize',15) grid on; % Now suppose you invested $100 after WWII and re-study the question % Full weight on risky asset year_1947 = year(46:end); y_1947 = y(46:end); rf_1947 = riskfree(46:end); years_invest_1947 = size(year_1947,1); cumu_1947 = principle; % cumulative value / initial cumulative = principle cumu_yearly_1947 = zeros(years_invest_1947,1); % store cumulative value for each investment year for i=1:years_invest_1947 cumu_1947 = cumu_1947*(1+y_1947(i)); % update by each period return cumu_yearly_1947(i) = cumu_1947; end % Full weight on riskfree asset cumu_rf_1947 = principle; cumu_yearly_rf_1947 = zeros(years_invest_1947,1); for i=1:years_invest_1947 cumu_rf_1947 = cumu_rf_1947*(1+rf_1947(i)); % update by each period return cumu_yearly_rf_1947(i) = cumu_rf_1947; end Sharpe_ratio_1947 = mean(y_1947-rf_1947)/std(y_1947); % Here we simply assume the variance of risk free =0 (not true however) % Print cumulative value %---------TABLES-----------% diary results; disp('---------------------------'); disp('Initial value at initial 1947'); disp('---------------------------'); disp(100); disp('---------------------------') disp('cumulative value of investing SP500, 1947-2015'); disp('---------------------------'); disp(cumu_1947); disp('---------------------------'); disp('cumulative value of investing risk free, 1947-2015'); disp('---------------------------'); disp(cumu_rf_1947); disp('---------------------------'); diary off; % Plot the WWII cumulative value series set(0,'DefaulttextInterpreter','latex'); subplot(2,1,2); plot(year_1947, cumu_yearly_1947, '-.', 'LineWidth',4); hold on plot(year_1947, cumu_yearly_rf_1947, '-.', 'LineWidth',4); hold on legend({ 'SP 500', 'Risk Free'},'Interpreter','latex','location','SW','FontSize',15); xlabel('Date','FontSize',10); ylabel('Return','FontSize',10); title('SP 500 and Risk Free Cumulative Value, 1947-2015','FontSize',15) grid on; %%%%%%%%%%%%%%%%%%%%%%%%% Home Work %%%%%%%%%%%%%%%%%%%%%%%%% % Rewrite the loop of computing cumulative values using "while" instead of "for" %% %%%%%%%%%%%%%%%%%%%%%%%%% Static Portfolio with equal weights %%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%% Now assume you choose 1/2 weight on sp and 1/2 weight on rf % New portfolio return from 1901-2015 y_portfolio = 0.5*y+0.5*riskfree; cumu_portfolio = principle; % cumulative value / initial cumulative = principle cumu_yearly_portfolio = zeros(years_invest,1); % store cumulative value for each investment year for i=1:years_invest cumu_portfolio = cumu_portfolio*(1+y_portfolio(i)); % update by each period return cumu_yearly_portfolio(i) = cumu_portfolio; end Sharpe_ratio_portfolio = mean(y_portfolio- riskfree)/(1/2*std(y_portfolio)); % Again, here we simply assume variance of rf = 0 which is not true % Print cumulative value including portfolio with equal weight %---------TABLES-----------% diary results; disp('---------------------------'); disp('Initial value at 1901'); disp('---------------------------'); disp(100); disp('---------------------------') disp('cumulative value of investing SP500, 1901-2015'); disp('---------------------------'); disp(cumu); disp('---------------------------'); disp('cumulative value of investing risk free, 1901-2015'); disp('---------------------------'); disp(cumu_rf ); disp('---------------------------'); disp('cumulative value of investing equal weight portfolio, 1901-2015'); disp('---------------------------'); disp(cumu_yearly_portfolio ); disp('---------------------------'); diary off; % Print Sharpe Ratio Compare %---------TABLES-----------% diary results; disp('---------------------------'); disp('Full weight on risky asset'); disp('---------------------------'); disp(Sharpe_ratio); disp('---------------------------') disp('Half weight on risky asset'); disp('---------------------------'); disp(Sharpe_ratio_portfolio); diary off; % Plot the cumulative value series set(0,'DefaulttextInterpreter','latex'); subplot(2,1,1); plot(year, cumu_yearly, 'LineWidth',4); hold on plot(year, cumu_yearly_rf, '-.', 'LineWidth',4); hold on plot(year, cumu_yearly_portfolio, '-.', 'LineWidth',4); hold on legend({ 'SP 500', 'Risk Free', 'Portfolio(equal weight)'},'Interpreter','latex','location','SW','FontSize',15); xlabel('Date','FontSize',10); ylabel('Cumulative value','FontSize',10); title('SP 500, Risk Free, Equal Weight Portfolio Cumulative Value, 1901-2015','FontSize',15) grid on; % New portfolio return from WWII to 2015 y_portfolio_1947 = 0.5*y_1947+0.5*rf_1947; cumu_portfolio_1947 = principle; % cumulative value / initial cumulative = principle cumu_yearly_portfolio_1947 = zeros(years_invest_1947,1); % store cumulative value for each investment year for i=1:years_invest_1947 cumu_portfolio_1947 = cumu_portfolio_1947*(1+y_portfolio_1947(i)); % update by each period return cumu_yearly_portfolio_1947(i) = cumu_portfolio_1947; end Sharpe_ratio_portfolio_1947 = mean(y_portfolio_1947- rf_1947)/(1/2*std(y_portfolio_1947)); % Again, here we simply assume variance of rf = 0 which is not true % Print cumulative value %---------TABLES-----------% diary results; disp('---------------------------'); disp('Initial value at initial 1947'); disp('---------------------------'); disp(100); disp('---------------------------') disp('cumulative value of investing SP500, 1947-2015'); disp('---------------------------'); disp(cumu_1947); disp('---------------------------'); disp('cumulative value of investing risk free, 1947-2015'); disp('---------------------------'); disp(cumu_rf_1947); disp('---------------------------'); disp('cumulative value of investing equal weight portfolio, 1947-2015'); disp('---------------------------'); disp(cumu_portfolio_1947); disp('---------------------------'); diary off; % Print Sharpe Ratio Compare after WWII %---------TABLES-----------% diary results; disp('---------------------------'); disp('Full weight on risky asset, after WWII'); disp('---------------------------'); disp(Sharpe_ratio_1947); disp('---------------------------') disp('Half weight on risky asset, after WWII'); disp('---------------------------'); disp(Sharpe_ratio_portfolio_1947); diary off; % Plot the WWII cumulative value series set(0,'DefaulttextInterpreter','latex'); subplot(2,1,2); plot(year_1947, cumu_yearly_1947, '-.', 'LineWidth',4); hold on plot(year_1947, cumu_yearly_rf_1947, '-.', 'LineWidth',4); hold on plot(year_1947, cumu_yearly_portfolio_1947, '-.', 'LineWidth',4); hold on legend({ 'SP 500', 'Risk Free', 'portfolio (equal weight)'},'Interpreter','latex','location','SW','FontSize',15); xlabel('Date','FontSize',10); ylabel('Return','FontSize',10); title('SP 500, Risk Free, Equal Weight Portfolio Cumulative Value, 1947-2015','FontSize',15) grid on; %% %%%%%%%%%%%%%%%%%%%%%%%%% Static Portfolio with different weights %%%%%%%%%%%%%%%%%%%%%%%%% % Now assume you invest w on risky asset nd (1-w) on risk free asset % from 1901 - 2015 % We evaluate the cumulative value and Sharpe ratio with w = [0,0.1,0.2,...,0.9,1] by a loop weight_choice = [0:0.1:1]; % generate a vector storing all weight candidates SR = zeros(size(weight_choice,2),1); % SR(j) stores the Sharpe ratio when w=j. All results assume variance of rf = 0 but not true cumu_portfolio_all = principle*ones(size(weight_choice,2),1); % cumu_portfolio(j,1) stores the cumulative value when w=j cumu_yearly_portfolio_all = zeros(years_invest,size(weight_choice,2)); % cumu_yearly_portfolio(:,j) store cumulative value for each investment year when w=j for j=1:size(weight_choice,2) w = weight_choice(j); y_portfolio_w = w*y+(1-w)*riskfree; std_port_w = w*std(y); SR(j) = mean(y_portfolio_w)/std_port_w; for i=1:years_invest cumu_portfolio_all(j,1) = cumu_portfolio_all(j,1)*(1+y_portfolio_w(i)); % update by each period return cumu_yearly_portfolio_all(i,j) = cumu_portfolio_all(j,1); end end % Print cumulative value including portfolio with equal weight %---------TABLES-----------% diary results; disp('---------------------------'); disp('Initial value at 1901'); disp('---------------------------'); disp(100); disp('---------------------------') disp('cumulative value of investing SP500, 1901-2015'); disp('---------------------------'); disp(cumu); disp('---------------------------'); disp('cumulative value of investing risk free, 1901-2015'); disp('---------------------------'); disp(cumu_rf ); disp('---------------------------'); disp('cumulative value of investing portfolio, 1901-2015, weights are in order of 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1'); disp('---------------------------'); disp(cumu_portfolio_all); disp('---------------------------'); diary off; % Print Sharpe Ratio Compare after WWII %---------TABLES-----------% diary results; disp('---------------------------'); disp('Full weight on risky asset, after WWII'); disp('---------------------------'); disp(Sharpe_ratio); disp('---------------------------') disp('Differenet weights on risky asset, 1901-2015, weights are in order of 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1'); disp('---------------------------'); disp(SR); diary off; % Plot the cumulative value series set(0,'DefaulttextInterpreter','latex'); subplot(2,1,1); plot(year, cumu_yearly_rf, 'LineWidth',2); hold on plot(year, cumu_yearly_portfolio_all(:,2), '-.', 'LineWidth',2); hold on plot(year, cumu_yearly_portfolio_all(:,3), '-.', 'LineWidth',2); hold on plot(year, cumu_yearly_portfolio_all(:,4), '-.', 'LineWidth',2); hold on plot(year, cumu_yearly_portfolio_all(:,5), '-.', 'LineWidth',2); hold on plot(year, cumu_yearly_portfolio_all(:,6), '--', 'LineWidth',2); hold on plot(year, cumu_yearly_portfolio_all(:,7), '--', 'LineWidth',2); hold on plot(year, cumu_yearly_portfolio_all(:,8), '--', 'LineWidth',2); hold on plot(year, cumu_yearly_portfolio_all(:,9), ':', 'LineWidth',2); hold on plot(year, cumu_yearly_portfolio_all(:,10),':', 'LineWidth',2); hold on plot(year, cumu_yearly_portfolio_all(:,11),':', 'LineWidth',2); hold on legend({ 'Risk Free', 'w = 0.1', 'w = 0.2', 'w = 0.3', 'w = 0.4', 'w = 0.5',... 'w = 0.6', 'w = 0.7', 'w = 0.8', 'w = 0.9', 'w = 1'},'Interpreter','latex','location','SW','FontSize',15); xlabel('Date','FontSize',10); ylabel('Cumulative value','FontSize',10); title('SP 500, Risk Free, Weighted Portfolio Cumulative Value, 1901-2015','FontSize',15) grid on; % Plot the Sharpe ratio set(0,'DefaulttextInterpreter','latex'); subplot(2,1,2); plot(weight_choice(2:end),SR(2:end), '-.', 'LineWidth',4); hold on legend({ 'Sharpe Ratio'},'Interpreter','latex','location','SW','FontSize',15); xlabel('Weight','FontSize',10); ylabel('Sharpe Ratio','FontSize',10); title('Sharpe Ratio of Different Portfolios','FontSize',15) grid on; %%%%%%%%%%%%%%%%%%%%%%%%% Home Work %%%%%%%%%%%%%%%%%%%%%%%%% %{ 1. Why the Sharpe ratio is monotonically decreasing over time? (Hint: recall our assumption) 2. At home, re-produce the last two figures with data after WWII %} %%%%%%%%%%%%%%%%%%%%%%%%% The End %%%%%%%%%%%%%%%%%%%%%%%%%