-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game_output.R
288 lines (254 loc) · 13.7 KB
/
Game_output.R
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
### 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 = "
# 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("Your payoff in each scenario, with ", num_of_firms-1," other firms:\n")
cat(vi , p0, Si, S0 , S_i, "[0, infinity]\n")
cat(vi, p1, Si, S1, S_i, "[0, ", K-S1-1, "]\n")
cat(vi, p2, Si, S2, S_i, "[0, ", K-S2-1, "]\n")
cat(vi, p1_1, Si, S1, S_i, " [", K-S1, " , infinity]\n")
cat(vi, p2_2, Si, S2, S_i,"[", K-S2, " , infinity]\n")
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: Si = ",
pareto$Si, " with vi(Si, S_i) = ",
pareto$Si_Payoff, ", if ", pareto$Scenario,
" & ", "Sj = ", pareto$Sj_left,
" , with payoff vj(Sj, Si) = ",
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: Si = ",
pareto$Si, " with vi(Si, S_i) = ",
pareto$Si_Payoff, ", if ", pareto$Scenario,
" & ", "Sj = [", pareto$Sj_left, " , ", pareto$Sj_right,
" ], with payoff vj(Sj, Si) = [ ",
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: Si = ",
pareto$Si, " with vi(Si, S_i) = ",
pareto$Si_Payoff, ", if ", pareto$Scenario,
" & ", "Sj = [", pareto$Sj_left, " , ",
pareto$Sj_right, " ], with payoff vj(Sj, Si) = [",
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(" ", "(", i, ")", "Nash Equilibrium ", i, ", for ", NEs$Scenario[i], ": \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")
}
}
}
#-----------------------------------------------------------------------------------------------------
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")
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")
print(game_payoffs(num_of_firms=i, best_resp=best_resp))
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")
cat("-------------------------------------------------------\n")
cat("\n")
cat("\n")
} else {
game_payoffs(num_of_firms=i, best_resp=best_resp)
}
}
}
#-----------------------------------------------------------------------------------------------------
#### run all
# game_simulations(9, best_resp="Yes")
# game_simulations(9, best_resp="No")