Compare commits

..

2 Commits

2 changed files with 63 additions and 27 deletions

View File

@ -14,7 +14,7 @@ function [f, df, d2f, R2] = likelihood_quadratic_approximation(particles, likeli
% - R2 [double] scalar, goodness of fit measure.
%
% REMARKS
% [1] Function f takes a n×m matrix as input argument (the function is evaluated in m points) and returns a m×1 vector.
% [1] Function f takes a n×m matrix as input argument (the approximated likelihood is evaluated in m points) and returns a m×1 vector.
% [2] Funtion df takes a n×1 vector as input argument (the point where the gradient is computed) and returns a n×1 vector.
% Copyright © 2024 Dynare Team
@ -121,7 +121,6 @@ function [f, df, d2f, R2] = likelihood_quadratic_approximation(particles, likeli
function xx = dcrossproducts(x)
xx = zeros(n, n*(n+1)/2);
size(xx)
for i = 1:n
base = (i-1)*n-sum(0:i-2);
incol = 1;
@ -138,4 +137,55 @@ function [f, df, d2f, R2] = likelihood_quadratic_approximation(particles, likeli
end
end
return % --*-- Unit tests --*--
%@test:1
% Create data
X = randn(10,1000);
Y = 1 + rand(1,10)*X.^2+0.01*randn(1,1000);
% Perform approximation
try
[f,df, d2f,R2] = likelihood_quadratic_approximation(X,Y);
t(1) = true;
catch
t(1) = false;
end
% Test returned arguments
if t(1)
try
y = f(randn(10,100));
t(2) = true;
if ~(rows(y)==100 && columns(y)==1)
t(2) = false;
end
catch
t(2) = false;
end
try
dy = df(zeros(10,1));
t(3) = true;
if ~(rows(dy)==10 && columns(dy)==1)
t(3) = false;
end
catch
t(3) = false;
end
t(4) = true;
if ~(rows(d2f)==10 && columns(d2f)==10)
t(4) = false
end
if ~(rows(d2f)==10 && columns(d2f)==10)
t(4) = false
end
t(4) = issymmetric(d2f);
t(4) = ispd(d2f);
t(5) = isscalar(R2);
t(5) = (R2>0) & (R2<1); % Note that in a nonlinear model nothing ensures that these inequalities are satisfied.
end
T = all(t);
%@eof:1
end

View File

@ -1,30 +1,15 @@
function [test, penalty] = ispd(A)
%@info:
%! @deftypefn {Function File} {[@var{test}, @var{penalty} =} ispd (@var{A})
%! @anchor{ispd}
%! @sp 1
%! Tests if the square matrix @var{A} is positive definite.
%! @sp 2
%! @strong{Inputs}
%! @sp 1
%! @table @ @var
%! @item A
%! A square matrix.
%! @end table
%! @sp 2
%! @strong{Outputs}
%! @sp 1
%! @table @ @var
%! @item test
%! Integer scalar equal to 1 if @var{A} is a positive definite sqquare matrix, 0 otherwise.
%! @item penalty
%! Absolute value of the uum of the negative eigenvalues of A. This output argument is optional.
%! @end table
%! @end deftypefn
%@eod:
% Test if the square matrix A is positive definite.
%
% INPUTS
% - A [double] n×n matrix.
%
% OUTPUTS
% - test [logical] scalar, true if and only if matrix A is positive definite (and symmetric...)
% - penalty [double] scalar, absolute value of the uum of the negative eigenvalues of A. This output argument is optional.
% Copyright © 2007-2022 Dynare Team
% Copyright © 2007-2024 Dynare Team
%
% This file is part of Dynare.
%
@ -54,6 +39,7 @@ if nargout>1
if isoctave && any(any(~isfinite(A))) % workaround for https://savannah.gnu.org/bugs/index.php?63082
penalty = 1;
else
% TODO: the penalty is only concerned with negative eigenvalues, we should also consider the case of non symmetric matrix A.
a = diag(eig(A));
k = find(a<0);
if k > 0
@ -61,4 +47,4 @@ if nargout>1
end
end
end
end
end