Anomaly detection - Multivariate Gaussian distribution

多元高斯分布(Multivariate Gaussian Distribution)(详细说明,便于理解) 在深入了解多元高斯分布前,可以先了解一下一元高斯分布。 接下来对多元高斯分布进行详细的说明与推导。 对于 维的向量(连续变量),多变量(多元, multivariate Gaussian)高斯分布形式为: 式中的是 维的均值向量,是的协方差矩阵(covariance matrix),是的行列式(determinan... 阅读详情

摘要: 本文是吴恩达 (Andrew Ng)老师《机器学习》课程,第十六章《异常检测》中第129课时《多变量高斯分布》的视频原文字幕。为本人在视频学习过程中记录下来并加以修正,使其更加简洁,方便阅读,以便日后查阅使用。现分享给大家。如有错误,欢迎大家批评指正,在此表示诚挚地感谢!同时希望对大家的学习能有所帮助.

————————————————

Let's talk about one possible extension to the anomaly detection algorithm: Multivariate Gaussian distribution.

Why we need multivariate Gaussian distribution

We'll start with an example of monitoring machines in the data center. If model the two features x_{1} (CPU load) and x_{2} (Memory Use) with Gaussian, they look like the two figures on the right. Let's say we have an example shown as the green cross (x_{1}\approx 0.4; x_{2}\approx 1.5). If look at the data which are shown as red croses, most of them lie in the blue ellipse region. And the two features sort of grow linearly with each other. Whereas for the green cross, the CPU load is very low but memory usage is very high. So the green cross should be raised as anomaly.

But the anomaly detection would fail to flag the green cross as anomaly. The p(x_{1})=p(0.4) and p(x_{2})=p(1.5) both are pretty / reasonably high. It turns out the anomaly detection algorithm is not realizing that the blue ellipse shows the high probability region. Instead, it "sees" those concentric circles and thinks inner circles have higher probability than outter ones. And thinks the green cross has pretty high probability. And actually it tends to think that everything on the, for example, 2nd circle has about equal probability, and doesn't realize the probability of the two blue crosses actually are very different.

Definition of multivariate Gaussian distribution

To fix this, we're going to develop a modified version of the anomaly detection algorithm using multivariate Gaussian / normal distribution. We have:

Instead of model p(x_{1}), p(x_{2}),..., separately, we're going to model p(x) all in one go. And here comes the formula:

p(x;\mu ,\Sigma )=\frac{1}{(2\pi)^{n/2}\left | \Sigma \right |^{\frac{1}{2}}}e^{(-\frac{1}{2}(x-\mu )^{T}\Sigma ^{-1}(x-\mu ))}

Where,

\left | \Sigma \right | is called the determinant (行列式) of Sigma (\Sigma). In Octave, you can compute it with det(\Sigma ).

Examples

Then what this p(x; \mu ,\Sigma ) looks like?

Example-1

Suppose we have:

Two features x_{1}, x_{2}, thus n=2

\mu =\begin{bmatrix} 0\\ 0 \end{bmatrix}   \Sigma =\begin{bmatrix} 1 & 0\\ 0 & 1 \end{bmatrix}(identity matrix)

The p(x) will look like figure-1.a. And its highest value at about when x_{1}=x_{2}=0. Down below is a contour plot. 

Following is the experiment I did. Just copy & paste the code into Octave to take a look. And its plot looks like figure-1.b.

function p = multivariate_normal(x, n, mean, covariance)
    x_m = x - mean;
    p=1/((2*pi)^(n/2)*det(covariance)^(0.5))*e^(-0.5*x_m'*inv(covariance)*x_m);
endfunction

n=2;
u=[0;0]
covariance=[1,0;0,1]

tx1 = tx2 = linspace(-4,4,100);

[xx, yy] = meshgrid (tx1, tx2);

for i=1:size(tx1)(2)
    for j=1:size(tx2)(2)
        x=[tx1(i);tx2(j)];
        px=multivariate_normal(x, n, u, covariance);
        pplot(j,i)=px;
    end
end

subplot(2,2,1)
#meshc(tx1, tx2, pplot)
surf(tx1, tx2, pplot)
axis([-4 4 -4 4 0 0.4])

subplot(2,2,3)
contourf(tx1, tx2, pplot)
axis([-4 4 -4 4])

Example-2

Let's check more examples by varing some of parameters.

Suppose we changed \Sigma as the following:

Two features x_{1}, x_{2}, thus n=2

\mu =\begin{bmatrix} 0\\ 0 \end{bmatrix}   \Sigma =\begin{bmatrix} 0.6 & 0\\ 0 & 0.6 \end{bmatrix}

Then it looks like figure-2.a. By shrinking the \Sigma, the width of the bump diminishes and the height also increases a bit because the integral of the volume under the surface is equal to 1. Similarly, figure-2.b is the plot via my above code in Octave.

Example-3

Suppose we changed \Sigma as the following:

Two features x_{1}, x_{2}, thus n=2

\mu =\begin{bmatrix} 0\\ 0 \end{bmatrix}   \Sigma =\begin{bmatrix} 2 & 0\\ 0 & 2 \end{bmatrix}

Then you'll have much wider and much flatter Gaussian as figure-3.a, 3.b & 3.c. Again 3.b is output of my above Octave codes.

Example-4

Suppose we changed \Sigma as the following, that is just change one of the elements of \Sigma:

Two features x_{1}, x_{2}, thus n=2

\mu =\begin{bmatrix} 0\\ 0 \end{bmatrix}   \Sigma =\begin{bmatrix} 0.6 & 0\\ 0 & 1\end{bmatrix}

This reduces the variance of feature x_{1}. And the plot looks like figure-4.a. And 4.b is output of my above Octave codes.

Example-5

Suppose we changed \Sigma as the following,

Two features x_{1}, x_{2}, thus n=2

\mu =\begin{bmatrix} 0\\ 0 \end{bmatrix}   \Sigma =\begin{bmatrix} 2 & 0\\ 0 & 1\end{bmatrix}

The plot looks like figure-5.a & 5.b. The distribution falls off more slowly as x_{1} moves away from 0. And falls off very rapidly as x_{2} moves away from 0. Figure-5.b is output of my Octave code above.

Example-6

One of the cool things of the multivariate Gaussian distribution is that you can also use it to model correlations between the data. That is we can use it to model the fact that x_{1} and x_{2} tend to be highly correlated with each other. Let's change the off diagonal entries of the covariance matrix. Figure-6.a is for \mu =\begin{bmatrix} 0\\ 0 \end{bmatrix}   \Sigma =\begin{bmatrix} 1 & 0\\ 0 & 1\end{bmatrix}. And figure-6.b is for \mu =\begin{bmatrix} 0\\ 0 \end{bmatrix}   \Sigma =\begin{bmatrix} 1 & 0.5\\ 0.5 & 1\end{bmatrix}.

Above plot can be generated via codes:

	function p = multivariate_normal(x, n, mean, covariance)
		x_m = x - mean;
		p=1/((2*pi)^(n/2)*det(covariance)^(0.5))*e^(-0.5*x_m'*inv(covariance)*x_m);
	endfunction

	n=2;
	u=[0;0]
	covariance=[1,0;0,1]

	tx1 = tx2 = linspace(-4,4,100);

	[xx, yy] = meshgrid (tx1, tx2);

	for i=1:size(tx1)(2)
		for j=1:size(tx2)(2)
			x=[tx1(i);tx2(j)];
			px=multivariate_normal(x, n, u, covariance);
			pplot(j,i)=px;
		end
	end

	subplot(2,2,1)
	#meshc(tx1, tx2, pplot)
	surf(tx1, tx2, pplot)
	axis([-4 4 -4 4 0 0.4])

	subplot(2,2,3)
	contourf(tx1, tx2, pplot)
	axis([-4 4 -4 4])



	covariance=[1,0.5;0.5,1]

	for i=1:size(tx1)(2)
		for j=1:size(tx2)(2)
			x=[tx1(i);tx2(j)];
			px=multivariate_normal(x, n, u, covariance);
			pplot(j,i)=px;
		end
	end

	subplot(2,2,2)
	surf(tx1, tx2, pplot)
	axis([-4 4 -4 4 0 0.4])

	subplot(2,2,4)
	contourf(tx1, tx2, pplot)
	axis([-4 4 -4 4])

Example-7

Figure-7.a is for \mu =\begin{bmatrix} 0\\ 0 \end{bmatrix}   \Sigma =\begin{bmatrix} 1 & 0\\ 0 & 1\end{bmatrix}. And figure-7.b is for \mu =\begin{bmatrix} 0\\ 0 \end{bmatrix}   \Sigma =\begin{bmatrix} 1 & 0.8\\ 0.8 & 1\end{bmatrix}. Comparing with example-6, as we increase the off-diagonal entries, from .5 to .8, what we get is a distribution that is more and more thinly peaked along this sort of x_{1}=x_{2}. line.

Example-8

If we set the off-diagnal entries to negative values, like what shown in figure-8.b where \mu =\begin{bmatrix} 0\\ 0 \end{bmatrix}  \Sigma =\begin{bmatrix} 1 & -0.5\\ -0.5 & 1\end{bmatrix}, then most of the probability now lie in this region where x_{1}=-x_{2}. Figure-8.a is for \mu =\begin{bmatrix} 0\\ 0 \end{bmatrix}   \Sigma =\begin{bmatrix} 1 & 0\\ 0 & 1\end{bmatrix} for comparison.

Similarly, following is for \mu =\begin{bmatrix} 0\\ 0 \end{bmatrix}   \Sigma =\begin{bmatrix} 1 & -0.8\\ -0.8 & 1\end{bmatrix}:

Example-9

So far, we have been changing \Sigma, now let's change \mu.

Figure-10.a is for \mu =\begin{bmatrix} 0\\ 0 \end{bmatrix}   \Sigma =\begin{bmatrix} 1 & 0\\ 0 & 1\end{bmatrix}, its distribution is centered at x_{1}=x_{2}=0. If we vary the mean \mu =\begin{bmatrix} 0\\ 0.5 \end{bmatrix}, it varies the peak of the distribution and looks like figure-10.b.

If  \mu =\begin{bmatrix} 1.5\\ -0.5 \end{bmatrix}, it looks like figure-11.b. Again, figure-11.a is just for comparison with \mu =\begin{bmatrix} 0\\ 0 \end{bmatrix}.

Hopefully looking at all these pictures gives you a sense of the sort of probability distribution that the Multivariate Gaussian Distribution allows you to capture. The key advantage of it is allows you to capture when you'd expect two different features to be positively correlated, or maybe negatively correlated.

<end>

 

 

 

 

【信息科学与工程学】【安全领域】第三十五篇 网络安全算法表 编号算法名称算法的逐步骤思考推理的数学方程式和分解的数学方程式参数/变量列表复杂度(时间/空间)1​AES (高级加密标准)​1. 密钥扩展:从初始密钥生成轮密钥。 2. 初始轮: 3. 主轮循环(Nr-1轮):  - SubBytes:(非线性字节替换)  - ShiftRows:对状态矩阵的行进行循环移位。  - MixColumns:(在GF(2⁸)上的矩阵乘法)  - AddRoundKey: 4. 最终轮:执行SubBytes、ShiftRows、AddR 阅读详情

相关推荐

国央企创新负责人如何通过平台实现科研成果的快速识别与转化?.docx

科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。

cannot import name multivariate_normal

今天在使用别人代码的时候,代码提示ImportError: cannot import name multivariate_normal虽然我已经安装了scipy库,但是由于版本较低,所以就报这个错误。import scipy print scipy.version.full_version就可以查看scipy的版本号解决上面问题的方法显然是更新scipy版本。pip install scipy -

Running_J的博客 3243

PotPlayer自用皮肤

自用 PotPlayer 皮肤,仅微调了字体大小与颜色,是按个人习惯调整,未做适配性调整。

解决python导包报错ImportError: cannot import name ‘xxx‘ from

花了较长时间,最后发现循环引用导致。既然循环引用了,只要让让它们别循环引用就行。 那么解决掉循环引用的部分即可正常! 解决办法:1,拆解成多个文件,将要互相引用的分开 2,缩小导入的范围,如一般情况下import在文件顶部,这时候可以将import移动到紧挨需要导入的代码上方即可; ...

空山新雨后,天气晚来秋 5万+

【CUT&TAG分析-加了spike in的样品】

使用 Bowtie2 或 Subread (subjunc) 来构建大肠杆菌的索引。bowtie2比对时设置参数–local, 局部比对。

weixin_57975238的博客 1398

HALCON实战:用两个工业相机实现无缝大视野拼接(附完整代码与避坑指南)

本文详细介绍了使用HALCON软件实现双工业相机图像无缝拼接的完整解决方案,涵盖硬件配置、相机标定、坐标系转换和图像拼接等关键步骤。通过高精度标定和矩阵变换技术,可构建大视野检测系统,适用于液晶面板、PCB等工业检测场景,显著提升检测效率和精度。

weixin_29050513的博客 191

Python报错:ImportError: cannot import name ‘xxx‘ from ‘xxx‘

Python报错:ImportError: cannot import name ‘xxx‘ from ‘xxx‘

weixin_61908666的博客 4万+

从接线到组态:手把手教你搞定PROFIBUS-DP与PA的混合网络搭建(基于西门子TIA Portal)

本文详细介绍了PROFIBUS-DP与PA混合网络的搭建方法,基于西门子TIA Portal平台,从硬件选型、网络架构设计到组态配置和现场调试,提供了一套完整的实战指南。重点解析了DP/PA耦合器的使用、地址映射策略及网络诊断技巧,帮助工程师高效构建高可靠性的工业自动化网络。

weixin_32103009的博客 302

ImportError :cannot import name xxxxxx 的三种类型的解决方法

网上关于这种问题的解决方案一大堆,但是绝大多数都是不适用,或者说解决不了问题,我根据别人所遇到的和我自己遇到的,对这个问题整理了一下,希望能解决这个问题。 一、缺少这个module或者func或者package 缺少python的各种package的话,就自己pip一下 根据路径去找,发现是否忘记导入了 二、命名问题 这个问题也是很难容易解决,找一下路径,对应一下两个name,改一下就行。 还有可能出现这种情况的是你使用的是别的官方的代码,版本升级,导致这个函数不用了,改成了另一个函数了,这个时候就要

Activewaste 21万+

PlutoSDR在matlab中的环境配置(包括驱动和硬件支持包)

双击下载到的驱动,除了点击接受协议和设置安装路径,其他只要点击下一步或者安装即可。登录或者注册你的账号,选择你的matlab版本(这里我的是2023b),点击下一步。设置安装路径,并选择我接受,点击下一步,即可安装完成,并在安装路径中得到。完成后,打开设备管理器,在端口和通用串行总线设备一栏,即可看见下图。点击进入,选择相应的系统安装包,这里我选择的Windows。可看到我们要安装的Pluto支持包,安装即可。右击桌面matlab,选择打开文件所在位置。右上角搜索pluto,选择并点击下一步。

weixin_53160338的博客 4864

Isaac Sim 入门教程(四)强化学习策略训练宇树人形机器人四足机器人示例

isaac_sim_policy_example Extension 是在 Isaac Sim 中部署 Isaac Lab 强化学习策略的一个框架和一组辅助函数。有关在 Isaac Sim 中训练和构建策略的详细信息,请访问在 Isaac Sim 中部署策略。该扩展程序默认已启用。如果被禁用,可在扩展管理器中搜索 isaacsim.robot.policy.example 重新启用。要运行下面的示例,请激活 “视窗”>“示例”>“机器人示例”,这将打开 “机器人示例 ”选项卡。

机器人控制与仿真应用 3925

cuda11.8 pytorch2.2.2 尝试安装cxtrack出现的问题报错

cuda11.8 pytorch2.2.2 python3.8尝试安装cxtrack。

z411470859的博客 254

关于“为爱发电”的一些说明~~

专栏教学资料+工程文件

数字IC/FPGA杂货铺,找一找,或许有你想要的~ 2674

Anomaly detection - Anomaly detection using the multivariate Gaussian distribution

Let's develop a different anomaly detection algorithm based on multivariate Gaussian distribution. Parameters fitting Figure-1The upper part of figure-1 is a recap for definition of multivariate Gaussian distribution. Also it shows a range of different d

王彩旗的技术博客 253

EXCEL VBA从入门到精通 前言

本书通过丰富的实例和案例,帮助读者更好地理解和应用Excel VBA编程,同时也介绍了常用的工具、资源、社区和协作平台等,方便读者进行进一步的学习和开发。我强烈推荐这本书给所有希望学习和掌握Excel VBA编程的读者。多样化的资源和工具介绍:本书介绍了丰富的工具和资源,包括官方文档、在线教程、代码库、论坛、博客等,方便读者进行进一步的学习和开发。详细的讲解和丰富的示例:本书从语法和对象开始讲解,通过丰富的实例和案例,帮助读者更好地理解和应用Excel VBA编程。第一节:什么是VBA 5。

1728

把入库交货单的 GOS 附件自动带进审批 Work Item:用 SOFM 对象打通 SAP Workflow 附件链路

本文介绍了一种在SAP ECC系统中将入库交货单的GOS附件自动附加到Workflow审批项中的解决方案。通过使用BOR对象引用列表和SOFM业务对象,审批人可直接在Work Item中查看附件,无需返回原单据界面。文章详细阐述了从BUS2015业务对象获取附件连接、转换LOIO_ID为SOFM对象引用,并将附件列表绑定到User Decision步骤的具体实现方法。该方案适用于经典SAP Business Workflow+BOR架构,核心ABAP代码可直接复用,有效解决了审批流程中的附件可见性问题。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 246

吴恩达机器学习笔记56-多元高斯分布及其在误差检测中的应用(Multivariate Gaussian Distribution & Anomaly Detection using the Multi...

一、多元高斯分布简介     假使我们有两个相关的特征,而且这两个特征的值域范围比较宽,这种情况下,一般的高斯分布模型可能不能很好地识别异常数据。其原因在于,一般的高斯分布模型尝试的是去同时抓住两个特征的偏差,因此创造出一个比较大的判定边界。   下图中是两个相关特征,洋红色的线(根据ε 的不同其范围可大可小)是一般的高斯分布模型获得的判定边界,很明显绿色的X 所代表的数据点很可能是异常值,但...

weixin_33847182的博客 490

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

机器学习- 吴恩达Andrew Ng Week9 知识总结 Anomaly Detection

Coursera课程地址 因为Coursera的课程还有考试和论坛,后续的笔记是基于Coursera https://www.coursera.org/learn/machine-learning/home/welcome 异常检测 Anomaly Detection 1. 问题动机 Problem motivation 就像在其他学习问题中一样,我们得到了一个数据集X(1), X(2), …, X(m). 然后我们给出了一个新的例子,x(test), 我们想知道这个新例子是否异常/异常。 我们定义了一个

AI架构师易筋 585

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

机器学习(Machine learning)11——Anomaly detection

机器学习(Machine learning)11——Anomaly detection

qq_41116319的博客 334

[吴恩达机器学习笔记]15非监督学习异常检测7-8使用多元高斯分布进行异常检测

15.异常检测 Anomaly detection 觉得有用的话,欢迎一起讨论相互学习~Follow Me 15.7-8 多变量高斯分布/使用多元高斯分布进行异常检测 -Multivariate Gaussian distribution/ Anomaly Detection using the Multivariate Gaussian Distribution 示例 假设...

泛用演化计算、通用人工智能优化模型 1199

[吴恩达机器学习笔记]15非监督学习异常检测7-8使用多元高斯分布进行异常检测...

15.异常检测 Anomaly detection 觉得有用的话,欢迎一起讨论相互学习~Follow Me 15.7-8 多变量高斯分布/使用多元高斯分布进行异常检测 -Multivariate Gaussian distribution/ Anomaly Detection using the Multivariate Gaussian Distribution 示例 假设下图所示的是数据中心...

weixin_34064653的博客 126

Fansmale 实验室论文列表 bibitem

摘要:本贴给出www.fansmale.com主要论文列表的bibitem,方便论文引用.

minfanphd的博客 2451

解决python导包报错ImportError: cannot import name ‘xxx‘ from‘xxx’

在使用第三方库时,要注意版本是否过高或者过低,可以尝试升级或降级 第三方库。

qq_40719268的博客 1万+

ImportError: cannot import name ‘XXXXX‘ from ‘XX‘解决方案

一、报错: File "./xxx/demo/demo.py", line 23, in <module> from predictor import FeatureExtractionDemo ImportError: cannot import name 'FeatureExtractionDemo' from 'predictor' 二、原因分析: 1、自己的python文件与导入的包重名了,导致引入路径错乱。【主要是 .py 文件路径的问题,当在同一文件下,存在子文件内有同样

qq_49641239的博客 6万+

高斯分布 (Gaussian distribution)

采用正态分布在很多应用中都是一个明智的选择。当我们由于缺乏关于某个实数上分布的先验知识而不知道该选择怎样的形式时,正态分布是默认的比较好的选择,其中有两个原因。...

weixin_40759186的博客 4121
上一篇: Anomaly detection - Choosing what features to use
下一篇: 如何把你的代码一步一步地放到github
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值