Fundamentals 3 min read

Implementing the Analytic Hierarchy Process (AHP) in Python: Step-by-Step Guide

This article demonstrates a Python implementation of the Analytic Hierarchy Process (AHP), explaining the function, consistency check, and providing example matrices that show both inconsistent and acceptable results with calculated eigenvalues and weight vectors.

Model Perspective
Model Perspective
Model Perspective
Implementing the Analytic Hierarchy Process (AHP) in Python: Step-by-Step Guide

The Analytic Hierarchy Process (AHP) is a decision‑making method that derives priority weights from a pairwise comparison matrix. The following Python function implements the core AHP calculations, including eigenvalue extraction, consistency index (CI) evaluation, and weight normalization.

<code>def ahp(judge_matrix):
    """ Compute AHP model
    Parameters:
        judge_matrix: pairwise comparison matrix
    Returns:
        ahp_weights: weight vector
    """
    judge_matrix = np.array(judge_matrix, dtype=np.float64)  # convert to numpy array
    n = judge_matrix.shape[0]  # matrix dimension
    eigenvalues, eigenvectors = np.linalg.eig(judge_matrix)  # eigenvalues and eigenvectors
    value_max = eigenvalues.max()  # maximum eigenvalue
    value_max_index = eigenvalues.argmax()  # index of max eigenvalue
    CI = (value_max - n) / (n - 1)  # consistency index
    RI_list = [0,0,0,0.58,0.9,1.12,1.24,1.32,1.41,1.45,1.49]  # random index list
    if CI / RI_list[n] > 0.1:
        return 'Judgement Matrix is NOT consistent'
    else:
        print('The Max Eigenvalue is ', value_max)
        ahp_weights = eigenvectors[:, value_max_index]
        ahp_weights = ahp_weights / ahp_weights.sum()  # normalize
        return ahp_weights
</code>

Example 1 uses an inconsistent matrix:

<code>weights = ahp(np.array([[1,2,3],[1/2,1,9],[1/3,1/9,1]]))
</code>

The function returns:

'Judgement Matrix is NOT consistent'

indicating that the pairwise comparisons violate the consistency threshold.

After adjusting the matrix to improve consistency:

<code>weights = ahp(np.array([[1,2,3],[1/2,1,1],[1/3,1,1]]))
</code>

The output shows an acceptable consistency:

The Max Eigenvalue is  (3.0182947072896287+0j)

and the resulting weight vector is:

array([0.54994561+0.j, 0.24021087+0.j, 0.20984352+0.j])

This demonstrates how the AHP implementation can be used to assess decision criteria, verify matrix consistency, and obtain normalized priority weights.

Pythondecision makingAHPAnalytic Hierarchy ProcessConsistency Check
Model Perspective
Written by

Model Perspective

Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.