*Introduction to R for Quantitative Finance* will show you how to solve real-world quantitative finance problems using the statistical computing languages R and QF. In this book, we will cover diverse topics ranging from Time Series Analysis to Financial Networks. Each chapter will briefly present the theory and deal with solving a specific problem using R.

Chapter 1, *Time Series Analysis* (Michael Puhle), explains working with time series data in R. Furthermore, you will learn how to model and forecast house prices, improve hedge ratios using cointegration, and model volatility.

Chapter 2, *Portfolio Optimization* (Péter Csóka, Ferenc Illés, Gergely Daróczi), covers the theoretical idea behind portfolio selection and shows how to apply this knowledge to real-world data.

Chapter 3, *Asset Pricing Models* (Kata Váradi, Barbara Mária Dömötör, Gergely Daróczi), builds on the previous chapter and presents models for the relationship between asset return and risk. We'll cover the Capital Asset Pricing Model and the Arbitrage Pricing Theory.

Chapter 4, *Fixed Income Securities* (Márton Michaletzky, Gergely Daróczi), deals with the basics of fixed income instruments. Furthermore, you will learn how to calculate the risk of such an instrument and construct portfolios that will be immune to changes in interest rates.

Chapter 5, *Estimating the Term Structure of Interest Rates* (Tamás Makara, Gergely Daróczi), introduces the concept of a yield curve and shows how to estimate it using prices of government bonds.

Chapter 6, *Derivatives Pricing* (Ágnes Vidovics-Dancs, Gergely Daróczi), explains the pricing of derivatives using discrete and continuous time models. Furthermore, you will learn how to calculate derivatives risk measures and the so-called "Greeks".

Chapter 7, *Credit Risk Management* (Dániel Havran, Gergely Daróczi), gives an introduction to the credit default models and shows how to model correlated defaults using copulas.

Chapter 8, *Extreme Value Theory* (Zsolt Tulassay), presents possible uses of Extreme Value Theory in insurance and finance. You will learn how to fit a model to the tails of the distribution of fire losses. Then we will use the fitted model to calculate Value-at-Risk and Expected Shortfall.

Chapter 9, *Financial Networks* (Edina Berlinger, Gergely Daróczi), explains how financial networks can be represented, simulated, visualized, and analyzed in R. We will analyze the interbank lending market and learn how to systemically detect important financial institutions.

All the code examples provided in this book should be run in the R console that is to be installed first on a computer. You can download the software for free and find the installation instructions for all major operating systems at http://r-project.org. Although we will not cover advanced topics such as how to use R in Integrated Development Environments, there are awesome plugins for Emacs, Eclipse, vi, or Notepad++ besides other editors, and we can also highly recommend trying RStudio, which is a free and open source IDE dedicated to R.

Apart from a working R installation, we will also use some user-contributed R packages that can be easily installed from the Comprehensive R Archive Network. To install a package, use the `install.packages`

command in the R console, shown as follows:

**> install.packages('zoo')**

After installation, the package should be also loaded first to the current R session before usage:

**> library(zoo)**

You can find free introductory articles and manuals on the R homepage, but this book is targeted towards beginners, so no additional R knowledge is assumed from the reader.

The book is aimed at readers who wish to use R to solve problems in quantitative finance. Some familiarity with finance is assumed, but we generally provide the financial theory as well. Familiarity with R is not assumed. Those who want to get started with R may find this book useful as we don't give a complete overview of the R language but show how to use parts of it to solve specific problems. Even if you already use R, you will surely be amazed to see the wide range of problems that it can be applied to.

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "we will employ some methods from the `forecast`

package"

A block of R code (usually a function's body) is set as follows:

logreturn <- function(x) { log(tail(x, -1) / head(x, -1)) }

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

```
logreturn <- function(x) {
log(
```**tail(x, -1)** / head(x, -1))
}

Any command-line input or output is written as follows:

> pi[1] 3.141593

Where "`>`

" shows that the R console is waiting for commands to be evaluated. Multiline expressions are started with the same symbol on the first line, but all the rest lines have a "`+`

" sign at the beginning to show that the last R expression is still to be finished.

**New terms** and **important words** are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "clicking the **Next** button moves you to the next screen".

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to `<[email protected]>`

, and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the **errata submission form** link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at `<[email protected]>`

with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

You can contact us at `<[email protected]>`

if you are having a problem with any aspect of the book, and we will do our best to address it.