Expert scorecards#

Weigh the factors of an expert scorecard from pairwise comparisons, as the analytic hierarchy process does. ahp_weights() turns a matrix of comparisons into weights, ahp_consistency() measures how consistent the comparisons are, and combine_weights() joins the weights of factors and sub-factors into global weights.

See Expert scorecards for a worked example and how to read the consistency ratio.

ahp_weights(matrix, *[, method, rtol])

Weigh factors from a pairwise comparison matrix, as the analytic hierarchy process does.

ahp_consistency(matrix, *[, rtol])

Measure how consistent the judgments of a pairwise comparison matrix are.

combine_weights(parent, children)

Combine the weights of factors and of their sub-factors into global weights.

kivi.expert.ahp_weights(matrix, *, method='eigenvector', rtol=0.001)#

Weigh factors from a pairwise comparison matrix, as the analytic hierarchy process does.

Entry \(a_{ij}\) of the matrix says how much more important factor \(i\) is than factor \(j\), on Saaty’s scale from 1 (equally important) to 9 (extremely more important), and its reciprocal \(a_{ji} = 1 / a_{ij}\) the other way round. The weights are proportional to the principal eigenvector of the matrix, or to the geometric means of its rows, and sum to 1.

Parameters:
matrixDataFrame or array_like

A square matrix of pairwise comparisons with a diagonal of 1 and reciprocal entries, \(a_{ij} a_{ji} = 1\), to the relative tolerance rtol. A DataFrame names the factors by its index, which must equal its columns; an array numbers them from 0.

method{“eigenvector”, “geometric”}, default “eigenvector”

"eigenvector" is Saaty’s principal eigenvector. "geometric" takes the geometric mean of each row, which gives the same weights for a consistent matrix and needs no eigenvalue computation.

rtolfloat, default 1e-3

The relative tolerance of the checks that the diagonal is 1 and that \(a_{ij} a_{ji}\) is 1. The default accepts reciprocals rounded to four decimals, such as 0.3333 for 1 / 3 or 0.1429 for 1 / 7; entries rounded to two decimals, such as 0.14, need a larger value, or write the fractions. Must be finite and non-negative. The matrix is used as it is given, so entries that are not exact reciprocals move the weights by about as much as they differ from them.

Returns:
pandas.Series

The weights, named weight and indexed by the factors in an index named factor. They are positive and sum to 1.

Raises:
ValueError

If matrix is not a square matrix of finite, positive numbers with a diagonal of 1 and reciprocal entries, a DataFrame’s index and columns differ, repeat a label or form a MultiIndex, method is not one of the two names, or rtol is not a finite, non-negative number.

See also

ahp_consistency

Check how consistent the judgments in the matrix are.

combine_weights

Weigh the sub-factors of a hierarchy.

Notes

For a consistent matrix, \(a_{ij} = w_i / w_j\), both methods give \(w\). Otherwise the eigenvector method solves \(A w = \lambda_{\max} w\) and the geometric method takes

\[w_i = \frac{\left(\prod_j a_{ij}\right)^{1/n}} {\sum_k \left(\prod_j a_{kj}\right)^{1/n}}.\]

For two or three factors the two methods give the same weights for every reciprocal matrix; they differ slightly from four factors on if the judgments are inconsistent. The legacy 0.2.x AHP class used the geometric method.

Examples

>>> import pandas as pd
>>> from kivi.expert import ahp_weights
>>> factors = ["sales", "margin", "leverage"]
>>> matrix = pd.DataFrame(
...     [[1, 2, 4], [1 / 2, 1, 2], [1 / 4, 1 / 2, 1]], index=factors, columns=factors
... )
>>> ahp_weights(matrix).round(4)
factor
sales       0.5714
margin      0.2857
leverage    0.1429
Name: weight, dtype: float64
kivi.expert.ahp_consistency(matrix, *, rtol=0.001)#

Measure how consistent the judgments of a pairwise comparison matrix are.

Judgments are consistent when they agree with each other: if factor A is twice as important as B and B twice as important as C, then A should be four times as important as C. The consistency ratio compares the inconsistency of the matrix with that of random judgments.

Parameters:
matrixDataFrame or array_like

A square matrix that compares at most 15 factors, as for ahp_weights.

rtolfloat, default 1e-3

