## Computational Finance

### Assignment 1: Comments, R Scripts

This assignment involved some computations that were repeated for different datasets. This kind of situation is ideal for either programming loops or functions. I chose to write a function (itself with a loop) and then call the function repeatedly.

Even the repeated calls to my function that could have been put in a loop:

```for (symbol in c("BIDU","GE","GSH","Z","^GSPC","^IXPC") doit(symbol,f)
```

In problems involving several loops, we should consider the various possibilities for nesting the loops or function calls. The slow place in a script for this assignment is in the call to get.stock.price. Therefore, once that call is made, the results should be used repeatedly (that is, do not make separate calls to get.stock.price just because you're going to compute simple returns one time and simple log returns another time!). We do have to make separate calls for the daily and monthly returns, however.

If we wanted to perform the same kind of computations for other stocks later, we could save the function in a file and source the file in a later R session.

I wrote this function so that it prints the answers; that is, I did not save the answers. I did it specifically for this assignment, but it is usually a bad design not to save answers. For this particular problem, a better design might have been to write the answers to a csv file, which could then be incorporated directly into a spreadsheet, which is what the assignment required.

This function required some statistics computed in the fBasics package. The function basicStats computes all of these things and returns them in an array structure. Note my references of the form stats["Stdev",1]. How do you know what kind of structure is returned by an R functions? You can use the R functions str and names. Just displaying the structure often indicates how it is constructed.

The fBasics package also contains separate functions for the individual statistics. For example, the function kurtosis computes the excess kurtosis, which is just one of the statistics computed by basicStats.

```doit <- function(symbol,freq){
require(fBasics)
P <-get.stock.price(symbol, start.date=c(1,1,2010), stop.date=c(5,15,2015),
freq=freq,full.table=FALSE, print.info=FALSE)
n <- length(P)
for (logreturn in c(0,1)){
if(logreturn){
R <- 100*diff(log(P))
cat("Log returns (percentages) for ",symbol,"\n")
}else {
R <- 100*diff(P)/P[-n]
cat("Simple returns (percentages) for ",symbol,"\n")
}
stats <- basicStats(R)
cat(" Mean ",stats["Mean",1],"\n",
"Stdev ",stats["Stdev",1],"\n",
"Skewness ",stats["Skewness",1],"\n",
"Excess kurtosis ",stats["Kurtosis",1],"\n",
"Minimum ",stats["Minimum",1],"\n",
"Maximum ",stats["Maximum",1],"\n"
)
tc <- stats["Mean",1]/(stats["Stdev",1]/sqrt(n))
pv <- pt(-abs(tc),n-1) + (1-pt(abs(tc),n-1)) # could use normal here
cat(" p-value for test that mean=0 ",pv,"\n")
tc <- stats["Skewness",1]/sqrt(6/n)
pv <- pnorm(-abs(tc)) + (1-pnorm(abs(tc))) # normal is almost the same as t
cat(" p-value for test that skewness=0 ",pv,"\n")
tc <- stats["Kurtosis",1]/sqrt(24/n) # basicStats computes excess kurtosis
pv <- pnorm(-abs(tc)) + (1-pnorm(abs(tc)))
cat(" p-value for test that excess kurtosis=0 ",pv,"\n")
}
}

############################################
library(fBasics)
# setwd("c:/Work/Lectures/Courses/HIT_Shenzhen") # set this to your working directory
source("financetools_funs.R") # this file must be in your working directory
for (f in c("d","m")){
doit("BIDU",f)
doit("GE",f)
doit("GSH",f)
doit("Z",f)
doit("^GSPC",f)
doit("^IXPC",f)
}
```