Objective
The goal of this code is to provide a basic framework for analyzing Nash Equilibria in a 2-player strategic game using payoff matrices. This is just an illustrative example and can be extended to more players or more complex games. The user can input the payoffs for both players in a matrix format, and the code will analyze the Nash Equilibria using a brute force method.
Explanation
Before diving into the code, let’s define the components:
- Players: In this case, we have two players. Each player chooses a strategy from a set of possible strategies.
- Payoff Matrix: The payoff matrix defines the reward or payoff each player receives depending on the combination of strategies chosen by both players.
- Nash Equilibrium: A Nash Equilibrium occurs when neither player can improve their payoff by unilaterally changing their strategy, given the other player’s strategy.
Code Breakdown
The following code is designed to:
- Input payoff matrices for two players.
- Calculate the best responses for each player.
- Identify the Nash Equilibria by checking for strategies where both players are playing their best responses to each other.
Excel VBA Code
Sub GameTheoryAnalysis()
' Declaring variables for matrices and dimensions
Dim Player1Strategies As Integer
Dim Player2Strategies As Integer
Dim PayoffMatrixPlayer1() As Double
Dim PayoffMatrixPlayer2() As Double
Dim BestResponsePlayer1() As Integer
Dim BestResponsePlayer2() As Integer
Dim NashEquilibrium As Boolean
Dim i As Integer, j As Integer
' Input Dimensions: Player 1 strategies and Player 2 strategies
Player1Strategies = InputBox("Enter the number of strategies for Player 1:")
Player2Strategies = InputBox("Enter the number of strategies for Player 2:")
' Initializing payoff matrices (These will be user input)
ReDim PayoffMatrixPlayer1(1 To Player1Strategies, 1 To Player2Strategies)
ReDim PayoffMatrixPlayer2(1 To Player1Strategies, 1 To Player2Strategies)
' Getting Payoff Matrix for Player 1
For i = 1 To Player1Strategies
For j = 1 To Player2Strategies
PayoffMatrixPlayer1(i, j) = InputBox("Enter the payoff for Player 1 at (" & i & ", " & j & "):")
Next j
Next i
' Getting Payoff Matrix for Player 2
For i = 1 To Player1Strategies
For j = 1 To Player2Strategies
PayoffMatrixPlayer2(i, j) = InputBox("Enter the payoff for Player 2 at (" & i & ", " & j & "):")
Next j
Next i
' Initialize best response arrays
ReDim BestResponsePlayer1(1 To Player1Strategies)
ReDim BestResponsePlayer2(1 To Player2Strategies)
' Looping to find the best responses for Player 1 and Player 2
' Best response for Player 1
For i = 1 To Player1Strategies
Dim MaxPayoff1 As Double
MaxPayoff1 = -999999 ' Setting an initially low value
For j = 1 To Player2Strategies
If PayoffMatrixPlayer1(i, j) > MaxPayoff1 Then
MaxPayoff1 = PayoffMatrixPlayer1(i, j)
BestResponsePlayer1(i) = j ' Store the best response column for Player 1
End If
Next j
Next i
' Best response for Player 2
For j = 1 To Player2Strategies
Dim MaxPayoff2 As Double
MaxPayoff2 = -999999 ' Setting an initially low value
For i = 1 To Player1Strategies
If PayoffMatrixPlayer2(i, j) > MaxPayoff2 Then
MaxPayoff2 = PayoffMatrixPlayer2(i, j)
BestResponsePlayer2(j) = i ' Store the best response row for Player 2
End If
Next i
Next j
' Outputting the Best Responses for both Players
MsgBox "Best responses for Player 1: " & Join(BestResponsePlayer1, ", ")
MsgBox "Best responses for Player 2: " & Join(BestResponsePlayer2, ", ")
' Checking for Nash Equilibria
NashEquilibrium = False
For i = 1 To Player1Strategies
For j = 1 To Player2Strategies
If BestResponsePlayer1(i) = j And BestResponsePlayer2(j) = i Then
MsgBox "Nash Equilibrium found at (" & i & ", " & j & ")"
NashEquilibrium = True
End If
Next j
Next i
If Not NashEquilibrium Then
MsgBox "No Nash Equilibrium found."
End If
End Sub
Step-by-Step Explanation
- Inputting the Payoff Matrices:
- The user is asked to input the number of strategies for both Player 1 and Player 2.
- The code then creates two payoff matrices: one for Player 1 and one for Player 2. Each matrix is populated with values from the user.
- Calculating Best Responses:
- The best response for each player is computed. A best response is simply the strategy that maximizes a player’s payoff, given the other player’s choice.
- For Player 1, the code loops through each of Player 1’s strategies and finds the strategy that maximizes the payoff for each strategy choice of Player 2.
- The same process is applied for Player 2.
- Checking for Nash Equilibrium:
- Once the best responses are calculated, the code checks each combination of strategies to see if both players are playing their best responses simultaneously.
- If both players are playing their best response to each other’s strategy, it is a Nash Equilibrium.
- If no Nash Equilibrium is found, the code informs the user.
Example Walkthrough
Let’s assume you have the following matrices for payoffs:
Player 1’s Payoff Matrix:
| Player 2 Strategy 1 | Player 2 Strategy 2 | |
| Player 1 Strategy 1 | 3 | 1 |
| Player 1 Strategy 2 | 0 | 2 |
Player 2’s Payoff Matrix:
| Player 2 Strategy 1 | Player 2 Strategy 2 | |
| Player 1 Strategy 1 | 2 | 0 |
| Player 1 Strategy 2 | 1 | 3 |
- The best response for Player 1 to Player 2’s Strategy 1 is Strategy 1 (because 3 > 0).
- The best response for Player 1 to Player 2’s Strategy 2 is Strategy 2 (because 2 > 1).
- The best response for Player 2 to Player 1’s Strategy 1 is Strategy 1 (because 2 > 0).
- The best response for Player 2 to Player 1’s Strategy 2 is Strategy 2 (because 3 > 1).
- Now, the code checks for a Nash Equilibrium by comparing the best responses. Since Player 1’s best response to Strategy 2 of Player 2 is Strategy 2 and Player 2’s best response to Strategy 2 of Player 1 is Strategy 2, this is a Nash Equilibrium.
Conclusion
This code provides a basic tool for analyzing a 2-player game’s Nash Equilibria using Excel VBA. By inputting the payoffs and strategies, the user can see the best responses for each player and identify any Nash Equilibria present. This code can be expanded and refined to handle more complex games, including games with more than two players or dynamic games.