1function [co, b, yhat] = cosn(H)
2% function [co, b, yhat] = cosn(H)
3% -------------------------------------------------------------------------
4% computes the cosine of the angle between the (endogenous variable) H(:,1)
5% and its projection onto the span of (exogenous variables) H(:,2:end)
6% Note: This is not the same as multiple correlation coefficient since the
7% means are not zero
8% =========================================================================
9% INPUTS
10%   * H     [n by k]
11%           Data matrix, endogenous variable y is in the first column,
12%           exogenous variables X are in the remaining (k-1) columns
13% -------------------------------------------------------------------------
14% OUTPUTS
15%   * co    [double] (approximate) multiple correlation coefficient
16%   * b     [k by 1] ols estimator
17%   * y     [n by 1] predicted endogenous values given ols estimation
18% -------------------------------------------------------------------------
19% This function is called by
20%   * identification_checks.m
21%   * ident_bruteforce.m
22% =========================================================================
23% Copyright (C) 2008-2019 Dynare Team
24%
25% This file is part of Dynare.
26%
27% Dynare is free software: you can redistribute it and/or modify
28% it under the terms of the GNU General Public License as published by
29% the Free Software Foundation, either version 3 of the License, or
30% (at your option) any later version.
31%
32% Dynare is distributed in the hope that it will be useful,
33% but WITHOUT ANY WARRANTY; without even the implied warranty of
34% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35% GNU General Public License for more details.
36%
37% You should have received a copy of the GNU General Public License
38% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
39% =========================================================================
40
41y = H(:,1);
42X = H(:,2:end);
43
44b=(X\y); %ols estimator
45if any(isnan(b)) || any(isinf(b))
46    b=0;
47end
48yhat =  X*b; %predicted values
49if rank(yhat)
50    co = abs(y'*yhat/sqrt((y'*y)*(yhat'*yhat)));
51else
52    co=0;
53end
54