P = NP: A Million Dollar Problem

In 2000, the Clay Math Institute awarded one million dollars each for seven important, long-standing math problems. Of these, only one has been solved since (the Poincaré conjecture). I will discuss another one, the P = NP problem. This is arguably one of the most relevant and famous problems in modern computer science. Solving the question cures cancer, obliterates all existent computer privacy, instantly beats everyone at chess and Tetris, reconstructs the full history of species from DNA, and would most likely create general mayhem on earth.

I stumbled upon this problem while reading The Master Algorithm by Pedro Domingos. He provides a very intuitive description of the problem: ‘P and NP are the two most important classes of problems in computer science. [..] A problem is in P if we can solve it efficiently, and it’s in NP if we can efficiently check its solution. The famous P = NP question is whether every efficiently checkable problem is also efficiently solvable’.

Now if we want to solve the problem and win the lottery, we need to dig a little deeper than that. As Domingos writes, the two most important classes of problems in computer science are P and NP. We designate any problem that can be solved in polynomial time to be P (polynomial), and with NP (non-deterministic polynomial) we mean any problem that can be solved in at least exponential time but for which the solution can be checked in polynomial time. The catch here is, if you find a way to efficiently solve one of the NP problems, you solve all of them because at their core, they are all the same. This is called NP-completeness.

For understanding what the aforementioned sentences mean, we need to grasp exactly what computer scientists mean when they talk about time. For solving problems, they do not use the normal, linear time that we know, but correct for the amount of elements in solving a problem, namely N. In this case, time is linear only for very simple problems. For example, let’s say you need to find the highest number in a list (or select for any other condition). Then you need to iterate through the entire list only once. This is a linear problem, in the sense that every added element (to the length of the list) adds only one extra computation/extra time unit. In the graph, it’s the O(n) line.

However, many problems in computer science are in polynomial time. This means that the amount of computations needed to solve the problem is the amount of elements raised to some power (for example N-squared or N3; O(n^2) in Figure 1). One notable problem in P is that of exactly figuring out what the maximal profit is in the case of multiple cost functions, called linear programming. For example, a car manufacturer disposes of one factory, a certain amount of employees and a certain amount of raw materials. The company can choose between building two car models that both have a different required amount of raw materials, labor, and retail price. Within the boundaries of available resources, you’d compute all possible combinations of amounts for both car models (so compute the full solution space), and go with the most profitable, or optimal, solution. For more info, check this page.

However, problems in NP need more than that amount of computing to be solved. The solution space is way larger than that of exponential problems. The official definition is a problem that can be solved in polynomial time by a non-deterministic Turing machine (Cook, 2000).

An ‘easier’ way to look at NP problems is this: They require at the very least an amount of computations that is equal to some number raised to N, sometimes even a factorial of N. So for example if N = 20, at least 220 computations are needed (Figure 1: O(2^n)) . This means that the number of computations increases so fast with N, that we can only compute exact solutions for very small problems. To illustrate this, I’ll use an example from MIT: If a computer takes a second to perform a computation with 100 elements in the case the algorithm is in linear time (N = amount of computations), that same algorithm will take ~3 hours if the amount of computations is equal to N3 (polynomial time), and will take 300 quintillion years if the computation time is equal to 2N (exponential time). This example should provide some insight into the timescale which is required for solving NP problems with large Ns. As a result, we can currently only solve very simple NP problems with only a few components.

However, NP problems are usually easily checkable in polynomial time. For example, computing the optimal solution for a game of Tetris requires a ridiculously large amount of computations, but it is very easy to see that one has solved Tetris. Same for Sudoku; solving is difficult, checking the solution for mistakes is easy.

Another way of looking at many NP problems is that for most NP problems, one needs to compute all possible combinations of the included elements to find the optimal solution. This is way worse even than 2N; in this case the amount of possible combinations is (N-1)!, which is, in the case of 9 elements, 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1=40320 (~1 hour), and in the case of 100 elements, too large to compute on my laptop. The point here is that the time increase for each added element is so large, that only very simple computations can be made with current methods.

An example of an NP problem is that of the travelling salesman. Let’s say that the salesman needs to visit a number of cities (N), that all have different distances from each other, and he wants to visit them in the most efficient route possible. In that case, he needs to compute distance to be travelled for all possible travel itineraries, which equals (N-1)!. That is, even for 10 cities, 362880 possibilities. In the case of the following picture, with 50 cities, that is approximately 6082818 * 1086 possibilities.

