How to decide between multiple equally good things? Simple, use math!

Learn about TOPSIS for for Multi Criteria Decision Making + Python Code

Navkiran Singh
Analytics Vidhya
5 min readMar 13, 2020

--

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!

A young women looking confused with a finger on lips and head tilted to the side. A question mark above her head.
Decision making can be hard.

Especially when most of the choices seem equally good — a typical scenario when buying smartphones, computers, automobiles, etc. Consider…

Table of choices
Do I buy mobile 1? Or do I splurge more and get mobile 3? Mobile 5 has the best camera though! Uhhh!

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.

Matrix containing m alternatives and n criteria

Step 2: Get the normalized matrix.

The matrix is normalized using this formula
Let’s first calculate this denominator for every column
Last row indicates the calculated denominators

Next, we divide each cell in a column with the corresponding denominator we just calculated.

For example, the value for cell (Mobile1, Price) becomes 250/564.579 i.e 0.4428

Step 3: Assign weights to criteria. Get the weighted normalized matrix by multiplying each cell with weight of that column(attribute/criteria).

(Mobile1, Price) = 0.4428 * 0.25

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.

Notation for ideal best
  • For negatives, ideal best is Min(cell values of that attribute)
  • For positives, ideal best is Max(cell values of that attribute)
Notation for ideal worst
  • 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.

Note the values in Ideal Best and Ideal Worst row and verify that they make sense to you

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…

From Best Calculation
From Worst Calculation

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

From Best value for Mobile 1
Calculation results of From Best and From Worst

Step 6: Calculate Performance ratio and use it to rank the alternatives.

Performance ratio can be calculated as follows…

Notice the numerator — it’s From Worst value. Can you think of a reason why?

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. 💡

Performance ratios and ranks. Highest P value has highest rank.
Coder Kitty 😻

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.

Code Snippet Gist

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.

…and we are done! 🎉

Thank you for reading and have a great rest of your day! 😄

If you have any feedback 💭 or noticed a mistake, please leave a comment. You may also reach me at my email or my personal site here. I’d ❤️to hear your thoughts.

Bye!

--

--