File: reduced_rank_cholesky.m

package info (click to toggle)
dynare 4.4.3-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 41,356 kB
  • ctags: 15,842
  • sloc: cpp: 77,029; ansic: 29,056; pascal: 13,241; sh: 4,811; objc: 3,061; yacc: 3,013; makefile: 1,479; lex: 1,258; python: 162; lisp: 54; xml: 8
file content (97 lines) | stat: -rw-r--r-- 2,625 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
function T = reduced_rank_cholesky(X)
% Computes the cholesky decomposition of a symetric semidefinite matrix or of a definite positive matrix.

%@info:
%! @deftypefn {Function File} { @var{T} =} reduced_rank_cholesky (@var{X})
%! @anchor{reduced_rank_cholesky}
%! @sp 1
%! Computes the cholesky decomposition of a symetric semidefinite matrix or of a definite positive matrix.
%! @sp 2
%! @strong{Inputs}
%! @sp 1
%! @table @ @var
%! @item X
%! n*n matrix of doubles to be factorized (X is supposed to be semidefinite positive).
%! @end table
%! @sp 2
%! @strong{Outputs}
%! @sp 1
%! @table @ @var
%! @item T
%! q*n matrix of doubles such that T'*T = X, where q is the number of positive eigenvalues in X.
%! @end table
%! @sp 2
%! @strong{Remarks}
%! @sp 1
%! [1] If X is not positive definite, then X has to be a symetric semidefinite matrix.
%! @sp 1
%! [2] The matrix T is upper triangular iff X is positive definite.
%! @sp 2
%! @strong{This function is called by:}
%! @sp 1
%! @ref{particle/sequential_importance_particle_filter}
%! @sp 2
%! @strong{This function calls:}
%! @sp 2
%! @end deftypefn
%@eod:

% Copyright (C) 2009-2011 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.    
    
[T,X_is_not_positive_definite] = chol(X);

if X_is_not_positive_definite
    n = length(X);
    [U,D] = eig(X);
    [tmp,max_elements_indices] = max(abs(U),[],1);
    negloc = (U(max_elements_indices+(0:n:(n-1)*n))<0);
    U(:,negloc) = -U(:,negloc);
    D = diag(D);
    tol = sqrt(eps(max(D))*length(D)*10);
    t = (abs(D) > tol);
    D = D(t);
    if ~(sum(D<0))
        T = diag(sqrt(D))*U(:,t)';
    else
        disp('reduced_rank_cholesky:: Input matrix is not semidefinite positive!')
        T = NaN;
    end
end

%@test:1
%$ n = 10;
%$ m = 100;
%$
%$ X = randn(n,m);
%$ X = X*X';
%$
%$ t = ones(2,1);
%$
%$ try
%$    T = reduced_rank_cholesky(X);
%$ catch
%$    t(1) = 0;
%$    T = all(t);
%$    return;
%$ end
%$
%$
%$ % Check the results.
%$ t(2) = dyn_assert(T,chol(X),1e-16);
%$ T = all(t);
%@eof:1