Limit matrix calculations

All ANP limit matrix functions live here.

Contains all limit matrix calculations

pyanp.limitmatrix._mat_pow2(mat, power, rescale=False)[source]

Calculates \(mat^N\) where \(N \geq power\) and N is a power of 2. It does this by squaring mat, and squaring that, etc, until it reaches the desired level. It takes at most floor(log_2(power))+1 matrix multiplications to do this, which is much preferred for large powers.

Parameters:
  • mat – The numpy array to raise to a power.
  • power – The power to be greater than or equal to
  • rescale – If True, after each mult we divide the matrix by its max
Returns:

The resulting power of the matrix

pyanp.limitmatrix.calculus(mat, error=1e-10, max_iters=5000, use_hierarchy_formula=True, col_scale_type=None, start_pow=None)[source]

Calculates the ‘Calculus Type’ limit matrix from superdecisions

Parameters:
  • mat – The scaled supermatrix to calculate the limit matrix of
  • error – The maximum error to allow between iterations
  • max_iters – The maximum number of iterations before we give up, after we calculate the start power
  • use_hierarchy_formula – If True and the matrix is for a hierarchy we use that formula instead.
  • col_scale_type – A string if ‘all’ it scales mat1 by max(mat1) and similarly for mat2 otherwise, it scales by column
  • start_pow – If None, we calculate the starting power to look for convergence, otherwise this should be an integer > 0
Returns:

The calculats limit matrix as a numpy array.

pyanp.limitmatrix.hiearhcy_formula(mat)[source]

Uses the hierarchy formula to calculate the limit matrix. This is essentially the normalization of the sum of higher powers of mat.

Parameters:mat – A square nump.array that you wish to find the limit matrix of, using the hiearchy formula.
Returns:The limit matrix, unless the matrix was not a hiearchy. If the matrix was not a hierarchy we return None
pyanp.limitmatrix.hierarchy_nodes(mat)[source]

Returns the indices of the nodes that are hierarchy ones. The others are not hierachy

Parameters:mat – A supermatrix (scaled or non-scaled, both work).
Returns:List of indices that are the nodes which are hierarhical.
pyanp.limitmatrix.limit_newhierarchy(mat, with_limit=False, error=1e-10, col_scale_type=None, max_count=1000)[source]

Performs the new hiearchy limit matrix calculation

Parameters:
  • mat – The matrix to perform the calculation on.
  • with_limit – Do we include the final limit step?
Returns:

The resulting numpy array

pyanp.limitmatrix.limit_sinks(mat, straight_normalizer=True)[source]

Performs the limit with sinks calculation. We break up the matrix into sinks and nonsinks, and use those pieces.

Parameters:
  • mat – The matrix to do the limit sinks calculation on.
  • straight_normalizer – If False we normalize at each step, if True we normalize at the end.
Returns:

The resulting numpy array result.

pyanp.limitmatrix.normalize(mat, inplace=False)[source]

Makes the columns of a matrix add to 1 (unless the column summed to zero, in which case it is left unchanged) Does this by dividing each column by the sum of that column.

Parameters:
  • mat – The matrix to normalize
  • inplace – If true normalizes the matrix sent in, otherwise it leaves that matrix alone, and returns a normalized copy
Returns:

If inplace=False, it returns the normalized matrix, leaving the param mat unchanged. Otherwise it returns nothing and normalizes the param mat.

pyanp.limitmatrix.normalize_cols_dist(mat1, mat2, tmp1=None, tmp2=None, tmp3=None, col_scale_type=None)[source]

Calculates the distance between matrices mat1 and mat2 after they have been column normalized. tmp1, tmp2, tmp3 are temporary matrices to store the normalized versions of things. This code could be called many times in a limit matrix calculation, and allocating and freeing those temporary storage bits could take a lot of cycles. This way you allocate them once at the top level loop of the limit matrix calculation and they are reused again and again.

If you do not wish to avail yourself of this savings, simply leave them as None’s and the algorithm will allocate as appropriate

Parameters:
  • mat1 – First matrix to compare
  • mat2 – The other matrix to compare
  • tmp1 – A temporary storage matrix of same size as mat1 and mat2. If None, it will be allocated inside the fx.
  • tmp2 – A temporary storage matrix of same size as mat1 and mat2. If None, it will be allocated inside the fx.
  • tmp3 – A temporary storage matrix of same size as mat1 and mat2. If None, it will be allocated inside the fx.
  • col_scale_type – A string if ‘all’ it scales mat1 by max(mat1) and similarly for mat2 otherwise, it scales by column
Returns:

The maximum difference between the column normalized versions of mat1 and mat2

pyanp.limitmatrix.priority(matrix, limit_calc=<function calculus>)[source]

Calculates the limit matrix and extracts priority from it. Really just a convenience function.

Parameters:
  • matrix – The scaled supermatrix to calculate the priority for
  • limit_calc – The limit matrix calculation to use
Returns:

The priority as a series

pyanp.limitmatrix.priority_from_limit(limit_matrix)[source]

Calculates the priority from a limit matrix, i.e. sums columns and divides by the number of columns.

Parameters:limit_matrix – The matrix to extract the priority from
Returns:1d numpy array of the priority
pyanp.limitmatrix.two_two_breakdown(mat, upper_right_indices)[source]

Given the indices for the upper right portion of a matrix, break the matrix down into a 2x2 matrix with the submatrices in each piece. Useful for limit matrix calculations that split the problem up into the hierarchical and network components and do separate calculations and then bring them together. :param mat: The matrix to split into

A B
C D

form, where A is the “upper_right_indcies” and D is the opposite

Parameters:upper_right_indices – List of indices of the upper right positions.
Returns:A list of [A, B, C, D] of those matrices
pyanp.limitmatrix.zero_cols(full_mat, non_zero=False)[source]

Returns the list of indices of columns that are zero or non_zero depending on the parameter non_zero

Parameters:
  • mat – The matrix to search over
  • non_zero – If False, returns the indices of columns that are zero, otherwise returns indices of columns that a not zero.
Returns:

A list of indices of columns of the type determined by the parameter non_zero.