# 5 Loops

## 5.1 For-loop

The for-loop is one of the most powerful tools in programming. When you are conduction asset pricing univariate/ bivariate portfolio sorts are one way to identify risk premiums. Imagine you have data on daily stock returns for two stocks and you want to buy at time t the best performing stock in t-1. Such a task can easily be handled with a for-loop. I will start with a simple for loop and show you in a later chapter how you can do portfolio sorts.

The following example teaches the notation in a for-loop and shows you what it does. First, you specify the index (i - this couls also be another variable). Second, you specify the range of the index (here: 1:5). This means, that i will take all integer values from 1 to 5. After the curly brackets follows the task. Here we simply specify that we want to print the index.

``````for(i in 1:5){
print(i)
}``````
``````##  1
##  2
##  3
##  4
##  5``````

Here is another simple example how to use for loops. Notice that the index variable can be chosen freely (here: x). This is important when you do nested for-loops.

``````for(x in 1:4){
print(x+3)
}``````
``````##  4
##  5
##  6
##  7``````

A nested for loop, is a loop within a loop.

``````for(i in 1:2){
for(j in 1:2){
print(i)
print(j)
}
}``````
``````##  1
##  1
##  1
##  2
##  2
##  1
##  2
##  2``````

## 5.2 Solve the Monty Hall Problem

Monty Hall was the host of a famous game show called Let’s make a deal in the 70s. During the game show, the competitors were confronted with three closed doors. Behind one of the three doors is the main prize (e.g. a car) and behind the other two doors are goats. Initially, the competitor of the game show chooses a door, where he thinks the main prize is located (e.g. door 1). After, Monty Hall would open one of the other two doors (e.g. door 3 - which does not contain the main prize) and asks the competitor: “Do you want to switch to door No. 2?” Figure 5.1: The Monty Hall problem

So what should the competitor do? The following simulation illustrates that by switching doors the competitor can increase his winning probability from initially 33.3% to 66.6%.

The following code simulates two different competitors. The first competitor chooses randomly one of the three doors and won’t change his decision (strategy 1). The second competitor picks randomly as well but switches the door in the second phase of the game (strategy 2).

``````# set a seed
set.seed(1)
# set the number of simulations
n <- 100000``````

The competitors randomly choose a door (e.g. 1,2 or 3).

``````# Step 1 choose a door
# i choose the following door
choice1 <- round(runif(n,min=1,max=3),0)``````

Set a variable that contains the main prize.

``````# generate random numbers where the car is
true.door<-round(runif(n,min=1,max=3),0)``````
``````# the organisar of the game does the following
open <- rep(NA,n)
for(i in 1:length(true.door)){
# 1=1
if(true.door[i]==choice1[i] & true.door[i]==1){
open[i] <- sample(c(2,3),1,replace=T)
# 2=2
}else if(true.door[i]==choice1[i] & true.door[i]==2){
open[i] <- sample(c(1,3),1,replace=T)
# 3=3
}else if(true.door[i]==choice1[i] & true.door[i]==3){
open[i] <- sample(c(1,2),1,replace=T)
}else{
open[i] <- 6-choice1[i]-true.door[i]
}
}
# make a dataframe
overview<- as.data.frame(cbind(choice1,open,true.door))
# stick to strategy one:
overview\$strategy1 <- ifelse(overview\$choice1==overview\$true.door,1,0)
# otherwise change to choice 2
overview\$choice2 <- 6-open-choice1
overview\$strategy2 <- ifelse(overview\$choice2==overview\$true.door,1,0)``````
``````# winning probability: strategy two
t<-barplot(c(sum(overview\$strategy1)/length(overview\$strategy1),
sum(overview\$strategy2)/length(overview\$strategy2)),width = 0.5,
names.arg=c("Strategy 1", "Strategy 2"),cex.names=0.8,ylim = c(0, 1),
xpd = FALSE,col="cornsilk",axes=F)
xval = seq(0, 1, 0.1)
axis(side = 2, at = xval, labels = FALSE, xpd=T)
axis(side = 2, at = xval, tick = FALSE, labels = xval, xpd=T,cex.axis=0.5)
text(t,c(0.4,0.65),label=c("~37%","~63%"),xpd=TRUE)`````` ``````# winning probability: strategy one
sum(overview\$strategy1)/length(overview\$strategy1)``````
``##  0.37347``
``````# winning probability: strategy two
sum(overview\$strategy2)/length(overview\$strategy2)``````
``##  0.62653``