Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning Quantitative Finance with R
Learning Quantitative Finance with R

Learning Quantitative Finance with R: Implement machine learning, time-series analysis, algorithmic trading and more

By Dr. Param Jeet , PRASHANT VATS
€32.99 €8.99
Book Mar 2017 284 pages 1st Edition
eBook
€32.99 €8.99
Print
€41.99
Subscription
€14.99 Monthly
eBook
€32.99 €8.99
Print
€41.99
Subscription
€14.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Mar 23, 2017
Length 284 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781786462411
Category :
Languages :
Table of content icon View table of contents Preview book icon Preview Book

Learning Quantitative Finance with R

Chapter 1.  Introduction to R

In this chapter, we will be discussing basic R concepts. This will serve as the background for upcoming chapters. We are not going to discuss each and every concept in detail for R. This chapter is meant for people who do not have any knowledge of the R language or beginners who are looking to pursue a career in quantitative finance or want to use R for quantitative financial analysis. This chapter can give you a start in learning how to write programs in R, and for writing complex programs, you can explore other books.

This chapter covers the following topics:

  • The need for R

  • How to download/install R

  • How to install packages

  • Data types

  • Import and export of different data types

  • How to write code expressions

  • Functions

  • How to execute R programs

  • Loops (for, while, if, and if...else)

The need for R


There are so many statistical packages which can be used for solving problems in quantitative finance. But R is not a statistical package but it is a language. R is a flexible and powerful language for achieving high-quality analysis.

To use R, one does not need to be a programmer or computer-subject expert. The knowledge of basic programming definitely helps in learning R, but it is not a prerequisite for getting started with R.

One of the strengths of R is its package system. It is vast. If a statistical concept exists, chances are that there is already a package for it in R. There exist many functionalities that come built in for statistics / quantitative finance.

R is extendable and provides plenty of functionalities which encourage developers in quant finance to write their own tools or methods to solve their analytical problems.

The graphing and charting facilities present in R are unparalleled. R has a strong relationship with academia. As new research gets published, the likelihood is that a package for the new research gets added, due to its open source nature, which keeps R updated with the new concepts emerging in quant finance.

R was designed to deal with data, but when it came into existence, big data was nowhere in the picture. Additional challenges dealing with big data are the variety of data (text data, metric data, and so on), data security, memory, CPU I/O RSC requirements, multiple machines, and so on. Techniques such as map-reducing, in-memory processing, streaming data processing, down-sampling, chunking, and so on are being used to handle the challenges of big data in R.

Furthermore, R is free software. The development community is fantastic and easy to approach, and they are always interested in developing new packages for new concepts. There is a lot of documentation available on the Internet for different packages of R.

Thus, R is a cost-effective, easy-to-learn tool. It has very good data handling, graphical, and charting capabilities. It is a cutting-edge tool as, due to its open nature, new concepts in finance are generally accompanied by new R packages. It is demand of time for people pursuing a career in quantitative finance to learn R.

How to download/install R


In this section, we are going to discuss how to download and install R for various platforms: Windows, Linux, and Mac.

Open your web browser and go to the following link: https://cran.rstudio.com/.

From the given link, you can download the required version according to the available operating system.

For the Windows version, click on Download R for Windows, and then select the base version and download Download R 3.3.1 for Windows for your Windows operating system, click on it, and select your favorite language option. Now click through the installer and it will take you through various options, such as the following:

  1. Setup Wizard.

  2. License Agreement.

  3. Select folder location where you want to install.

  4. Select the component. Select the option according to the configuration of your system; if you do not know the configuration of your system, then select all the options.

  5. If you want to customize your setup, select the option.

  6. Select the R launch options and desktop shortcut options according to your requirements.

R download and installation is complete for Windows.

Similarly, you click on your installer for Linux and Mac and it will take you through various options of installation.

How to install packages


R packages are a combination of R functions, compiled code, and sample data, and their storage directory is known as a library. By default, when R is installed, a set of packages gets installed and the rest of the packages you have to add when required.

A list of commands is given here to check which packages are present in your system:

>.libPaths()

The preceding command is used for getting or setting the library trees that R knows about. It gives the following result:

"C:/Program Files/R/R-3.3.1/library"

After this, execute the following command and it will list all the available packages:

>library()

There are two ways to install new packages.

Installing directly from CRAN

