%pylab inline # Helper functions def other(x): return 1-x def first(x): return x[0] def second(x): return x[1] # Print a 2x2 psayoff matrix def print_mat(payoffs): for i in range(2): for j in range(2): print payoffs[i][j], print # Find Nash equilibria (2x2 matrix) def Nash(payoffs): for i in range(2): for j in range(2): if first(payoffs[i][j])>=first(payoffs[other(i)][j]) and second(payoffs[i][j])>=second(payoffs[i][other(j)]): print moves[i],moves[j],' ===> resulting in:',payoffs[i][j] # prisoners dillema moves=['C','D'] payoffs=[[(-1,-1),(-3,0)],[(0,-3),(-2,-2)]] print_mat(payoffs) Nash(payoffs) # Choosing sides moves=['left','right'] payoffs=[[(10,10),(0,0)],[(0,0),(10,10)]] print_mat(payoffs) Nash(payoffs) # Pure coordination game moves=['Party','Home'] payoffs=[[(10,10),(0,0)],[(0,0),(5,5)]] print_mat(payoffs) Nash(payoffs) # Battle of the sexes moves=['Party','Home'] payoffs=[[(10,5),(0,0)],[(0,0),(5,10)]] print_mat(payoffs) Nash(payoffs) # Stag hunt moves=['Stag','Hare'] payoffs=[[(10,10),(0,8)],[(8,0),(7,7)]] print_mat(payoffs) Nash(payoffs) def mixed(payoff): top,bottom=payoffs (A,a),(C,c)=top (B,b),(D,d)=bottom print "Player 1" p = float((d-b))/(a+d-b-c) print p, "for ",moves[0] print 1-p, "for ",moves[1] print "Player 2" q = float((D-C))/(A+D-B-C) print q, "for ",moves[0] print 1-q, "for ",moves[1] moves=['Party','Home'] payoffs=[[(10,5),(0,0)],[(0,0),(5,10)]] print_mat(payoffs) mixed(payoffs) # Here is how you can get the computer to do the algebera for you. from sympy import * init_printing() a,b,c,d,p = symbols("a b c d p") A,B,C,D,q = symbols("A B C D q") # to find p where LEFT=RIGHT we solve LEFT-RIGHT=0 for p. print "p=" pretty_print(solve("p*a + (1-p)*b - p*c - (1-p)*d",p)) # to find q where UP=DOWN we solve UP-DOWN=0 for q. print "q=" pretty_print(solve("q*A + (1-q)*C - q*B - (1-q)*D",q)) r,p,s = symbols("r p s") solve(["-p+s-r+s", "r-s+r-p", "1-r-p-s"])