Homework 1

Due by 10/9/2016.

Complete exercises and replicate outputs. Note that only PDF and HTML formats will be accepted. All R code you used to generate figures should be included in the document.

There are plenty of options for creating PDF documents with inline R code such as knitr, sweave, sense.io, jupyter, Anaconda cloud or simply save the plots as .png images and add them manually to your document together with R code.

Your output can be stylistically different from the outputs below (e.g. vectors or matrices will show up with row/column numbers). This doesn't matter as long as you have the correct numbers, labels and graphs.

1. Matrices and vectors

Find a generation mechanism for the following vectors / matrices and replicate it in R using for loops or vectors / matrices.

a)

Out[22]:
  1. 1
  2. 4
  3. 10
  4. 20
  5. 35
  6. 56
  7. 84
  8. 120
  9. 165
  10. 220

b)

Out[23]:
  1. 1
  2. 1
  3. 2
  4. 3
  5. 5
  6. 8
  7. 13
  8. 21
  9. 34
  10. 55
  11. 89
  12. 144
  13. 233
  14. 377
  15. 610

c)

Out[24]:
1 2 3 4 5 6 7 8 910
2 4 6 8101214161820
3 6 912151821242730
4 81216202428323640
5101520253035404550

2. Plotting

Plot density function and cumulative distribution function of normally distributed random variable with $\mu = 0$ and $\sigma^2 = 1$. Plot them side by side. Don't forget to include proper labels and titles.

3. Dataframes

Load data from dow_jones_index.csv into a dataframe and do the following:

hint: Don't forget to convert date from Factor type to Date type. You can see the representation and type with str(myobject) command.

a) Plot MSFT stock price in time.

hint: if you're having problems displaying english month names, try Sys.setlocale("LC_TIME", "C")

Out[27]:
'C'

b) Calculate weekly MSFT stock returns and display their histogram with 5 bins.

hint: Let $p_t$ stand for stock price at time $t$, stock return is then defined as $$ r_t = \frac{p_t}{p_{t-1}} - 1 $$

c) Calculate mean and standard deviation of MSFT returns.

Mean of MSFT returns:  -0.00598
Standard deviation of MSFT returns:  0.02062