Anomaly Detection and Recommender Systems
Anomaly Detection
1.1 Gaussian (Normal) distribution
-
Formulation: p ( x ; μ , σ 2 ) = 1 2 π σ 2 e − ( x − μ ) 2 2 σ 2 p(x;\mu,\sigma^2)=\frac{1}{\sqrt{2\pi\sigma^2}}e^{-\frac{(x-\mu)^2}{2\sigma^2}} p(x;μ,σ2)=2πσ21e−2σ2(x−μ)2
where μ \mu μ is the mean and σ 2 \sigma^2 σ2 controls the variance. -
Estimate the mean value for i-th feature: μ i = 1 m ∑ j = 1 m x ( j ) \mu_i = \frac{1}{m}\sum_{j=1}^m{x^{(j)}} μi=m1∑j=1mx(j)
-
Estimate the variance for the i-th feature: σ i 2 = 1 m ∑ j = 1 m ( x ( j ) − μ j ) 2 \sigma^2_i = \frac{1}{m}\sum_{j=1}^m{(x^{(j)}-\mu_j)^2} σi2=m1∑j=1m(x(j)−μj)2
1.2 Program: Estimate Gaussian
- input: Training dataset matrix X, (mxn matix), with n features
- output: 1. n-dimension vector sigma2 that holds the variances of all the features, 2. n-dimension vector mu that holds the mean of all the features
function [mu sigma2] = estimateGaussian(X)
%ESTIMATEGAUSSIAN This function estimates the parameters of a
%Gaussian distribution using the data in X
% [mu sigma2] = estimateGaussian(X),
% The input X is the dataset with each n-dimensional data point in one row
% The output is an n-dimensional vector mu, the mean of the data set
% and the variances sigma^2, an n x 1 vector
%
% Useful variables
[m, n] = size(X);
% You should return these values correctly
mu = zeros(n, 1);
sigma2 = zeros(n, 1);
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the mean of the data and the variances
% In particular, mu(i) should contain the mean of
% the data for the i-th feature and sigma2(i)
% should contain variance of the i-th feature.
%
mu = mean(X);
sigma2 = var(X, 1); % 1 -- 1/m rather than 1/(m-1)
mu = mu(:);
sigma2 = sigma2(:);
% =============================================================
% method 2:
% vectorized implementation
mu = 1/m * sum(X);
sigma2 = 1/m * sum((X - repmat(mu, m, 1)).^2);
%repmat(mu, m, 1) means create a mx1 matrix, all the elements is mu
end
1.3 Selecting the threshold, ϵ \epsilon ϵ
- use a cross validation set { ( x c v ( 1 ) , y c v ( 1 ) ) , … , ( x c v ( m ) , y c v ( m ) ) } \{(x^{(1)}_{cv},y^{(1)}_{cv}),\ldots, (x^{(m)}_{cv},y^{(m)}_{cv})\} {(xcv(1),ycv(1)),…,(xcv(m),ycv(m))},
- where the label y=1 corresponds to an anomalous example
- For each cross validation example, we will compute p ( x c v ( i ) ) p(x^{(i)}_{cv}) p(xcv(i))
-
F
1
F_1
F1 score: tells you how well you’re doing on finding the ground truth anomalies given a certain threshold (how many examples the current threshold classifies correctly and incorrectly)
- The score is computed using precision (prec) and recall (rec):
- F 1 = 2 ( p r e c ) ( r e c ) p r e c + r e c F_1 = \frac{2 ( prec)(rec)}{prec + rec} F1=prec+rec2(prec)(rec)
- p r e c = t p t p + f p prec = \frac{tp}{tp + fp} prec=tp+fptp
- r e c = t p t p + f n rec = \frac{tp}{tp + fn} rec=tp+fntp
- true positives: the ground truth label says it’s an anomaly and our algorithm correctly classified it as an anomaly
- false positives: the ground truth label says it’s not an anomaly, but our algorithm incorrectly classified it as an anomaly (右上角)
- false negatives: the ground truth label says it’s an anomaly, but our algorithm incorrectly classified it as not being anomalous.
selectThreshold: select the “best” ϵ \epsilon ϵ based on the F1 score
function [bestEpsilon bestF1] = selectThreshold(yval, pval)
%SELECTTHRESHOLD Find the best threshold (epsilon) to use for selecting
%outliers
% [bestEpsilon bestF1] = SELECTTHRESHOLD(yval, pval) finds the best
% threshold to use for selecting outliers based on the results from a
% validation set (pval) and the ground truth (yval).
%
% given: the cross validation lable yval and possibility of pval
bestEpsilon = 0;
bestF1 = 0;
F1 = 0;
stepsize = (max(pval) - min(pval)) / 1000;
for epsilon = min(pval):stepsize:max(pval)
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the F1 score of choosing epsilon as the
% threshold and place the value in F1. The code at the
% end of the loop will compare the F1 score for this
% choice of epsilon and set it to be the best epsilon if
% it is better than the current choice of epsilon.
%
% Note: You can use predictions = (pval < epsilon) to get a binary vector
% of 0's and 1's of the outlier predictions
cvPredictions = (pval < epsilon);
tp = sum ((cvPredictions == 1) & (yval == 1));
fp = sum ((cvPredictions == 1) & (yval == 0));
fn = sum ((cvPredictions == 0) & (yval == 1));
prec = tp / (tp + fp);
rec = tp / (tp + fn);
F1 = 2 * prec * rec / (prec + rec);
% =============================================================
if F1 > bestF1
bestF1 = F1;
bestEpsilon = epsilon;
end
end
end
Recommender System
2.1 Collaborative fitlering objective function and gradient
Data decription:
- The matrix Y (a num_movies x num_users matrix) stores the ratings y ( i , j ) y^{(i,j)} y(i,j) (from 1 to 5)
- The matrix R is an binary-valued indicator matrix, where R(i,j) = 1 if user j gave a rating to movie i, and R(i,j) = 0 otherwise
The objective of collaborative filtering:
to predict movie ratings for the movies that users have not yet rated, that is, the entries with R(i,j) = 0
Collaborative filtering learning algorithm:
- considers a set of n-dimensional feature vectors (for movie) x ( 1 ) , … , x ( n m ) x^{(1)},\ldots,x^{(n_m)} x(1),…,x(nm) and θ ( 1 ) , … , θ ( n u ) \theta^{(1)},\ldots, \theta^{(n_u)} θ(1),…,θ(nu) (for audience)
- prediction for movie i by user j is: y ( i , j ) = ( θ ( j ) ) T x ( i ) y^{(i,j)} = (\theta^{(j)})^T x^{(i)} y(i,j)=(θ(j))Tx(i)
- the parameters to the function (i.e., the values that you are trying to learn) are X and Theta (two variables to learn)
- cost function: (without regularization)
J ( x ( i ) , … , x ( n m ) , θ ( 1 ) , … , θ ( n u ) ) = 1 2 ∑ ( i , j ) : r ( i , j ) = 1 ( ( θ ( j ) ) T x ( i ) − y ( i , j ) ) 2 J\left(x^{(i)},\ldots,x^{(n_m)},\;\theta^{(1)},\ldots,\theta^{(n_u)}\right) =\frac{1}{2}\sum_{(i,j):r(i,j)=1}\left((\theta^{(j)})^Tx^{(i)}-y^{(i,j)}\right)^2 J(x(i),…,x(nm),θ(1),…,θ(nu))=21∑(i,j):r(i,j)=1((θ(j))Tx(i)−y(i,j))2
2.2 Collaborative filtering gradient
- ∂ J ∂ x k ( i ) = ∑ j : r ( i , j ) = 1 ( ( θ ( j ) ) T x ( i ) − y ( i , j ) ) θ k ( j ) \frac{\partial J}{\partial x_k^{(i)}} = \sum_{j:r(i,j)=1}\left((\theta^{(j)})^T x^{(i)} -y^{(i,j)}\right)\theta_k^{(j)} ∂xk(i)∂J=∑j:r(i,j)=1((θ(j))Tx(i)−y(i,j))θk(j)
- ∂ J ∂ θ k ( j ) = ∑ i : r ( i , j ) = 1 ( ( θ ( j ) ) T x ( i ) − y ( i , j ) ) x k ( i ) \frac{\partial J}{\partial \theta_k^{(j)}} = \sum_{i:r(i,j)=1}\left((\theta^{(j)})^T x^{(i)} -y^{(i,j)}\right) x_k^{(i)} ∂θk(j)∂J=∑i:r(i,j)=1((θ(j))Tx(i)−y(i,j))xk(i)
To vectorize the above expression, you can start by indexing into Theta and Y to select only the elements of interest (that is, those with
r
(
i
,
j
)
=
1
r(i,j) = 1
r(i,j)=1):
Explain: can set idx = find(R(i,:)==1) to be a list of all the users that have rated movie . This will allow you to create the temporary matrices Theta_temp = Theta(idx,:) and Y_temp = Y(i,idx) that index into Theta and Y to give you only the set of users which have rated the i-th movie
the derivatives for the feature vector fof the i-th movie is:
Regularized cost function
- J ( x ( i ) , … , x ( n m ) , θ ( 1 ) , … , θ ( n u ) ) = 1 2 ∑ ( i , j ) : r ( i , j ) = 1 ( ( θ ( j ) ) T x ( i ) − y ( i , j ) ) 2 + ( λ 2 ∑ j = 1 n u ∑ k = 1 n ( θ k ( j ) ) 2 ) + ( λ 2 ∑ i = 1 n m ∑ k = 1 n ( x k ( i ) ) 2 ) J\left(x^{(i)},\ldots,x^{(n_m)},\;\theta^{(1)},\ldots,\theta^{(n_u)}\right) =\frac{1}{2}\sum_{(i,j):r(i,j)=1}\left((\theta^{(j)})^Tx^{(i)}-y^{(i,j)}\right)^2 +\left(\frac{\lambda}{2}\sum_{j=1}^{n_u}{\sum_{k=1}^{n}{(\theta_k^{(j)})^2}}\right) +\left(\frac{\lambda}{2}\sum_{i=1}^{n_m}{\sum_{k=1}^{n}{(x_k^{(i)})^2}}\right) J(x(i),…,x(nm),θ(1),…,θ(nu))=21∑(i,j):r(i,j)=1((θ(j))Tx(i)−y(i,j))2+(2λ∑j=1nu∑k=1n(θk(j))2)+(2λ∑i=1nm∑k=1n(xk(i))2)
- ∂ J ∂ x k ( i ) = ∑ j : r ( i , j ) = 1 ( ( θ ( j ) ) T x ( i ) − y ( i , j ) ) θ k ( j ) + λ x k ( i ) \frac{\partial J}{\partial x_k^{(i)}} = \sum_{j:r(i,j)=1}\left((\theta^{(j)})^T x^{(i)} -y^{(i,j)}\right)\theta_k^{(j)}+\lambda x_k^{(i)} ∂xk(i)∂J=∑j:r(i,j)=1((θ(j))Tx(i)−y(i,j))θk(j)+λxk(i)
- ∂ J ∂ θ k ( j ) = ∑ i : r ( i , j ) = 1 ( ( θ ( j ) ) T x ( i ) − y ( i , j ) ) x k ( i ) + λ θ k ( j ) \frac{\partial J}{\partial \theta_k^{(j)}} = \sum_{i:r(i,j)=1}\left((\theta^{(j)})^T x^{(i)} -y^{(i,j)}\right) x_k^{(i)}+\lambda\theta_k^{(j)} ∂θk(j)∂J=∑i:r(i,j)=1((θ(j))Tx(i)−y(i,j))xk(i)+λθk(j)
完整代码:
function [J, grad] = cofiCostFunc(params, Y, R, num_users, num_movies, ...
num_features, lambda)
%COFICOSTFUNC Collaborative filtering cost function
% [J, grad] = COFICOSTFUNC(params, Y, R, num_users, num_movies, ...
% num_features, lambda) returns the cost and gradient for the
% collaborative filtering problem.
%
% Unfold the U and W matrices from params
X = reshape(params(1:num_movies*num_features), num_movies, num_features);
Theta = reshape(params(num_movies*num_features+1:end), ...
num_users, num_features);
% You need to return the following values correctly
J = 0;
X_grad = zeros(size(X));
Theta_grad = zeros(size(Theta));
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost function and gradient for collaborative
% filtering. Concretely, you should first implement the cost
% function (without regularization) and make sure it is
% matches our costs. After that, you should implement the
% gradient and use the checkCostFunction routine to check
% that the gradient is correct. Finally, you should implement
% regularization.
%
% Notes: X - num_movies x num_features matrix of movie features
% Theta - num_users x num_features matrix of user features
% Y - num_movies x num_users matrix of user ratings of movies
% R - num_movies x num_users matrix, where R(i, j) = 1 if the
% i-th movie was rated by the j-th user
%
% You should set the following variables correctly:
%
% X_grad - num_movies x num_features matrix, containing the
% partial derivatives w.r.t. to each element of X
% Theta_grad - num_users x num_features matrix, containing the
% partial derivatives w.r.t. to each element of Theta
%
% sum(sum(R.*M)) is the sum of all the elements of M for which the corresponding element in R equals 1.
J = 1/2 * sum(sum((R.* ((X*Theta') - Y)).^2));
X_grad = (R .* (X*Theta' - Y)) * Theta;
Theta_grad = (R .* (X*Theta' - Y))' * X;
% With regularization
J = J + lambda/2 * (sum(sum(Theta.^2)) + sum(sum(X.^2)));
X_grad = X_grad + lambda * X;
Theta_grad = Theta_grad + lambda * Theta;
% =============================================================
grad = [X_grad(:); Theta_grad(:)];
end