Chapter 9 Game
klippy::klippy(lang=c("r","python"))conda_install("r-reticulate", "requests", pip=T)from pathlib import Path
import requests
response = requests.get("https://www.dropbox.com/s/tli8ukqlnxd0z01/game.py?dl=1")
filepath = Path.cwd() / 'game.py'
filepath.write_text(response.text)
from pathlib import Path
sys.path.extend([str(Path.cwd())])9.1 Nash Equilibrium
| Actions | not confess | confess |
|---|---|---|
| not confess | (3,3) | (0,5) |
| confess | (5,0) | (1,1) |
from itertools import product
import numpy as np
import nashpy as nash
from game import Payoff, Game
# Extract A, B from normal form
A = np.array([[3,0],[5,1]]) # row player
B = np.array([[3,5],[0,1]]) # col player
nashgame = nash.Game(A,B)
nashgamestrategy: probability distribution on actions
[eq for eq in nashgame.support_enumeration()]actions = ('notConfess', 'confess')
payoff = Payoff(actions, actions)
payoff.notConfess_notConfess=(3,3)
payoff.confess_notConfess=(5,0)
payoff.notConfess_confess=(0,5)
payoff.confess_confess=(1,1)
payoff.show()
payoff.__validate__()
game = Game(payoff)[eq for eq in game.__nash__.support_enumeration()]9.2 R usage
- Type conversions: https://rstudio.github.io/reticulate/#type-conversions
library(econR)
library(reticulate)
pyfilepath <- . %//% "game.py"
source_python(pyfilepath)
import("numpy", as="np")
import("nashpy", as="nash")
nash <- import("nashpy")
actions = list("notConfess", "confess")
payoff = Payoff(actions, actions)
payoff$confess_confess = list(3, 3)
payoff$confess_notConfess = list(5, 0)
payoff$notConfess_confess = list(0, 5)
payoff$notConfess_notConfess = list(1, 1)
game1 = Game(payoff)
game1$payoff_matrices
equilibrium = game1$nash$support_enumeration()
game1$equilibrium()
game1$eqs9.3 module details
def validate(self):
#self=payoff
undefinedPayoffs = [key for key in self.__actionCombos__ if self.__dict__[key]==()]
flag_validation = undefinedPayoffs==[]
if not flag_validation:
print('The following payoffs undefined:')
print(undefinedPayoffs)
return(flag_validation)
validate(payoff)from itertools import product
import numpy as np
import nashpy as nash
class Payoff:
def __init__(self, actions1, actions2):
self.__actions1__ = actions1
self.__actions2__ = actions2
self.__actionCombos__ = ['{}_{}'.format(*combo) for combo in product(actions1, actions2)]
for cell in self.__actionCombos__:
self.__dict__[cell]=()
def show(self):
print({cell: self.__dict__[cell] for cell in self.__actionCombos__})
def __validate__(self):
#self=payoff
undefinedPayoffs = [key for key in self.__actionCombos__ if self.__dict__[key]==()]
flag_validation = undefinedPayoffs==[]
if not flag_validation:
print('The following payoffs undefined:')
print(undefinedPayoffs)
return(flag_validation)class Game:
def __init__(self, payoff):
# composed of
self.payoff = payoff
self.__dim__ = len(payoff.__actions1__), len(payoff.__actions2__)
self.__ncombo__ = np.prod(self.__dim__)
@property
def __payoffMatrixDict__(self):
nrow, ncol = self.__dim__
returnValue = { payoff.__actionCombos__[index]: value
for index, value in
enumerate(
product(range(nrow), range(ncol)))}
return returnValue
@property
def payoff_matrices(self):
self=game
payoff = self.payoff
nrow, ncol = len(payoff.__actions1__), len(payoff.__actions2__)
A = np.zeros((nrow, ncol))
B = np.zeros((nrow, ncol))
matrixDict = self.__payoffMatrixDict__
for key in payoff.__actionCombos__:
A[matrixDict[key]], B[matrixDict[key]] =payoff.__dict__[key]
return {"player1": A, "player2": B}
@property
def __nash__(self):
payoffMatrices = self.payoff_matrices
A, B = payoffMatrices["player1"], payoffMatrices["player2"]
nashgame = nash.Game(A,B)
return nashgameknitr::opts_chunk$set(message=F, eval=F)
klippy::klippy(lang=c("r","python"))