Contents: Anomaly detection and Recommender System

Anomaly detection

In a nutshell, it’s about using training set to obtain maximum likelyhood estimation(MLE) of parameters of Normal distribution, then use it to predict the probability of new examples being anomalies.

  • Use training sets to calculate MLE of parameters :
  • compute p(x) for new example :
  • preidct y based on p(x) with ϵ

Note that we assume all features are all independent, if any correlation exists amongest them, then either create new features manually (e.g. ) to detect anomalies instead of the original ones (), or use the multivariate Normal distribution (as the parameters of corvariace matrix ∑ generates potential relations automatically).

Evaluation of performace

Classification accuracy is not a good way to measure the algorithm’s performance, because of skewed classes (so an algorithm that always predicts y = 0 will have high accuracy). So people usually prefer F-Score, Precision & Recall or some other methods.

Split between train & cv sets and Choosing ϵ

  • We are not able to use anomalies for training (get MLEs) so shall not include anu of them in the training sets. Instead use these anomalies in cv and test sets with a 50:50 split would be a nice choice. For instance, if we have 10000 normal and 20 abnormal examples, use 6000 of the normal ones for training, 2000 of normal ones and 10 of abnormal ones for cross validation and the rest for test.

  • The course did not introduce any method of choosing ϵ automatically, so we have to choose it manually (e.g. base on the performance on cross validation set).

Anomaly detection vs. Supervised learning

The usage of anomaly detection is kind of like classification isn’t it? You may wondering why not use supervised learning such as logistic regression instead, so the below explained how supervised learning would perform in two cases:

  • skewed data —- Hard for supervised algorithm to learn. As there exists potentially many different types of anormalies, so future anomalies may look nothing like any of the anomalous examples we’ve seen so far.
  • balanced data —- Enough positive examples for algorithm to get a sense of what positive examples are like.

So use supervised learning algorithm when the number of positive and negative examples are both large, otherwise anomaly detection would be prefered.

The course gives some application scenario which can help you understand better:

Anomaly detection Supervised learning
Fraud detection Email spam classification (easy to get large number of spams)
Manufacturing (e.g. aircraft engines) Weather prediction (sunny/ rainy/etc).
Monitoring machines in a data center Cancer classification

Non-gaussian features

If the data of a feature not bell-shaped curve, using one-to-one transformation (e.g. natural log for positively skewed data) might be a good idea.

Recommender System

Content Based Recommendation

For this approachm, we assume that the features of contents are available to us. That means, for instance, we know how much romance or action content is in a movie, such that we are able to use these features to make predictions. The course gave the example below:

Movie Alice (1) Bob (2) Carol (3) David (4) (romance) (action)
Love at last 5 5 0 0 0.9 0
Romance forever 5 ? ? 0 1.0 0.01
Cute puppies of love ? 4 0 ? 0.99 0
Nonstop car chases 0 0 5 4 0.1 1.0
Swords vs. karate 0 0 5 ? 0 0.9

We now define some notations for later problem formulation:

  • = no. users
  • = no. movies
  • if user j has rated movie i
  • = rating given by user j to movie i (define only if )
  • = feature vector for movie i
  • : For each user j, learn a parameter to predict how this user would rate movie i with stars.

For example, with the parameter , we predict Alice would rate Cute puppies of love a stars.

Optimization objective

To learn a single parameter for user j:

So to learn all parameters simultaneously, we simply sum cost functions over all users :

Gradient Descend update

for (number of features).

Collaborative Filtering

Now the roles are swaped. Given the parameters θ’s, the algorithm is able to predict x’s as the following:

With Gradient Descend update:

Therefore, given , we can estimate .
And given , we can estimate .

This algorithm that we are going to use is so-called Collaborative Filtering, where θ’s and x’s are estimated to estimate another.
You might have noticed the blue-highlighted parts in the cost functions above are actually the same. In fact, rather than update one follow by another, we are able to come up with a more efficent and elegant way to train the algorithm:

where

Note that the constant term ‘s and ‘s are no longer needed so are removed.

In the algorithm we described, it is necessary to initialize x’s and θ’s to small random values to serves as symmetry breaking (similar to the random initialization of a neural network’s parameters) and ensures the algorithm learns features x’s that are different from each other.

Summarised procedure

  1. Initialize to small random values.
  2. Minimize using gradient descent or advanced optimization algorithm. E.g.
  3. For a user with parameters θ and a movie with (learned) features x, predict a star rating of .

Vectorization: Low rank matrix factorization

It is intuitive to see that the whole process can be vectorised, we take the moving rating status as an example:

By vectorizing , we are able to write the hypothesis in a much more beatiful way:

This matrix is also called a low rank matrix.

For each movie i , we learn a feature vector . E.g. for romance, for action, etc.

Therefore, in order to find whether movie j is similar to movie i, we can compare their distance:

such that small distance would reasonably suggest similarity. So to find 5 most similar movies to movie i, just find the 5 movies with the smallest distance.

Mean normalization

In the case where users have not rate any movie yet, the algorithm would predict these users to rate 0 for all movies as that minimise the cost function. Mean normalization is helpful to avoid this to happen by replacing with where μ is the means of movie ratings rated by some other users.

However, unlike some other applications of feature scaling, we did not scale the movie ratings by dividing by standard deviation. This is because all the movie ratings are already comparable (e.g. 0 to 5 stars), so they are already on similar scales.