Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab 15: Choosing with Several Criteria (AHP and TOPSIS)

Open In Colab

Lab 15: Choosing with Several Criteria (AHP and TOPSIS)

In this lab you rank options on several criteria: you screen out dominated options, derive weights with AHP, rank with a weighted sum and with TOPSIS, and test how far the weights can move before the answer changes. Then you apply the responsible-analytics checklist to your own capstone.

The question. Qahwa House must choose one importer for the green coffee of its house blend (about 50 tonnes a year) for the next two years. Five importers are scored on cost, quality, reliability, sustainability and supply risk. Which one should the board sign with, and how sure can it be?

How this lab works

  • Run the cells from top to bottom with Shift + Enter. Replace every ... with your code, then run the check cell under it.

  • Extended track: after Part D, try the ★ challenges.

  • Part E prepares your capstone defence: complete it with your team.

  • This lab is not graded (lab marks count the best 12 of Labs 1 to 14), but its last part is what you will be asked about at your defence.

You may use an AI assistant for code. If you do, add a short note at the end: what you asked, what you kept and what you changed.

ColumnMeaningBetter when
supplier, originthe importer and where its coffee comes from
cost_sar_kglanded price of green coffee, SAR per kglower
cupping_scorequality on the specialty-coffee cupping scale, 0–100higher
on_time_pctshare of the last 24 months’ shipments that arrived on time (%)higher
sustainability_scorethe purchasing team’s audit score, 0–100higher
supply_riskthe purchasing team’s risk rating, 1 (low) to 10 (high)lower

The importers and their scores are invented for this course.

Part A: The decision matrix

The cell below builds the decision matrix X (one row per importer, one column per criterion, as floats) and the Series better, which says for each criterion whether a lower or a higher value is better.

A1. Cost alone

Store in cheapest the name of the cheapest importer, and in extra_red_sea how much more Red Sea Traders would cost per year than the cheapest, for 50 tonnes (50,000 kg), in SAR.

A2. Dominated options

Option a dominates option b when a is at least as good as b on every criterion and strictly better on at least one. The table good flips the sign of the lower is better criteria, so that higher is better everywhere. Complete dominates(a, b), then store in dominated the list of importers dominated by at least one other.

From here on we work with the four importers that remain, X4.

Part B: Weights with AHP

The board compared the criteria two at a time on Saaty’s 1–9 scale (row against column; for example, cost is 4 times as important as sustainability):

costqualityreliabilitysustainabilityrisk
cost11242
quality11232
reliability1/21/2122
sustainability1/41/31/211/2
risk1/21/21/221

B1. The priority vector

The weights are the eigenvector of the largest eigenvalue of this matrix, scaled to add up to 1. Use np.linalg.eig(board): it returns the eigenvalues and a matrix whose columns are the eigenvectors (keep the real parts). Store the weights as a Series weights indexed by crit, and the largest eigenvalue in lam.

B2. Consistency

Compute the consistency index CI=(λmax⁡−n)/(n−1)CI = (\lambda_{\max} - n) / (n - 1) and the consistency ratio CR=CI/RICR = CI / RI, with the random index RIRI = 1.12 for nn = 5 criteria. Store them in CI and CR.

B3. A second opinion

The finance manager filled in the same comparisons. Write a function ahp(matrix) that returns the weights (as a Series indexed by crit) and the CR, then apply it to finance. Store the results in w_fin and CR_fin.

B4. Find the three judgments in finance that go round in a circle (A more important than B, B more important than C, but C more important than A). What would you say to the finance manager?

Your answer here (double-click to edit).

Part C: Two ranking methods

C1. Min–max normalisation and the weighted sum

Complete minmax(X) so that every criterion is on a 0-to-1 scale among the importers, with 1 for the best and 0 for the worst: (x−min⁡)/(max⁡−min⁡)(x - \min)/(\max - \min) when higher is better, (max⁡−x)/(max⁡−min⁡)(\max - x)/(\max - \min) when lower is better. Then store the weighted-sum scores of X4 with the board’s weights in wsm.

C2. TOPSIS

Complete topsis(X, weights), which returns the closeness of each importer:

  1. divide each column by its length, ∑x2\sqrt{\sum x^2}, and multiply by the weights: V;

  2. the ideal takes the best value of each column of V (the largest if higher is better, the smallest otherwise), the anti-ideal the worst (given);

  3. the distances to both, and the closeness d−/(d++d−)d^- / (d^+ + d^-).

Store the closeness of X4 with the board’s weights in closeness.

Part D: How sure is the answer?

with_weight(weights, criterion, value) sets one weight to value and scales the other four in proportion, so that the total stays 1.

D1. When does the winner change?

Using the grid grid (weights from 0 to 1 in steps of 0.001), store in flip_wsm the smallest sustainability weight above the board’s at which the weighted-sum winner is no longer Red Sea Traders, and in flip_topsis the same for TOPSIS.

D2. “Let’s keep it neutral: equal weights”

A board member proposes 20% for every criterion. Store the weighted-sum winner with equal weights in winner_equal.

D3. Your recommendation

Write three sentences for the board: which importer to sign with (with its scores under both methods and the cost difference with the runner-up), how sure the answer is (the flip points), and why equal weights are not a neutral choice.

Write your recommendation here (double-click to edit).

Part E: Before your defence

Apply the responsible-analytics checklist of the reading to your capstone. Answer each line in one or two sentences, with a number where one fits; your team should be able to give the same answers at the oral defence.

#QuestionYour answer
1Which decision does your app support, and which manager makes it?
2What are the alternatives, and what does the manager care about (objective or criteria)?
3Where does the data come from, how did you clean it, and does it contain personal data?
4What does the predictive part predict, and how large is its error on data it has not seen?
5What does the decision model recommend?
6Which inputs or weights would change the recommendation, and by how much?
7What does the model leave out, and what could go wrong if it is wrong?
8Does the recommendation affect groups of people differently? How did you check?
9What did you use AI for, and how did you check it?
10In one sentence, with a number: what should the manager do?

E1. Pick the input or weight your recommendation is most sensitive to, and write the sentence for your memo: “If ... were ... instead of ..., we would recommend ... instead.”

Your answer to E1 here (double-click to edit).

★ Extended challenges

★1. Who gets the voucher? Rebuild Week 7’s churn model (data qahwa_members_2025.csv; pd.get_dummies on branch with drop_first=True; train_test_split(..., test_size=0.3, random_state=631, stratify=y); LogisticRegression(max_iter=1000)) and the rule send a voucher when p > 1/6. For each branch, compute on the test set the share of members who get a voucher and the share of leavers who get one (recall). Then fit the model again without the branch columns. How do the gaps between branches and the test-set profit (+50 SAR per leaver reached, −10 SAR per voucher sent to a member who stays) change?

★2. Negotiate. The board likes Jazan Highlands’ sustainability. At what price per kg (in steps of 0.5 SAR) would Jazan Highlands become the board’s choice under both methods, with the board’s weights? How much would that cost per year compared with Red Sea Traders at 31 SAR/kg?

★3. Your own weights. Fill in your own pairwise comparisons of the five criteria. Is your CR below 0.10? Which importer wins with your weights, under each method?

AI-use log (if you used an AI assistant)

What you asked, what you kept, what you changed.