The relative tolerance of the checks that the diagonal is 1 and that reciprocal entries multiply to 1, as for ahp_weights.

Returns:
pandas.Series

The statistics, indexed by lambda_max, ci, ri and cr in an index named statistic:

  • lambda_max, the principal eigenvalue of the matrix, at least the number of factors \(n\);

  • ci, the consistency index, zero for consistent judgments;

  • ri, Saaty’s random index for \(n\);

  • cr, the consistency ratio ci / ri.

Raises:
ValueError

If matrix is not a valid comparison matrix or rtol is not a finite, non-negative number, as for ahp_weights, or matrix compares more than 15 factors, the largest order with a tabulated random index.

See also

ahp_weights

The weights that the matrix gives.

Notes

With \(n\) factors,

\[\mathrm{CI} = \frac{\lambda_{\max} - n}{n - 1}, \qquad \mathrm{CR} = \frac{\mathrm{CI}}{\mathrm{RI}}.\]

Saaty suggested that judgments with a CR above 0.1 should be reviewed, and later (1994) tightened the limit to 0.05 for three factors and 0.08 for four. These are rules of thumb, not tests, and the function does not raise for a larger CR. Every matrix of one or two factors is consistent, so its ci, ri and cr are 0. For a reciprocal matrix \(\lambda_{\max} \ge n\); a computed value below \(n\), from rounding, is set to \(n\).

The random index is the average CI of random reciprocal matrices on Saaty’s scale. The table, for 3 to 15 factors, is K. Tone’s, The Analytic Hierarchy Process: Decision Making (in Japanese, 1986), as T. Obata and S. Shiraishi list it in Table 2 of arXiv:2607.07950. Saaty’s books print 1.48 for 12 factors instead of 1.53, which is generally taken for a typographical error. The legacy 0.2.x class estimated \(\lambda_{\max}\) as the mean of \((A w)_i / w_i\) for the geometric-mean weights, which equals the exact value for up to three factors, and used almost the same random indices.

Examples

>>> import numpy as np
>>> from kivi.expert import ahp_consistency
>>> matrix = np.array([[1, 3, 5], [1 / 3, 1, 3], [1 / 5, 1 / 3, 1]])
>>> ahp_consistency(matrix).round(4)
statistic
lambda_max    3.0385
ci            0.0193
ri            0.5800
cr            0.0332
dtype: float64
kivi.expert.combine_weights(parent, children)#

Combine the weights of factors and of their sub-factors into global weights.

In a hierarchy, each factor is split into sub-factors that are weighed against each other. The global weight of a sub-factor is the weight of its factor times its weight within the factor, so the global weights of all sub-factors still sum to 1.

Parameters:
parentSeries

The weights of the factors, such as the result of ahp_weights. They must be finite and non-negative and sum to 1 to a relative tolerance of 1e-6.

childrenmapping

For each factor of parent, a Series of the weights of its sub-factors, indexed by their labels, such as the result of ahp_weights. Each must be finite and non-negative, sum to 1 and name each sub-factor once. Every factor of parent needs an entry, and no other key is allowed: a factor without sub-factors takes a Series of one label with the weight 1.

Returns:
pandas.Series

The global weights, named weight and indexed by factor and sub_factor, in the order of parent and then of each factor’s sub-factors.

Raises:
ValueError

If parent is not a Series, children is not a mapping of Series, its keys are not the factors of parent, or a set of weights is not finite, non-negative and summing to 1.

See also

ahp_weights

Weigh the factors and the sub-factors of each factor.

Notes

The function joins two levels, the factors and their sub-factors. For a hierarchy of more levels, combine them level by level: pass the global weights with the factor level dropped, weights.droplevel("factor"), as parent of the next call, whose result then names the last two levels. This needs sub-factors of different factors to have different names, because the labels of parent must be distinct.

Examples

>>> import pandas as pd
>>> from kivi.expert import combine_weights
>>> parent = pd.Series({"finance": 0.75, "market": 0.25})
>>> children = {
...     "finance": pd.Series({"leverage": 0.6, "margin": 0.4}),
...     "market": pd.Series({"position": 1.0}),
... }
>>> combine_weights(parent, children)
factor   sub_factor
finance  leverage     0.45
         margin       0.30
market   position     0.25
Name: weight, dtype: float64