Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Quantitative Finance with R

You're reading from   Learning Quantitative Finance with R Implement machine learning, time-series analysis, algorithmic trading and more

Arrow left icon
Product type Paperback
Published in Mar 2017
Publisher Packt
ISBN-13 9781786462411
Length 284 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
PRASHANT VATS PRASHANT VATS
Author Profile Icon PRASHANT VATS
PRASHANT VATS
Dr. Param Jeet Dr. Param Jeet
Author Profile Icon Dr. Param Jeet
Dr. Param Jeet
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Introduction to R 2. Statistical Modeling FREE CHAPTER 3. Econometric and Wavelet Analysis 4. Time Series Modeling 5. Algorithmic Trading 6. Trading Using Machine Learning 7. Risk Management 8. Optimization 9. Derivative Pricing

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

Loops are instructions for automating a multistep process by organizing sequences of actions by grouping the parts which need to be repeated. All the programming languages come up with built-in constructs, which allow the repetition of instructions or blocks of instructions. In programming languages, there are two types of loops.

Decision-making is one of the significant components of programming languages. This can be achieved in R programming by using the conditional statement if...else. The syntax, along with an example, is given here.

Let us first discuss if and else conditional statements and then we will discuss loops.

if statement

Let us first see how if and else work in R. The general syntax for an if clause is given here:

if (expression) { 
   statement 
} 

If an expression is correct then the statement gets executed else nothing happens. An expression can be a logical or numeric vector. In the case of numeric vectors, 0 is taken as False and the rest are taken as True, for example:

>x<-5 
>if(x>0) 
>+ { 
>+ print(" I am Positive") 
>+ } 

When the preceding code gets executed then it prints I am Positive.

if...else statement

Now let us see how the if and else conditions work in R. Here is the syntax:

if(expression){ 
   statement1 
} else { 
   statement2 
} 

The else part is evaluated in case if the if part is False, for example:

> x<--5 
> if(x>0) 
>+ { 
>+ print(" I am Positive") 
>+ }else 
>+{ 
>+ print(" I am Negative") 
>+} 

When the preceding code gets executed, it prints I am Negative.

for loop

These loops are executed for a defined number of times and are controlled by a counter or index and incremented at each cycle. Please find here the syntax of the for loop construct:

for (val in sequence) { 
    statement 
} 

Here is an example:

>Var <- c(3,6,8,9,11,16) 
>counter <- 0 
>for (val in Var) { 
>+    if(val %% 2 != 0)  counter = counter+1 
>+} 
print(counter) 

When the preceding code gets executed, it counts the number of odd numbers present in vector c, that is, 3.

while loop

while loops are the loops which are set at onset for verifying the logical condition. The logical condition is tested at the start of the loop construct. Here is the syntax:

while (expression) { 
   statement 
} 

Here, the expression is evaluated first and, if it is true, the body of the for loop gets executed. Here is an example:

>Var <- c("Hello") 
>counter <- 4 
>while (counter < 7) { 
>+   print(Var) 
>+   counter = counter+ 1 
>+} 

Here, first the expression gets evaluated and, if it is true, the body of the loop gets executed and it keeps executing till the expression returns False.

apply()

apply() is a function in R used for quick operations on a matrix, vector, or array and can be executed on rows, columns, and on both together. Now let us try to find the sum of rows of a matrix using the apply function. Let us execute the following code:

> sample = matrix(c(1:10), nrow = 5 , ncol = 2) 
> apply(sample, 1,sum) 

It generates the sum row-wise.

sapply()

sapply() operates over a set of data such as a list or vector, and calls the specified function for each item. Let us execute the following code to check the example:

> sapply(1:5, function(x) x^3) 

It computes cubes for 1 to 5.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime