How to decide between multiple equally good things? Simple, use math!
Learn about TOPSIS for for Multi Criteria Decision Making + Python Code
Were you recently in the market to buy something? Maybe a new phone, a laptop or a TV? Then you know the more choices we have, the harder it becomes to make a guilt free final choice!
Especially when most of the choices seem equally good — a typical scenario when buying smartphones, computers, automobiles, etc. Consider…
The figure shows a typical multi criteria decision making problem. Multi criteria because we have more than one criteria differentiating our alternatives. In two choices with few attributes, it’d be an easy binary choice but what to do here?
Luckily this can be formulated as a mathematical problem and a great thing about mathematical problems is that our computers are excellent at solving them! So let’s get started.
Intuition for TOPSIS and its full form
TOPSIS stands for Technique for Order of Preference by Similarity to Ideal Solution. The full form will become clearer on the basis of the concept so read on.
It’s basic idea is that the chosen alternative should have the shortest geometric distance from the positive ideal solution and the longest geometric distance from the negative ideal solution.
In other words, get as close as to the best choice you can and run very far away from the worst choice.
TOPSIS has the following steps:
Step 1: Convert the Looks column to a numerical rating system. We need all columns to be numerical.
The end result will be matrix with m rows and n columns with every cell having numerical values.
Step 2: Get the normalized matrix.
Next, we divide each cell in a column with the corresponding denominator we just calculated.
Step 3: Assign weights to criteria. Get the weighted normalized matrix by multiplying each cell with weight of that column(attribute/criteria).
Step 4: Determining the Ideal Best and Worst.
Each attribute has an impact associated with it. Mark attributes as having positive impact/beneficial (you want to get more of this) or as having negative impact/non-beneficial ( you want to make it less).
Now find Ideal Best and Ideal Worst using the rules below.
- For negatives, ideal best is Min(cell values of that attribute)
- For positives, ideal best is Max(cell values of that attribute)
- For negatives, ideal worst is Max(cell values of that attribute)
- For positives, ideal worst is Min(cell values of that attribute)
The rules can be understood intuitively. Price is a negative so ideal best solution will try to minimize it and ideal worst solution will have the maximum price. Camera is a positive so ideal best solution has the best camera — max megapixels and the ideal worst has the minimum megapixels.
Step 5: Calculate From Best and From Worst values.
From Best is euclidean distance from the best solution and From Worst is euclidean distance from the worst solution.
Calculated as follows…
So many calculations! Don’t worry, we can let the computer do all this. After all that is the point of programming — make the computer do the boring stuff! 💻 For now focus on the concepts.
As an example calculation, consider euclidean distance From Best for mobile 1, it’s calculated as follows
Step 6: Calculate Performance ratio and use it to rank the alternatives.
Performance ratio can be calculated as follows…
After calculations, we rank the alternatives according to their performance ratios, highest to lowest. This makes sense since higher P implies that a particular alternative is further away from the worst solution (since the numerator is From Worst value). That’s why it’s in the numerator. 💡
With this, we are finished with the concept! 🙌
Onto the code!
The complete python code for TOPSIS can be found at my github repository.
Core logic is in /src/topsis_navkiran.py , rest is boilerplate for pypi.
I am posting the main parts below with comments.
However, please refer to the github repository for complete code which can run.
Consider forking it and making a pull request, if you find a bug or have an improvement to make.
It’s also available as pypi package here.