# “Machine learning - Anomaly detection”

### Anomaly detection

#### Model-based outlier detection

Z-score measures the probability of :

Issue: The outlines contributes to the value of and and assume it is uni-modal. (The probability distribution function has only 1 peak.)

The Gaussian probability distribution is

- Locate features that indicate anomaly behavior
- Collect training dataset
- Calculate and for every feature
- For a new testing data , compute the probability

- Flag the data if

However, features in may be co-related. The diagram below shows weight and height are co-related. The green zone are datapoints consider as normal. The blue dot falls within the normal height or weight of the population. But knowing the person is much taller but yet much ligher, we should flag this data as abnormal. Nevertheless, the equation above does not account for the co-relationship between variables.

To compensate that, we should not compute individually as in

Instead we need to compute the probability using a multivariate Gaussian distribution. The covariance matrix will compensate any co-relationship between features and make the necessary adjustments.

which is the expected value function.

#### Graphical outlier detection

Plot data to locate outliner visually:

- Use Box plot for 1 variable at a time
- Scattering plot for 2 variables at a time
- Scattering array to look at multiple combination at a time. But still ploting 2 variables at a time
- Scattering plot of 2-D PCA

#### Cluster-Based outlier detection

- Cluster the data
- Find points that do not belong to a cluster, (density based clustering) or

- Far away from the center of the cluster, (K-medians) or

- Only join a hierarchy clustering at the coarse grain level

#### Global distance-based outlier detection: KNN

We can measure the distance of a datapoint from its neighbors to detect outliner.

- Calculate the average distance for its K-neighbors
- Choose the biggest values as outliners
- Good for locate global outlier

#### Outlier-ness

Nevertheless, some datapoints may be close to a cluster in the global sense but should not be consider as part of it after consider the average distance among the cluster’s members. Members in the Cluster C2 is closer together than Cluster C1. So even the blue dot is only away from the green dot, it is not consider as part of the cluster C2 while the red dot will be consider as part of C1.

The average distance for from its k neighbors is:

To consider whether it is outlier, we need to consider how close to its neighbors and how close those neighbors are with their neighbors .

If , the datapoint between itself and the cluster is greater than the average distance among the cluster members.

#### Influenced outlierness

However, we may still have problems for clusters that are very close. The circled green dot has high even though it is part of the green cluster because it counts the red dots as its closest neighbors.

In influenced outlierness, we are not finding the average distance of its neighbors’ neighbors. Instead, we find the average distance of its neighbors that consider as its neighbors.

Then we replace the denominator with the average distance of those tick dots.

#### Supervised outlier detection

We can use supervising learning to determine whether a datapoint is an outlier. This method can detect complex rule but will require the labeling of the training data.