Priority calculations module

All pairwise matrix to priority vector calculations

@author: Dr. Bill Adams

pyanp.priority.geom_avg(vals) → float[source]

Compute the geometric average of a list of values.

Parameters:vals – A list-like of numbers
Returns:The geometric average of the values.
pyanp.priority.geom_avg_mat(mat, coeffs=None) → numpy.ndarray[source]

Computes the geometric average of the columns of a matrix.

Parameters:
  • mat – Must be an numpy.array of shape [nRows, nCols]
  • coeffs – If not None, it is a list like object with nColsOfMat elements. We multiply column 0 of mat by coeffs[0], column 1 of mat by coeffs[1], etc and then do the geometric average of the columns. Essentially this weights the columns.
Returns:

An np.array of dimension [nRowsOfMat], i.e. a vector. that is the weighted geometric average of the columns of the matrix mat.

pyanp.priority.harker_fix(mat: numpy.ndarray) → numpy.ndarray[source]

Performs Harker’s fix on the numpy matrix mat. It returns a copy with the fix. The function does not change the matrix mat.

Parameters:mat – A square numpy.
Returns:A copy of mat with Harker’s fix applied to it
pyanp.priority.incon_gci(mat: numpy.ndarray, use_harker: bool = True, only_count_nonzero=True) → float[source]

Calculates the inconsistency of a pairwise matrix using the GCI formula from

Parameters:
  • mat – A numpy.array of shape [size,size] of pairwise comparisons.
  • use_harker – Should we apply Harker’s fix before the calculation?
  • only_count_nonzero – Should we only average nonzero comparisons (this is not the official defn of gci)
Returns:

The inconsistency.

pyanp.priority.incon_std(mat: numpy.ndarray, error: float = 1e-10, use_harker: bool = True) → float[source]

Calculates the inconsistency of a pairwise matrix using the standard Saaty AHP/ANP theoretic formula.

Parameters:
  • mat – A numpy.array of shape [size,size] of pairwise comparisons.
  • error – The error to use for the pri_eigen calculation
  • use_harker – Should we apply Harker’s fix before the calculation?
Returns:

The inconsistency.

pyanp.priority.inconsistency_divisor(mat_or_size) → float[source]

Calculates the inconsistency divisor for a matrix, or the size of a matrix. The inconsistency divisor is what you divide (eigenvalue - size) by to get the inconsistency.

Parameters:mat_or_size – Either a pairwise matrix, or simply the size of the pairwise matrix (which is what determines the inconsistency divisor).
Returns:The inconsistency divisor
pyanp.priority.mat_gci(mat1: numpy.ndarray, mat2: numpy.ndarray, only_count_nonzero=True) → float[source]

Calculates the GCI of two matrices :param mat1: :param mat2: :return:

pyanp.priority.prerr_euclidratio(pwmat, privec)[source]

Calculates the euclidean distance error between the pairwise matrix and the ratio matrix of a priority vector.

This calculates using the following formula

\[\sqrt{ \sum_{i,j} \left(\frac{pwmat[i, j] - privec[i]}{privec[j]} \right)^2}\]
Parameters:
  • pwmat – A numpy.array of shape [size, size] of pairwise comparisons.
  • privec – A numpy.array of share [size] of the priority vector to compare this pairwise matrix to.
Returns:

The error/distance between the ratio matrix and the matrix.

pyanp.priority.prerr_ratio_avg(pwmat, privec)[source]

Calculates priority error using the arithmetic average of ratio distance of pwmat from the ratio matrix of privec

It averages:

\[ratio\_greater\_1(pwmat[i, j], (privec[i]/privec[j])) - 1\]

where

\[\begin{split}ratio\_greater\_1(a,b) = \begin{cases} 1 & \hbox{ if a or b = 0 } \\ max(a/b, b/a) & \hbox{otherwise} \end{cases}.\end{split}\]
Parameters:
  • pwmat – A numpy.array of shape [size, size] of pairwise comparisons
  • privec – A numpy.array of shape [size] of priortiy vector
Returns:

The ratio average priority vector

pyanp.priority.prerr_ratio_prod(pwmat, privec)[source]

Calculates priority error using the geometric average of ratios of pwmat and the ratio matrix of privec, the formula is:

\[\sqrt[n(n-1)/2]{\prod_{i=1, j=i+1} ratio\_greater\_1(pwmat[i, j], privec[i]/privec[j])}\]
Parameters:
  • pwmat – A numpy.array of shape [size, size] of pairwise comparisons
  • privec – A numpy.array of shape [size] of priortiy vector
Returns:

The calculated error

pyanp.priority.pri_eigen(mat: numpy.ndarray, error: float = 1e-10, use_harker: bool = False, return_eigenval: bool = False)[source]

Calculates the largest eigen vector of a matrix.

Parameters:
  • mat – A square numpy array.
  • use_harker=False – Should we apply Harker’s fix before computing?
  • return_eigenval=False – If True it returns only the eigenvalue, otherwise only returns the eigenvector.
Return numpy.array:
 

The largest eigenvector that is the normalized (sum to 1) largest eigenvector as a numpy.array of shape [size] if return_eigenval=False, otherwise returns the eigenvalue as a number.

pyanp.priority.pri_expeigen(mat, error=1e-10)[source]

Calculates priorities using exponential (aka multiplicative) eigenvector

Parameters:
  • mat – An numpy.array of shape [size, size] of pairwise comparisions.
  • error=1e-10 – The convergence error term
Return numpy.array:
 

The resulting exponential eigenvector as a numpy.array of shape [size]

pyanp.priority.pri_geomavg(mat)
Calculates the priorities using the geometric mean method, aka Log Least
Squares Method (LLSM).
Parameters:mat – An numpy.array of dimension [size,size]
Return numpy.array:
 The resulting llsm priority vector as a numpy.array of shape [size]
pyanp.priority.pri_llsm(mat)[source]
Calculates the priorities using the geometric mean method, aka Log Least
Squares Method (LLSM).
Parameters:mat – An numpy.array of dimension [size,size]
Return numpy.array:
 The resulting llsm priority vector as a numpy.array of shape [size]
pyanp.priority.ratio_greater_1(a, b)[source]

The ratio of a to b (or b to a) that is larger than or equal to 1.

Parameters:
  • a – A numerical value for the ratio calculation
  • b – Another numerical value
Returns:

1 if a or b is 0, otherwise max(a/b, b/a)

pyanp.priority.ratio_mat(pv) → numpy.ndarray[source]

Returns the ratio matrix of a vector

Parameters:pv – An array-like object with len(pv)=size
Returns:A numpy.array of shape [size, size] of the ratios
pyanp.priority.size_array_like(mat_or_size)[source]

Returns the size of an array like or integer

Parameters:mat_or_size – Either an integer (specifying the size) or an array-like or numpy.array of shape [size, size]. If array-like list of lists, we only use len(mat_or_size), we do not check that the array-like is actually square.
Returns:The parameter if it was an integer, len(mat_or_size) if param is a list, or mat_or_size.shape[0] if mat_or_size is a numpy.ndarray
pyanp.priority.utmrowlist_to_npmatrix(list_of_votes) → numpy.ndarray[source]

Convert a list of values to a pairwise matrix, assuming the list is the upper triangular part only

Parameters:list_of_votes – An array like, the first elements are the top row of the UTM part of the matrix. Then it goes to the second row, etc.
Returns:A numpy.array of the full pairwise comparison matrix.