-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgamblersRuin.R
More file actions
35 lines (26 loc) · 822 Bytes
/
gamblersRuin.R
File metadata and controls
35 lines (26 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
rm(list = ls()); #remove previously saved objects from current workspace
simGamblersRuin <- function(gamblerInitialCap, bankInitialCap, theta){
#gambler's initial capital X0 = a
a = gamblerInitialCap;
#bank's initial capital B
B = bankInitialCap;
#total capital involved in the game
K = a + B;
Xn = numeric();
Xn[1] = a;
i = 2;
while (Xn[i-1] > 0 && Xn[i-1] < K) {
Xn[i] = Xn[i-1] + sample(c(1, -1), size = 1, prob = c(theta, 1-theta));
i = i + 1;
}
return(Xn);
}
gamblersRuinResults <- function(gamblerInitialCap, bankInitialCap, theta){
Xn = simGamblersRuin(gamblerInitialCap, bankInitialCap, theta);
if(Xn[length(Xn)] == 0){
print('Gambler is Bankrupt!');
} else {
print('Bank is Bankrupt!');
}
return(Xn);
}