CRAN stands for Comprehensive R Archive Network. It is a network of FTP web servers throughout the globe for storing identical, up-to-date versions of code and documentation for R.

The following command is used to install the package directly from the CRAN web page. You need to choose the appropriate mirror:

>install.packages("Package")

For example, if you need to install the ggplot2 or forecast package for R, the commands are as follows:

>install.packages("ggplot2")
>install.packages("forecast")

Installing packages manually

Download the required R package manually and save the ZIP version at your designated location (let's say /DATA/RPACKAGES/) on the system.

For example, if we want to install ggplot2, then run the following command to install it and load it to the current R environment. Similarly, other packages can also be installed:

>install.packages("ggplot2", lib="/data/Rpackages/")
>library(ggplot2, lib.loc="/data/Rpackages/")

Data types


In any programming language, one needs to store various pieces of information using various variables. Variables are reserved memory locations for storing values. So by creating a variable, one is reserving some space in the memory. You may like to store various types of data types, such as character, floating point, Boolean, and so on. On the basis of data type, the operating system allocates memory and decides what can be stored in reserved memory.

All the things you encounter in R are called objects.

R has five types of basic objects, also known as atomic objects, and the rest of the objects are built on these atomic objects. Now we will give an example of all the basic objects and will verify their class:

  • Character:

    We assign a character value to a variable and verify its class:

            >a <- "hello"
            >print(class(a))
    

    The result produced is as follows:

            [1] "character"
    
  • Numeric:

    We assign a numeric value to a variable and verify its class:

            >a <- 2.5
            >print(class(a))
    

    The result produced is as follows:

            [1] "numeric"
    
  • Integer:

    We assign an integer value to a variable and verify its class:

            >a <- 6L
            >print(class(a))
    

    The result produced is as follows:

            [1] "integer"
    
  • Complex:

    We assign an integer value to a variable and verify its class:

            >a <- 1 + 2i
            >print(class(a))
    

    The result produced is as follows:

            [1] "complex"
    
  • Logical (True/false):

    We assign an integer value to a variable and verify its class:  

            >a <- TRUE
    >print(class(a))
    

    Then the result produced is as follows:

    [1] "logical"
    

The basic types of objects in R are known as vectors and they consist of similar types of objects. They cannot consist of two different types of objects at the same time, such as a vector consisting of both character and numeric.

But list is an exception, and it can consist of multiple classes of objects at the same time. So a list can simultaneously contain a character, a numeric, and a list.

Now we will discuss the common data types present in R and give at least one example for each data type discussed here.

Vectors

Vectors have already been defined. If we want to construct a vector with more than one element, we can use the c() function which combines the elements into a vector, for example:

>a<-"Quantitative" 
>b<-"Finance" 
>c(a,b) 

This produces the following result:

[1] "Quantitative" "Finance"   

Similarly:

>Var<-c(1,2,3) 
>Var 

This produces the following result:

[1] 1 2 3 

Lists

A list is an R object that consists of multiple types of objects inside it, such as vectors and even lists. For example, let's construct a list and print it using code:

#Create a List and print it 
>List1 = list(c(4,5,6),"Hello", 24.5) 
>print(List1) 

When we execute the previous command, it produces the following result:

[[1]] 
[1] 4 5 6 
   
[[2]] 
[1] "Hello" 
 
[[3]] 
[1] 24.5 

We can extract the individual elements of the list according to our requirements.

For example, in the preceding case, if we want to extract the second element:

>print(List1[2]) 

Upon executing the preceding code, R creates the following output:

[[1]] 
[1] "Hello" 

One can merge the two lists using the function c(); for example:

>list1 <- list(5,6,7) 
>list2 <- list("a","b","c") 
>Combined_list <-c(list1,list2) 
>print(Combined_list) 

Upon executing the preceding command, we get the combined list:

[[1]] 
[1] 5 
 
[[2]] 
[1] 6 
 
[[3]] 
[1] 7 
 
[[4]] 
[1] "a" 
 
[[5]] 
[1] "b" 
 
[[6]] 
[1] "c" 

Matrices

A matrix is a two-dimensional rectangular dataset, and it is created by vector input to the matrix() function.

For example, create a matrix with two rows and three columns, and print it:

>M <- matrix(c(1,2,3,4,5,6), nrow = 2, ncol = 3) 
>print(M) 

When we execute the preceding code, it produces the following result:

     [,1] [,2] [,3] 
[1,]    1    3    5 
[2,]    2    4    6 

Arrays

Matrices are confined to only two dimensions, but arrays can be of any dimension. The array() function takes a dim attribute, which creates the needed dimensions.

For example, create an array and print it:

>a <- array(c(4,5),dim = c(3,3,2)) 
>print(a) 

When we execute the previous code, it produces the following result:

, , 1 
     [,1] [,2] [,3] 
[1,]    4    5    4 
[2,]    5    4    5 
[3,]    4    5    4 
 
, , 2 
 
     [,1] [,2] [,3] 
[1,]    5    4    5 
[2,]    4    5    4 
[3,]    5    4    5 

Factors

Factors are R objects that are created using a vector. It stores the vector along with the distinct elements present in the vector as labels. Labels are always in character form, irrespective of whether it is numeric, character, or Boolean.

Factors are created using the factor() function, and the count of levels is given by n levels; for example:

>a <-c(2,3,4,2,3) 
>fact <-factor(a) 
>print(fact) 
>print(nlevels(fact)) 

When the preceding code gets executed, it generates the following results:

[1] 2 3 4 2 3 
Levels: 2 3 4 
[1] 3 

DataFrames

DataFramesare tabular-form data objects where each column can be of different form, that is, numeric, character, or logical. Each column consists of a list of vectors having the same length.

DataFrames are generated using the function data.frame(); for example:

>data <-data.frame( 
>+Name = c("Alex", "John", "Bob"), 
>+Age = c(18,20,23), 
>+Gender =c("M","M","M") 
>+) 
>print(data) 

When the preceding code gets executed, it generates the following result:

  Name Age Gender 
1 Alex  18      M 
2 John  20      M 
3  Bob  23      M 
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Understand the basics of R and how they can be applied in various Quantitative Finance scenarios
  • Learn various algorithmic trading techniques and ways to optimize them using the tools available in R.
  • Contain different methods to manage risk and explore trading using Machine Learning.

Description

The role of a quantitative analyst is very challenging, yet lucrative, so there is a lot of competition for the role in top-tier organizations and investment banks. This book is your go-to resource if you want to equip yourself with the skills required to tackle any real-world problem in quantitative finance using the popular R programming language. You'll start by getting an understanding of the basics of R and its relevance in the field of quantitative finance. Once you've built this foundation, we'll dive into the practicalities of building financial models in R. This will help you have a fair understanding of the topics as well as their implementation, as the authors have presented some use cases along with examples that are easy to understand and correlate. We'll also look at risk management and optimization techniques for algorithmic trading. Finally, the book will explain some advanced concepts, such as trading using machine learning, optimizations, exotic options, and hedging. By the end of this book, you will have a firm grasp of the techniques required to implement basic quantitative finance models in R.

What you will learn

[*]Get to know the basics of R and how to use it in the field of Quantitative Finance [*]Understand data processing and model building using R [*]Explore different types of analytical techniques such as statistical analysis, time-series analysis, predictive modeling, and econometric analysis [*] Build and analyze quantitative finance models using real-world examples [*] How real-life examples should be used to develop strategies [*] Performance metrics to look into before deciding upon any model [*] Deep dive into the vast world of machine-learning based trading [*] Get to grips with algorithmic trading and different ways of optimizing it [*] Learn about controlling risk parameters of financial instruments

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Mar 23, 2017
Length 284 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781786462411
Category :
Languages :

Table of Contents

16 Chapters
Learning Quantitative Finance with R Chevron down icon Chevron up icon
Credits Chevron down icon Chevron up icon
About the Authors Chevron down icon Chevron up icon
About the Reviewer Chevron down icon Chevron up icon
www.PacktPub.com Chevron down icon Chevron up icon
Customer Feedback Chevron down icon Chevron up icon
Preface Chevron down icon Chevron up icon
1. Introduction to R Chevron down icon Chevron up icon
2. Statistical Modeling Chevron down icon Chevron up icon
3. Econometric and Wavelet Analysis Chevron down icon Chevron up icon
4. Time Series Modeling Chevron down icon Chevron up icon
5. Algorithmic Trading Chevron down icon Chevron up icon
6. Trading Using Machine Learning Chevron down icon Chevron up icon
7. Risk Management Chevron down icon Chevron up icon
8. Optimization Chevron down icon Chevron up icon
9. Derivative Pricing Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.