About

This worksheet has three main taks: analyze the time series of returns, assess for normal distribution, and check for exponential behavior of prices time series.

Setup

Remember to always set your working directory to the source file location. Go to ‘Session’, scroll down to ‘Set Working Directory’, and click ‘To Source File Location’. Read carefully the below and follow the instructions to complete the tasks and answer any questions. Submit your work to RPubs as detailed in previous notes.

Note

For clarity, tasks/questions to be completed/answered are highlighted in red color (color visible only in preview mode) and numbered according to their particular placement in the task section. Type your answers outside the red color tags!

Quite often you will need to add your own code chunk. Execute sequentially all code chunks, preview, publish, and submit link on Sakai following the naming convention. Make sure to add comments to your code where appropriate. Use own language!

Any sign of plagiarism, will result in dissmissal of work!


Task 1: Plot of Returns & Testing for Normality Distribution

In this task we will look at various type of daily returns calculations for comparison and to test normality.

# Require will load the package only if not installed 
# Dependencies = TRUE makes sure that dependencies are install
if(!require("quantmod",quietly = TRUE))
  install.packages("quantmod",dependencies = TRUE, repos = "https://cloud.r-project.org")
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Version 0.4-0 included new data defaults. See ?getSymbols.

##### 1A) Follow the instructions on p.41 to generate four plots for daily, daily log, weekly, and monthly returns. Select a stock of your choice and a time period long enough (5-10 years) to capture the returns behavior.

getSymbols("GOOGL",src="yahoo") ##data starts from 2007
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## 
## WARNING: There have been significant changes to Yahoo Finance data.
## Please see the Warning section of '?getSymbols.yahoo' for details.
## 
## This message is shown once per session and may be disabled by setting
## options("getSymbols.yahoo.warning"=FALSE).
## [1] "GOOGL"
gglRd = periodReturn(GOOGL, period="daily")
gglLg = periodReturn(GOOGL, period = "daily", type = "log")
gglWk = periodReturn(GOOGL, period = "weekly")
gglMn = periodReturn(GOOGL, period = "monthly")
plot(gglRd, main="GOOGLE daily returns")

plot(gglLg, main="GOOGLE daily log returns")

plot(gglWk, main="GOOGLE weekly returns")

plot(gglMn, main="GOOGLE monthly returns")

##### 1B) For the case of daily log returns only, write down the mathematical formula representing the calculation in the code. Confirm integrity of your mathematical formula by selecting a recent data point from your time series object, substituting the corresponding values in the formula to manually calculate the log return, and comparing both results.

\(r_{n} = ln(P_{t}) - ln(P_{t-1})\) Adjusted price at 01/03/2017 is 234.0290 Adjusted price at 01/03/2017 is 241.8719 \(ln(241.8719) - ln(234.0290) = .03296321\) This matches the value given by the code, 2007-01-04 3.296300e-02.

##### 1C) Check the normality of the daily returns using the R function qqnorm() to generate a Q-Q plot. For the function to work properly, you will need to extract first the numeric values from the time series object. Note that a time series object contains both a date and a corresponding value. To extract the numerical value only, on can use the R function as.numeric() on the time series object. Explain what the Y and X axis of the Q-Q plot represent, and share your observation on the normality of the returns distribution.

GoogleValue = as.numeric(gglRd)
qqnorm(GoogleValue)

The y axes represents the norm value of the daily returns. The x axes shows their distribution against the 4 negative and positive quartiles. The data has a fairly normal distribution, with an even number of values falling within about +- 2.8 of the center and only a few outliers on both ends.

Task 2: Density Distribution

Another way to assess the normality of a distribution, other than a Q-Q plot, is to look at the actual density distribution and compare to a normal distribution.

##### 2A) Follow the example in R Lab 2.7.9/p. 70 to generate the density distribution for your stock of choice. Comment on your results.

ggl = getSymbols("GOOGL",src= "yahoo")
gglRd = periodReturn(GOOGL, period = "daily", type = "log")
dsd=density(gglRd) #estimate density of daily log ret
yl = c(min(dsd$y),max(dsd$y)) #set y limits
plot(dsd,main=NULL,ylim=yl)
##plot the normal density with mean, stdv of gglRd
a=seq(min(gglRd),max(gglRd),.001)
points(a,dnorm(a,mean(gglRd),sd(gglRd)), type = "l", lty = 2)

gglRdWk = periodReturn(GOOGL, period = "weekly", type = "log")
dsdWk=density(gglRdWk) #estimate density of daily log ret
ylWk = c(min(dsdWk$y),max(dsdWk$y)) #set y limits
plot(dsd,main=NULL,ylim=ylWk)
##plot the normal density with mean, stdv of gglRd
a=seq(min(gglRdWk),max(gglRdWk),.001)
points(a,dnorm(a,mean(gglRdWk),sd(gglRdWk)), type = "l", lty = 2)

gglRdMn = periodReturn(GOOGL, period = "monthly", type = "log")
dsdMn=density(gglRdMn) #estimate density of daily log ret
ylMn = c(min(dsdMn$y),max(dsdMn$y)) #set y limits
plot(dsd,main=NULL,ylim=ylMn)
##plot the normal density with mean, stdv of gglRd
a=seq(min(gglRdMn),max(gglRdMn),.001)
points(a,dnorm(a,mean(gglRdMn),sd(gglRdMn)), type = "l", lty = 2)

These are all similar to a normal distribution, but as expected the daily chart is smoother than the weekly chart which is smoother than the monthly chart. When going from daily to monthly, there will be less data points and and large jumps in values will be more noticeable.

Task 3: Exponential Behavior of Prices & Curve Fitting

In general, the price history of a stock, over a sufficiently large time window, tends to follow an exponential curve. Many other economic indicators like GDP, population growth, and inflation also follow exponential growth over a long time. Keep in mind that for investment purposes we care more about returns and not prices.

##### 3A) Follow the example in R Lab 2.7.2/p. 67 or R Labs 2 from book’s website (*) to generate an exponential fit for the Dow Jones Industrial Average DJIA. In case the suggested command in the book does not work, consider using instead the command in the code chunk below to capture the DIJA prices.

require(quantmod)
getSymbols("DJIA",src="FRED")
## [1] "DJIA"
serie = DJIA
price = as.numeric(serie)
time = index(serie)
x = 1:length(price)
model = lm(log(price)~x)
expo = exp(model$coef[1]+model$coef[2]*x)
plot(x=time,y=price, main = "Dow Jones", type = "l")

line(time,expo)
## 
## Call:
## line(time, expo)
## 
## Coefficients:
## [1]  -55519.117       4.429

##### 3B) Write down the mathematical form representing the exponential function in the code. Substitute for the exact coefficients in the exponential form and clearly label the variables in the function, in particular the time index.

\(expo_{n} = E^{intercept + n*x}\) Where intercept = coeff[1] and x = coeff[2] calculated by the linear regression model. expo is a vector containing these values.

##### 3C) Repeat the exercise in 3A) for AAPL Adjusted prices.

require(quantmod)
getSymbols("AAPL",src="yahoo")
## [1] "AAPL"
serie = AAPL$AAPL.Adjusted
price = as.numeric(serie)
time = index(serie)
x = 1:length(price)
model = lm(log(price)~x)
#test1 = model$coef[1]
#test2 = model$coef[2]*x
expo = exp(model$coef[1]+model$coef[2]*x)
plot(x=time,y=price, main = "APPLE", type = "l")

line(time,expo)
## 
## Call:
## line(time, expo)
## 
## Coefficients:
## [1]  -586.91906     0.04146

*http://computationalfinance.lsi.upc.edu