Mastering R For Quantitative Finance - Sample Chapter
Mastering R For Quantitative Finance - Sample Chapter
Ferenc Ills has an MSc degree in mathematics from Etvs Lornd University.
A few years after graduation, he started studying actuarial and financial mathematics,
and he is about to pursue his PhD from Corvinus University of Budapest. In recent
years, he has worked in the banking industry. Currently, he is developing statistical
models with R. His interest lies in large networks and computational complexity.
Miln Badics has a master's degree in finance from the Corvinus University
of Budapest. Now, he is a PhD student and a member of the PADS PhD
scholarship program. He teaches financial econometrics, and his main
research topics are time series forecasting with data-mining methods,
financial signal processing, and numerical sensitivity analysis on
interest rate models. He won the competition of the X. Kochmeisterprize organized by the Hungarian Stock Exchange in May 2014.
dm Banai has received his MSc degree in investment analysis and risk management
from Corvinus University of Budapest. He joined the Financial Stability department
of the Magyar Nemzeti Bank (MNB, the central bank of Hungary) in 2008. Since 2013,
he is the head of the Applied Research and Stress Testing department at the Financial
System Analysis Directorate (MNB). He is also a PhD student at the Corvinus University
of Budapest since 2011. His main research fields are solvency stress-testing, funding
liquidity risk, and systemic risk.
Gergely Darczi is an enthusiast R package developer and founder/CTO of an
R-based web application at Rapporter. He is also a PhD candidate in sociology
and is currently working as the lead R developer at CARD.com in Los Angeles.
Besides teaching statistics and doing data analysis projects for several years, he has
around 10 years of experience with the R programming environment. Gergely is the
coauthor of Introduction to R for Quantitative Finance, and is currently working on
another Packt book, Mastering Data Analysis with R, apart from a number of journal
articles on social science and reporting topics. He contributed to the book by reviewing
and formatting the R source code.
Barbara Dmtr is an assistant professor of the department of Finance at Corvinus
University of Budapest. Before starting her PhD studies in 2008, she worked for several
multinational banks. She wrote her doctoral thesis about corporate hedging. She lectures
on corporate finance, financial risk management, and investment analysis. Her main
research areas are financial markets, financial risk management, and corporate hedging.
Gergely Gabler is the head of the Business Model Analysis department at the
banking supervisory division of National Bank of Hungary (MNB) since 2014.
Before this, he used to lead the Macroeconomic Research department at Erste
Bank Hungary after being an equity analyst since 2008. He graduated from
the Corvinus University of Budapest in 2009 with an MSc degree in financial
mathematics. He has been a guest lecturer at Corvinus University of Budapest
since 2010, and he also gives lectures in MCC College for advanced studies.
He is about to finish the CFA program in 2015 to become a charterholder.
Pter Juhsz holds a PhD degree in business administration from the Corvinus
University of Budapest and is also a CFA charterholder. As an associate professor,
he teaches corporate finance, business valuation, VBA programming in Excel, and
communication skills. His research field covers the valuation of intangible assets,
business performance analysis and modeling, and financial issues in public procurement
and sports management. He is the author of several articles, chapters, and books mainly
on the financial performance of Hungarian firms. Besides, he also regularly acts as
a consultant for SMEs and is a senior trainer for EY Business Academy in the
EMEA region.
Istvn Margitai is an analyst in the ALM team of a major banking group in the CEE
region. He mainly deals with methodological issues, product modeling, and internal
transfer pricing topics. He started his career with asset-liability management in Hungary
in 2009. He gained experience in strategic liquidity management and liquidity planning.
He majored in investments and risk management at Corvinus University of Budapest.
His research interest is the microeconomics of banking, market microstructure, and the
liquidity of order-driven markets.
Balzs Mrkus has been working with financial derivatives for over 10 years.
He has been trading many different kinds of derivatives, from carbon swaps to
options on T-bond futures. He was the head of the Foreign Exchange Derivative Desk
at Raiffesien Bank in Budapest. He is a member of the advisory board at Pallas Athn
Domus Scientiae Foundation, and is a part-time analyst at the National Bank of Hungary
and the managing director of Nitokris Ltd, a small proprietary trading and consulting
company. He is currently working on his PhD about the role of dynamic hedging at
the Corvinus University of Budapest, where he is affiliated as a teaching assistant.
Pter Medvegyev has an MSc degree in economics from the Marx Kroly University
Budapest. After completing his graduation in 1977, he started working as a consultant
in the Hungarian Management Development Center. He got his PhD in Economics in
1985. He has been working for the Mathematics department of the Corvinus University
Budapest since 1993. His teaching experience at Corvinus University includes stochastic
processes, mathematical finance, and several other subjects in mathematics.
Julia Molnr is a PhD candidate at the Department of Finance, Corvinus University
of Budapest. Her main research interests include financial network, systemic risk, and
financial technology innovations in retail banking. She has been working at McKinsey
& Company since 2011, where she is involved in several digital and innovation studies
in the area of banking.
Balzs rpd Szcs is a PhD candidate in finance at the Corvinus University of
Budapest. He works as a research assistant at the Department of Finance at the same
university. He holds a master's degree in investment analysis and risk management.
His research interests include optimal execution, market microstructure, and forecasting
intraday volume.
Cointegration
From now on, we will consider a vector of time series yt , which consists of the
elements yt(1) , yt( 2) yt( n ) each of them individually representing a time series, for
instance, the price evolution of different financial products. Let's begin with the
formal definition of cointegrating data series.
The n 1 vector yt of time series is said to be cointegrated if each of the series are
individually integrated in the order d (in particular, in most of the applications the
series are integrated of order 1, which means nonstationary unit-root processes, or
'
random walks), while there exists a linear combination of the series yt , which is
integrated in the order d 1 (typically, it is of order 0, which is a stationary process).
Intuitively, this definition implies the existence of some underlying forces in the
economy that are keeping together the n time series in the long run, even if they all
seem to be individually random walks. A simple example for cointegrating time
series is the following pair of vectors, taken from Hamilton (1994), which we will use
to study cointegration, and at the same time, familiarize ourselves with some basic
simulation techniques in R:
xt = xt 1 + ut , ut N ( 0,1)
yt = xt + vt , vt N ( 0,1)
[8]
Chapter 1
The unit root in yt will be shown formally by standard statistical tests. Unit root
tests in R can be performed using either the tseries package or the urca package;
here, we use the second one. The following R code simulates the two series of
length 1000:
#generate the two time series of length 1000
set.seed(20140623)
N <- 1000
x <- cumsum(rnorm(N))
plot(x, type='l')
lines(y,col="red")
[9]
For both of the simulated series, the test statistic is larger than the critical value at the
usual significance levels (1 percent, 5 percent, and 10 percent); therefore, we cannot
reject the null hypothesis, and we conclude that both the series are individually unit
root processes.
Now, take the following linear combination of the two series and plot the
resulted series:
zt = yt xt
z = y - gamma*x
plot(z,type='l')
[ 10 ]
Chapter 1
zt clearly seems to be a white noise process; the rejection of the unit root is
confirmed by the results of ADF tests:
summary(ur.df(z,type="none"))
Simple linear regressions can be fitted by using the lm function. The residuals can
be obtained from the resulting object as shown in the following example. The ADF
test is run in the usual way and confirms the rejection of the null hypothesis at all
significant levels. Some caveats, however, will be discussed later in the chapter:
#Estimate the cointegrating relationship
coin <- lm(y ~ x -1)
coin$resid
summary(ur.df(coin$resid))
Now, consider how we could turn this theory into a successful trading strategy.
At this point, we should invoke the concept of statistical arbitrage or pair trading,
which, in its simplest and early form, exploits exactly this cointegrating relationship.
These approaches primarily aim to set up a trading strategy based on the spread
between two time series; if the series are cointegrated, we expect their stationary
linear combination to revert to 0. We can make profit simply by selling the relatively
expensive one and buying the cheaper one, and just sit and wait for the reversion.
The term statistical arbitrage, in general, is used for many sophisticated
statistical and econometrical techniques, and this aims to exploit
relative mispricing of assets in statistical terms, that is, not in
comparison to a theoretical equilibrium model.
[ 11 ]
What is the economic intuition behind this idea? The linear combination of time series
that forms the cointegrating relationship is determined by underlying economic
forces, which are not explicitly identified in our statistical model, and are sometimes
referred to as long-term relationships between the variables in question. For example,
similar companies in the same industry are expected to grow similarly, the spot and
forward price of a financial product are bound together by the no-arbitrage principle,
FX rates of countries that are somehow interlinked are expected to move together,
or short-term and long-term interest rates tend to be close to each other. Deviances
from this statistically or theoretically expected comovements open the door to various
quantitative trading strategies where traders speculate on future corrections.
The concept of cointegration is further discussed in a later chapter, but for that,
we need to introduce vector autoregressive models.
yt = A1 yt 1 + + A p yt p + ut
Here, A i are n n the coefficient matrices for all i = 1 p , and ut is a vector white
noise process with a positive definite covariance matrix. The terminology of vector
white noise assumes lack of autocorrelation, but allows contemporaneous correlation
between the components; that is, ut has a non-diagonal covariance matrix.
[ 12 ]
Chapter 1
The matrix notation makes clear one particular feature of VAR models: all variables
depend only on past values of themselves and other variables, meaning that
contemporaneous dependencies are not explicitly modeled. This feature allows us
to estimate the model by ordinary least squares, applied equation-by-equation. Such
models are called reduced form VAR models, as opposed to structural form models,
discussed in the next section.
Obviously, assuming that there are no contemporaneous effects would be an
oversimplification, and the resulting impulse-response relationships, that is, changes
in the processes followed by a shock hitting a particular variable, would be misleading
and not particularly useful. This motivates the introduction of structured VAR (SVAR)
models, which explicitly models the contemporaneous effects among variables:
What makes the SVAR modeling interesting and difficult at the same time is
the so-called identification problem; the SVAR model is not identified, that is,
parameters in matrix A cannot be estimated without additional restrictions.
How should we understand that a model is not identified? This
basically means that there exist different (infinitely many) parameter
matrices, leading to the same sample distribution; therefore, it is not
possible to identify a unique value of parameters based on the sample.
[ 13 ]
) = L (L )
' 1
, which gives
L LT . Now, we conclude
Impulse-response analysis is usually one of the main goals of building a VAR model.
Essentially, an impulse-response function shows how a variable reacts (response) to a
shock (impulse) hitting any other variable in the system. If the system consists of K
variables, K 2 impulse response functions can be determined. Impulse responses can
be derived mathematically from the Vector Moving Average representation (VMA) of
the VAR process, similar to the univariate case (see the details in Ltkepohl (2007)).
[ 14 ]
Chapter 1
Equity return: This specifies the Microsoft price index from January 01, 2004
to March 03, 2014
Stock index: This specifies the S&P500 index from January 01, 2004 to
March 03, 2014
US Treasury bond interest rates from January 01, 2004 to March 03, 2014
Our primary purpose is to make a forecast for the stock market index by using the
additional variables and to identify impulse responses. Here, we suppose that there
exists a hidden long term relationship between a given stock, the stock market as
a whole, and the bond market. The example was chosen primarily to demonstrate
several of the data manipulation possibilities of the R programming environment
and to illustrate an elaborate concept using a very simple example, and not because
of its economic meaning.
We use the vars and quantmod packages. Do not forget to install and load those
packages if you haven't done this yet:
install.packages('vars');library('vars')
install.packages('quantmod');library('quantmod')
The Quantmod package offers a great variety of tools to obtain financial data directly
from online sources, which we will frequently rely on throughout the book. We use
the getSymbols()function:
getSymbols('MSFT', from='2004-01-02', to='2014-03-31')
getSymbols('SNP', from='2004-01-02', to='2014-03-31')
getSymbols('DTB3', src='FRED')
By default, yahoofinance is used as a data source for equity and index price series
(src='yahoo' parameter settings, which are omitted in the example). The routine
downloads open, high, low, close prices, trading volume, and adjusted prices. The
downloaded data is stored in an xts data class, which is automatically named
by default after the ticker (MSFT and SNP). It's possible to plot the closing prices
by calling the generic plot function, but the chartSeries function of quantmod
provides a much better graphical illustration.
[ 15 ]
The components of the downloaded data can be reached by using the following
shortcuts:
Cl(MSFT)
#closing prices
Op(MSFT)
#open prices
Hi(MSFT)
Lo(MSFT)
ClCl(MSFT)
Ad(MSFT)
Thus, for example, by using these shortcuts, the daily close-to-close returns can be
plotted as follows:
chartSeries(ClCl(MSFT))
Interest rates are downloaded from the FRED (Federal Reserve Economic Data)
data source. The current version of the interface does not allow subsetting of dates;
however, downloaded data is stored in an xts data class, which is straightforward
to subset to obtain our period of interest:
DTB3.sub <- DTB3['2004-01-02/2014-03-31']
[ 16 ]
Chapter 1
<- diff(log(Ad(SNP)))
To proceed, we need a last data-cleansing step before turning to VAR model fitting.
By eyeballing the data, we can see that missing data exists in T-Bill return series,
and the lengths of our databases are not the same (on some dates, there are interest
rate quotes, but equity prices are missing). To solve these data-quality problems, we
choose, for now, the easiest possible solution: merge the databases (by omitting all data
points for which we do not have all three data), and omit all NA data. The former is
performed by the inner join parameter (see help of the merge function for details):
dataDaily <- na.omit(merge(SNP.ret,MSFT.ret,DTB3.sub), join='inner')
Here, we note that VAR modeling is usually done on lower frequency data.
There is a simple way of transforming your data to monthly or quarterly frequencies,
by using the following functions, which return with the opening, highest, lowest,
and closing value within the given period:
SNP.M
<- to.monthly(SNP.ret)$SNP.ret.Close
A simple reduced VAR model may be fitted to the data by using the VAR() function
of the vars package. The parameterization shown in the following code allows a
maximum of 4 lags in the equations, and choose the model with the best (lowest)
Akaike Information Criterion value:
var1 <- VAR(dataDaily, lag.max=4, ic="AIC")
For a more established model selection, you can consider using VARselect(),
which provides multiple information criteria (output omitted):
>VARselect(dataDaily,lag.max=4)
The resulting object is an object of the varest class. Estimated parameters and
multiple other statistical results can be obtained by the summary() method or the
show() method (that is, by just typing the variable):
summary(var1)
var1
[ 17 ]
There are other methods worth mentioning. The custom plotting method for the
varest class generates a diagram for all variables separately, including its fitted
values, residuals, and autocorrelation and partial autocorrelation functions of the
residuals. You need to hit Enter to get the new variable. Plenty of custom settings
are available; please consult the vars package documentation:
plot(var1)
coef(var1)
residuals(var1)
fitted(var1)
Phi(var1)
Predictions using our estimated VAR model can be made by simply calling the
predict function and by adding a desired confidence interval:
var.pred <- predict(var1, n.ahead=10, ci=0.95)
Impulse responses should be first generated numerically by irf(), and then they can
be plotted by the plot() method. Again, we get different diagrams for each variable,
including the respective impulse response functions with bootstrapped confidence
intervals as shown in the following command:
var.irf <- irf(var1)
plot(var.irf)
Now, consider fitting a structural VAR model using parameter restrictions described
earlier as an A-model. The number of required restrictions for the SVAR model that
K K 1
is identified is ( 2 ) ; in our case, this is 3.
See Ltkepohl (2007) for more details. The number of additional
K K + 1)
restrictions required is (
, but the diagonal elements are
2
The point of departure for an SVAR model is the already estimated reduced form
of the VAR model (var1). This has to be amended with an appropriately structured
restriction matrix.
For the sake of simplicity, we will use the following restrictions:
Chapter 1
These restrictions enter into the SVAR model as 0s in the A matrix, which is
as follows:
1 a12
a13
0 1
0 a32
0
1
When setting up the A matrix as a parameter for SVAR estimation in R, the positions
of the to-be estimated parameters should take the NA value. This can be done with
the following assignments:
amat <amat[2,
amat[2,
amat[3,
diag(3)
1] <- NA
3] <- NA
1] <- NA
Finally, we can fit the SVAR model and plot the impulse response functions
(the output is omitted):
svar1 <- SVAR(var1, estmethod='direct', Amat = amat)
irf.svar1 <- irf(svar1)
plot(irf.svar1)
We can consistently estimate the cointegrating relationship between the two series
by running a simple linear regression. To simplify coding, we define the variables
x1 and x2 for the two series, and y for the respective vector series. The other
variable-naming conventions in the code snippets will be self-explanatory:
x1=as.numeric(na.omit(DTB3.sub))
x2=as.numeric(na.omit(DTB6.sub))
y = cbind(x1,x2)
cregr <- lm(x1 ~ x2)
r = cregr$residuals
[ 20 ]
Chapter 1
The two series are indeed cointegrated if the residuals of the regression (variable r),
that is, the appropriate linear combination of the variables, constitute a stationary
series. You could test this with the usual ADF test, but in these settings, the
conventional critical values are not appropriate, and corrected values should be used
(see, for example Phillips and Ouliaris (1990)).
It is therefore much more appropriate to use a designated test for the existence of
cointegration, for example, the Phillips and Ouliaris test, which is implemented in
the tseries and in the urca packages as well. The most basic tseries version is
demonstrated as follows:
install.packages('tseries');library('tseries');
po.coint <- po.test(y, demean = TRUE, lshort = TRUE)
The null hypothesis states that the two series are not cointegrated, so the low p value
indicates rejection of null and presence of cointegration.
The Johansen procedure is applicable for more than one possible cointegrating
relationship; an implementation can be found in the urca package:
yJoTest = ca.jo(y, type = c("trace"), ecdet = c("none"), K = 2)
######################
# Johansen-Procedure #
######################
Eigenvalues (lambda):
[1] 0.0160370678 0.0002322808
r <= 1 |
r = 0
test 10pct
5pct
1.76
8.18 11.65
6.50
1pct
[ 21 ]
DTB3.l2
DTB3.l2
DTB6.l2
1.000000
1.000000
DTB3.l2
DTB6.l2
The test statistic for r = 0 (no cointegrating relationship) is larger than the critical
values, which indicates the rejection of the null. For r 1 , however, the null cannot
be rejected; therefore, we conclude that one cointegrating relationship exists. The
cointegrating vector is given by the first column of the normalized eigenvectors
below the test results.
The final step is to obtain the VECM representation of this system, that is, to run an
OLS regression on the lagged differenced variables and the error correction term
derived from the previously calculated cointegrating relationship. The appropriate
function utilizes the ca.jo object class, which we created earlier. The r = 1 parameter
signifies the cointegration rank which is as follows:
>yJoRegr = cajorls(dyTest, r=1)
>yJoRegr
$rlm
Call:
lm(formula = substitute(form1), data = data.mat)
Coefficients:
x1.d
x2.d
ect1
-0.0370159
-0.0072971
constant
-0.0041984
-0.0016892
x1.dl1
0.1277872
0.1538121
x2.dl1
0.0006551
-0.0390444
[ 22 ]
Chapter 1
$beta
ect1
x1.l1
1.000000
x2.l1 -0.994407
Volatility modeling
It is a well-known and commonly accepted stylized fact in empirical finance that
the volatility of financial time series varies over time. However, the non-observable
nature of volatility makes the measurement and forecasting a challenging exercise.
Usually, varying volatility models are motivated by three empirical observations:
In the following code, we demonstrate these stylized facts based on S&P asset prices.
Data is downloaded from yahoofinance, by using the already known method:
getSymbols("SNP", from="2004-01-01", to=Sys.Date())
chartSeries(Cl(SNP))
Our purpose of interest is the daily return series, so we calculate log returns from the
closing prices. Although it is a straightforward calculation, the Quantmod package
offers an even simpler way:
ret <- dailyReturn(Cl(SNP), type='log')
[ 23 ]
[ 24 ]
Chapter 1
Next, we look at the histogram and/or the empirical distribution of daily log returns
of S&P and compare it with the normal distribution of the same mean and standard
deviation. For the latter, we use the function density(ret), which computes the
nonparametric empirical distribution function. We use the function curve()with an
additional parameter add=TRUE to plot a second line to an already existing diagram:
m=mean(ret);s=sd(ret);
par(mfrow=c(1,2))
hist(ret, nclass=40, freq=FALSE, main='Return histogram');curve(dnorm(x,
mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")
plot(density(ret), main='Return empirical distribution');curve(dnorm(x,
mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")
par(mfrow=c(1,1))
The excess kurtosis and fat tails are obvious, but we can confirm numerically
(using the moments package) that the kurtosis of the empirical distribution of our
sample exceeds that of a normal distribution (which is equal to 3). Unlike some other
software packages, R reports the nominal value of kurtosis, and not excess kurtosis
which is as follows:
> kurtosis(ret)
daily.returns
12.64959
[ 25 ]
It might be also useful to zoom in to the upper or the lower tail of the diagram.
This is achieved by simply rescaling our diagrams:
# tail zoom
plot(density(ret), main='Return EDF - upper tail', xlim = c(0.1, 0.2),
ylim=c(0,2));
curve(dnorm(x, mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")
# QQ-plot
qqnorm(ret);qqline(ret);
[ 26 ]
Chapter 1
[ 27 ]
t = tt
q
i =1
j =1
t2 = + i t2i + j t2 j
Here, t is usually the disturbance term of a conditional mean equation (in practice,
usually an ARMA process) and t ~ i.i.d. ( 0,1) . That is, the conditional volatility process
2
is determined linearly by its own lagged values t j and the lagged squared
2
, the last predicted variance t 1 ,
and the new information t 1 (see Andersen et al. (2009)). It is easy to see how the
2
[ 28 ]
Chapter 1
Our empirical example will be the analysis of the return series calculated from
the daily closing prices of Apple Inc. based on the period from Jan 01, 2006 to
March 31, 2014. As a useful exercise, before starting this analysis, we recommend
that you repeat the exploratory data analysis in this chapter to identify stylized
facts on Apple data.
Obviously, our first step is to install a package, if not installed yet:
install.packages('rugarch');library('rugarch')
To get the data, as usual, we use the quantmod package and the getSymbols()
function, and calculate return series based on the closing prices.
#Load Apple data and calculate log-returns
getSymbols("AAPL", from="2006-01-01", to="2014-03-31")
ret.aapl <- dailyReturn(Cl(AAPL), type='log')
chartSeries(ret.aapl)
An obvious way to proceed is to fit this model to our data, that is, to estimate
the unknown parameters by maximum likelihood, based on our time series of
daily returns:
aapl.garch11.fit = ugarchfit(spec=garch11.spec, data=ret.aapl)
The function provides, among a number of other outputs, the parameter estimations
, , 1 , 1 :
> coef(aapl.garch11.fit)
mu
omega
alpha1
beta1
[ 29 ]
Estimates and various diagnostic tests can be obtained by the show() method of the
generated object (that is, by just typing the name of the variable). A bunch of other
statistics, parameter estimates, standard error, and covariance matrix estimates
can be reached by typing the appropriate command. For the full list, consult the
ugarchfit object class; the most important ones are shown in the following code:
coef(msft.garch11.fit)
#estimated coefficients
vcov(msft.garch11.fit)
infocriteria(msft.garch11.fit)
newsimpact(msft.garch11.fit)
signbias(msft.garch11.fit)
fitted(msft.garch11.fit)
residuals(msft.garch11.fit)
uncvariance(msft.garch11.fit)
uncmean(msft.garch11.fit)
Standard GARCH models are able to capture fat tails and volatility clustering, but to
explain asymmetries caused by the leverage effect, we need more advanced models.
To approach the asymmetry problem visually, we will now describe the concept of
news impact curves.
News impact curves, introduced by Pagan and Schwert (1990) and Engle and Ng
(1991), are useful tools to visualize the magnitude of volatility changes in response to
shocks. The name comes from the usual interpretation of shocks as news influencing
the market movements. They plot the change in conditional volatility against shocks
in different sizes, and can concisely express the asymmetric effects in volatility. In
the following code, the first line calculates the news impacts numerically for the
previously defined GARCH(1,1) model, and the second line creates the visual plot:
ni.garch11 <- newsimpact(aapl.garch11.fit)
plot(ni.garch11$zx, ni.garch11$zy, type="l", lwd=2, col="blue",
main="GARCH(1,1) - News Impact", ylab=ni.garch11$yexpr, xlab=ni.
garch11$xexpr)
[ 30 ]
Chapter 1
t = tt
q
log t2 = + it i + ( t i E t i
i =1
) ) +
j =1
log ( t2 j )
[ 31 ]
> coef(aapl.egarch11.fit)
alpha1
beta1
gamma1
mu
omega
0.961968640
0.176796061
[ 32 ]
Chapter 1
t = tt
q
i =1
j =1
t2 = + ( i + i I t i ) t2i + j t2 j
where
1 if t 1 < 0
I t i =
0 if t 1 0
> coef(aapl.egarch11.fit)
mu
omega
alpha1
beta1
gamma1
0.961968640
0.176796061
[ 33 ]
Thanks to the specific functional form, the news impact curve for a
Threshold-GARCH is less flexible in representing different responses, there is
a kink at the zero point which can be seen when we run the following command:
ni.tgarch11 <- newsimpact(aapl.tgarch11.fit)
plot(ni.tgarch11$zx, ni.tgarch11$zy, type="l", lwd=2, col="blue",
main="TGARCH(1,1) - News Impact",
ylab=ni.tgarch11$yexpr, xlab=ni.tgarch11$xexpr)
[ 34 ]
Chapter 1
Once we have an estimated model and technically a fitted object, forecasting the
conditional volatility based on that is just one step:
aapl.garch11.fit = ugarchfit(spec=garch11.spec, data=ret.aapl, out.
sample=20)
aapl.garch11.fcst = ugarchforecast(aapl.garch11.fit, n.ahead=10,
n.roll=10)
The plotting method of the forecasted series provides the user with a selection menu;
we can plot either the predicted time series or the predicted conditional volatility.
plot(aapl.garch11.fcst, which='all')
[ 35 ]
Summary
In this chapter, we reviewed some important concepts of time series analysis, such as
cointegration, vector-autoregression, and GARCH-type conditional volatility models.
Meanwhile, we have provided a useful introduction to some tips and tricks to start
modeling with R for quantitative and empirical finance. We hope that you find these
exercises useful, but again, it should be noted that this chapter is far from being
complete both from time series and econometric theory, and from R programming's
point of view. The R programming language is very well documented on the
Internet, and the R user's community consists of thousands of advanced and
professional users. We encourage you to go beyond books, be a self-learner, and do
not stop if you are stuck with a problem; almost certainly, you will find an answer
on the Internet to proceed. Use the documentation of R packages and the help files
heavily, and study the official R-site, http://cran.r-project.org/, frequently.
The remaining chapters will provide you with numerous additional examples to
find your way in the plethora of R facilities, packages, and functions.
Fry, Renee and Pagan, Adrian (2011). Sign Restrictions in Structural Vector
Autoregressions: A Critical Review. Journal of Economic Literature,
American Economic Association, vol. 49(4), pages 938-60, December.
[ 36 ]
Chapter 1
Martin, Vance; Hurn, Stan and Harris, David (2013). Econometric Modelling
with Time Series. Specification, Estimation and Testing, Cambridge
University Press
Rachev, Svetlozar T., Hsu, John S.J., Bagasheva, Biliana S. and Fabozzi, Frank
J. (2008). Bayesian Methods in Finance. John Wiley & Sons.
Tsay, Ruey S. (2010). Analysis of Financial Time Series, 3rd edition, Wiley
[ 37 ]
www.PacktPub.com
Stay Connected: