gameanalysis.utils module

Utilities helpful for game analysis

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.allclose_perm(aarr, barr, **kwargs)[source]

allclose but for any permutation of aarr and barr

Parameters
  • aarr (array_like) – Identically sized arrays of floats. This will determine if any permutation of their first axis will make allclose true.

  • barr (array_like) – Identically sized arrays of floats. This will determine if any permutation of their first axis will make allclose true.

  • kwargs – Additional arguments to pass to isclose.

gameanalysis.utils.allequal_perm(aarr, barr)[source]

Test if two arrays are equal for any permutation of their first axis

Parameters
  • aarr (array_like) – Identically sized arrays. This will determine if any permutation of their first axis will make them equal.

  • barr (array_like) – Identically sized arrays. This will determine if any permutation of their first axis will make them equal.

gameanalysis.utils.asubsequences(array, seq=2, axis=0)[source]

Return sub-sequences of an array

This returns a new array with leading dimension seq representing a length seq sub-sequence of the input array. The following dimensions are preserved except for axis, which is seq - 1 shorter due to edge effects. This method returns a view, so no data copying happens.

Parameters
  • array (ndarray) – Array to take subsequences over.

  • seq (int, optional) – Length of subsequences to take.

  • axis (int, optional) – The axis to treat as the sequence to take sub-sequences of.

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

Converts and array of axis elements back to an axis

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.check(condition, message, *args, **kwargs)[source]

Check state and raise exception if not valid

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.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(num, prob)[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(prob, num) - 1) but does so more efficiently.

gameanalysis.utils.hash_array(array)[source]

Hash an array

gameanalysis.utils.iload(jfp, **kwargs)[source]

Load a file as a generator of json objects

gameanalysis.utils.iloads(jstring, **kwargs)[source]

Load a string as a generator of json objects

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.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.subsequences(iterable, seq=2)[source]

Return subsequences of an iterable

Each element in the generator will be a tuple of seq elements that were ordered in the iterable.

gameanalysis.utils.timeout(seconds=None, exception=<class 'TimeoutError'>)[source]

Timeout code

A TimeoutError is raised if code doesn’t finish within seconds. The result can be used as a decorator or as a context manager. This will work in child threads, but as a significant performance hit.

Parameters
  • seconds (int, optional) – The number of seconds until timeout. If omitted, no timeout is used, and this is essentially a no op

  • exception (Exception, optional) – The exception to raise. IF omitted, a generic TimeoutError is used.