# Chapter 1. Financial Markets, Prices and Risk (in R/MATLAB)

Copyright 2011, 2016, 2018 Jon Danielsson. This code is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. The GNU General Public License is available at: https://www.gnu.org/licenses/.
The original 2011 R code will not fully work on a recent R because there have been some changes to libraries. The latest version of the Matlab code only uses functions from Matlab toolboxes.
The GARCH functionality in the econometric toolbox in Matlab is trying to be too clever, but can't deliver and could well be buggy. If you want to try that, here are the docs (estimate). Besides, it can only do univariate GARCH and so can't be used in Chapter 3. Kevin Sheppard's MFE toolbox is much better, while not as user friendly, it is much better written and is certainly more comprehensive. It can be downloaded here and the documentation here is quite detailed.

##### Listing 1.1/1.2: Download S&P500 data in R Last updated June 2018

library(tseries)
library(zoo)
price = zoo(read.csv('index.csv', header=TRUE, sep=','))
y=diff(log(price))                                       # calculate returns
plot(y)                                                  # plot returns
y=coredata(y)                                            # get core data

##### Listing 1.1/1.2: Download S&P 500 data in MATLAB Last updated August 2016

price = csvread('index.csv', 1, 0);
y=diff(log(price));                 % calculate returns
plot(y)                             % plot returns

##### Listing 1.3/1.4: Sample statistics in R Last updated June 2018

library(moments)
mean(y)
sd(y)
min(y)
max(y)
skewness(y)
kurtosis(y)
acf(y,1)
acf(y^2,1)
jarque.bera.test(y)
Box.test(y, lag = 20, type = c("Ljung-Box"))
Box.test(y^2, lag = 20, type = c("Ljung-Box"))

##### Listing 1.3/1.4: Sample statistics in MATLAB Last updated June 2018

%% the function sacf uses Kevin Sheppard's MFE toolbox
%% download at https://www.kevinsheppard.com/MFE_Toolbox
mean(y)
std(y)
min(y)
max(y)
skewness(y)
kurtosis(y)
sacf(y,1,[],0)
sacf(y.^2,1,[],0)
[h,pValue,stat]=jbtest(y);
[h,pValue,stat]=lbqtest(y,'lags',20);
[h,pValue,stat]=lbqtest(y.^2,'lags',20);
%% NOTE: in MATLAB 2018a, some functions require name-value pairs
%% e.g. MATLAB 2016a: [h,pValue,stat] = lbqtest(y,20)

##### Listing 1.5/1.6: ACF plots and the Ljung-Box test in R Last updated June 2018

library(MASS)
library(stats)
q = acf(y,20)
q1 = acf(y^2,20)
plot(q,main="ACF of daily returns")
plot(q1,main="ACF of squared daily returns")

##### Listing 1.5/1.6: ACF plots and the Ljung-Box test in MATLAB Last updated August 2016

%% subplots here are just for ease of visualization
subplot(1,2,1)
autocorr(y, 20)
subplot(1,2,2)
autocorr(y.^2, 20)

##### Listing 1.7/1.8: QQ plots in R Last updated June 2018

library(car)
qqPlot(y)
qqPlot(y,distribution="t",df=5)

##### Listing 1.7/1.8: QQ plots in MATLAB Last updated 2011

%% subplots here are just for ease of visualization
subplot(1,2,1)
qqplot(y)
subplot(1,2,2)
qqplot(y, fitdist(y,'tLocationScale'))

##### Listing 1.9/1.10: Download stock prices in R Last updated June 2018

p = ts(read.csv('stocks.csv',header=TRUE,sep=','))
y=diff(log(p))
print(cor(y))                                      # correlation matrix

##### Listing 1.9/1.10: Download stock prices in MATLAB Last updated 2011

price = csvread('stocks.csv', 1, 0);
y=diff(log(price));
corr(y)                              % correlation matrix