From 265970c1c90fc044ce1af68c54b35c67a3ff13ee Mon Sep 17 00:00:00 2001
From: Michel Juillard <michel.juillard@mjui.fr>
Date: Sun, 22 Feb 2015 21:54:39 +0100
Subject: [PATCH] adding global test for identification for sbvar

---
 matlab/ms-sbvar/identification/exclusions.m   |  2 +-
 .../sbvar_global_identification_check.m       | 77 +++++++++++++++++++
 .../test_global_identification_FAILURE.mod    | 16 ++++
 .../test_global_identification_OK.mod         | 15 ++++
 4 files changed, 109 insertions(+), 1 deletion(-)
 create mode 100644 matlab/ms-sbvar/sbvar_global_identification_check.m
 create mode 100644 tests/ms-sbvar/test_global_identification_FAILURE.mod
 create mode 100644 tests/ms-sbvar/test_global_identification_OK.mod

diff --git a/matlab/ms-sbvar/identification/exclusions.m b/matlab/ms-sbvar/identification/exclusions.m
index 9632356fe2..a2447b1ba1 100644
--- a/matlab/ms-sbvar/identification/exclusions.m
+++ b/matlab/ms-sbvar/identification/exclusions.m
@@ -1,4 +1,4 @@
-function [Ui,Vi,n0,np,ixmC0Pres] = exclusions(nvar,nexo,options_ms)
+function [Ui,Vi,n0,np,ixmC0Pres,Qi] = exclusions(nvar,nexo,options_ms)
 % function [Ui,Vi,n0,np,ixmC0Pres] = exclusions(nvar,nexo,options_ms)
 %
 % INPUTS
diff --git a/matlab/ms-sbvar/sbvar_global_identification_check.m b/matlab/ms-sbvar/sbvar_global_identification_check.m
new file mode 100644
index 0000000000..642220f54f
--- /dev/null
+++ b/matlab/ms-sbvar/sbvar_global_identification_check.m
@@ -0,0 +1,77 @@
+function indent = sbvar_global_identification_check(options_)
+% function sbvar_global_identification_check(options_.ms)
+%
+% INPUTS
+%    options_ms:    (struct)    options
+%
+% OUTPUTS
+%    ident:          (boolean) false = not identified; true = identified
+%
+% SPECIAL REQUIREMENTS
+%    none
+
+% Copyright (C) 2015 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/>.
+
+ident = false;
+
+if isequal(options_.ms.restriction_fname, 'upper_cholesky') || ...
+        isequal(options_.ms.restriction_fname, 'lower_cholesky')
+    ident = true;
+    if ~options_.noprint
+        disp(' ')
+        disp('SBVAR: Cholesky identification is always identified')
+        disp(' ')
+    end
+    return
+end
+nvar = length(options_.varobs);   % number of endogenous variables
+nexo = 1;
+   
+[Uiconst,Viconst,n0,np,ixmC0Pres,Qi] = exclusions(nvar,nexo,options_.ms );
+
+% order column constraints by rank
+Qranks = zeros(nvar,2);
+for j=1:nvar
+    Qranks(j,:) = [j,rank(Qi{j})];
+end
+Qranks = sortrows(Qranks,-2);
+
+ident = true;
+
+for j=1:nvar
+    i = Qranks(j,1);
+    for k=1:1
+        M = [Qi{i}*rand(size(Qi{i},1),nvar);[eye(j) zeros(j,nvar- ...
+                                                          j)]];
+        disp([j,k,rank(M)])
+        if rank(M) < nvar
+            ident = false
+            break
+        end
+    end
+end
+
+if ~options_.noprint
+    disp(' ')
+    if ident
+        disp('The sufficient condition for SBVAR identification is met')
+    else
+        disp('WARNGING: The sufficient condition for SBVAR identification is not met')
+    end
+    disp(' ')
+end
\ No newline at end of file
diff --git a/tests/ms-sbvar/test_global_identification_FAILURE.mod b/tests/ms-sbvar/test_global_identification_FAILURE.mod
new file mode 100644
index 0000000000..8ff9cfdddc
--- /dev/null
+++ b/tests/ms-sbvar/test_global_identification_FAILURE.mod
@@ -0,0 +1,16 @@
+// same as test_lower_cholesky.mod, but using exclusion syntax
+var R Pie Y;
+
+varobs Y Pie R;
+
+svar_identification;
+exclusion lag 0;
+equation 1, Pie, Y;
+exclusion lag 1;
+equation 2, Y;
+end;
+
+sbvar_global_identification_check(options_);
+
+sbvar(datafile = data,freq=4,initial_year=1959,final_year=2005,nlags=4);
+
diff --git a/tests/ms-sbvar/test_global_identification_OK.mod b/tests/ms-sbvar/test_global_identification_OK.mod
new file mode 100644
index 0000000000..7e54d9223c
--- /dev/null
+++ b/tests/ms-sbvar/test_global_identification_OK.mod
@@ -0,0 +1,15 @@
+// same as test_lower_cholesky.mod, but using exclusion syntax
+var R Pie Y;
+
+varobs Y Pie R;
+
+svar_identification;
+exclusion lag 0;
+equation 1, Pie, Y;
+equation 2, Y;
+end;
+
+sbvar_global_identification_check(options_);
+
+sbvar(datafile = data,freq=4,initial_year=1959,final_year=2005,nlags=4);
+
-- 
GitLab