Please use D2L to turn in both the DOC or Word output and your R Markdown file in.

Recreate the traveling politician example with seven islands that have the following population totals.

```
set.seed(03022018)
population.totals <- c(123,942,234,553,890,1458,99)
library(knitr)
kable(data.frame(island = as.character(1:7), pop = population.totals))
```

island | pop |
---|---|

1 | 123 |

2 | 942 |

3 | 234 |

4 | 553 |

5 | 890 |

6 | 1458 |

7 | 99 |

Using the code from Lecture, this can be packaged as a function:

```
Politician_explore <- function(population.totals, num.steps){
# function for exploring politician
# ARGS: population totals: vector of populations totals for N islands, ordered east-to-west
# num.steps - number of steps for politician to make
# RETURNS: vector of length num.steps, where each value is the location at time i
# also creates plot with relative population and politician steps if all islands have been visited
par(mfcol=c(2,1))
num.islands <- length(population.totals)
relative.population <- population.totals / sum(population.totals)
# initialize politician
island.location <- rep(1,num.steps) # start at first island
# algorithm
for (i in 2:num.steps){
direction <- sample(c('right','left'),1)
if (direction == 'right'){
proposed.island <- island.location[i-1] + 1
if (proposed.island == (num.islands + 1)) {
island.location[i] <- island.location[i-1] #no island n+1 exists, stay at island n
} else {
prob.move <- relative.population[proposed.island] / relative.population[island.location[i-1]]
if (runif(1) < prob.move){
# move
island.location[i] <- proposed.island
} else{
#stay
island.location[i] <- island.location[i-1]
}
}
}
if (direction == 'left'){
proposed.island <- island.location[i-1] - 1
if (proposed.island == 0) {
island.location[i] <- island.location[i-1] #no island 0 exists, stay at island 1
} else {
prob.move <- relative.population[proposed.island] / relative.population[island.location[i-1]]
if (runif(1) < prob.move){
# move
island.location[i] <- proposed.island
} else{
#stay
island.location[i] <- island.location[i-1]
}
}
}
}
if (length(table(island.location)) < num.islands){
message('all islands have not been visited, plots suppressed')
} else {
barplot(relative.population, names.arg = as.character(1:num.islands), main='Relative Population')
barplot(table(island.location) / num.steps, names.arg = as.character(1:num.islands),
main=paste(num.steps, ' Politician Steps'))
}
return(island.location)
}
```

Let your politican walk for 100 steps. Do you think this is an adequate amount of time for the politician to spend time proportional to the population of each island? why or why not?

`locations <- Politician_explore(population.totals, 100)`

`## all islands have not been visited, plots suppressed`

`table(locations)`

```
## locations
## 1 2 3 4 5 6
## 8 58 13 8 9 4
```

The politician has not visited all of the islands so more steps seem necessary.

Let your politician walk for 100,000 steps and recreate the 4 plots from lecture:

`locations <- Politician_explore(population.totals, 100000)`

- relative population of islands
- relative time spent in each island
- first 15 steps
- first 100 steps.

```
par(mfcol=c(1,1))
plot(locations[1:15], type='b', ylab = 'island', xlab = 'time', ylim=c(1,7), main =
"Politician's first 15 steps", pch = 16)
```

```
plot(locations[1:100], type='b', ylab = 'island', xlab = 'time', ylim=c(1,7), main =
"Politician's first 100 steps", pch = 16)
```

Do you believe 100,000 steps is sufficient for the politician to spend time proportional to the population of each island? why or why not?

Yes, all of the islands have been visited in what looks roughly to be propoortional to the population of the islands.

Assume your politician starts on island 1 on day 1, given this algorithm, compute the following probabilities, where \(l(i)\) is the politician location on day \(i\).

- \(Pr[l(1) = 1] = 1\)
- \(Pr[l(2) = 1] = 0\)
- \(Pr[l(2) = 2] = \frac{1}{2}\) - always make move when proposing to go to island 2
- \(Pr[l(3) = 2] = \left(\frac{1}{2} \times \frac{1}{2} \times \left(1- \frac{123}{942}\right)\right) + \left(\frac{1}{2} \times \frac{1}{2} \times \left(1-\frac{234}{942}\right)\right) + \left(\frac{1}{2} \times\frac{1}{2} \right) \approx\) 0.655

There are three ways to get to island 2 on day 3:

```
1. Move to island two on day 2, propose to move to island 1 on day 3 and reject move.
2. Move to island two on day 2, propose to move to island 3 on day 3 and reject move.
3. Stay at island 1 on day 2 (by proposing to go to island 0), then propose to move to island 2 on day 2.
```

- \(Pr[l(3) = 5] = 0\)
- finally take a guess at \(Pr[l(100,000) = 3] = \frac{234}{4299}\) = 0.054