gameanalysis.utils module

gameanalysis.utils.acartesian2(*arrays)[source]

Array cartesian product in 2d

Produces a new ndarray that has the cartesian product of every row in the input arrays. The number of columns is the sum of the number of columns in each input. The number of rows is the product of the number of rows in each input.

Parameters:*arrays ([ndarray (xi, s)]) –
gameanalysis.utils.acomb(n, k, repetition=False)[source]

Compute an array of all n choose k options

The result will be an array shape (m, n) where m is n choose k optionally with repetitions.

gameanalysis.utils.axis_to_elem(array, axis=-1)[source]

Converts an axis of an array into a unique element

In general, this returns a copy of the array, unless the data is contiguous. This usually requires that the last axis is the one being merged.

Parameters:
  • array (ndarray) – The array to convert an axis to a view.
  • axis (int, optional) – The axis to convert into a single element. Defaults to the last axis.
gameanalysis.utils.comb(n, k)[source]

Return n choose k

This function works on arrays, and will properly return a python integer object if the number is too large to be stored in a 64 bit integer.

gameanalysis.utils.comb_inv(cmb, k)[source]

Return the inverse of comb

Given a number of combinations, and the size of subset we’re choosing, compute the integer lower bound, i.e. return n* such that comb(n*, k) <= cmb < comb(n* + 1, k).

gameanalysis.utils.deprecated(func)[source]

Mark a function as deprecated

gameanalysis.utils.elem_to_axis(array, dtype, axis=-1)[source]

Converts and array of axis elements back to an axis

gameanalysis.utils.game_size(players, strategies)[source]

Number of profiles in a symmetric game with players and strategies

gameanalysis.utils.game_size_inv(size, players)[source]

Inverse of game_size

Given a game size and a number of players, return a lower bound on the number of strategies s* such that game_size(players, s*) <= size < game_size(players, s* + 1)`.

gameanalysis.utils.geometric_histogram(n, p)[source]

Return the histogram of n draws from a geometric distribution

This function computes values from the same distribution as np.bincount(np.random.geometric(p, n) - 1) but does so more efficiently.

class gameanalysis.utils.hash_array(array)[source]

Bases: object

gameanalysis.utils.is_sorted(iterable, *, key=None, reverse=False, strict=False)[source]

Returns true if iterable is sorted

Parameters:
  • iterable (iterable) – The iterable to check for sorted-ness.
  • key (x -> y, optional) – Apply mapping function key to iterable prior to checking. This can be done before calling, but this ensures identical calls as sorted.
  • reverse (bool, optional) –
  • and reverse function as they for sorted (key) –
gameanalysis.utils.iunique(iterable)[source]

Return an iterable of unique items ordered by first occurrence

gameanalysis.utils.memoize(member_function)[source]

Memoize computation of single object functions

gameanalysis.utils.multinomial_mode(p, n)[source]

Compute the mode of n samples from multinomial distribution p.

Notes

Algorithm from [3], notation follows [4].

[3]Finucan 1964. The mode of a multinomial distribution.
[4]Gall 2003. Determination of the modes of a Multinomial distribution.
gameanalysis.utils.one_line(string, line_width=80)[source]

If string s is longer than line width, cut it off and append “…”

gameanalysis.utils.only(iterable)[source]

Return the only element of an iterable

Throws a value error if the iterable doesn’t contain only one element

gameanalysis.utils.prefix_strings(prefix, num)[source]

Returns a list of prefixed integer strings

gameanalysis.utils.prod(collection)[source]

Product of all elements in the collection

gameanalysis.utils.random_strings(min_length, max_length=None, digits='abcdefghijklmnopqrstuvwxyz')[source]

Return a random string

Parameters:
  • min_length (int) – The minimum length string to return.
  • max_length (int, optional) – The maximum length string to return. If None or unspecified, this is the same as min_length.
  • num (int, optional) – The number of strings to return. If None or unspecified this returns a single string, otherwise it returns a generator with length num.
  • digits (str, optional) – The optional digits to select from.
gameanalysis.utils.repeat(iterable, reps)[source]

Repeat each element of iterable reps times

gameanalysis.utils.simplex_project(array)[source]

Return the projection onto the simplex

gameanalysis.utils.unique_axis(array, axis=-1, **kwargs)[source]

Find unique axis elements

Parameters:
  • array (ndarray) – The array to find unique axis elements of
  • axis (int, optional) – The axis to find unique elements of. Defaults to the last axis.
  • **kwargs (flags) – The flags to pass to numpys unique function
Returns:

  • uniques (ndarray) – The unique axes as rows of a two dimensional array.
  • *args – Any other results of the unique functions due to flags