https://github.com/jdiedrichsen/pcm_toolbox
Raw File
Tip revision: 4e290a8b2c0d0820f868b7bcb60a3da7bb30e6ee authored by Jörn Diedrichsen on 26 April 2023, 01:59:24 UTC
Update pcm_estimateRegression.m
Tip revision: 4e290a8
pcm_makePD.m
function Gpd = pcm_makePD(G,th)
% function Gpd = pcm_makePD(G,th)
% Enforces that G is semi-positive definite by removing possible negative
% eigenvectors from the square matrix 
% INPUT: 
%       G:      estimated KxK second momement matrix 
%       th:     threshold
% OUTPUT:
%       Gpd :   semi-positive definite version of G   

if nargin<2
    th = eps;
end
    
% Make the G-estimate postive definite: 
G = (G+G')/2;        % Symmetrize 
[V,lam_G] = eig(full(G));
dS    = diag(lam_G);
idx   = dS>th;
Gpd     = V(:,idx)*lam_G(idx,idx)*V(:,idx)'; 
back to top