Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168691
Image: ubuntu2004

Code that defines a very simple function entitled "TwobyTwoNashEquilibria" and uses simple algebra to compute the equilibria for a 2 by 2 bi-matrix game.

def TwobyTwoNashEquilibria(A,B=0): "Computes the Equilibria for a general 2 by 2 bi matrix game defined by $(A,B)\in\mathbb{R}^{2\\times2}\\times\mathbb{R}^{2\\times 2}$. If B is omitted from input, a zero sum game is assumed." A=matrix(A) if B==0: B=-A else: B=matrix(B) if max(A[0]-A[1])<0: # This implies that the second row strategy dominates the first row strategy p=0 if B[1,0]<=B[1,1]: q=0 else: q=1 elif min(A[0]-A[1])>0: # This implies that the first row strategy dominates the second row strategy p=1 if B[0,0]<=B[0,1]: q=0 else: q=1 elif max(transpose(B)[0]-transpose(B)[1])<0: # This implies that the second column strategy dominates the first column strategy q=0 if A[0,1]<=A[1,1]: p=0 else: p=1 elif min(transpose(B)[0]-transpose(B)[1])>0: # This implies that the first column strategy dominates the second column strategy q=0 if A[0,0]<=A[1,0]: p=0 else: p=1 else: #This computes 0<p<1 and 0<q<1 that ensure equilibria in the case of no dominated strategies. p=(B[1,1]-B[1,0])/(B[0,0]+B[1,1]-B[0,1]-B[1,0]) q=(A[1,1]-A[0,1])/(A[0,0]+A[1,1]-A[0,1]-A[1,0]) return [(p,1-p),(q,1-q)] TwobyTwoNashEquilibria?

File: /tmp/tmpXNMwn5/___code___.py

Type: <type ‘function’>

Definition: TwobyTwoNashEquilibria(A, B=0)

Docstring:

Computes the Equilibria for a general 2 by 2 bi matrix game defined by (A,B)\in\mathbb{R}^{2\times2}\times\mathbb{R}^{2\times 2}. If B is omitted from input, a zero sum game is assumed.

Obtaining the equilibria strategies for: matching pennies

TwobyTwoNashEquilibria([[1,-1],[-1,1]])
[(1/2, 1/2), (1/2, 1/2)]

Obtaining the equilibria strategies for: prisoner's dilemma

TwobyTwoNashEquilibria([[4,1],[5,2]],[[4,5],[1,2]])
[(0, 1), (0, 1)]

Obtaining the equilibria strategies for the box kick game of this blog post.

TwobyTwoNashEquilibria([[20,80],[95,70]])
[(5/17, 12/17), (2/17, 15/17)]