17
$\begingroup$

There are many ways to draw a sequential game 'by hand'. By drawing the game I mean this:

enter image description here

Displaying players' decision points, available actions and payoffs.

Is there any way I can do this in R or a similar high level programming language? To be precise: I do not want to plot geometric equations, I want to define a structure (players, points, connections, payoffs) and have the program plot it.

I am currently browsing the 'igraph' package but I am having some difficulty labeling so I am wondering if there is a better way.

$\endgroup$
1
  • 1
    $\begingroup$ If you consider LaTeX to be a "similar high level programming language", you might find the answers to this question on TeX.SE (and other related questions there) useful. $\endgroup$ Feb 6, 2017 at 18:26

5 Answers 5

12
$\begingroup$

LaTeX with forest

The forest package of LaTeX allows you to draw game trees with pretty simple syntax. After copying a pre-set template into the LaTeX preamble, one can build up the game tree using a nested [] syntax, then the program takes care of node placement/spacing/etc.

  • pros: customizability (you can annotate the game tree in any way you want) and font/style consistency
  • cons: inputting the game tree elements (player/action names, payoffs, info sets) is still somewhat manual, especially if the tree is large; and a somewhat steep learning curve if you're not already familiar with the TikZ package

Here's an example from the forest manual ("Decision tree" in Section 5.1):

\begin{forest} 
decision tree
[N,plain content
  [I;{p_1=0.5},plain content,elo={yshift=4pt}
    [{5,1};a]
    [II;b,plain content
      [{1,2};m]
      [{2,3};n]
    ]
  ]
  [II;{p_2=0.5},plain content,elo={yshift=4pt}
    [;c
      [{1,0};z]
      [{2,2};t]
    ]
    [;d
      [{3,1};z]
      [{0,0};t]
    ]
  ]{\draw[dashed](!1.anchor)--(!2.anchor) node[pos=0.5,above]{I};}
]
\end{forest}

enter image description here

Game Theory Explorer

The game theory explorer is developed by a few people at LSE. It allows users to input matrix-form games or build extensive form games through a GUI. It also seems capable of converting between normal and extensive form games. Additionally, the software comes with a solver that looks for Nash equilibria of the inputted game.

  • pros: GUI; solver; no need for local installation; (limited) customizability
  • cons: no support for annotation; font/style inconsistency with rest of document

Screenshots: enter image description here

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you very much for the loaded response! Just tried the game theory explorer. They migrated to: gametheoryexplorer.org with wonderful and brief tutorials on youtube. Amazing! $\endgroup$
    – Leshui
    Apr 23, 2021 at 3:30
7
$\begingroup$

Mathematica has a graph building and drawing capability.

So, if you built the graph in Mathematica, then you could plot it using settings of your choosing.

In Mathematica, you might use the TreeGraph as way to build the graph, and TreePlot as a way to plot it. For example, the following code generates a tree with the nodes labeled by coordinate and has a similar format to what you want:

