%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Matlab Beginning Practice % Updated Spring 2021 % 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % This exercises is intended to help you start learning Matlab %% % Getting started % 1. Vector & matrix x = [3 4 7 11]; % create a row vector (spaces) b = [3;4;7;11]; % create a column vector (spaces) length(x) % show the length of x size(x) % show the size of x z = x'; % ' means transpose so z is the transpose of x n = zeros(5,1); % create a column vector with all elements = 0 nn = ones(1,5); % create a row vector with all elements = 1 y = eye(3); % create a 3 by 3 identity matrix yy = inv(y); % create an inverse matrix of y; Another way is yy=y^(-1) % 2. Display in the command line a text disp('string can be added here'); % display a string disp(y); % display a variable % 3. Functions and plotting x = 0:pi/100:2*pi; % pi=3.1415926.... y = sin(x); % y shows sin of x plot(x,y) % plot(X,Y) creates a 2-D line plot of the data in Y versus the corresponding values in X. % 4. For loop s = 10; h = zeros(10,1); for c = 1:s h(c) = c; % when c=1, h(1)=1; c=2, h(2)=2,..., c=10, h(10)=10 end %{ Sytax of for loop usually like: for index = values statements end %} %% % Keeping warm up % 1. Load data load ADD YOUR DATASET NAME HERE % load mat. file dataset = readtable('ADD YOUR FILE NAME HERE.csv'); % load csv. file and store data into dataset DATAset = readtablexlsread('ADD YOUR FILE NAME HERE.xlsx','ADD THE SHEET NAME HERE'); % load one sheet in the xlsx. file and store data into DATAset % 2. Matrix calculation a = [1,2,3,4]; % a is 1 by 4 b = [2,3,4,5]; % b is 1 by 4 abprime = a*b'; % ab' is 1 by 1 aprimeb = a'*b; % a'b is 4 by 4 % 3. Simulate data x = normrnd(0,1,100,1); % generates a 100 by 1 vector such that each number is from the normal distribution with mean 0 and standard deviation 1. e = normrnd(0,1,100,1); y = 3*x+e; % y =3*x+e % 4. OLS estimation beta_ols = (x'*x)^(-1)*x'*y; % This is the OLS estimates of the coefficients y_hat = x*beta_ols; % get 100 by 1 fitted value by OLS % 5. Scatter plot and the fitted line scatter(x, y) % scatter plot of (x,y) hold on plot(x, y_hat) % plot of fitted line hold on legend({ 'y', 'fitted y'}); % show legend xlabel('x'); ylabel('y'); grid on %% % The real challenge one (cross-sectional data) % 1. generate a 100 by 1 vector of x by using x = rand(100,1); % 2. generate a 100 by 1 vector of e by using e = normrnd(0,1,100,1); % 3. let alpha = 1 and beta = 2; % 4. generate y = alpha + beta*x + e; % 5. regress y on x by using beta hat =(x'*x)^(-1)*x'*y; % 6. collect the fitted y (say, y_hat); % 7. plot the fitted line on the figure of data scatter % The real challenge two (time series data) % 1. let y = zeros(100,1); % 2. let y(1) = normrnd(0,1); % 3. let b = 0.5 & e = normrnd(0,1,100,1); % 4. using the for loop to generate y(t) = 0.5*y(t-1)+e(t) % 5. estimate the model using ToEstMdl = arima(1,0,0); & [Est, ~, ~] = estimate(ToEstMdl, y, 'Display','off'); % 6. collect the residual by using resid = infer(Est,y); % 7. plot the ACF figure via autocorr(resid)