FINANCIAL ECONOMETRICS ASSIGNMENT 1
INSTRUCTIONS
Matlab is a popular program and you should be able to find the answer or help to a wealth of these (and future) questions online. Three things help make a person a good programmer: 1) having a good eye when debugging code, and 2) knowing how to search the internet to help you solve problems and write better code, and 3) tenacity.
The approach you should probably take to solving more difficult coding questions is the following. Start simple and make sure you understand the simple problem before working your way up to the hard problem. Take the time to understand everything step by step. Don’t rush. Problem-solving wisdom from J. Michael Steele:
“What is the simplest problem that you cannot solve?”
DETAILS ON SUBMISSION
For this assignment only, simply copying the Matlab code into Word, manually typing in the solutions below the relevant copied code, and printing it out the Word file will be sufficient for turning in the assignment. More specifically, please use comments (%) to organize your assignment in the order the questions are asked, and copy the Matlab output below the code that was used to generate it. For example, if I ask for question Q, “What is x3 + 2xy + z4?” where x = 5, y = 10, and z = 20 then your Word file for this question should look like:
%% Question Q x = 5; y = 10; z = 20;
xˆ3 + 2*x*y + zˆ4 % Answer: 160,225
%% Question Q+1 ...
Don’t print directly from Matlab, the formatting is cumbersome. In addition, please email the TA a copy of your Matlab codes that were used to do the assignment.
1
Getting to know Matlab
For this exercise, input the following expressions into Matlab:
u=112358 1
1 2
v= 3 5
PROBLEMS
8
1 0
x=01 1 2
y=34
1212 z=3434
1212
x x w=yy
- What command would pull x out of w? (Hint: w([?], [?]) is the same as x)
- What command would pull [x; y] out of w? Is there more than one? If there are, list
all alternatives
- What command would pull y out of z? List all alternatives
- Computethevaluesofu+v′,v+u′,vu,uv,andxy
- Computethevaluesof(x+y)2 andx2 +xy+yx+y2. Aretheythesame? Is x2 + 2xy + y2 the same?
- What is the value of −24? What about (−2)4?
- Usingboththeoperator‘:’and‘linspace’,createthesequence0,0.01,0.02,…,0.99,1
- What is the value of log(exp(1000)) both analytically and in Matlab? Why do they differ?
Basic Constructions
You should write a separate function or script for each of the following problems: 2
- A Fibonacci sequence is 1, 1, 2, 3, 5, 8, 13, . . ., where the 1st element is 1, the 3rd is 2, the 5th is 5, the 7th is 13, and so on. Use a loop (for or while) to compute the 18th element. Call this script ‘fibonacci.m’
- The built-in function ‘rand’ draws random numbers from the standard uniform dis- tribution between 0 and 1. See ‘help rand’. Write a script that generates an array of 6 random numbers between 0 and 15. The program should then sort the array so that the numbers appear in increasing order. Call this script ‘sortnumbers.m’
- Assume some program has been written for the task of adding all integers i = 1,2,…,10:
some_number = 0; i = 1; while j < 11;
some_number += i display(some_number)
- (a) Identify the errors in the program and list them
- (b) Write a new version of the program with errors corrected. Run this program and confirm that it gives the correct output. Call this program ‘whileloopserrors.m’
- Compute π with two schemes (a) Leibniz:
(b) Euler:
π = 8 ∞ 1
(4k + 1)(4k + 3) k=0
∞ 1
π = 6
k2
If only the first K terms of each sum are used as an approximation to π, each modified scheme will have computed it with some error. Return the results for up to 4 decimal points for both schemes with K = 10, 50, 100, 1000. Call this function ‘piapprox(K)’ that takes K as an input; ie, the first line in your function should look like
function [pi_Leibniz, pi_Euler] = piapprox(K)
3
k=1
13. Write a function that takes a positive integer N as an input and then draws N random integers in the interval [1,6] (both ends inclusive). In the program, count how many of the numbers, M, that equal 6 and then return M/N. The first line should look like
function prob = countdie(N),
where prob is the probability returned and countdie is the function name (‘count- die.m’ would be saved in your current directory)
Hints:
- Use 1+floor(6*rand) to draw a random integer between 1 and 6
- Print out all the random numbers on the screen so that you can check for your- self that the counting is correct
- Run the program with a small value for N (eg, N = 10) to confirm that it works as intended
Now return the function when N = 50, 100, 1000. For large N , this function com- putes the probability prob = M/N of getting a six when throwing a dice
Basic Data Operations
The file ‘HW1data.txt’ is included on Moodle. Please load it into your Matlab workspace. The first column is the CRSP date, the second is the return on a broad basket of equities (NYSE/AMEX/NASDAQ/ARCA), the third is the total return on the S&P500. All data is monthly.
- Compute the monthly means and standard deviations of both returns
- Compute the monthly correlation between both returns
- Count the number of negative returns in both the S&P500 and basket of equities
- Construct an indicator variable that takes the value of 1 when both returns are nega- tive. (Hint: using ‘find’ and ‘and’ will probably help here.) Compute the correlation of returns conditional on this indicator variable. How does this compare to the corre- lation of all returns? How does this compare to the correlation when both returns are positive?
- Compound both of these monthly returns into December-to-December annual re- turns. Make a table that contrasts the mean, standard deviation, skewness, and kur- tosis of monthly returns with annual returns for both assets. What does annual com- pounding do? Do skewness and kurtosis become more or less like a normal distribu-
tion? Does the correlation between assets change much? Does crudely annualizing
the monthly mean return by a factor of 12 and the monthly standard deviation by match up with the true annualized statistics?
4
√
12