-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game Output.Rmd
360 lines (313 loc) · 17.2 KB
/
Game Output.Rmd
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
---
title: "Appendix"
author: "Game Estimations"
output:
pdf_document: default
html_document: default
---
### *Denote*:
+ *$P_j$*: is any player that is not $P_i$
+ *Column $Sj$*: Range of possible strategies for Player j, which whenever summed with the other players' strategies, satisfy the given scenario. It can take any value in that range that is also 0, 1 or 2. For example, in *Table 3: Game Payoffs* we have 3 firms playing the game, where the second row has $Sj=[0,2]$, which is interpreted as the available strategies 0, 1 and 2 for all other players that are not Player i, such that whenever combined they satisfy $\sum_{i=1}^n S_i < K \leftrightarrow \sum_{i=1}^n S_i < 4$ (because K = number of firms + 1 and $Si=1$). Thus, we define Player 1's strategy as $S_1=1$, Player 2's strategy as $S_2=[0,2]$ and Player 3's strategy as $S_3=[0,2]$; then $(S_1, S_2, S_3) = \{(1,0,0), (1,1,0), (1,1,1), (1,0,1), (1,0,2), (1,2,0) \}$ are suggested in $Sj=[0,2]$ because they satisfy $S_1+S_2+S_3<4 \leftrightarrow \sum_{i=1}^nS_i < 4$.
+ *Column $Vj(Sj, Si)$*: Range of possible payoffs for Player j.
```{r setup, include=FALSE}
library(pander)
panderOptions('knitr.auto.asis', FALSE)
library(knitr)
library(kableExtra)
library(crayon)
```
```{r, include=FALSE}
### Functions ###
# By: Dayanara M. Diaz Vargas #
### ###
#---------------------------------------------------------------------------------------------
game_payoffs <- function(num_of_firms, p=0.5, K="none", S0=0, S1=1, S2=2, best_resp="Yes"){
# set K
if(K == "none"){
K <- num_of_firms +1 # resource
}
# Probabilities
pr_e <- floor(num_of_firms/2)/num_of_firms
pr_n <- 1-pr_e
# Payoffs
p0 <- S0
p1 <- S1-p
p2 <- S2-p
p1_1 <- (S1-p)*pr_e+(-p)*pr_n
p2_2 <- (S2-p)*pr_e+(-p)*pr_n
# recurrent words
vi <- "You get = "
Si <- ", if you choose = "
S_i <- " & the rest of the firms choose in aggregate = "
# return
if(best_resp == "Yes"){
# also return payoffs
payoffs <- data.frame(
Scenario = c("Sum_Si<K", "Sum_Si<K", "Sum_Si<K", "Sum_Si=>K", "Sum_Si=>K", "Sum_Si=>K"),
Si = c(0, 1, 2, 1, 2, 0),
Sj_left = c(S0, S0, S0,
ifelse(((num_of_firms-2)*S2+S1*2)>=K, S1, S2),
ifelse(((num_of_firms-2)*S2+S1*2)>=K, S1, S2),
ifelse(((num_of_firms-2)*S2+S1*2)>=K, S1, S2)),
Sj_right = c(S2,
if((num_of_firms-2)*S0+S2+S1<K){
S2
} else if((num_of_firms-2)*S0+S1+S1<K){
S1
} else {
S0
},
if((num_of_firms-2)*S0+S2+S2<K){
S2
} else if((num_of_firms-2)*S0+S1+S2<K){
S1
} else {
S0
},
S2, S2, S2),
Si_Payoff = c(p0, p1, p2, p1_1, p2_2, p0),
Sj_Payoff_left = c(p0, p0, p0,
ifelse(((num_of_firms-2)*S2+S1*2)>=K, p1_1, p2_2),
ifelse(((num_of_firms-2)*S2+S1*2)>=K, p1_1, p2_2),
ifelse(((num_of_firms-2)*S2+S1*2)>=K, p1_1, p2_2)),
Sj_Payoff_right = c(p2,
if((num_of_firms-2)*S0+S2+S1<K){
p2
} else if((num_of_firms-2)*S0+S1+S1<K){
p1
} else {
p0
},
if((num_of_firms-2)*S0+S2+S2<K){
p2
} else if((num_of_firms-2)*S0+S1+S2<K){
p1
} else {
p0
},
p2_2, p2_2, p2_2)
)
return(payoffs)
} else {
cat("\n")
cat("---Number of firms = ", num_of_firms, "and K = ", num_of_firms+1,"---\n")
cat("\n")
cat(" Your expected payoff in each scenario, with ", num_of_firms-1," other firms:\n")
cat("\n")
cat(" ", vi , p0, Si, S0 , S_i, "[ 0 ,", S2, "]\n")
cat("\n")
cat(" ", vi, p1, Si, S1, S_i, "[ 0 , ", K-S1-1, "]\n")
cat("\n")
cat(" ", vi, p2, Si, S2, S_i, "[ 0 , ", K-S2-1, "]\n")
cat("\n")
cat(" ", vi, p1_1, Si, S1, S_i, " [", K-S1 ,",", S2*(num_of_firms-1), "]\n")
cat("\n")
cat(" ", vi, p2_2, Si, S2, S_i,"[", K-S2 ,",", S2*(num_of_firms-1), "]\n")
cat("\n")
cat("\n")
cat("____________________________________________")
cat("\n")
cat("\n")
}
}
#----------------------------------------------------------------------------------------------
game_pareto <- function(num_of_firms, K="none"){
# set K
if(K == "none"){
K <- num_of_firms +1 # resource
}
# payoffs
pays <- game_payoffs(num_of_firms=num_of_firms, best_resp="Yes")
# pareto optimal strategy
pareto <- pays[which((pays$Si_Payoff) == max(pays$Si_Payoff)), ]
# number of other firms that choose each strategy
nl <- 1 # num. of firms that choose the minimum strategy
nr <- 0 # num. of firms that choose the maximum strategy
nm <- 0 # num. of firms that choose the median strategy
m <- if(pareto$Sj_right==2 & pareto$Sj_left==0){
m <- 1
} else {
m <- "None"
}
if(m == "None"){
nr <- ifelse(pareto$Sj_right==0,
floor((K - pareto$Si - nl*pareto$Sj_left)/1)-1,
floor((K - pareto$Si - nl*pareto$Sj_left)/pareto$Sj_right)-1)
nl <- num_of_firms-nr-1
} else {while((nr*pareto$Sj_right+pareto$Si+nl*pareto$Sj_left+nm*m< K) & (nr+nl+nm+1<num_of_firms)){
nm <- ifelse((nr*pareto$Sj_right+pareto$Si+nl*pareto$Sj_left+(nm+1)*m<K) &
(nr+nl+nm+1<num_of_firms) &
(((ifelse(pareto$Sj_right==0,
floor((K - pareto$Si - nl*pareto$Sj_left - nm*m)/1),
floor((K - pareto$Si - nl*pareto$Sj_left - nm*m)/pareto$Sj_right)))*pareto$Sj_right+pareto$Si+nl*pareto$Sj_left+(nm)*m)<K),
0, nm+1)
if((nr*pareto$Sj_right+pareto$Si+nl*pareto$Sj_left+nm*m<K) & (nr+nl+nm+1<num_of_firms)){
nr <- ifelse(pareto$Sj_right==0,
floor((K - pareto$Si - nl*pareto$Sj_left - nm*m)/1),
floor((K - pareto$Si - nl*pareto$Sj_left - nm*m)/pareto$Sj_right))
if((nr*pareto$Sj_right+pareto$Si+nl*pareto$Sj_left+nm*m< K) & (nr+nl+nm+1<num_of_firms)){
nl <- num_of_firms-nr-nm-1
}
}}
}
# print result
if(m == "None" & pareto$Sj_left==pareto$Sj_right){
cat("Pareto Optimality is reached when: $S_i$ = ",
pareto$Si, " with $V_i(S_i, S_j)$ = ",
pareto$Si_Payoff, ", if ", ifelse(pareto$Scenario=="Sum_Si<K",
"$\\sum_{i=1}^n S_i < K$", "$\\sum_{i=1}^n S_i \\geq K$"),
" & ", "$S_j$ = ", pareto$Sj_left,
" , with payoff $V_j(S_j, S_i)$ = ",
pareto$Sj_Payoff_left, ". With ",
nl, ifelse(nl==1, "other firm ", "other firms "), "choosing ",
pareto$Sj_left," and viceversa.\n")
cat("\n")
cat("That is: \n")
if(pareto$Sj_left==pareto$Si){
cat(nl+1, " firms choose ", pareto$Sj_left, " units", ", each with a payoff of ", pareto$Sj_Payoff_left,".\n")
} else {
cat(nl, "firms choose ", pareto$Sj_left, " units, with a payoff of ", pareto$Sj_Payoff_left, ". \n", "1 firm chooses ", pareto$Si, " units, with a payoff of ", pareto$Si_Payoff, ". \n")
}
} else if (m == "None" & pareto$Sj_left!=pareto$Sj_right){
cat("Pareto Optimality is reached when: $S_i$ = ",
pareto$Si, " with vi(Si, S_i) = ",
pareto$Si_Payoff, ", if ", ifelse(pareto$Scenario=="Sum_Si<K",
"$\\sum_{i=1}^n S_i < K$", "$\\sum_{i=1}^n S_i \\geq K$"),
" & ", "$S_j$ = [", pareto$Sj_left, " , ", pareto$Sj_right,
" ], with payoff $V_j(S_j, S_i)$ = [ ",
pareto$Sj_Payoff_left, " , ",
pareto$Sj_Payoff_right," ]. With ",
nl, ifelse(nl==1, "other firm choosing", "other firms choosing"),
pareto$Sj_left, " and ", nr ,
ifelse(nr==1, "other firm choosing", "other firms choosing"),
pareto$Sj_right," and viceversa.\n")
cat("\n")
cat("That is: \n")
if((pareto$Sj_left==pareto$Si) | (pareto$Sj_left==pareto$Si)){
cat(ifelse(pareto$Sj_left==pareto$Si, nl+1, nl), " firms choose ", pareto$Sj_left, " units", ", with a payoff of ", pareto$Sj_Payoff_left,
". \n", ifelse(pareto$Sj_left==pareto$Si, nr, nr+1), "firms choose ", pareto$Sj_right, " units, with a payoff of ", pareto$Sj_Payoff_right,
". \n")
} else {
cat(nl, "firms choose ", pareto$Sj_left, " units, with a payoff of ", pareto$Sj_Payoff_left,
". \n", "1 firm chooses ", pareto$Si, " units, with a payoff of ", pareto$Si_Payoff,
". \n", nr, "firms choose", pareto$Sj_right, "units, with a payoff of ", pareto$Sj_Payoff_right,
". \n")
}
} else {
cat("Pareto Optimality is reached when: $S_i$ = ",
pareto$Si, " with $V_i(S_i, S_j)$ = ",
pareto$Si_Payoff, ", if ", ifelse(pareto$Scenario=="Sum_Si<K",
"$\\sum_{i=1}^n S_i < K$", "$\\sum_{i=1}^n S_i \\geq K$"),
" & ", "$S_j$ = [", pareto$Sj_left, " , ",
pareto$Sj_right, " ], with payoff $V_j(S_j, S_i)$ = [",
pareto$Sj_Payoff_left, " , ",
pareto$Sj_Payoff_right, "]. With ",
nr, ifelse(nr==1, "other firm ", "other firms "), "choosing ", pareto$Sj_right,
"; ", nm, ifelse(nm==1, "other firm choosing", "other firms choosing"), m,
", and ", nl, ifelse(nl==1, "other firm", "other firms"),
" choosing", pareto$Sj_Payoff_left,", and viceversa.\n")
cat("\n")
cat("That is: \n")
if((pareto$Sj_left==pareto$Si) | (pareto$Sj_left==pareto$Si)){
cat(ifelse(pareto$Sj_left==pareto$Si, nl+1, nl), " firms choose ", pareto$Sj_left, " units", ", with a payoff of ", pareto$Sj_Payoff_left,
". \n", nm, " firms choose ", m, " units, with a payoff of ", 0,
". \n", ifelse(pareto$Sj_left==pareto$Si, nr, nr+1), "firms choose ", pareto$Sj_right, " units, with a payoff of ", pareto$Sj_Payoff_right,
". \n")
} else {
cat(ifelse(pareto$Sj_left==pareto$Si, nl+1, nl), "firms choose ", pareto$Sj_left, " units, with a payoff of ", pareto$Sj_Payoff_left,
". \n", nm, " firms choose ", m, " units, with a payoff of ", 0,
". \n", ifelse(pareto$Sj_left==pareto$Si, nr, nr+1), "firms choose", pareto$Sj_right, "units, with a payoff of ", pareto$Sj_Payoff_right,
". \n")
}
}
}
#-----------------------------------------------------------------------------------------------------
game_NE <- function(num_of_firms, K="none") {
num_of_firms=3
K="none"
# set K
if(K == "none"){
K <- num_of_firms +1 # resource
}
# payoffs
pays <- game_payoffs(num_of_firms=num_of_firms, best_resp="Yes")
# Nash Equilibriums
NEs <- pays[(pays$Si == pays$Sj_right | pays$Si == pays$Sj_right | (pays$Si == pays$Sj_right-pays$Sj_left-1 & pays$Si == 1)) & (pays$Scenario == "Sum_Si<K" | pays$Scenario == "Sum_Si=>K"),]
# results
cat("There ", ifelse(nrow(NEs)==1, " is ", " are "), nrow(NEs), ifelse(nrow(NEs)==1, " Nash Equilibrium ", " Nash Equilibriums "), ".\n")
cat("\n")
for(i in 1:nrow(NEs)){
cat("\n")
cat(" ", "(", i, ")", "Nash Equilibrium ", i, ", for ", ifelse(NEs$Scenario[i]=="Sum_Si<K",
"$\\sum_{i=1}^n S_i < K$", "$\\sum_{i=1}^n S_i \\geq K$"), ": \n")
if(NEs$Sj_left[i]==NEs$Sj_right[i]){
cat(" ", num_of_firms, " firms choose ", NEs$Sj_left[i], " units, each with a payoff of ", NEs$Sj_Payoff_left[i],". \n")
cat("\n")
} else if (NEs$Sj_left[i]!=NEs$Sj_right[i] & NEs$Sj_left[i]==NEs$Si[i]){
cat(" ", num_of_firms, " firms choose ", NEs$Sj_left[i], " units, each with a payoff of ", NEs$Sj_Payoff_left[i],". \n")
cat("\n")
} else if (NEs$Sj_left[i]!=NEs$Sj_right[i] & NEs$Sj_right[i]==NEs$Si[i]){
cat(" ", num_of_firms, " firms choose ", NEs$Sj_right[i], " units, each with a payoff of ", NEs$Sj_Payoff_right[i],". \n")
cat("\n")
} else {
cat(" ", num_of_firms, " firms choose ", NEs$Si[i], " units, each with a payoff of ", NEs$Si_Payoff[i],". \n")
cat("\n")
}
}
}
#-----------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------
```
```{r, message = FALSE, echo= FALSE, warning = FALSE, results='asis'}
game_simulations <- function(num_simulations, best_resp="Yes", pareto="Yes", NE="Yes"){
for (i in 2:num_simulations) {
if(best_resp=="Yes" & pareto == "No"){
cat("Game of ", i, " firms:")
cat("\n")
cat("Game of ", i, " firms:")
cat("\n")
print(game_payoffs(num_of_firms=i, best_resp=best_resp))
cat("\n")
} else if (best_resp=="Yes" & pareto == "Yes" & NE=="Yes") {
cat("Game of ", i, " firms:")
cat("\n")
temp <- game_payoffs(num_of_firms=i, best_resp=best_resp)
temp <- data.frame(lapply(temp[,-1], function(x) round(x, 3)))
temp$Scenarios <- c('$ \\sum_{i=1}^n S_i < K$', '$ \\sum_{i=1}^n S_i < K$', '$ \\sum_{i=1}^n S_i < K$', '$ \\sum_{i=1}^n S_i \\geq K$', '$ \\sum_{i=1}^n S_i \\geq K$', '$ \\sum_{i=1}^n S_i \\geq K$')
temp$Sj <- paste("[", temp$Sj_left, ",", temp$Sj_right, "]")
temp$Sj_payoff <- paste("[", temp$Sj_Payoff_left, ",", temp$Sj_Payoff_right, "]")
temp <- temp[, -c(2,3,5,6)]
temp <- cbind(temp[,3], temp[,-3])
colnames(temp) <- c("Scenarios", "Si", "Vi(Si, Sj)", paste0("Sj", footnote_marker_number(1)), paste0("Vj(Sj, Si)", footnote_marker_number(2)))
print(kable_styling(kbl(temp, caption = "Game Payoffs", align = "l", booktabs = T, escape = F) %>%
kable_paper(latex_options = c("striped"), full_width = F) %>%
kable_styling(latex_options = "hold_position") %>%
footnote(number = c("Sj = Range of possible strategies for Player j.", "Vj(Sj, Si) = Range of possible payoffs for Player j.")) %>%
row_spec(0, bold = T)%>%
column_spec(1, bold = T)))
cat("\n")
cat("---- Pareto Equilibrium --- ")
cat("\n")
game_pareto(num_of_firms=i)
cat("\n")
cat("---- Nash Equilibrium --- ")
cat("\n")
game_NE(num_of_firms=i)
cat("\n\n\\pagebreak\n")
} else {
game_payoffs(num_of_firms=i, best_resp=best_resp)
}
}
}
```
## Game Predictions
```{r, message = FALSE, echo= FALSE, warning = FALSE, results='asis'}
#### run all
game_simulations(15)
```
## Reference for Students
```{r, message = FALSE, echo= FALSE, warning = FALSE, results='asis'}
#### run all
game_simulations(15, best_resp="No")
```