Title: | gtree basic functionality to model and solve games |
---|---|
Description: | gtree basic functionality to model and solve games |
Authors: | Sebastian Kranz |
Maintainer: | Sebastian Kranz <[email protected]> |
License: | GPL >= 2.0 |
Version: | 0.1.1 |
Built: | 2024-12-03 02:54:54 UTC |
Source: | https://github.com/skranz/gtree |
Specify an action in a stage
action(name, set, strategyMethodDomain = NULL, ...)
action(name, set, strategyMethodDomain = NULL, ...)
name |
The variable name of the action |
set |
The set of different action values. Can be a formula that depends on other game variables. |
strategyMethodDomain |
if not NULL the action shall be specified via strategy method in an experiment. State the variable name upon which the action conditions Only used when running an experiment or analysing experimental data |
Other Define Stage: natureMove
,
stage
A simple function to define case distinctions
case_distinction(...)
case_distinction(...)
Other Helper Functions: is_true
Return conditional expected equilibrium outcomes
eq_cond_expected_outcomes(game, ..., fixed.list = list(...), fixed.vars = NULL, like.factor = NULL)
eq_cond_expected_outcomes(game, ..., fixed.list = list(...), fixed.vars = NULL, like.factor = NULL)
game |
the game object for which equilibria were computed e.g. with |
... |
variable names and their assumed value. We set the probabilities of the conditioned variable values to 1. These correspond to equilibrium outcomes given an unexpected tremble that makes the variables take the specified values. Variables can take multiple values. We then compute conditional equilibrium outcomes for each combination of values |
fixed.list |
Alternativly to ... a named list with values to fix. |
fixed.vars |
Alternative to ... or fixed.list, a vector of variable names. If provided, we compute the conditional expected outcomes holding fixed every possible combination of the variables stated in fixed.vars |
like.factor |
an optional character vector of names of numerical variables that shall be presented like qualitative variables. |
Other eq: eq_cond_outcomes
,
eq_expected_outcomes
, eq_li
,
eq_outcomes
, eq_tables
,
game_gambit_solve_qre
,
game_gambit_solve
,
game_solve_spe
Return conditional equilibrium outcomes
eq_cond_outcomes(game, ..., fixed.list = list(...), fixed.vars = NULL)
eq_cond_outcomes(game, ..., fixed.list = list(...), fixed.vars = NULL)
game |
the game object for which equilibria were computed e.g. with |
... |
variable names and their assumed value. We set the probabilities of the conditioned variable values to 1. These correspond to equilibrium outcomes given an unexpected tremble that makes the variables take the specified values. Variables can take multiple values. We then compute conditional equilibrium outcomes for each combination of values |
fixed.list |
Alternativly to ... a named list with values to fix. |
fixed.vars |
Alternative to ... or fixed.list, a vector of variable names. If provided, we compute the conditional expected outcomes holding fixed every possible combination of the variables stated in fixed.vars |
Other eq: eq_cond_expected_outcomes
,
eq_expected_outcomes
, eq_li
,
eq_outcomes
, eq_tables
,
game_gambit_solve_qre
,
game_gambit_solve
,
game_solve_spe
Each row will describe a possible expected
equilibrium outcome. For numerical variables like
payoff_1
the expected value on the equilibrium
path is returned.
eq_expected_outcomes(game, like.factor = NULL)
eq_expected_outcomes(game, like.factor = NULL)
game |
the game object for which prevoiously equilibria were computed e.g. with |
like.factor |
an optional character vector of names of numerical variables that shall be presented like qualitative variables. |
For qualitative variables, we generate a string like
"accept(0.3),reject(0.2)"
describing the moves
that occur with positive probability and those
probabilities on the equilibrium path.
Other eq: eq_cond_expected_outcomes
,
eq_cond_outcomes
, eq_li
,
eq_outcomes
, eq_tables
,
game_gambit_solve_qre
,
game_gambit_solve
,
game_solve_spe
Return the computed equilibria using the internal representation
eq_li(game, ...)
eq_li(game, ...)
Other eq: eq_cond_expected_outcomes
,
eq_cond_outcomes
,
eq_expected_outcomes
,
eq_outcomes
, eq_tables
,
game_gambit_solve_qre
,
game_gambit_solve
,
game_solve_spe
If we have mixed strategies or moves of nature an equilibrium outcome will consist of several rows. One row for each pure outcome that occurs with positive probability.
eq_outcomes(game, add.move.probs = FALSE)
eq_outcomes(game, add.move.probs = FALSE)
game |
the game object for which previously equilibria were computed e.g. with |
add.move.probs |
if |
Typically eq_expected_outcomes
will
deliver a version that is easier to read.
It will take expected values and reduce
each outcome to one row.
Yet eq_outcomes
may be more useful for automatical
analysis.
Other eq: eq_cond_expected_outcomes
,
eq_cond_outcomes
,
eq_expected_outcomes
, eq_li
,
eq_tables
,
game_gambit_solve_qre
,
game_gambit_solve
,
game_solve_spe
Other eq: eq_cond_expected_outcomes
,
eq_cond_outcomes
,
eq_expected_outcomes
, eq_li
,
eq_tables
,
game_gambit_solve_qre
,
game_gambit_solve
,
game_solve_spe
Best take a look at the Vignettes to understand this format.
eq_tables(game, reduce.tables = TRUE, combine = 2, eq.ind = seq_along(game$eq.li), ignore.keys = NULL, eq.li = game$eq.li[eq.ind], ...)
eq_tables(game, reduce.tables = TRUE, combine = 2, eq.ind = seq_along(game$eq.li), ignore.keys = NULL, eq.li = game$eq.li[eq.ind], ...)
reduce.tables |
(default = TRUE). Shall we try to reduce the rows and columns of the key tables be reduced to get a subset of neccessary keys that perfectly predict the chosen value of an action? |
combine |
if 0 generate separate tables for each equilibrium. If 1 bind the tables of each variable over all equilibria. If 2 (default) also collapse the rows that are the same for different equilibria and add a column eq.inds that contains all equilibrium numbers as a comma separated string |
eq.ind |
Vector of integers specifying the indices of all equilibria that shall be considered. By default all equilibria. |
ignore.keys |
A character vector of variables that will always be removed from the key variables, without any check whether they are neccessary or not. |
Other eq: eq_cond_expected_outcomes
,
eq_cond_outcomes
,
eq_expected_outcomes
, eq_li
,
eq_outcomes
,
game_gambit_solve_qre
,
game_gambit_solve
,
game_solve_spe
For an already compiled game, we try to change parameters in a fashion that is faster than a complete recompilation.
game_change_param(game, ..., params = list(), verbose = isTRUE(game$options$verbose > 0))
game_change_param(game, ..., params = list(), verbose = isTRUE(game$options$verbose > 0))
Other Modify Game: game_copy
,
game_set_preferences
,
pref_change_params
Other Game Parameters: make_game_params
new_game
Compile a game defined with new_game
game_compile(game, branching.limit = 10000, for.internal.solver = FALSE, add.sg = for.internal.solver, add.spi = for.internal.solver, add.spo = for.internal.solver, force = FALSE, verbose = game$options$verbose, ...)
game_compile(game, branching.limit = 10000, for.internal.solver = FALSE, add.sg = for.internal.solver, add.spi = for.internal.solver, add.spo = for.internal.solver, force = FALSE, verbose = game$options$verbose, ...)
Other Build Game: make_game_options
,
make_game_params
, stage
Make a deep copy of a game
game_copy(game)
game_copy(game)
Other Modify Game: game_change_param
,
game_set_preferences
,
pref_change_params
Allows to study the game under the assumption that a player strongly prefers to chose one particular move of an action variable.
game_fix_action_preferences(game, ..., actions = list(...), util.add = 1000)
game_fix_action_preferences(game, ..., actions = list(...), util.add = 1000)
... |
directly the named arguments from which |
actions |
a named list. The names correspond to action names and the values either to fixed values of the action or to a formula. If it is a formula the action value can depend on earlier computed variables. |
If you want to fix mixed strategies, use the he related function game_fix_action_preferences
transforms the corresponding action into a move of nature.
For fixing pure strategies game_fix_action_preferences
is preferable when using the gambit-logit
solver that can
find sequential equilibria, by using logit trembles.
Other Fix Actions: game_fix_actions
,
game_prefer_outcomes
Other Preferences: game_prefer_outcomes
,
game_set_preferences
,
pref_change_params
,
pref_custom
, pref_envy
,
pref_heterogeneous_players
,
pref_ineqAv
, pref_lossAv
,
pref_payoff
The function corresponds the provided actions into moves of nature with specified move probabilities. Can be a useful step when checking for existence of equilibria with particular structure.
game_fix_actions(game, ..., actions = list(...), tremble.prob = NULL)
game_fix_actions(game, ..., actions = list(...), tremble.prob = NULL)
... |
directly the named arguments from which |
actions |
a named list. The names correspond to action names. The default value to fix mixed strategies is a table that specifies conditional move probabilities (see example). If you want to fix pure actions you can also provide arguments as in |
tremble.prob |
If a positive number, we assume that with this probability the player trembles and then chooses a random action with uniform probability. Trembles can be useful to enforce some sequential rationality in continuation play, but note that uniform trembles are not neccessarily the correct form of trembles to find sequential equilibria or trembling hand perfect equilibria. |
For fixing pure strategies game_fix_action_preferences
is preferable when using the gambit-logit
solver that can find sequential equilibria, by using logit trembles.
Other Fix Actions: game_fix_action_preferences
,
game_prefer_outcomes
You need to install Gambit http://www.gambit-project.org to use this function.
game_gambit_solve(game, gambit.command = NULL, mixed = FALSE, just.spe = TRUE, qre.lambda = NULL, gambit.dir = first.non.null(getOption("gtree.gambit.dir"), ""), efg.dir = NULL, efg.file = NULL, verbose = isTRUE(game$options$verbose >= 1), add.q.flag = TRUE, ...)
game_gambit_solve(game, gambit.command = NULL, mixed = FALSE, just.spe = TRUE, qre.lambda = NULL, gambit.dir = first.non.null(getOption("gtree.gambit.dir"), ""), efg.dir = NULL, efg.file = NULL, verbose = isTRUE(game$options$verbose >= 1), add.q.flag = TRUE, ...)
game |
the game object created with new_game |
gambit.command |
A Gambit command line command with options but not file name. For example |
mixed |
relevant if no explicit gambit.command is given. If FALSE (default) only pure strategy equilibria will be computed, otherwise try to compute one mixed equilibrium. |
just.spe |
if TRUE compute only SPE. If FALSE all NE will be computed. |
qre.lambda |
if not NULL compute a logit QRE equilibrium using the gambit-logit solver and the specified value of lambda. |
gambit.dir |
The directory where to find the Gambit command line solvers. Ideally, you put this directory into the search path of your system and can keep the default |
efg.dir |
To solve via Gambit we first write the game tree into an .efg file. If |
efg.file |
If NULL a default file name for the efg file will be generated based on the name of the game and the specified preferences. But you can specify a custom name here. |
verbose |
if TRUE show some extra information |
add.q.flag |
The gambit command line solver should always be called with the option "-q" for gtree to be able to parse the returned output. If add.q.flag is TRUE we will add this flag if you have not yet added it to your |
Other eq: eq_cond_expected_outcomes
,
eq_cond_outcomes
,
eq_expected_outcomes
, eq_li
,
eq_outcomes
, eq_tables
,
game_gambit_solve_qre
,
game_solve_spe
Other Gambit: game_write_efg
This function computes logit agent quantal response equilibria using the Gambit solver gambit-logit. For a short description see the Wikipedia article and the gambit-logit solver's https://gambitproject.readthedocs.io/en/latest/tools.html#gambit-logit-compute-quantal-response-equilbria. Details are in the article https://link.springer.com/article/10.1007/s00199-009-0443-3 by Theodore Turocy. But unfortunately, the article can only be found behind a pay wall.
game_gambit_solve_qre(game, gambit.command = "gambit-logit -q -l", gambit.dir = "", efg.file = NULL, efg.dir = NULL, verbose = isTRUE(game$options$verbose >= 1))
game_gambit_solve_qre(game, gambit.command = "gambit-logit -q -l", gambit.dir = "", efg.file = NULL, efg.dir = NULL, verbose = isTRUE(game$options$verbose >= 1))
For a description of the arguments see game_gambit_solve
Other eq: eq_cond_expected_outcomes
,
eq_cond_outcomes
,
eq_expected_outcomes
, eq_li
,
eq_outcomes
, eq_tables
,
game_gambit_solve
,
game_solve_spe
Allows to study the game under the assumption that a player strongly prefers to chose one particular move of an action variable.
game_prefer_outcomes(game, player1 = NULL, player2 = NULL, player3 = NULL, ..., player.prefs = list(player1 = player1, player2 = player2, player3 = player3, ...))
game_prefer_outcomes(game, player1 = NULL, player2 = NULL, player3 = NULL, ..., player.prefs = list(player1 = player1, player2 = player2, player3 = player3, ...))
player1 |
A formula describing which utility levels should be added to the current utility function of player 1 (see example). If NULL we don't add utilities for player 1. Similar for the other players 2-4. |
... |
additional formulas for games with more than 4 players. |
player.prefs |
by default equal to |
If you want to fix mixed strategies, use the he related function game_fix_action_preferences
transforms the corresponding action into a move of nature.
For fixing pure strategies game_fix_action_preferences
is preferable when using the gambit-logit
solver that can
find sequential equilibria, by using logit trembles.
Other Fix Actions: game_fix_action_preferences
,
game_fix_actions
Other Preferences: game_fix_action_preferences
,
game_set_preferences
,
pref_change_params
,
pref_custom
, pref_envy
,
pref_heterogeneous_players
,
pref_ineqAv
, pref_lossAv
,
pref_payoff
See make_game_options
for a description of the
available options.
game_set_options(game, ...)
game_set_options(game, ...)
Other Game Options: make_game_options
This function sets players preferences to a parametrized preference type. To specify completely custom preferences use game_set_util_fun instead.
game_set_preferences(game, pref)
game_set_preferences(game, pref)
game |
The game object |
pref |
A preference created with a function starting with |
Other Preferences: game_fix_action_preferences
,
game_prefer_outcomes
,
pref_change_params
,
pref_custom
, pref_envy
,
pref_heterogeneous_players
,
pref_ineqAv
, pref_lossAv
,
pref_payoff
Other Modify Game: game_change_param
,
game_copy
, pref_change_params
With the default arguments the internal gtree solver is used to find all pure strategy subgame perfect equilibria of the game.
game_solve_spe(game, mixed = FALSE, just.spe = TRUE, use.gambit = mixed | !just.spe, verbose = isTRUE(game$options$verbose >= 1), gambit.command = NULL, ...)
game_solve_spe(game, mixed = FALSE, just.spe = TRUE, use.gambit = mixed | !just.spe, verbose = isTRUE(game$options$verbose >= 1), gambit.command = NULL, ...)
game |
the game object created with new_game |
use.gambit |
solve via Gambit. Changing |
Other eq: eq_cond_expected_outcomes
,
eq_cond_outcomes
,
eq_expected_outcomes
, eq_li
,
eq_outcomes
, eq_tables
,
game_gambit_solve_qre
,
game_gambit_solve
Write game as a Gambit efg file
game_write_efg(game, file.with.dir = file.path(dir, file), file = tg.efg.file.name(game$tg), dir = getwd(), verbose = !isTRUE(game$options$verbose == 0))
game_write_efg(game, file.with.dir = file.path(dir, file), file = tg.efg.file.name(game$tg), dir = getwd(), verbose = !isTRUE(game$options$verbose == 0))
game |
The game object |
file.with.dir |
The file with full path. If NULL create a default name |
file |
The file name without directory |
dir |
The directory of a file |
Other Gambit: game_gambit_solve
Return a data frame of all possible outcomes
get_outcomes(game, reduce.cols = TRUE)
get_outcomes(game, reduce.cols = TRUE)
game |
the game object defined with |
reduce.cols |
if TRUE remove some technical columns |
Returns logical vector replacing NA by FALSE
is_true(vec)
is_true(vec)
vec |
A vector of logical values (possible containing NA) or values that can be transformed to logicals |
Other Helper Functions: case_distinction
new_game
Specify the game options inside new_game
make_game_options(verbose = TRUE, ...)
make_game_options(verbose = TRUE, ...)
game_set_options
Other Build Game: game_compile
,
make_game_params
, stage
Other Game Options: game_set_options
new_game
. To change the parameters of an existing game call game_change_params
.Specify the game parameters
This function is only to be used inside new_game
. To change the parameters of an existing game call game_change_params
.
make_game_params(numPlayers = 2, ...)
make_game_params(numPlayers = 2, ...)
Other Build Game: game_compile
,
make_game_options
, stage
Other Game Parameters: game_change_param
Specify a random move of nature in a stage
natureMove(name, set, probs = NULL, table = NULL, fixed = NULL, tremble.prob = NULL, ...)
natureMove(name, set, probs = NULL, table = NULL, fixed = NULL, tremble.prob = NULL, ...)
name |
The variable name of the variable |
set |
The set of different values. Can be a rhs only formula. |
probs |
The probability of each element in set. If NULL all moves are equally likely. Can be a rhs formula |
Other Define Stage: action
,
stage
See the examples on gtree website for detailed explanation.
new_game(gameId, params = game_params(), options = make_game_options(), stages, variant = "", check = TRUE)
new_game(gameId, params = game_params(), options = make_game_options(), stages, variant = "", check = TRUE)
Change the parameters of a preference object
pref_change_params(pref, ..., params = list(), label = NULL, players = 1:2, numPlayers = length(players))
pref_change_params(pref, ..., params = list(), label = NULL, players = 1:2, numPlayers = length(players))
Other Modify Game: game_change_param
,
game_copy
,
game_set_preferences
Other Preferences: game_fix_action_preferences
,
game_prefer_outcomes
,
game_set_preferences
,
pref_custom
, pref_envy
,
pref_heterogeneous_players
,
pref_ineqAv
, pref_lossAv
,
pref_payoff
Create a custom preference
pref_custom(..., params = NULL, label = "custom")
pref_custom(..., params = NULL, label = "custom")
... |
Unquoted that describe the utility as a function of the parameters of the game and possible preference parameters. Should be ordered by players. Names are irrelevant. |
params |
An optional list of parameters that are used in the formulas above |
label |
A label for the preference, should contain info about the parameters |
type |
A general type label independet of the parameters |
Other Preferences: game_fix_action_preferences
,
game_prefer_outcomes
,
game_set_preferences
,
pref_change_params
,
pref_envy
,
pref_heterogeneous_players
,
pref_ineqAv
, pref_lossAv
,
pref_payoff
Fehr-Schmidt inequality aversion with envy only
pref_envy(alpha = 0, player = 1:numPlayers, numPlayers = 2, ...)
pref_envy(alpha = 0, player = 1:numPlayers, numPlayers = 2, ...)
alpha |
the degree of envy |
player |
player(s) for which the preferences apply. Per default 1:2 |
numPlayers |
number of players in game per default 2 |
Other Preferences: game_fix_action_preferences
,
game_prefer_outcomes
,
game_set_preferences
,
pref_change_params
,
pref_custom
,
pref_heterogeneous_players
,
pref_ineqAv
, pref_lossAv
,
pref_payoff
Combine preferences for different players
pref_heterogeneous_players(..., prefs = list(...), label = NULL)
pref_heterogeneous_players(..., prefs = list(...), label = NULL)
... |
all preferences ordered by players |
prefs |
alternatively the preferences as a list object |
label |
optional label of preferences. If NULL the individual labels will be pasted together |
type |
label of the combined preference type |
Other Preferences: game_fix_action_preferences
,
game_prefer_outcomes
,
game_set_preferences
,
pref_change_params
,
pref_custom
, pref_envy
,
pref_ineqAv
, pref_lossAv
,
pref_payoff
Fehr-Schmidt inequality aversion.
pref_ineqAv(alpha = 0, beta = 0, player = 1:numPlayers, numPlayers = 2, ...)
pref_ineqAv(alpha = 0, beta = 0, player = 1:numPlayers, numPlayers = 2, ...)
alpha |
the degree of envy |
beta |
the degree of guilt |
player |
player(s) for which the preferences apply. Per default 1:2 |
numPlayers |
number of players in game per default 2 |
Other Preferences: game_fix_action_preferences
,
game_prefer_outcomes
,
game_set_preferences
,
pref_change_params
,
pref_custom
, pref_envy
,
pref_heterogeneous_players
,
pref_lossAv
, pref_payoff
'Linear loss aversion preferences with a single reference point
pref_lossAv(lambda = 2, r = 0, player = 1:numPlayers, numPlayers = 2)
pref_lossAv(lambda = 2, r = 0, player = 1:numPlayers, numPlayers = 2)
lambda |
factor by which losses loom larger than gains (default = 2) |
r |
The reference point, by default 0. Can be a vector in order to have different reference points for different players. |
player |
player(s) for which the preferences apply. Per default 1:2 |
numPlayers |
number of players in game per default 2 |
Other Preferences: game_fix_action_preferences
,
game_prefer_outcomes
,
game_set_preferences
,
pref_change_params
,
pref_custom
, pref_envy
,
pref_heterogeneous_players
,
pref_ineqAv
, pref_payoff
This means the player is simply a risk neutral expected payoff maximizer.
pref_payoff(player = 1:2, ...)
pref_payoff(player = 1:2, ...)
player |
player(s) for which the preferences apply. Per default 1:2 |
Other Preferences: game_fix_action_preferences
,
game_prefer_outcomes
,
game_set_preferences
,
pref_change_params
,
pref_custom
, pref_envy
,
pref_heterogeneous_players
,
pref_ineqAv
, pref_lossAv
Specify a stage for a game
stage(name, player = NULL, condition = NULL, observe = NULL, compute = NULL, nature = NULL, actions = NULL, ...)
stage(name, player = NULL, condition = NULL, observe = NULL, compute = NULL, nature = NULL, actions = NULL, ...)
name |
Name of the stage |
player |
The player who acts in this stage. Can be a rhs formula. If an action is chosen in the stage, there must be a unique player. If it is a stage in which no actions take place, the player variable multiple players can be set. Each player observes the variables specified under observe. |
condition |
A logical condition specifying whether the stage will be run. Can be a rhs formula. If it evaluates to FALSE the stage will not be shown, i.e. no observations are made and no actions are chosen. Also no computations in this stage take place. |
observe |
A vector of variable names specifying which variables are observed by the player(s) at this stage. Is relevant to correctly specify the information sets in the extensive form game. |
compute |
A list of formulas like 'compute=list(payoff_1 ~ x-5)'. The lhs specifies a variable name and the rhs a DETERMINISTIC formula. The variables are computed at the beginning of the stage before actions and moves of nature take place. This means they can be used e.g. in formulas for action sets of the same stage. |
nature |
A list of moves of nature, i.e. random variables from a finite set. E.g. nature=list(natureMove("proposer",c(1,2),prob=c(0.4,0.6)). |
actions |
A list of actions. E.g. actions=list(action("offer",~0:cake_size) |
Other Define Stage: action
,
natureMove
Other Build Game: game_compile
,
make_game_options
,
make_game_params