Chapter 9 Game
::klippy(lang=c("r","python")) klippy
conda_install("r-reticulate", "requests", pip=T)
from pathlib import Path
import requests
= requests.get("https://www.dropbox.com/s/tli8ukqlnxd0z01/game.py?dl=1")
response = Path.cwd() / 'game.py'
filepath
filepath.write_text(response.text)
from pathlib import Path
str(Path.cwd())]) sys.path.extend([
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
= np.array([[3,0],[5,1]]) # row player
A = np.array([[3,5],[0,1]]) # col player
B = nash.Game(A,B)
nashgame nashgame
strategy: probability distribution on actions
for eq in nashgame.support_enumeration()] [eq
= ('notConfess', 'confess')
actions = Payoff(actions, actions)
payoff =(3,3)
payoff.notConfess_notConfess=(5,0)
payoff.confess_notConfess=(0,5)
payoff.notConfess_confess=(1,1)
payoff.confess_confess
payoff.show()
payoff.__validate__()= Game(payoff) game
for eq in game.__nash__.support_enumeration()] [eq
9.2 R usage
- Type conversions: https://rstudio.github.io/reticulate/#type-conversions
library(econR)
library(reticulate)
<- . %//% "game.py"
pyfilepath source_python(pyfilepath)
import("numpy", as="np")
import("nashpy", as="nash")
<- import("nashpy")
nash
= list("notConfess", "confess")
actions = 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)
payoff
= Game(payoff)
game1 $payoff_matrices
game1= game1$nash$support_enumeration()
equilibrium $equilibrium()
game1$eqs game1
9.3 module details
def validate(self):
#self=payoff
= [key for key in self.__actionCombos__ if self.__dict__[key]==()]
undefinedPayoffs = undefinedPayoffs==[]
flag_validation 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
= [key for key in self.__actionCombos__ if self.__dict__[key]==()]
undefinedPayoffs = undefinedPayoffs==[]
flag_validation 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):
= self.__dim__
nrow, ncol = { payoff.__actionCombos__[index]: value
returnValue for index, value in
enumerate(
range(nrow), range(ncol)))}
product(return returnValue
@property
def payoff_matrices(self):
self=game
= self.payoff
payoff = len(payoff.__actions1__), len(payoff.__actions2__)
nrow, ncol = np.zeros((nrow, ncol))
A = np.zeros((nrow, ncol))
B = self.__payoffMatrixDict__
matrixDict for key in payoff.__actionCombos__:
=payoff.__dict__[key]
A[matrixDict[key]], B[matrixDict[key]] return {"player1": A, "player2": B}
@property
def __nash__(self):
= self.payoff_matrices
payoffMatrices = payoffMatrices["player1"], payoffMatrices["player2"]
A, B = nash.Game(A,B)
nashgame return nashgame
::opts_chunk$set(message=F, eval=F)
knitr::klippy(lang=c("r","python")) klippy