mercredi 2 mars 2022

Creating a "Prisoner's Dilemma Table" in R

I am working with the R programming language. Recently, I thought of the following "game" to illustrate "mixed strategies and comparative advantages":

  • There are two Players: Player 1 and Player 2
  • There are two Coins: Coin 1 and Coin 2
  • Coin 1 lands on "Heads" with a probability of 0.5 and "Tails" with a probability of 0.5
  • Coin 2 lands on "Heads" with a probability of 0.7 and "Tails" with a probability of 0.3
  • If Coin 1 is "Heads", a score of -1 is obtained; if Coin 1 is "Tails", a score of +1 is obtained
  • If Coin 2 is "Heads", a score of -3 is obtained; if Coin 1 is "Tails", a score of +4 is obtained

In this game, Player 1 always starts first - Player 1 chooses either Coin 1 or Coin 2, flips the coin that they select and gets a "score". Then, Player 2 chooses either Coin 1 or Coin 2, flips the coin that they select and get a "score". The Player with the higher score wins, the Player with the lower score loses (a "tie" is also possible).

In this game, Coin 1 can be seen as a "medium risk and medium reward" option, whereas Coin 2 can be seen as a "high risk and high reward" option. Since Player 1 always starts first, Player 2 will always have an advantage - Player 2 gets to see what Player 1 chose:

  • If Player 1 chose the "high risk and high reward" option (Coin 2) and got a "bad result" (i.e. a big negative score), Player 2 does not need to choose the "high risk and high reward" option - Player 2 can win by selecting the "low risk and low reward" option (Coin 1).

  • If Player 1 chose the "high risk and high reward" option and got a "good result" (i.e. a big positive score), Player 2 now needs to choose the "high risk and high reward" option - Player 2 can only win by also selecting the "high risk and high reward" option. Player 2 needs to place all his "eggs in one basket" by selecting the "high risk and high reward" option if we wants to stand a chance of winning.

  • Similar logic can be used to rationalize the coin choice for Player 2 given that Player 1 has selected the "low risk and low reward" option.

I wanted to create a scenario where Player 1 and Player 2 are playing this game, but they do not have access to these probabilities upfront - instead, they only have access to 100 rounds (i.e. iterations) of this game. The goal is to "study" these iterations and build an optimal play strategy based on this iterations. Thus, I simulated 100 random iterations of this game in R:

score_coin_1 = c(-1,1)

score_coin_2 = c(-3, 4)


results <- list()

for (i in 1:100)

{

iteration = i


player_1_coin_choice_i = sample(2, 1, replace = TRUE)
player_2_coin_choice_i = sample(2, 1, replace = TRUE)

player_1_result_i = ifelse(player_1_coin_choice_i == 1, sample(score_coin_1, size=1, prob=c(.5,.5)),  sample(score_coin_2, size=1, prob=c(.7,.3)) )
player_2_result_i = ifelse(player_2_coin_choice_i == 1, sample(score_coin_1, size=1, prob=c(.5,.5)), sample(score_coin_2, size=1, prob=c(.7,.3)))

winner_i = ifelse(player_1_result_i > player_2_result_i, "PLAYER_1", ifelse(player_1_result_i == player_2_result_i, "TIE", "PLAYER_2"))

my_data_i = data.frame(iteration, player_1_coin_choice_i, player_2_coin_choice_i, player_1_result_i, player_2_result_i , winner_i )

 results[[i]] <- my_data_i

}



results_df <- data.frame(do.call(rbind.data.frame, results))

head(results_df)
  iteration player_1_coin_choice_i player_2_coin_choice_i player_1_result_i player_2_result_i winner_i
1         1                      1                      1                -1                 1 PLAYER_2
2         2                      1                      2                -1                -3 PLAYER_1
3         3                      2                      2                 4                -3 PLAYER_1
4         4                      1                      2                 1                -3 PLAYER_1
5         5                      2                      1                 4                 1 PLAYER_1
6         6                      2                      2                 4                -3 PLAYER_1



one_one <- results_df[which(results_df$player_1_coin_choice_i == 1 & results_df$player_2_coin_choice_i == 1), ]
one_two <- results_df[which(results_df$player_1_coin_choice_i == 1 & results_df$player_2_coin_choice_i == 2), ]
two_one <- results_df[which(results_df$player_1_coin_choice_i == 2 & results_df$player_2_coin_choice_i == 1), ]
two_two <- results_df[which(results_df$player_1_coin_choice_i == 2 & results_df$player_2_coin_choice_i == 2), ]

Then, I analyzed the results (e.g. "one_two_sum" = player 1 chose coin 1 and player 2 chose coin 2):

    library(dplyr)
    
    one_one_sum = data.frame(one_one %>% 
      group_by(winner_i) %>% 
      summarise(n = n()))
    
    one_two_sum = data.frame(one_two %>% 
      group_by(winner_i) %>% 
      summarise(n = n()))
    
    two_one_sum = data.frame(two_one %>% 
      group_by(winner_i) %>% 
      summarise(n = n()))
    
    two_two_sum = data.frame(two_two %>% 
      group_by(winner_i) %>% 
      summarise(n = n()))

For instance, suppose Player 1 chose "Coin 1":

one_one_sum
  winner_i  n
1 PLAYER_1  9
2 PLAYER_2 10
3      TIE  9

> one_two_sum
  winner_i  n
1 PLAYER_1 23
2 PLAYER_2  6
 

Based on these results, it appears that if Player 1 picks "Coin 1", Player 2 should also pick "Coin 1", seeing that he has a 10/29 chance of winning and a 9/29 chance of "tie" (overall, a 19/29 chance of not losing).

My Question : I would like to make a "2 x 2" table that contains all outcomes of the simulation and indicate the optimal strategy Player 2 should choose for each of Player 1 (as seen in "Prisoner's Dilemma" :

enter image description here

In my case, this table would look something like this:

enter image description here

Currently I make making and formatting this table in Microsoft Excel.

Is it possible to make this in R directly using the results of the simulation?

Thanks!




Aucun commentaire:

Enregistrer un commentaire