## 一. Density Estimation 密度估计

### 1. 高斯分布

$$p(x;\mu,\sigma^2)=\frac{1}{\sqrt{2\pi}\sigma}exp(-\frac{(x-\mu)^2}{2\sigma^2})$$

$$\mu_j = \frac{1}{m} \sum_{i=1}^{m}x_j^{(i)};;;,;;; \delta^2_j = \frac{1}{m} \sum_{i=1}^{m}(x_j^{(i)}-\mu_j)^2$$

\begin{align*} p(x)&=p(x_1;\mu_1,\sigma_1^2)p(x_2;\mu_2,\sigma_2^2) \cdots p(x_n;\mu_n,\sigma_n^2)\ &=\prod_{j=1}^{n}p(x_j;\mu_j,\sigma_j^2)\ &=\prod_{j=1}^{n} \frac{1}{\sqrt{2\pi}\sigma_{j}}exp(-\frac{(x_{j}-\mu_{j})^2}{2\sigma_{j}^2}) \end{align*}

### 3. 算法评估

• 真阳性、假阳性、真阴性、假阴性
• 查准率（Precision）与 召回率（Recall）
• F1 Score

## 三. Multivariate Gaussian Distribution (Optional)

### 1. 多元高斯分布模型

$\Sigma$ 是一个协方差矩阵，所以它衡量的是方差。减小 $\Sigma$ 其宽度也随之减少，增大反之。

$\Sigma$ 中第一个数字是衡量 $x_1$ 的，假如减少第一个数字，则可从图中观察到 $x_1$ 的范围也随之被压缩，变成了一个椭圆。

### 2. 参数估计

$$\mu=\frac{1}{m}\sum_{i=1}^{m}{x^{(i)}}$$

$$\Sigma=\frac{1}{m}\sum_{i=1}^{m}{(x^{(i)}-\mu)(x^{(i)}-\mu)^T}$$

### 3. 算法流程

1. 选择一些足够反映异常样本的特征 $x_j$ 。
2. 对各个样本进行参数估计：
$$\mu=\frac{1}{m}\sum_{i=1}^{m}{x^{(i)}}$$
$$\Sigma=\frac{1}{m}\sum_{i=1}^{m}{(x^{(i)}-\mu)(x^{(i)}-\mu)^T}$$
3. 当新的样本 x 到来时，计算 $p(x)$ ：

$$p(x)=\frac{1}{(2\pi)^{\frac{n}{2}}|\Sigma|^{\frac{1}{2}}}exp(-\frac{1}{2}(x-\mu)^T\Sigma^{-1}(x-\mu))$$

### 模型定义

\begin{align*} p(x)&=p(x_1;\mu_1,\sigma_1^2)p(x_2;\mu_2,\sigma_2^2) \cdots p(x_n;\mu_n,\sigma_n^2)\ &=\prod_{j=1}^{n}p(x_j;\mu_j,\sigma_j^2)\ &=\prod_{j=1}^{n} \frac{1}{\sqrt{2\pi}\sigma_{j}}exp(-\frac{(x_{j}-\mu_{j})^2}{2\sigma_{j}^2}) \end{align*}

$$p(x)=\frac{1}{(2\pi)^{\frac{n}{2}}|\Sigma|^{\frac{1}{2}}}exp(-\frac{1}{2}(x-\mu)^T\Sigma^{-1}(x-\mu))$$

## 四. Anomaly Detection 测试

### 1. Question 1

For which of the following problems would anomaly detection be a suitable algorithm?

A. Given a dataset of credit card transactions, identify unusual transactions to flag them as possibly fraudulent.

B. Given data from credit card transactions, classify each transaction according to type of purchase (for example: food, transportation, clothing).

C. Given an image of a face, determine whether or not it is the face of a particular famous individual.

D. From a large set of primary care patient records, identify individuals who might have unusual health conditions.

A、D 才适合异常检测算法。

### 2. Question 2

Suppose you have trained an anomaly detection system for fraud detection, and your system that flags anomalies when $p(x)$ is less than ε, and you find on the cross-validation set that it is missing many fradulent transactions (i.e., failing to flag them as anomalies). What should you do?

A. Decrease $\varepsilon$

B. Increase $\varepsilon$

### 3. Question 3

Suppose you are developing an anomaly detection system to catch manufacturing defects in airplane engines. You model uses

$$p(x) = \prod_{j=1}^{n}p(x_{j};\mu_{j},\sigma_{j}^{2})$$

You have two features $x_1$ = vibration intensity, and $x_2$ = heat generated. Both $x_1$ and $x_2$ take on values between 0 and 1 (and are strictly greater than 0), and for most "normal" engines you expect that $x_1 \approx x_2$. One of the suspected anomalies is that a flawed engine may vibrate very intensely even without generating much heat (large $x_1$, small $x_2$), even though the particular values of $x_1$ and $x_2$ may not fall outside their typical ranges of values. What additional feature $x_3$ should you create to capture these types of anomalies:

A. $x_3 = \frac{x_1}{x_2}$

B. $x_3 = x_1^2\times x_2^2$

C. $x_3 = (x_1 + x_2)^2$

D. $x_3 = x_1 \times x_2^2$

### 4. Question 4

Which of the following are true? Check all that apply.

A. When evaluating an anomaly detection algorithm on the cross validation set (containing some positive and some negative examples), classification accuracy is usually a good evaluation metric to use.

B. When developing an anomaly detection system, it is often useful to select an appropriate numerical performance metric to evaluate the effectiveness of the learning algorithm.

C. In a typical anomaly detection setting, we have a large number of anomalous examples, and a relatively small number of normal/non-anomalous examples.

D. In anomaly detection, we fit a model p(x) to a set of negative (y=0) examples, without using any positive examples we may have collected of previously observed anomalies.

### 5. Question 5

You have a 1-D dataset $\begin{Bmatrix} x^{(i)},\cdots,x^{(m)} \end{Bmatrix}$ and you want to detect outliers in the dataset. You first plot the dataset and it looks like this:

Suppose you fit the gaussian distribution parameters $\mu_1$ and $\sigma_1^2$ to this dataset. Which of the following values for $\mu_1$ and $\sigma_1^2$ might you get?

A. $\mu = -3$,$\sigma_1^2 = 4$

B. $\mu = -6$,$\sigma_1^2 = 4$

C. $\mu = -3$,$\sigma_1^2 = 2$

D. $\mu = -6$,$\sigma_1^2 = 2$

GitHub Repo：Halfrost-Field

Follow: halfrost · GitHub

### Subscribe to Halfrost's Field | 冰霜之地

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!