0

어떻게이 오류를 해결할 수 있습니까?오류 : chol : 입력 행렬이 양수이어야합니다.

mvnpdf.m

% y = mvnpdf(x,mu,Sigma) 
% Compute multivariate normal pdf for x given mean mu and covariance matrix 
% sigma. The dimension of x is d x p, mu is 1 x p and sigma is p x p. 

function pdf = mvnpdf(x,mu,sigma) 
    [d,p] = size(x); 
    % mu can be a scalar, a 1xp vector or a nxp matrix 
    if nargin == 1, mu = 0; end 
    if all(size(mu) == [1,p]), mu = repmat(mu,[d,1]); end 
    if nargin < 3 
    pdf = (2*pi)^(-p/2) * exp(-sumsq(x-mu,2)/2); 
    else 
    r = chol(sigma); 
    pdf = (2*pi)^(-p/2) * exp(-sumsq((x-mu)/r,2)/2)/prod(diag(r)); 
    end 

pdfdep.m

function pdfmx = pdfdep(train, test) 
% computes probability density for all classes 
% assuming feature independence 
% train - train set; the first column contains label 
% used to compute mean and variation for all classes 
% test - test set (without labels) 
% pdfmx - matrix of probability density for all classes 
% class with label idx is stored in pdfmx(:,idx) 

    classnb = rows(unique(train(:,1))); 

    pdfmx = ones(rows(test), classnb); 

    for cl=1:classnb 
     clidx = train(:,1) == cl; 

     mu = mean(train(clidx,2:end)); 
     sigma = cov(train(clidx,2:end)); 

     pdfmx(:,cl) = mvnpdf(test, mu, sigma); 
    end 

mat.txt

1 2 3 4 5 6 7 8 
2 3 4 5 6 7 8 1 
3 4 5 6 7 8 1 2 
4 5 6 7 8 1 2 3 
1 8 7 6 5 4 3 2 
2 7 6 5 4 3 2 9 
3 6 5 4 3 2 9 8 
4 5 4 3 2 9 8 7 
1 8 7 6 5 4 3 2 
3 6 5 4 3 2 9 8 

오류 메시지 :

>> mat2 = mat; 
>> pdfdep(mat, mat2) 
error: chol: input matrix must be positive definite 
error: called from 
    mvnpdf at line 13 column 7 
    pdfdep at line 20 column 15 
>> 

답변

2

오류가 매우 자명에게있다는

input matrix must be positive definite

은 매트릭스 (sigma)을 따라서 당신이 그것에 콜레 분해를 실행할 수 없습니다, 명확한 긍정적되지 않음을 의미합니다. 좋은 방법으로 공분산을 계산하는 데는 여러 가지 방법이 있습니다. 데이터가 열화되었을 때 경험적 추정치 (cov로 전화하는 것)를 계산하는 것은 작동하지 않습니다 (낮은 차원의 매니 폴드에 있음).

cov(X) + eps * I 

대신

cov(X) 

따라서 단지

sigma = cov(train(clidx,2:end)); 

이 추가 + eps * I (I이고 통합 변경 : 가장 간단한 솔루션 중 하나는 형태의 "뽑아 추정"을 사용하는 것입니다 적절한 차원의 단위 행렬).

+1

죄송합니다. 작동 안함! – anonymous

+1

그렇습니다. 충분히 큰 엡실론으로 문제를 해결할 수 있습니다. – lejlot

관련 문제