有没有一种有效的方法来计算返回矩阵p的多元高斯(如下所示),即利用某种矢量化?我知道矩阵p是对称的,但是对于大小为40000x3的矩阵,例如,这将需要相当长的时间。
Matlab代码示例:
DataMatrix = [3 1 4; 1 2 3; 1 5 7; 3 4 7; 5 5 1; 2 3 1; 4 4 4];
[rows, cols ] = size(DataMatrix);
I = eye(cols);
p = zeros(rows);
for k = 1:rows
p(k,:) = mvnpdf(DataMatrix(:,:),DataMatrix(k,:),I);
end
答案 0 :(得分:2)
迭代地,我们正在执行
{
"repositories": [
{
"type": "composer",
"url": "http://composer.typo3.org/"
},
{
"type": "package",
"package": {
"name": "metaxos/exaibbrplus",
"version": "dev-2016",
"type": "typo3-cms-extension",
"source": {
"url": "https://metaxos@bitbucket.org/metaxos/exaibbrplus.git",
"type": "git",
"reference": "release/2016"
},
"autoload": {
"psr-4": {
"Metaxos\\Exaibbrplus\\": "Classes/"
}
},
}
}
],
"name": "Metaxos/ibbrating2016",
"require": {
"typo3/cms": "7.6.2",
"bk2k/bootstrap-package" : "dev-master",
"typo3-ter/compatibility6" : "7.6.0",
"typo3-ter/extension-builder" : "7.6.0",
"metaxos/exaibbrplus": "dev-2016"
},
"extra": {
"typo3/cms": {
"cms-package-dir": "{$vendor-dir}/typo3/cms",
"web-dir": "web"
}
}
}
语法为:mvnpdf(DataMatrix(:,:),DataMatrix(k,:),I)
。
因此,与我们的输入的对应关系变为:
mvnpdf(X,Mu,Sigma)
对于与我们的情况相关的尺寸,源代码X = DataMatrix(:,:);
Mu = DataMatrix(k,:);
Sigma = I
会缩减为 -
mvnpdf.m
现在,如果Sigma始终是一个单位矩阵,我们也会%// Store size parameters of X
[n,d] = size(X);
%// Get vector mean, and use it to center data
X0 = bsxfun(@minus,X,Mu);
%// Make sure Sigma is a valid covariance matrix
[R,err] = cholcov(Sigma,0);
%// Create array of standardized data, and compute log(sqrt(det(Sigma)))
xRinv = X0 / R;
logSqrtDetSigma = sum(log(diag(R)));
%// Finally get the quadratic form and thus, the final output
quadform = sum(xRinv.^2, 2);
p_out = exp(-0.5*quadform - logSqrtDetSigma - d*log(2*pi)/2)
作为单位矩阵。因此,R
与X0 / R
相同,后者保存为X0
。所以,基本上是xRinv
因此,原始代码 -
quadform = sum(X0.^2, 2);
缩减为 -
for k = 1:rows
p(k,:) = mvnpdf(DataMatrix(:,:),DataMatrix(k,:),I);
end
现在,如果输入矩阵的大小为[n,d] = size(DataMatrix);
[R,err] = cholcov(I,0);
p_out = zeros(rows);
K = sum(log(diag(R))) + d*log(2*pi)/2;
for k = 1:rows
X0 = bsxfun(@minus,DataMatrix,DataMatrix(k,:));
quadform = sum(X0.^2, 2);
p_out(k,:) = exp(-0.5*quadform - K);
end
,您可能想要在此处停止。但是在系统资源允许的情况下,您可以按照下面的讨论对所有内容进行矢量化。
既然我们已经看到实际发生了什么并且计算看起来可以并行化,那么现在是时候在40000x3
与他的好朋友一起使用bsxfun
了{ {3}}对于矢量化解决方案,如此 -
3D