The aforementioned example is an example of a sub-class of NP problems, namely NP-hard problems. These are problems that cannot be solved in polynomial time and whose solutions cannot be checked in polynomial time. In the aforementioned example, this implicates that, even if you were able to have a computer put out a solution for optimal travel, you would never be sure if the computer got it right. Another example of an NP-hard problem is chess; if you have a computer find the optimal move to make, how would you even know that the computer got it right?

Here, I’ll recap NP-completeness; even though the travelling salesman problem, sudoku, cracking encryption and reverse engineering gene sequences seem not even remotely similar, they’re all the same problem at heart. They all consist of finding a unique, perfect solution, in which with current methods, iterating through all possible solutions is required. Therefore, solving one means solving them all. You’ll need to prove that problems that can only be solved in exponential or factorial time, perfect solutions exist as well in polynomial time (all relative to N). In terms that we can all understand, this implies that for receiving the million dollars, you need to prove that you can find the perfect combination of elements for a problem, without going through all possible combinations.

Now the aforementioned does not mean that scientists have not found solutions to any of the NP problems in this essay. For example, if you look at the map of the United States, it is likely that you could draw a line between all the cities that is going to be very close to the optimal solution. This is one way scientists approach NP problems; through heuristics. These are decision models that do not require computing all possible solutions, but that use a set of rules to approximate the solution. For example in the travelling salesman problem, one could set the rule that the computer/algorithm needs to move to the closest next city. This might in some cases result in an approximate solution. However, for things like cracking 50-digit passwords and reconstructing species from gene codes, this will not get us very far.

It is not very likely that mathematicians will ever find evidence for P = NP. For example. In a 2002 study, MIT researchers found that 61 computer scientists thought that P probably is not equal to NP, and 9 thought that is does (6). However, some of those told the researcher that they just said that to be controversial. Time has proven them right; no solution has arisen in the 16 years since. Thus, the general consensus us that P is not equal to NP, but no evidence has been found for this statement either.

Then why does anyone still look for the solution to P = NP? Firstly, many breakthroughs in computer science come from someone searching a solution and accidentally finding ways to make algorithms more efficient or powerful. Secondly, looking makes one understand one of the major limitations of computer science and how to deal with it. Thirdly, it provides insight into some major problems in modern society. And lastly, it’s just interesting. So have a go!

I refer to this video for another quite intuitive explanation of P=NP.

Multivariate Outlier Detection with Isolation Forests

Recently, I was struggling with a high-dimensional dataset that had the following structure: I found a very small amount of outliers, all easily identifiable in scatterplots. However, one group of cases happened to be quite isolated, at a large distance from more common cases, on a few variables. Therefore, when I tried to remove outliers that were at three, four, or even five standard deviations from the mean, I would also delete this group.

Fortunately, I ran across a multivariate outlier detection method called isolation forest, presented in this paper by Liu et al. (2012).

This unsupervised machine learning algorithm almost perfectly left in the patterns while picking off outliers, which in this case were all just faulty data points. I’ve used isolation forests on every outlier detection problem since. In this blog post, I’ll explain what an isolation forest does in layman’s terms, and I’ll include some Python / scikit-learn code for you to apply to your own analyses.

Outliers

First, some outlier theory. Univariate outliers are all cases in one’s data that are quite far from the mean in terms of standard deviation on a certain variable. Don’t confuse them with influential cases. Influential cases may be outliers, and vice versa, but they’re not identical. Multivariate outliers, which we are discussing in this post, are essentially cases that display a unique or divergent pattern on variables.

Outliers may have two causes:

  • There may be mistakes present in your data. Maybe someone filled in a faulty number or just typed 999; maybe the application generating your data contains some weird logic. You definitely want to remove these cases before analysis.
  • Some cases are quite abnormal in your data, but are valid. In this case, it’s up to the data scientist to remove them or not. If you’re for example doing a regression, those outliers may strongly influence your results (Cook’s distance), and you’ll remove them from the data. However, a clustering algorithm will just put the abnormal group of cases in a separate cluster.

What I find isolation forests to do well, is that they first start at picking off the false or bad cases, and only when those are all identified, will start on the valid, abnormal cases. This is because valid cases, however abnormal, are often still grouped together, where bad cases are truly unique. This is not true for all analyses; if a default is 999 for example, there may be many cases with that value on some variable. It’s up to the data scientist to be vigilant in those cases.

