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[2]:
  1. 1
  2. 4
  3. 10
  4. 20
  5. 35
  6. 56
  7. 84
  8. 120
  9. 165
  10. 220

b)

Out[3]:
  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[4]:
1 2 3 4 5 6 7 8 910
2 4 6 8101214161820
3 6 912151821242730
4 81216202428323640
5101520253035404550

2. Distributions

Print density function and cumulative distribution function of normally distributed random variable with $\mu = 0$ and $\sigma^2 = 1$ at points $(-4, -3, \ldots, 4)$. Can you find from these numbers any interesting function properties (e.g. monotonicity, limits, ...)?

Density function
[1] 0.0001338302 0.0044318484 0.0539909665 0.2419707245 0.3989422804
[6] 0.2419707245 0.0539909665 0.0044318484 0.0001338302

Cumulative distribution function
[1] 3.167124e-05 1.349898e-03 2.275013e-02 1.586553e-01 5.000000e-01
[6] 8.413447e-01 9.772499e-01 9.986501e-01 9.999683e-01

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) Calculate weekly MSFT stock returns get density from 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 $$ hint: use h = hist(..., plot=F) to return histogram object without plotting the histogram itself. Accessing its properties is done with $, e.g. h$attribute.

Out[25]:
  1. 4.16666666666667
  2. 10.4166666666667
  3. 18.75
  4. 12.5
  5. 4.16666666666667

c) Calculate mean and standard deviation of MSFT returns.

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