Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there any game theory package around? I mean supporting functions which take a game in matrix form and tell you all about nash equilibra, best strategies, ect

share|improve this question
Have you looked at and the linked sources? – Michael Stern Jan 24 '13 at 3:24
thank you @MichaelStern . It is an excellent reference. You should put it as an answer. – magma Jan 24 '13 at 10:38
up vote 8 down vote accepted

I have this hope that Mathematica 10 will make the efforts in my NashEquilibriaIn33Games Demonstration seem extremely primitive and that it will directly implement a good chunk of game theory. I think it's time for Wolfram Research to take the leap into this now well-established area of Math/Econ/Biology, etc. The work being done in algorithmic game theory perhaps creates some additional motivation. There's stuff now in Graph and DiscreteMarkovProcess that might well provide some new support for such an initiative. I would love being able to write Nash[some representation of a strategic form game,Method->"LemkeHowson"] or Nash[some visual depiction of an extensive form game,"SubgamePerfect"->True].

In the mean time, here is some code that will help find pure (but only pure) strategy Nash Equilibrium in n-player games. I have little doubt that both of these methods can be substantially improved, but perhaps they will provide a start.

g4 = ReplacePart[
RandomInteger[{-100, 100}, {5, 5, 7, 8, 4}], {3, 4, 5, 6} -> {100, 
 100, 100,100}](* four player game with at least one manually inserted \
Nash equilibrium just so we can make sure it works*);

nashPos[g_] := 
 Select[Position[g, _, {ArrayDepth[g] - 1}, Heads -> False], 
 pos \[Function] 
 And @@ Table[
 Extract[g, Append[pos, player]] >= 
    Table[Append[ReplacePart[pos, player -> choice], 
      player], {choice, 
      Range[Dimensions[g][[player]]]}]]], {player, 
  Range[ArrayDepth[g] - 1]}]]

eliminate[g_, {startingAugmentedStrategyCombinations_, ns_}] := 
Module[{candidate = First[startingAugmentedStrategyCombinations],
 complementaryPattern, augmentedComplementaryStrategyCombinations,
 scoreAugmentationPositions, nsAug, newNS, eliminationPatterns, 
complementaryPattern = 
ReplacePart[candidate, Last[candidate] -> _];
augmentedComplementaryStrategyCombinations = 
Cases[Position[g, _, {ArrayDepth[g]}, Heads -> False], 
scores = Extract[g, augmentedComplementaryStrategyCombinations];
best = Max[scores];
bestQList = Map[score \[Function] Equal[score, best], scores];
eliminateStrategyCombinations = 
Map[asc \[Function] ReplacePart[asc, -1 -> _], 
Pick[augmentedComplementaryStrategyCombinations, bestQList, 
eliminateAugmentedStrategyCombinations = 
Pick[augmentedComplementaryStrategyCombinations, bestQList, True];
scoreAugmentationPositions = 
Map[asc \[Function] Most[asc], 
Pick[augmentedComplementaryStrategyCombinations, bestQList, True]];
nsAug = 
SparseArray[Thread[Rule[scoreAugmentationPositions, 1]], 
newNS = Normal[nsAug] + ns;
eliminationPatterns = 
Alternatives @@ 
newAugmentedStrategyCombinations = 
Alternatives @@ eliminationPatterns];
{newAugmentedStrategyCombinations, Normal[nsAug] + ns}


nashPos2[g_] := 
eliminate[g, #] &, {Position[g, _, {ArrayDepth[g]}, 
  Heads -> False], SparseArray[{}, Most@Dimensions[g]]}, 
x \[Function] Length[x[[1]]] > 0, 1, 1500][[2]], 
ArrayDepth[g] - 1]
share|improve this answer
absolutely! Have you done more on that matter? – Stefan Jan 24 '13 at 19:54
Thank you Seth. Nice having you round here. I suggest you follow @Mr.Wizard advice. Beside the demonstration, did you create a standalone package with general game theory functions that you are willing to share? – magma Jan 25 '13 at 0:18
Sorry. I was unaware that making suggestions about Mathematica development in response to a question was a breach of StackExchange etiquette. No, I wish I were smart enough to write the kind of code that would be required to do this well. The book referenced at does provide some useful game theory code, including an implementation of Lemke Howson. As atonement for my breach, I will shortly offer two pieces of code that should be able to find pure (and only pure) strategy Nash equilibrium in n-player games. – Seth Chandler Jan 25 '13 at 14:33

Have you looked at and the linked sources?

It relies on formulae and solution methods from Hal Varian's 1996 book "Computational Economics and Finance: Modeling and Analysis with Mathematica" (which I have had on my Amazon wish list for years but have never gotten around to buying.)

The sample notebooks from Varian's book can be found in the Wolfram Library Archive.

The Wolfram demonstrations site includes other examples that may be of use.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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