Isolation Forests

As the name suggests, isolation forests are based on random forests. I’ll dig into decision trees and random forests some other time, but here’s what you need to know; decision trees split data into classes in order to minimize prediction error. On each iteration, the tree gets to make one split on one of the included variables to removing the most entropy, or degree of uncertainty. For example, a decision tree could first split cases into younger and older people, when predicting SES. Subsequently, it could split the younger group into people with and without college degrees to remove entropy, and so on.

Introduce random forests; large, powerful ensembles of trees, in which individual quality of each tree is diminished due to random splits, but with low prediction error due to trees outperforming other trees gaining a larger weighting in the final decision.

An isolation forest is based on the following principles (according to Liu et al.); outliers are the minority and have abnormal behaviour on variables, compared to normal cases. Therefore, given a decision tree whose sole purpose is to identify a certain data point, less dataset splits should be required for isolating an outlier, than for isolating a common data point. This is illustrated in the following plot:

Based on this, essentially what an isolation forest does, is construct a decision tree for each data point. In each tree, each split is based on selecting a random variable, and a random value on that variable. Subsequently, data points are ranked on how little splits it took to identify them. Given that the model’s instructions were to identify X% as outliers, the top X% cases on rank score are returned.

Isolation forests perform well because they deliberately target outliers, instead of defining abnormal cases based on normal case behaviour in the data. They are also quite efficient; I’ve easily applied them on datasets containing millions of cases.

Now for the practical bit. Let’s generate some data. I generate a large sample of definite inliers, then some valid outliers, then some bad cases.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.ensemble import IsolationForest

##first, build a simulated 2-dimensional dataset
inliers = [[np.random.normal(50, 5, 1), np.random.normal(50, 5, 1)] for i in range(900)]
outliers1 = [[np.random.normal(70, 1, 1), np.random.normal(70, 1, 1)] for i in range(50)]
outliers2 = [[np.random.normal(50, 20, 1), np.random.normal(50, 20, 1)] for i in range(50)]

#merge and reshape the data points
df = inliers+ outliers1 + outliers2
v1, v2 = [i[0] for i in df],[i[1] for i in df]

df = pd.DataFrame({'v1': v1, 'v2': v2})
df = df = df.sample(frac=1).reset_index(drop=True)

##and look at the data
plt.figure(figsize = (20,10))
plt.scatter(df['v1'], df['v2'])
plt.show()

Subsequently, we’ll train an isolation forest to identify outliers and examine the results. 5% of the set were generated as outliers. However, some of these are likely to have ended up as inliers. Therefore I set the outlier identification threshold to 4%.

##apply an Isolation forest
outlier_detect = IsolationForest(n_estimators=100, max_samples=1000, contamination=.04, max_features=df.shape[1])
outlier_detect.fit(df)
outliers_predicted = outlier_detect.predict(df)

#check the results
df['outlier'] = outliers_predicted
plt.figure(figsize = (20,10))
plt.scatter(df['v1'], df['v2'], c=df['outlier'])
plt.show()

As you see, the isolation forest nicely separates bad cases from actual data patterns, with barely any errors. If I would now want to remove the rightmost cluster as well, I could just increase my removal threshold. The next plot is the result with the threshold set to 15%. One can clearly see that it now starts picking at cases in the smaller cluster and in the periphery of the larger cluster. Keep in mind that this algorithm will always incur slight error, due to random generation of splits.

Hyperparameter tuning

This is how to tune the following parameters for optimal performance:

  • n_estimators: The number of decision trees in the forest. According to Liu et al., 100 should be sufficient in most cases.
  • max_samples: Given large datasets, you might want to train on random subsets of cases to decrease training time. This parameter lets you determine subset size.
  • contamination: The proportion of the data you want to identify as outlier. As demonstrated before, this parameter requires some trial and error combined with scatterplot visualisation, given no prior knowledge.
  • max_features: The amount of variables that should be used to define outliers on. Should be set to the amount of variables that you have in almost any situation. This feature allows one to iterate over variables, and do univariate outlier detection on each variable without specifying a standard deviation threshold.

That’s all you need to know for applying isolation forests to multivariate outlier detection! Please refer to this blog if you use any information written here and the scikit-learn documentation in your own work.