Game theory in R with the new gtree package (2024)

[This article was first published on Economics and R - R posts, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

gtree is a new R package that allows to specify extensive form games using stages, similar as one specifies economic experiments with ztree or otree.

Let us specify a simple ultimatum game in gtree. A proposer can offer the responder between 0 and 10 Euros. If the responder accepts the offer, the proposer keeps 10 Euro minus the proposed amount. If the responder rejects, both get nothing.

library(gtree)game = new_game( gameId = "Ultimatum_Dictator", options = make_game_options(verbose=FALSE), params = list(numPlayers=2,cake=10), stages = list( stage("proposerStage", player=1, actions = list( action("offer",~0:cake) ) ), stage("responderStage", player=2, observe = c("offer"), actions = list( action("accept",c(FALSE,TRUE)) ) ), stage("payoffStage", compute=list( payoff_1 ~ ifelse(accept, cake-offer,0), payoff_2 ~ ifelse(accept, offer,0) ) ) ))

The following code solves for all pure strategy subgame perfect equilibria and shows the outcomes.

game %>% game_solve() %>% eq_outcomes() %>% select(eqo.ind,offer, accept, payoff_1, payoff_2)## # A tibble: 2 x 5## eqo.ind offer accept payoff_1 payoff_2## <int> <int> <lgl> <dbl> <dbl>## 1 1 1 TRUE 9 1## 2 2 0 TRUE 10 0

We have two different equilibrium outcomes: the proposer either offers 0 or 1 and in both equilibrium outcomes the offer will be accepted.

The function eq_tables() also shows us the behavior off the equilibrium path:

game %>% eq_tables() ## $offer## # A tibble: 2 x 2## offer eq.inds## <int> <chr> ## 1 0 2 ## 2 1 1 ## ## $accept## # A tibble: 3 x 2## accept eq.inds## <lgl> <chr> ## 1 FALSE 1 ## 2 TRUE 2 ## 3 TRUE 1,2

We see that all offers above 0 are always accepted, and the two equilibria differ by whether an offer of 0 is accepted or rejected.

In behavioral economics the ultimatum game is often used to illustrate that many people have social preferences that go beyond the maximization of own payoffs only. One simple form of social preferences is inequality aversion (Fehr & Schmidt, 1999) The following code solves the game assuming inequality averse preferences with an envy parameter of alpha=0.5 but no guilt (beta=0). This means the effective utility function of both players are given by

`u_1 = payoff_1 - 0.5*max(payoff_2-payoff_2,0)``u_2 = payoff_2 - 0.5*max(payoff_1-payoff_1,0)`game %>% game_set_preferences(pref_ineqAv(alpha=0.5, beta=0)) %>% game_solve() %>% eq_tables()## $offer## # A tibble: 1 x 2## offer eq.inds## <int> <chr> ## 1 3 1 ## ## $accept## # A tibble: 11 x 3## # Groups: offer [11]## offer accept eq.inds## <int> <lgl> <chr> ## 1 0 FALSE 1 ## 2 1 FALSE 1 ## 3 2 FALSE 1 ## 4 3 TRUE 1 ## 5 4 TRUE 1 ## 6 5 TRUE 1 ## 7 6 TRUE 1 ## 8 7 TRUE 1 ## 9 8 TRUE 1 ## 10 9 TRUE 1 ## 11 10 TRUE 1

Now we have a unique equilibrium in which the proposer offers 3 and every offer below 3 would be rejected.

The internal gtree solver used above is quite limited. It can only compute pure strategy subgame perfect equilibria. To access a much larger set of game theoretic solvers, one can use gtree together with the Gambit command line solvers. The following code uses the gambit-logit solver to compute a logit quantal response equilibrium (QRE) for the ultimatum game using the previously set inequality aversion preferences:

game %>% game_gambit_solve(qre.lambda=2) %>% eq_tables()## $offer## # A tibble: 9 x 3## # Groups: offer [9]## offer .prob eq.inds## <int> <dbl> <chr> ## 1 0 0.00000116 1 ## 2 1 0.00000120 1 ## 3 2 0.00000718 1 ## 4 3 0.602 1 ## 5 4 0.353 1 ## 6 5 0.0438 1 ## 7 6 0.000646 1 ## 8 7 0.00000952 1 ## 9 8 0.000000140 1 ## ## $accept## # A tibble: 20 x 4## # Groups: offer, accept [20]## offer accept .prob eq.inds## <int> <lgl> <dbl> <chr> ## 1 0 FALSE 1.000 1 ## 2 0 TRUE 0.0000264 1 ## 3 1 FALSE 0.998 1 ## 4 1 TRUE 0.00179 1 ## 5 2 FALSE 0.892 1 ## 6 2 TRUE 0.108 1 ## 7 3 FALSE 0.108 1 ## 8 3 TRUE 0.892 1 ## 9 4 FALSE 0.00179 1 ## 10 4 TRUE 0.998 1 ## 11 5 FALSE 0.0000264 1 ## 12 5 TRUE 1.000 1 ## 13 6 FALSE 0.00000320 1 ## 14 6 TRUE 1.000 1 ## 15 7 FALSE 0.000000388 1 ## 16 7 TRUE 1 1 ## 17 8 FALSE 0.0000000471 1 ## 18 8 TRUE 1 1 ## 19 9 TRUE 1 1 ## 20 10 TRUE 1 1

We see how player 1 now makes all offers with positive probabilities but mainly concentrates on offers 3,4 and 5. Interestingly, an offer of 2 is still very unlikely and will be rejected with very high probability.

Gambit has an own Python interface to directly construct the game tree of extensive form games. While it is a matter of taste, I think for many games studied in economic experiments it is simpler to specify stages instead of directly constructing a game tree. For a comparision you can look at a gtree implementation of a sender-receiver game whose Gambit Python interface implementation is linked as example on the Gambit main page.

Take a look at the gtree page https://skranz.github.io/gtree for more examples and features. On Github you can find gtree here: https://github.com/skranz/gtree. Please open an issue if you have some feature request, bug report or question concerning gtree.

Playing games in the web: Rock Paper Scissors Laser Mirror

gtreeWebPlay is a companion package to gtree that helps building simply shiny apps that a allow to play a game. Below I embedded an app to play Rock-Paper-Scissors-Laser-Mirror:

  • Laser beats rock, paper and scissors.

  • Mirror beats laser but is beaten by rock, paper and scissors.

  • Otherwise as usual.

You play agains the population of all previous users. (And the first users play against the equilibrium strategies.) Let us see whether you can beat the crowd:

Kuhn Poker

Below is a gtreeWebPlay app that allows you to play a round of Kuhn Poker.(https://skranz.github.io/gtree/articles/t03_kuhn_poker.html). The deck has only three cards: Jack, Queen and Ace and two players who each get one card after they both put one dollar into the blind. Below is an embedded app that allows you to play it against a randomly chosen earlier player:

A game theoretic analysis of Kuhn poker using gtree can be found in this tutorial

Game theory in R with the new gtree package (1)

Related

To leave a comment for the author, please follow the link and comment on their blog: Economics and R - R posts.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Game theory in R with the new gtree package (2024)

FAQs

What is best reply in game theory? ›

A best response is a best strategy given what you think the other player will do. A dominant strategy is one that is a best response to all possible strategies. When players are mutually best responding, we have a Nash equilibrium.

Is there a lot of math in game theory? ›

This is because game theory is an applied mathematical discipline, and therefore its determining side is the real-life phenomena it tries to model. The secondary, nonetheless essential side is of course mathematics.

What is the best outcome in game theory? ›

Nash equilibrium is a concept in game theory where the game reaches an optimal outcome. This is a state that gives individual players no incentive to deviate from their initial strategy.

What is the optimal strategy for a game theory? ›

An optimal strategy is one that provides the best payoff for a player in a game. Optimal Strategy = A strategy that maximizes a player's expected payoff. Games are of two types: cooperative and noncooperative games.

What is a never best response game theory? ›

We say that an action is a never-best response if it is not optimal against any belief about other players' actions. A never-best response action is not rationalizable by definition.

What is game theory short answer? ›

game theory, branch of applied mathematics that provides tools for analyzing situations in which parties, called players, make decisions that are interdependent. This interdependence causes each player to consider the other player's possible decisions, or strategies, in formulating strategy.

Why is game theory hard? ›

The extensive gains from the application of game theory in economics lure many business leaders to learn this subject. However, before starting, you may wonder how hard it is to learn Game theory. Game theory is a highly challenging subject as it comprises complex math.

What level of math is needed for game theory? ›

Students must know basics of linear algebra (matrix multiplication, geometric interpretation of vectors), analysis (continuity, closed sets), and probability theory (expected value, conditional probability, independence of random events).

Is calculus needed in game theory? ›

Game theory actually has a little less of a math requirement than other areas of economics. For instance, since most game theory models involve discreet choices, you can learn a lot of game theory without having to use calculus. At the very least, you should be familiar with algebra before you learn game theory.

What is the most famous example of game theory? ›

The prisoner's dilemma is the most well-known example of game theory.

Why is game theory so popular? ›

In addition to being used to describe, predict, and explain behavior, game theory has also been used to develop theories of ethical or normative behavior and to prescribe such behavior. In economics and philosophy, scholars have applied game theory to help in the understanding of good or proper behavior.

What are the two strategies of game theory? ›

Pure and mixed strategies

In particular, it determines the move a player will make for any situation they could face. A player's strategy set is the set of pure strategies available to that player. A mixed strategy is an assignment of a probability to each pure strategy.

What are the 4 rules of game theory? ›

There are 4 main elements in game theory:
  • Player: The strategic decision-maker.
  • Strategy: The rules that apply in the specific game.
  • Outcome: The result after a decision has been made.
  • Equilibrium: The point in a game where both players have made their decisions and can't make any other move.
Jun 10, 2021

What is the dominance rule in game theory? ›

Dominant strategy game theory refers to a circ*mstance where one player is at a significant advantage in comparison to another player due to the strategy they are employing. Irrespective of what the other player chooses to do, the player using the dominant strategy has full control over the outcome of the game.

What is a strongly dominant strategy in game theory? ›

What is Dominant Strategy? The dominant strategy in game theory refers to a situation where one player has superior tactics regardless of how their opponent may play. Holding all factors constant, that player enjoys an upper hand in the game over the opposition.

What is the best response dynamic in game theory? ›

Best response (BR) dynamics is a simple and natural method by which players proceed toward a NE via the following local search method: as long as the strat- egy profile is not a NE, an arbitrary player is chosen to improve her utility by deviating to her best strategy given the profile of others.

Is best response a dominant strategy? ›

If a player has a dominant​ strategy, then it is his best​ response; however, every best response is not always a dominant strategy.

How to find the best response function? ›

To find best response function of firm 1, look at its payoff as a function of its output, given output of firm 2. Any price greater than p2 is a best response to p2: B1(p2) = {p1 : p1 > p2}. Note: a price between p2 and c is a best response!

What is best response with inertia? ›

In best-response with inertia, each agent forms beliefs about the actions of other agents, and takes an action that either maximizes its expected utility computed with respect to its beliefs (best-responds) or continues to take its former action (shows inertia).

Top Articles
How to Get a Tennessee F Endorsem*nt
FREE Tennessee DMV Driving Practice Test 2024 | TN
Funny Roblox Id Codes 2023
Golden Abyss - Chapter 5 - Lunar_Angel
Www.paystubportal.com/7-11 Login
Joi Databas
DPhil Research - List of thesis titles
Cooking Chutney | Ask Nigella.com
Shs Games 1V1 Lol
Evil Dead Rise Showtimes Near Massena Movieplex
Steamy Afternoon With Handsome Fernando
Which aspects are important in sales |#1 Prospection
Detroit Lions 50 50
Zürich Stadion Letzigrund detailed interactive seating plan with seat & row numbers | Sitzplan Saalplan with Sitzplatz & Reihen Nummerierung
Grace Caroline Deepfake
978-0137606801
Justified Official Series Trailer
Patrick Bateman Notebook
London Ups Store
Committees Of Correspondence | Encyclopedia.com
Pizza Hut In Dinuba
Jinx Chapter 24: Release Date, Spoilers & Where To Read - OtakuKart
How Much You Should Be Tipping For Beauty Services - American Beauty Institute
Free Online Games on CrazyGames | Play Now!
Sizewise Stat Login
VERHUURD: Barentszstraat 12 in 'S-Gravenhage 2518 XG: Woonhuis.
Jet Ski Rental Conneaut Lake Pa
Unforeseen Drama: The Tower of Terror’s Mysterious Closure at Walt Disney World
Ups Print Store Near Me
C&T Wok Menu - Morrisville, NC Restaurant
How Taraswrld Leaks Exposed the Dark Side of TikTok Fame
Dashboard Unt
Access a Shared Resource | Computing for Arts + Sciences
Black Lion Backpack And Glider Voucher
Duke University Transcript Request
Lincoln Financial Field, section 110, row 4, home of Philadelphia Eagles, Temple Owls, page 1
Jambus - Definition, Beispiele, Merkmale, Wirkung
Netherforged Lavaproof Boots
Ark Unlock All Skins Command
Craigslist Red Wing Mn
School Tool / School Tool Parent Portal
D3 Boards
Jail View Sumter
Nancy Pazelt Obituary
Birmingham City Schools Clever Login
Trivago Anaheim California
Thotsbook Com
Vérificateur De Billet Loto-Québec
Vci Classified Paducah
Www Pig11 Net
Ty Glass Sentenced
Latest Posts
Article information

Author: Dan Stracke

Last Updated:

Views: 5645

Rating: 4.2 / 5 (43 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Dan Stracke

Birthday: 1992-08-25

Address: 2253 Brown Springs, East Alla, OH 38634-0309

Phone: +398735162064

Job: Investor Government Associate

Hobby: Shopping, LARPing, Scrapbooking, Surfing, Slacklining, Dance, Glassblowing

Introduction: My name is Dan Stracke, I am a homely, gleaming, glamorous, inquisitive, homely, gorgeous, light person who loves writing and wants to share my knowledge and understanding with you.