Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't know what this title is for... #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 199 additions & 0 deletions Exercises/Exerciseset1/ExerciseSet1(2).Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@

---
title: "Exercise Set 1"
author: "T. Evgeniou"
date: "8 Jan 2016"
output: pdf_document
---


<br>

The purpose of this exercise is to become familiar with:

1. Basic statistics functions in R;
2. Simple matrix operations;
3. Simple data manipulations;
4. The idea of functions as well as some useful customized functions provided.

While doing this exercise we will also see how to generate replicable and customizable reports. For this purpose the exercise uses the R Markdown capabilities (see [Markdown Cheat Sheet](https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf) or a [basic introduction to R Markdown](http://rmarkdown.rstudio.com/authoring_basics.html)). These capabilities allow us to create dynamic reports. For example today's date is `r Sys.Date()` (you need to see the .Rmd to understand that this is *not* a static typed-in date but it changes every time you compile the .Rmd - if the date changed of course).

Before starting, make sure you have pulled the [exercise files](https://github.com/InseadDataAnalytics/INSEADAnalytics/tree/master/Exercises/Exerciseset1) on your github repository (if you pull the course github repository you also get the exercise set files automatically). Moreover, make sure you are in the directory of this exercise. Directory paths may be complicated, and sometimes a frustrating source of problems, so it is recommended that you use these R commands to find out your current working directory and, if needed, set it where you have the main files for the specific exercise/project (there are other ways, but for now just be aware of this path issue):

```{r echo=TRUE, eval=FALSE, tidy=TRUE}
getwd()
setwd("Exercises/Exerciseset1/")
list.files()
```

**Note:** you can always use the `help` command in Rstudio to find out about any R function (e.g. type `help(list.files)` to learn what the R function `list.files` does).

Let's now see the exercise.

**IMPORTANT:** You should answer all questions by simply adding your code/answers in this document through editing the file ExerciseSet1.Rmd and then clicking on the "Knit HTML" button in RStudio. Once done, please post your .Rmd and html files in your github repository.

### Exercise Data

We download daily prices (open, high, low, close, and adjusted close) and volume data of publicly traded companies and markets from the web (e.g. Yahoo! or Google, etc). This is done by sourcing the file data.R as well as some helper functions in helpersSet1.R which also installs a number of R libraries (hence the first time you run this code you will see a lot of red color text indicating the *download* and *installation* process):

```{r eval = TRUE, echo=TRUE, error = FALSE, warning=FALSE,message=FALSE,results='asis'}
source("helpersSet1.R")
source("dataSet1.R")
```

We have `r nrow(StockReturns)` days of data, starting from `r rownames(StockReturns)[1]` until `r tail(rownames(StockReturns),1)`.

### Part I: Statistics of S&P Daily Returns

Here are some basic statistics about the S&P returns:

1. The cumulative returns of the S&P index during this period is `r round(100*sum(StockReturns[,1]),1)`%.
2. The average daily returns of the S&P index during this period is `r round(100*mean(StockReturns[,1]),3)`%;
2. The standard deviation of the daily returns of the S&P index during this period is `r round(100*sd(StockReturns[,1]),3)`%;

Here are returns of the S&P in this period (note the use of the helper function pnl_plot - defined in file helpersSet1.R):

```{r echo=FALSE, comment=NA, warning=FALSE, message=FALSE,results='asis',fig.align='center', fig.height=4,fig.width= 6, fig=TRUE}
SPY = StockReturns[,"SPY"]
pnl_plot(SPY)
```

#### Questions

1. Notice that the code also downloads the returns of Apple during the same period. Can you explain where this is done in the code (including the .R files used)?
2. What are the cumulative, average daily returns, and the standard deviation of the daily returns of Apple in the same period?
3. *(Extra points)* What if we want to also see the returns of another company, say Yahoo!, in the same period? Can you get that data and report the statistics for Yahoo!'s stock, too?

##### Answers

1. This is done in dataSet1.R. Apple's ticker, along with the S&P 500, is included into the vector "myticket". After creating the matrix with only SPY as a column, the code adds a column that corresponds to the Apple ticker. This is done at line 23.

2. The cumulative returns of Apple is `r round(100*sum(StockReturns[,2]),1)`%;The average daily return of Apple is `r round(100*mean(StockReturns[,2]),3)`%; The standard deviation of the daily returns of Apple is `r round(100*sd(StockReturns[,2]),3)`%.

3. The cumulative returns of Yahoo is `r round(100*sum(StockReturns[,3]),1)`%;The average daily return of Yahoo is `r round(100*mean(StockReturns[,3]),3)`%; The standard deviation of the daily returns of Yahoo is `r round(100*sd(StockReturns[,3]),3)`%.

<br>
<br>

### Part II: Simple Matrix Manipulations

For this part of the exercise we will do some basic manipulations of the data. First note that the data are in a so-called matrix format. If you run these commands in RStudio (use help to find out what they do) you will see how matrices work:

```{r eval = FALSE, echo=TRUE}
class(StockReturns)
dim(StockReturns)
nrow(StockReturns)
ncol(StockReturns)
StockReturns[1:4,]
head(StockReturns,5)
tail(StockReturns,5)
```

We will now use an R function for matrices that is extremely useful for analyzing data. It is called *apply*. Check it out using help in R.

For example, we can now quickly estimate the average returns of S&P and Apple (of course this can be done manually, too, but what if we had 500 stocks - e.g. a matrix with 500 columns?) and plot the returns of that 50-50 on S&P and Apple portfolio:

```{r echo=FALSE, comment=NA, warning=FALSE, message=FALSE,results='asis',fig.align='center', fig=TRUE}
portfolio = apply(StockReturns,1,mean)
names(portfolio) <- rownames(StockReturns)
pnl_plot(portfolio)
```


We can also transpose the matrix of returns to create a new "horizontal" matrix. Let's call this matrix (variable name) transposedData. We can do so using this command: `transposedData = t(StockReturns)`.

#### Questions

1. What R commands can you use to get the number of rows and number of columns of the new matrix called transposedData?
2. Based on the help for the R function *apply* (`help(apply)`), can you create again the portfolio of S&P and Apple and plot the returns in a new figure below?

##### Answers
1. Number of rows:`nrow(transposedData)` ; number of columns: `ncol(transpoedData)` #how come r doesn't recognize transposedData when trying to calculate

2.
This is what I tried but it didn't work.

{r echo=FALSE, comment=NA, warning=FALSE, message=FALSE,results='asis',fig.align='center', fig=TRUE}
portfolio2 = apply(transposedData,2,mean)
names(portfolio2) <- colnames(transposedData)
pnl_plot(portfolio2)


<br>
<br>

### Part III: Reproducibility and Customization

This is an important step and will get you to think about the overall process once again.

#### Questions

1. We want to re-do all this analysis with data since 2001-01-01: what change do we need to make in the code (hint: all you need to change is one line - exactly 1 number! - in data.R file), and how can you get the new exercise set with the data since 2001-01-01?
2. *(Extra Exercise)* Can you get the returns of a few companies and plot the returns of an equal weighted portfolio with those companies during some period you select?

##### Answers
1. Change startDate to 2001-01-01

2. Yes, I can.
<br>
<br>

### Part IV: Read/Write .CSV files

Finally, one can read and write data in .CSV files. For example, we can save the first 20 days of data for S&P and Apple in a file using the command:

```{r eval = TRUE, echo=TRUE, comment=NA, warning=FALSE, message=FALSE,results='asis'}
write.csv(StockReturns[1:20,c("SPY","AAPL")], file = "twentydays.csv", row.names = TRUE, col.names = TRUE)
```

Do not get surpsised if you see the csv file in your directories suddenly! You can then read the data from the csv file using the read.csv command. For example, this will load the data from the csv file and save it in a new variable that now is called "myData":

```{r eval = TRUE, echo=TRUE, comment=NA, warning=FALSE, message=FALSE,results='asis'}
myData <- read.csv(file = "twentydays.csv", header = TRUE, sep=";")
```

Try it!

#### Questions

1. Once you write and read the data as described above, what happens when you run this command in the console of the RStudio: `sum(myData != StockReturns[1:20,])`
2. *(Extra exercise)* What do you think will happen if you now run this command, and why:

```{r eval = FALSE, echo=TRUE}
myData + StockReturns[1:40,])
```

**Your Answers here:**
<br>
<br>

### Extra Question

Can you now load another dataset from some CSV file and report some basic statistics about that data?

<br>

### Creating Interactive Documents

Finally, just for fun, one can add some interactivity in the report using [Shiny](http://rmarkdown.rstudio.com/authoring_shiny.html).All one needs to do is set the eval flag of the code chunk below (see the .Rmd file) to "TRUE", add the line "runtime: shiny" at the very begining of the .Rmd file, make the markdown output to be "html_document", and then press "Run Document".

```{r, eval=FALSE, echo = TRUE}
sliderInput("startdate", "Starting Date:", min = 1, max = length(portfolio),
value = 1)
sliderInput("enddate", "End Date:", min = 1, max = length(portfolio),
value = length(portfolio))

renderPlot({
pnl_plot(portfolio[input$startdate:input$enddate])
})
```

### Endless explorations (optional homework)

This is a [recent research article](http://poseidon01.ssrn.com/delivery.php?ID=851091091009083082092113118102076099034023058067019062072066007100008111081022102123034016097101060099003106125099002090116089026058012038004030005113111105079028059062024121067073126072090091089069014121102110107075029090001011087028011082124103085&EXT=pdf) that won an award in 2016. Can you implement a simple strategy as in Figure 1 of this paper? You may find these R commands useful: `names`, `which`, `str_sub`,`diff`,`as.vector`, `length`, `pmin`, `pmax`, `sapply`, `lapply`,`Reduce`,`unique`, `as.numeric`, `%in%`
![A Simple Trading Startegy](simpletrade.png)

What if you also include information about bonds? (e.g. download the returns of the the ETF with ticker "TLT") Is there any relation between stocks and bonds?

Have fun.

1 change: 1 addition & 0 deletions lol.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#LOL new script