Skip to main content
Mathematics LibreTexts

3.3: Using Sage to Graph Lines and Solve Equations

  • Page ID
    82769
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In this section we will use technology to graph lines and solve for the intersection point. In particular, we will use an open online resource called Sage.

    Let's continue to consider the game given in Table \(3.2.1\) by

    Table \(3.3.1\): Example \(3.1.1\) with Named Strategies
    C D
    A \(1\) \(0\)
    B \(-1\) \(2\)

    Recall, our goal is to determine how often Player 1 should play A and how often she should play B.

    We will follow the same steps as in Section 3.2. Let \(p\) be the probability that Player 1 plays B. Let \(m\) be the payoff to Player 1. Since we are trying to find a mixed strategy for Player 1, we will pick a strategy for Player 2 and try to determine the possible payoffs for Player 1.

    Let us determine some pairs \((p, m)\text{.}\)

    Step 1: Assume Player 2 plays pure strategy \(C\text{.}\)

    Step 1a: If Player 1 always plays \(A\text{,}\) then we are considering the strategy pair \([A, C]\text{.}\) Since Player 1 never plays \(B\text{,}\) \(p=0\text{.}\) The payoff to Player 1 for \([A, C]\) is \(m=1\text{.}\) Thus, for the strategy pair \([A, C]\) we get \((p, m)=(0, 1)\text{.}\)

    Step 1b: If Player 1 always plays \(B\text{,}\) then we are considering the strategy pair \([B, C]\text{.}\) Since Player 1 always plays \(B\text{,}\) \(p=1\text{.}\) The payoff to Player 1 for \([A, C]\) is \(m=-1\text{.}\) Thus, for the strategy pair \([B, C]\) we get \((p, m)=(1, -1)\text{.}\)

    Step 1c: Now we want to know what Player 1's payoff will be as she varies the probability, \(p\text{,}\) with which she plays \(B\text{.}\) We can draw a graph where the \(x\)-axis represents to probability with which she plays B (\(p\)) and the \(y\)-axis represents the expected payoff (\(m\)). Thus, when Player 1 plays only \(A\text{,}\) she is playing \(B\) with probability \(0\); when Player 1 plays only B, she is playing B with probability \(1\). It might be easier to remember if you label your graph as in Figure \(3.2.1\).

    Step 1d: Now we can use Sage to plot the points we determined in Step 1a and Step 1b and the line between them. This line represents Player 2's pure strategy \(C\text{.}\) See Figure \(3.2.2\). Click on the “run” button to plot the line between the points \((0, 1)\) and \((1, -1)\text{.}\)

    u=(0,1);
    v=(1,-1);
    show(line([u, v], thickness=2, color=('blue'))+point(u, color=('blue'), pointsize=70)+point(v, color=('blue'), pointsize=70), figsize=4)

    Before moving on, let's again, make sure we understand what this line represents. Any point on it represents the expected payoff to Player 1 as she varies her strategy, assuming Player 2 only plays \(C\). In this case, we can see that as she plays \(B\) more often, her expected payoff goes down. You can now use this Sage cell to plot any line for Player 2's pure strategy \(C\text{.}\) Just edit the values for the points \(u\) and \(v\text{.}\) Go ahead and try it! (Don't worry the original values will reset when you refresh the page.)

    Now let's do the same thing, assuming Player 2 plays only \(D\text{.}\)

    Step 2: Assume Player 2 plays pure strategy \(D\text{.}\)

    Step 2a: If Player 1 always plays \(A\text{,}\) then we are considering the strategy pair \([A, D]\text{.}\) Since Player 1 never plays \(B\text{,}\) \(p=0\text{.}\) The payoff to Player 1 for \([A, D]\) is \(m=0\text{.}\) Thus, for the strategy pair \([A, D]\) we get \((p, m)=(0, 0)\text{.}\)

    Step 2b: If Player 1 always plays \(B\text{,}\) then we are considering the strategy pair \([B, D]\text{.}\) Since Player 1 always plays \(B\text{,}\) \(p=1\text{.}\) The payoff to Player 1 for \([B, D]\) is \(m=2\text{.}\) Thus, for the strategy pair \([B, D]\) we get \((p, m)=(1, 2)\text{.}\)

    Step 2c: Now, on our same graph from Step 1, we can plot the points we determined in Step 2a and Step 2b. We will connect them with a line representing Player 2's pure strategy \(D\text{.}\) See Figure \(3.2.3\).

    u=(0,1);
    v=(1,-1);
    w=(0,0);
    z=(1, 2);
    show(line([u, v], thickness=2, color=('blue'))+point(u, color=('blue'), pointsize=70)
      +point(v, color=('blue'), pointsize=70)
      +line([w, z], thickness=2, color=('red'))
      +point(w, color=('red'), pointsize=70)+point(z, color=('red'), pointsize=70), figsize=4)

    Now we can see that if Player 2 plays only \(D\text{,}\) then Player 1 does best by playing only \(B\text{.}\) Again, you can use this Sage cell to plot both Player 2's pure strategies. Points \(u\) and \(v\) are for strategy \(C\text{,}\) while points \(w\) and \(z\) are for strategy \(D\text{.}\)

    As we saw in Section 3.2, for each choice of \(p\text{,}\) the top line represents the highest expected value for Player 1; the bottom line represents the lowest expected value for Player 1; the area between the lines represents the possible expected values for Player 1. Thus, Player 1 wants to maximize the minimum expected value, which means she wants to find the maximin strategy. And, as we saw in Section 3.2, the maximin strategy occurs at the intersection of the two lines.

    Step 3: Find the intersection of the two lines.

    Step 3a: Find the equation for Line C. This is the line passing through the points \((0, 1)\) and \((1, -1)\text{.}\) It has slope \(-2\) and \(y\)-intercept \(1\). Thus, it has equation \(m=-2p+1\text{.}\) (Recall the \(x\)-axis represents probability \(p\) and the \(y\)-axis represents expected payoff \(m\text{.}\))

    Step 3b: Find the equation for Line D. This is the line passing through the points \((0, 0)\) and \((1, 2)\text{.}\) It has slope \(2\) and \(y\)-intercept \(0\). Thus, it has equation \(m=2p\text{.}\)

    Step 3c: Now we can use technology to help us solve the equations.

    p, m = var('p, m')
    solve([m==-2*p+1, m==2*p], p, m)[0]

    The solution for Player 1 is \((p, m)\text{.}\) Where \(p\) is the probability Player 1 plays B, and \(m\) is the expected payoff to Player 1.

    We can use this Sage cell to solve for \(p\) and \(m\) for any \(2\times 2\) game by editing the equations \(m==-2*p+1, m==2*p\text{.}\)

    Step 4: Determine Player 1's maximin mixed strategy. Recalling that \(p\) is the probability that Player 1 plays \(B\text{,}\) we know that Player 1 will play \(B\) with probability \(\dfrac{1}{4}\), and thus, play A with probability \(\dfrac{3}{4}\). The expected payoff for Player 1, \(m\text{,}\) is \(\dfrac{1}{2}\). It is important to check the algebraic solution with where the intersection point appears on the graph. Although we are using technology to help us graph and solve for the intersection point, we need to be able to catch any errors we make entering the information into Sage.

    We have seen that we can use the same matrix with Player 1's payoffs to find the strategy for Player 2. Using the same game matrix as above:

    \(\begin{bmatrix}1 & 0 \\-1 & 2 \end{bmatrix}\)

    and continuing to label Player 1's strategies by \(A\) and \(B\text{,}\) and Player 2's strategies by \(C\) and \(D\text{,}\) we can graph lines for Player 1's pure strategies \(A\) and \(B\text{.}\) We now let the \(x\)--axis represent the probability that Player 2 plays \(D\text{.}\) In the Sage applet below, for \(u\) and \(v\) enter the coordinates of two points that determine the line for when Player 1 plays \(A\text{,}\) then the two points for \(w\) and \(z\) that determine the line for when Player 1 plays \(B\text{.}\) We will then have Sage graph the lines. You can enter new values for \(u, v, w, z\) if you would like to draw the graph for a different matrix.

    @interact(layout=dict(top=[['u','v'],['w','z']]))
    def endpoints(u=vector((0,1.0)), v=vector((1,0.0)), w=vector((0,-1.0)), z=vector((1, 2.0))):
      L1 = line([u, v], thickness=2, color=('blue'))
      L2 = line([w, z], thickness=2, color=('red'))
      P1 = point(u, color=('blue'), pointsize=70)
      P2 = point(v, color=('blue'), pointsize=70)
      P3 = point(w, color=('red'), pointsize=70)
      P4 = point(z, color=('red'), pointsize=70)
      pretty_print(html("Enter the coordinates of the endpoints for the two lines you'd like to graph. Note that u and v are for one line, w and z for the other."))
      show(L1+L2+P1+P2+P3+P4, figsize=4)

    Now determine and enter the equations of the two lines and have Sage solve for the intersection point.

    p, m = var('p, m')
    @interact
    def intersection(Slope1=-1,Intercept1=1,Slope2=3,Intercept2=-1):
      Eq1 = m==Slope1*p+Intercept1
      Eq2 = m==Slope2*p+Intercept2
      S = solve([Eq1, Eq2], p, m)[0]
      pretty_print(html("The intersection point is $%s$, $%s$."%(latex(S[0]),latex(S[1]))))

    You can now use these last two Sage cells to solve any \(2 \times 2\) game with a mixed strategy equilibrium. You can also take some time to experiment with what happens if the game has a pure strategy equilibrium.

    Now use the Sage cells to help you analyze some more games!

    Exercise 3.3.1 : Use Sage to Find a Mixed Strategy Equilibrium

    Consider the zero-sum game \(\begin{bmatrix}-4 & 4 \\2 & -2 \end{bmatrix}\).

    1. Does this game have a pure strategy equilibrium?
    2. Just by looking at the matrix, do you think this game will be fair? (Would you rather be Player 1 or Player 2?)
    3. Use Sage to draw the appropriate graph for this game. Which pure strategy does the blue line represent? Which pure strategy does the red line represent?
    4. Use your graph to determine if there is a mixed strategy equilibrium point. If there is, use Sage to determine how often Player 1 should play each strategy. What is the expected payoff to each player?
    5. Is this game fair? Explain. Compare your answer to (b).
    Exercise 3.3.2 : More Practice with Sage

    Consider the zero-sum game \(\begin{bmatrix}0 & 1 \\1 & -10 \end{bmatrix}\).

    1. Does this game have a pure strategy equilibrium?
    2. Just by looking at the matrix, do you think this game will be fair? (Would you rather be Player 1 or Player 2?)
    3. Use Sage to draw the appropriate graph for this game. Which pure strategy does the blue line represent? Which pure strategy does the red line represent?
    4. Use your graph to determine if there is a mixed strategy equilibrium point. If there is, use Sage to determine how often Player 1 should play each strategy. What is the expected payoff to each player?
    5. Is this game fair? Explain. Compare your answer to (b).

    This page titled 3.3: Using Sage to Graph Lines and Solve Equations is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by Jennifer A. Firkins Nordstrom via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.