|
|
Line 1: |
Line 1: |
| = Theory = | | = Theory = |
| | | |
− | Financial theory has shown that, for efficient financial markets, the best forecast for the price of an asset tomorrow is its price today. Otherwise, financial agents would buy or sell the asset until this is the case. This is one of the formulations of “the efficient market hypothesis”. In terms of returns this means that <math>E(r_t|\mathcal{F}_{t-1})=0</math>. This hypothesis can be tested using arbitrary model specifications and estimation methods. The simplest estimation method is standard OLS (link) with robust standard errors (link). The simplest model specifications are linear:
| + | Usually you will have data saved in some file, like Excel files, csv (comma seperated values) file or a text file. These are the most common formats in which you can download data from various databases. |
− | | |
− | <math>\begin{aligned}
| |
− | r_t&=\phi_0+\phi_1 r_{t-1}+ \beta D_{day=5}+e_t,\ H_0:\beta=0, H_a\ \beta\ne 0\\
| |
− | r_t&=\phi_0+ \beta D_{day=5}+e_t,\ H_0:\beta=0, H_a\ \beta\ne 0\\
| |
− | r_t&=\phi_0+ \sum_{i=1}^4 \beta_i D_{day=i}+e_t, \ H_0: \beta_i=0, H_a\exists i:\ \beta_i\ne 0\\
| |
− | r_t&=\phi_0+ \phi_i r_{t-1}+\sum_{i=1}^4 \beta_i D_{day=i}+e_t, \ H_0: \beta_i=0, H_a\exists i:\ \beta_i\ne 0\end{aligned}</math>
| |
− | | |
− | I will demonstrate the implementation of the first model in the list. Implement the others on your own.
| |
− | | |
− | = Algorithm =
| |
− | | |
− | # Import data in MATLAB (data are downloaded from Yahoo finance, http://finance.yahoo.com, adjusted daily close prices, MSFT from Jan, 1 2000 to Sept, 10 2012).
| |
− | # Construct a vector of log-returns
| |
− | # Construct <math>y</math> vector and <math>X</math> matrix. For this purpose a dummy for the day of the week is needed.
| |
− | # Run OLS optimization, that is estimate <math>\hat\beta</math> from the regression <math>y=X\beta+u</math> <link>
| |
− | # Compute standard errors <link>
| |
− | # Test the hypothesis
| |
− | | |
− | •
| |
− | | |
− | = Implementation =
| |
− | | |
− | == Import data in MATLAB (keyword: data import) ==
| |
− | | |
− | MATLAB has a very wide range of importing procedures. The most straightforward and user-friendly is the MATLAB import wizard. It opens via File/Import data menu. The next step is to select the data file of interest. The import wizard is quite intuitive. It works for a variety of standard file formats and can generate a MATLAB code to import similar files in the future (check box, right bottom corner). The import wizard works well for well-structured import files. For data files with more complicated structure the <source enclose=none>textscan</source> function is used. There are two objects in the workspace after importing MSFT.txt: a matrix object <source enclose=none>data</source> and a cell object <source enclose=none>textdata</source>. MATLAB attempts to import all data columns as numerical data. If it fails, these columns are automatically dumped in a cell array <source enclose=none>textdata</source>. As a result, all dates are converted to text in a cell array <source enclose=none>textdata</source> and all numbers (prices) are stored in a data vector.
| |
− | | |
− | == Construction of Log-returns ==
| |
− | | |
− | Log-returns are defined as <math>r_t=\ln(p_t)-\ln(p_{t-1})</math> The first return <math>r_1</math> is not defined, since <math>p_0</math> is not known. The long way to implement this in MATLAB is:
| |
− | | |
− | <source>r=zeros(T,1);
| |
− | for i=2:T
| |
− | %This way r(1)=0 by construction
| |
− | r(i)=log(p(i))-log(p(i-1));
| |
− | end</source>
| |
− | The same result can be achieved in a shorter way using the fact that MATLAB can extract submatrices from a matrix, that is <source enclose=none>p(2:end,:)</source> will select all elements but the first row in a matrix, and <source enclose=none>p(1:end-1,:)</source> will select all elements but the last.
| |
− | | |
− | <source>r=zeros(T,1);
| |
− | %This way r(1)=0 by construction
| |
− | r(2:end)=log(p(2:end))-log(p(1:end-1));</source>
| |
− | The same result can be achieved using <source enclose=none>y=diff(x)</source>. The command generates a vector <math>y</math>, such that <source enclose=none>y(i)=x(i+1)-x(i)</source>. For return series <source enclose=none>y(i+1)=x(i+1)-x(i)</source> is needed. To correct for this, a vertical concatenation of vectors <link> is used. The code collapses to
| |
− | | |
− | <source>r=[0;diff(log(p))];</source>
| |
− | == Constructing day-of-the-week dummy (keywords: loops, if-then-else statements, logical operations, vectorization) ==
| |
− | | |
− | To be able to convert <source enclose=none>textdata</source> to the day of the week variable, the date variable has to be converted to MATLAB date form. In MATLAB, date variables are stored as number of days since 01/01/0000.<br />
| |
− | <source enclose=none>Ddate=datenum(textdata(2:end,1))</source> converts all entries from the first column of <source enclose=none>textdata</source> starting from the second position until the end of it. For the sake of sanity, it is always a good idea to check <source enclose=none>Ddate</source> after conversion. For this purpose you can use MATLAB function <source enclose=none>datestr</source>. If the date conversion is successful, <source enclose=none>datestr(Ddate(1))</source> gives exactly the same date as in <source enclose=none>textdata(2,1)</source>. Otherwise, you have to check whether month and day are not switched.<br />
| |
− | <source enclose=none>Wkday=weekday(Ddate)</source> constructs a weekday indicator variable. It assigns values from 1 to 7 for different days of the week, i.e. 1 – Sunday, ..., 7 – Saturday. The last step is to construct a dummy variable for Friday.
| |
− | | |
− | === The longest way ===
| |
− | | |
− | <ol>
| |
− | <li><p>Create a vector of zeros <source enclose=none>Dw</source> of the same length as the return series</p></li>
| |
− | <li><p>Check whether the first observation of <source enclose=none>Wkday</source> is Friday, that is check whether <source enclose=none>Wkday(i)==6</source> [1st] for <math>i</math>=1</p></li>
| |
− | <li><p>If (2) is True, then <source enclose=none>Dw(i)=1</source>, else <source enclose=none>Dw(i)=0</source> [3rd] for <math>i</math>=1</p></li>
| |
− | <li><p>Repeat lines 2 – 3 for <math>i=2,3,...,T</math>, where <math>T</math> is the sample size</p>
| |
− | <source>T=length(y); %defines a number of steps in a loop
| |
− | Dw=zeros(T,1); %initializes a vector of 0s. It is quite important for performance for large T
| |
− | for i=1:T %starts the loop
| |
− | %filling the dummy variable
| |
− | if Wkday(i)==6
| |
− | Dw(i)=1;
| |
− | else
| |
− | Dw(i)=0;
| |
− | end</source></li></ol>
| |
− | | |
− | •
| |
− | | |
− | === Slightly shorter way ===
| |
− | | |
− | Since in MATLAB logical expression <source enclose=none>Wkday(i)==6</source> is 1, if True, and 0, if False, lines 2 – 3 can be combined in one line <source enclose=none>Dw(i)=Wkday(i)==6</source>. Then, the slightly shorter version of the long algorithm would be:
| |
− | | |
− | <ol>
| |
− | <li><p>Create a vector of zeros <source enclose=none>Dw</source> of the same length as the return series</p></li>
| |
− | <li><p>Check whether the first observation of <source enclose=none>Wkday</source> is Friday, i.e.<br />
| |
− | <source enclose=none>Dw(i)=(Wkday(i)==6)</source> [1st] for <math>i</math>=1</p></li>
| |
− | <li><p>Repeat line 2 for <math>i=2,3,...,T</math>, where <math>T</math> is the sample size</p>
| |
− | <source>T=length(y); %defines a number of steps in a loop
| |
− | Dw=zeros(T,1); %initializes a vector of 0s. It is quite important for a speed for large T
| |
− | for i=1:T %starts the loop
| |
− | %filling the dummy variable
| |
− | Dw(i)=Wkday(i)==6;
| |
− | end</source></li></ol>
| |
− | | |
− | •
| |
− | | |
− | === A shorter way ===
| |
− | | |
− | '''Note:'''
| |
− | | |
− | We need a first step in our algorithm since MATLAB works in the following way. The first time MATLAB runs <source enclose=none>Dw(i)=0</source>, it checks whether PC has a long enough continuous chunk of memory. If “yes”, MATLAB creates a vector variable <source enclose=none>Dw</source> that has <math>i</math> components. If “no”, MATLAB stops with error. The next time MATLAB runs <source enclose=none>Dw(j)=0</source>, it check whether <source enclose=none>Dw</source> has <math>j</math> or more components. If “yes”, MATLAB changes the <math>j</math>th component of <source enclose=none>Dw</source> variable to 0. If “no”, MATLAB checks PC memory and if there is a long enough continuous chunk of memory that can accomodate a vector with <math>j</math> components, this variable is created ''and the content of the previous <math>Dw</math> is copied onto the first <math>i</math> components of vector <source enclose=none>Dw</source>''. Otherwise, it stops with “” error. As a result, without the first step in our algorithm, MATLAB creates <math>T</math> different variables in the loop. It is not that important for small <math>T</math>, but it becomes time-consuming once <math>T</math> increases. Irrelevant example:
| |
− | | |
− | <source>close all;clear all;clc;
| |
− | T=2000;
| |
− | tic;
| |
− | %Slow cycle, takes roughly 9 seconds (Core 2 Duo, 2.86Mhz)
| |
− | for i=1:T
| |
− | count(i,i)=toc;
| |
− | end
| |
− | plot(diag(count))
| |
− | tic;
| |
− | %Fast cycle, takes 2.5 milliseconds
| |
− | for i=T:-1:1
| |
− | count1(i,i)=toc;
| |
− | end
| |
− | figure
| |
− | plot(flipud(diag(count1)))</source>
| |
− | Keeping this in mind, the code can be rewritten as:
| |
− | | |
− | <ol>
| |
− | <li><p>Check whether first observation of Wkday is Friday, i.e. <source enclose=none>D(i)=Wkday(i)==6</source> [1st] for <math>i</math>=1</p></li>
| |
− | <li><p>Repeat line [1st] for <math>i=2,3,...,T</math>, where <math>T</math> is a sample size</p>
| |
− | <source>T=length(y); %defines the number of steps in the loop
| |
− | for i=T:-1:1 %starts the loop
| |
− | %filling the dummy variable
| |
− | Dw(i)=Wkday(i)==6;
| |
− | end</source></li></ol>
| |
− | | |
− | === The shortest way ===
| |
− | | |
− | The shortest way is to use the vector power of MATLAB. By default, MATLAB operates on matrices, not on scalars. Then, the expression <source enclose=none>Wkday==6</source> will generate a vector of 1s if this condition is True and 0s if it is not. Thus, everything can be collapsed to:
| |
− | | |
− | <source> Dw=Wkday==6;</source>
| |
− | Please note, that the last method
| |
− | | |
− | # Is at least as efficient as the first two (and usually more efficient).
| |
− | # Is much shorter (and thus, there is a smaller chance for mistakes).
| |
− | # Does not require initialization of the variable <source enclose=none>Dw</source> since assignment occurs just once.
| |
− | | |
− | == Constructing <math>y</math> and <math>X</math> ==
| |
− | | |
− | A vector of <source enclose=none>y</source> is constructed in the following way. The first observation of <source enclose=none>y</source> corresponds to the second observation of vector <source enclose=none>r</source>. The last observation of <source enclose=none>y</source> corresponds to the last observation of <source enclose=none>r</source>. The first observation of matrix <source enclose=none>X</source> corresponds to the first observations of vector <math>r</math> and the second observation of dummy variable <source enclose=none>Dw</source>. The last observation of <source enclose=none>X</source> correspond to the first before the last observation of <source enclose=none>r</source> and the last observation of <source enclose=none>Dw</source>. <math>r_2=\phi r_1 + \beta D_2^{Friday}+e_2</math> <math>r_T=\phi r_{T-1} + \beta D_T^{Friday}+e_T</math> The code is:
| |
− | | |
− | <source>y=r(2:end);
| |
− | X=[r(1:end-1) Dw(2:end)];</source>
| |
− | = OLS implementation =
| |