Book Image

Learning Quantitative Finance with R

By : Dr. Param Jeet, PRASHANT VATS
Book Image

Learning Quantitative Finance with R

By: Dr. Param Jeet, PRASHANT VATS

Overview of this book

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.
Table of Contents (16 chapters)
Learning Quantitative Finance with R
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

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