Mastering the Analytic Hierarchy Process (AHP) with Python: Step‑by‑Step Guide
This article introduces the Analytic Hierarchy Process (AHP), explains its hierarchical modeling, mathematical foundations, step‑by‑step implementation, and provides complete Python code for constructing comparison matrices, calculating weights, and performing consistency checks to ensure reliable results.
Analytic Hierarchy Process
AHP is a widely used multi‑criteria decision‑making method that decomposes a complex problem into a hierarchy of goal, criteria, and alternatives, using pairwise comparison matrices to derive weights.
Implementation Steps
The typical procedure includes:
Define the hierarchy (goal, criteria, alternatives).
Construct pairwise comparison matrices for each level.
Compute weight vectors (eigenvector or averaging methods).
Calculate consistency ratio (CR) and consistency index (CI) to check matrix consistency.
Aggregate weights across levels to obtain overall priorities.
Evaluate alternatives using the final weight vector.
Mathematical Formulation
For a comparison matrix A = [a_ij], a_ij represents the relative importance of element i over j, with a_ij = 1 / a_ji and a_ii = 1. The principal eigenvalue λ_max and eigenvector w satisfy A w = λ_max w. The consistency index is CI = (λ_max – n)/(n‑1) and the consistency ratio is CR = CI / RI, where RI is the random index for matrix size n.
Python Implementation
The following Python code builds a 4‑criterion comparison matrix (Price, Quality, Design, Brand), computes the eigenvector‑based weights, and performs a consistency check.
<code>import numpy as np
# Build comparison matrix
matrix = np.array([[1, 1/2, 1/3, 1/4],
[2, 1, 1/2, 1/3],
[3, 2, 1, 1/2],
[4, 3, 2, 1]])
# Normalize and compute weights
eig_val, eig_vec = np.linalg.eig(matrix)
max_eig_val = max(eig_val)
max_eig_vec = eig_vec[:, list(eig_val).index(max_eig_val)]
weights = max_eig_vec / sum(max_eig_vec)
for i in range(len(weights)):
print(f"Criterion {i+1} weight: {round(weights[i].real, 3)}")
</code>Sample output shows Brand having the highest weight (≈0.467), followed by Design, Quality, and Price.
Consistency Check
<code># calculate consistency ratio
eigenvalues, eigenvectors = np.linalg.eig(matrix)
max_eigenvalue = max(eigenvalues)
n = len(matrix)
ci = (max_eigenvalue - n) / (n - 1)
RI = [0, 0, 0.58, 0.9, 1.12, 1.24, 1.32, 1.41, 1.45, 1.49]
cr = ci / RI[n-1]
if cr < 0.1:
print("Consistency check passed, CR =", round(cr, 2))
else:
print("Consistency check failed, CR =", round(cr, 2))
</code>The check passes, confirming the matrix is acceptably consistent.
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".
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.