TreePlot[{1 -> 4, 1 -> 1, 1 -> 5, 2 -> 4, 3 -> 6, 3 -> 9, 4 -> 8, 
  4 -> 10, 6 -> 7, 8 -> 9}, 
 EdgeRenderingFunction -> ({If[First[#2] === Last[#2], Red, Black], 
     Arrow[#1, .1], 
     Text[#2, LineScaledCoordinate[#1, .5], Background -> White]} &), 
 VertexLabeling -> True]

enter image description here

Also, note that there are graphical packages like TikZ in Tex which have very powerful graph drawing capability. TikZ is mostly used in unix environments, though, and will not store data structures or do calculations like Mathematica. It is purely a graphical drawing capability.

The graph you have used as an illustration has the font typically used in TeX, so it was probably produced with TikZ or another TeX package. If you want a Mathematica equation to look like that you can use the TraditionalForm function, or explicitly specify the Computer Modern font.

$\endgroup$
0
4
$\begingroup$

If you use LaTeX, you can also draw game trees with the istgame package, which is based on TikZ.

The manual contains lots of examples with full codes including:

  • game trees in any direction: downwards, upwards, eastwards, -45 degree, etc.
  • labelling players, action labels, and payoffs
  • decision nodes, chance nodes, terminal nodes
  • various information sets
  • various arrows on branches (backward induction)
  • various continua of actions
  • subgames
  • signaling games

With the istgame package, you can draw game trees like you draw them by hand.

enter image description here

\documentclass{standalone}

\usepackage{istgame}

\begin{document}

\begin{istgame}
\xtdistance{15mm}{40mm}
\istroot(0){1}
  \istb{a_1}[al]
  \istb{a_2}[ar]
  \endist
\xtdistance{15mm}{20mm}
\istroot(1)(0-1){2}
  \istb{b_1}[al]{5,5}
  \istb{b_2}[ar]{1,5}
  \endist
\istroot(1)(0-2){2}
  \istb{b_3}[al]{3,6}
  \istb{b_4}[ar]{2,2}
  \endist
\end{istgame}

\end{document}
$\endgroup$
1
  • $\begingroup$ This is great, thank you! $\endgroup$
    – Giskard
    Nov 29, 2019 at 13:30
2
$\begingroup$

enter image description here

I have used LaTeX with tikz package. The following code is used to generate this Centepede Game :

\documentclass{article}

\usepackage{tikz}
\usepackage{bodegraph}
\usepackage[printwatermark]{xwatermark}


\begin{document}


\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=1.3cm,
  thick,main node/.style={circle,fill=blue!20,draw,font=\sffamily\normalsize\bfseries}]

  \node[main node] (1) {1};
  \node[main node] (2) [right of=1] {2};
  \node[main node] (3) [right of=2] {1};
  \node[main node] (4) [right of=3] {2};
  \node[main node] (5) [right of=4] {1};
  \node[main node] (6) [right of=5] {2};
    \node (7) [below of=1] {$(1,0)$};
       \node (8) [below of=2] {(0,2)};
    \node (9) [below of=3] {$(3,1)$};
    \node (10) [below of=4] {$(2,4)$};
  \node (11) [below of=5] {$(5,3)$};
    \node (12) [below of=6] {$(4,6)$};
      \node (13) [right of=6] {$(7,5)$};
       \path[every node/.style={font=\sffamily\small\scshape}]
   (1) edge node [above] {In} (2)
   edge node [left] {Out} (7)
        (2) edge node [above] {In} (3)
   edge node [left] {Out} (8)
      (3) edge node [above] {In} (4)
   edge node [left] {Out} (9)
      (4) edge node [above] {In} (5)
   edge node [left] {Out} (10)
      (5) edge node [above] {In} (6)
   edge node [left] {Out} (11)
      (6) edge node [above] {In} (13)
   edge node [left] {Out} (12);
\end{tikzpicture} 
\end{document}

Here is another example:

enter image description here

Code for the above game is :

\documentclass{article}

\usepackage{tikz}
\usepackage{bodegraph}
\usepackage[printwatermark]{xwatermark}


\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=3cm,
  thick,main node/.style={circle,fill=blue!20,draw,font=\sffamily\Large\bfseries}]

  \node[main node] (1) {1};
  \node[main node] (2) [below right of=1] {2};
  \node[main node] (3) [below left of=2] {1};
  \node (4) [below left of=1] {$(1,1)$};
   \node (5) [below right of=2] {(0,2)};
    \node (6) [below left of=3] {$(10,0)$};
    \node (7) [below right of=3] {$(5,5)$};

 \path[every node/.style={font=\sffamily\small\scshape}]
   (1) edge node [right] {Play} (2)
   edge node [left] {Not Play} (4)
      (2) edge node [right] {Distrust} (5)
   edge node [left] {Trust} (3)
      (3) edge node [left] {Steal} (6)
   edge node [right] {Share} (7);
\end{tikzpicture}
\end{document}
$\endgroup$
2
$\begingroup$

Just for your convenience

If you are interested in the istgame package, you can do like this (to draw Amit's examples):

For the centipede game:

enter image description here

\documentclass{standalone}

\usepackage{istgame}

\begin{document}

\begin{istgame}
%% for arrows (optional)
\xtShowArrows
\xtShowEndPoints[ellipse node]
\xtHideTerminalNodes
%% some more optional settings
\setistEllipseNodeStyle{15pt}[blue!20] % node style
\setistmathTF*001<textbf>{textsc}      % input mode changer
\setistgrowdirection{-45}              % direction
%% tree
\xtdistance{10mm}{20mm}
\istrooto(1){1}
  \istbt{Out}[l]{(1,0)}[b] \istb{In}[a]            \endist
\istrooto(2)(1-2){2}
  \istbt{Out}[l]{(0,2)}[b] \istb{In}[a]            \endist
\istrooto(3)(2-2){1}
  \istbt{Out}[l]{(3,1)}[b] \istb{In}[a]            \endist
\istrooto(4)(3-2){2}
  \istbt{Out}[l]{(2,4)}[b] \istb{In}[a]            \endist
\istrooto(5)(4-2){1}
  \istbt{Out}[l]{(5,3)}[b] \istb{In}[a]            \endist
\istrooto(6)(5-2){2}
  \istbt{Out}[l]{(4,6)}[b] \istbt{In}[a]{(7,5)}[r] \endist
\end{istgame}

\end{document}

The tree structure itself is simple to draw.

enter image description here

\documentclass{standalone}

\usepackage{istgame}

\begin{document}

\begin{istgame}[font=\scriptsize]
\setistmathTF*001{textsc}  % input mode changer
\setistgrowdirection{-45}  % direction
%% tree
\xtdistance{10mm}{20mm}
\istroot(1){1}
  \istb{Out}[l]{(1,0)}[b] \istb{In}[a]           \endist
\istroot(2)(1-2){2}
  \istb{Out}[l]{(0,2)}[b] \istb{In}[a]           \endist
\istroot(3)(2-2){1}
  \istb{Out}[l]{(3,1)}[b] \istb{In}[a]           \endist
\istroot(4)(3-2){2}
  \istb{Out}[l]{(2,4)}[b] \istb{In}[a]           \endist
\istroot(5)(4-2){1}
  \istb{Out}[l]{(5,3)}[b] \istb{In}[a]           \endist
\istroot(6)(5-2){2}
  \istb{Out}[l]{(4,6)}[b] \istb{In}[a]{(7,5)}[r] \endist
\end{istgame}

\end{document}


For the another example:

enter image description here

\documentclass{standalone}

\usepackage{istgame}

\begin{document}

\begin{istgame}[font=\scriptsize]
%% for arrows (optional)
\xtShowArrows
\xtShowEndPoints[ellipse node]
\xtHideTerminalNodes
%% some more optional settings
\setistEllipseNodeStyle{15pt}[blue!20] % node style
\setistmathTF*001<textbf>{textsc} % input mode changer
%% tree
\xtdistance{15mm}{30mm}
\istrooto(1){1}
  \istbt{Not Play}[al]{(1,1)}
  \istb{Play}[ar]
  \endist
\istrooto(2)(1-2){2}
  \istb{Trust}[al]
  \istbt{Distrust}[ar]{(0,2)}
  \endist
\istrooto(3)(2-1){1}
  \istbt{Steal}[al]{(10,10)}
  \istbt{Share}[ar]{(5,5)}
  \endist
\end{istgame}

\end{document}

Again, the tree structure is simple to draw.

enter image description here

\documentclass{standalone}

\usepackage{istgame}

\begin{document}

\begin{istgame}[font=\scriptsize]
\setistmathTF*001{textsc}        % input mode changer
%% tree
\xtdistance{15mm}{30mm}
\istroot(1){1}
  \istb{Not Play}[al]{(1,1)}
  \istb{Play}[ar]
  \endist
\istroot(2)(1-2){2}
  \istb{Trust}[al]
  \istb{Distrust}[ar]{(0,2)}
  \endist
\istroot(3)(2-1){1}
  \istb{Steal}[al]{(10,10)}
  \istb{Share}[ar]{(5,5)}
  \endist
\end{istgame}

\end{document}

(ADDED) If you want the tree to be eastward:

enter image description here

\documentclass{standalone}

\usepackage{istgame}

\begin{document}

\begin{istgame}[font=\scriptsize]
\setistmathTF*001{textsc}        % input mode changer
\setistgrowdirection'{east}      % direction
%% tree
\xtdistance{15mm}{30mm}
\istroot(1)<180>{1}
  \istb{Not Play}[al]{(1,1)}
  \istb{Play}[bl]
  \endist
\istroot(2)(1-2)<180>{2}
  \istb{Trust}[al]
  \istb{Distrust}[bl]{(0,2)}
  \endist
\istroot(3)(2-1)<180>{1}
  \istb{Steal}[al]{(10,10)}
  \istb{Share}[bl]{(5,5)}
  \endist
\end{istgame}

\end{document}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.