Commit f0addc9e authored by Stéphane Adjemian's avatar Stéphane Adjemian
Browse files

Added routines from dynare-breaks branch.

parent 4444a850
function dataset_ = compute_acov(dataset_)
% Computes the (multivariate) auto-covariance function of the sample (possibly with missing observations).
%@info:
%! @deftypefn {Function File} {@var{dataset_} =} compute_corr(@var{dataset_},@var{nlag})
%! @anchor{compute_acov}
%! This function computes the (multivariate) auto-covariance function of the sample (possibly with missing observations).
%!
%! @strong{Inputs}
%! @table @var
%! @item dataset_
%! Dynare structure describing the dataset, built by @ref{initialize_dataset}
%! @item nlag
%! Integer scalar. The maximum number of lags of the autocovariance function.
%! @end table
%!
%! @strong{Outputs}
%! @table @var
%! @item dataset_
%! Dynare structure describing the dataset, built by @ref{initialize_dataset}
%! @end table
%!
%! @strong{This function is called by:}
%! @ref{descriptive_statistics}.
%!
%! @strong{This function calls:}
%! @ref{ndim}, @ref{compute_cova}, @ref{demean}, @ref{nandemean}.
%!
%! @strong{Remark 1.} On exit, a new field is appended to the structure: @code{dataset_.descriptive.acov} is a
%! @tex{n\times n\times p} array (where @tex{n} is the number of observed variables as defined by @code{dataset_.info.nvobs},
%! and @tex{n} is the maximum number of lags given by the second input @code{nlag}).
%!
%! @strong{Remark 2.} If @code{dataset_.descriptive.cova} does not exist, the covariance matrix is computed prior to the
%! computation of the auto-covariance function.
%!
%! @end deftypefn
%@eod:
% Copyright (C) 2011 Dynare Team
% stephane DOT adjemian AT univ DASH lemans DOT fr
%
% 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/>.
if ~isfield(dataset_.descriptive,'cova')
dataset_ = compute_cova(dataset_);
end
dataset_.descriptive.acov = zeros(dataset_.nvobs,dataset_.nvobs,nlag);
data = transpose(dataset_.data);
for lag=1:nlag
for i=1:dataset_.info.nvobs
for j=1:dataset_.info.nvobs
if dataset_.missing.state
dataset_.descriptive.acov(i,j,lag) = nanmean(nandemean(data(lag+1:end,i)).*nandemean(data(1:end-lag,j)));
else
dataset_.descriptive.acov(i,j,lag) = mean(demean(data(lag+1:end,i)).*demean(data(1:end-lag,j)));
end
end
end
end
\ No newline at end of file
function dataset_ = compute_corr(dataset_)
% Computes the correlation matrix of the sample (possibly with missing observations).
%@info:
%! @deftypefn {Function File} {@var{dataset_} =} compute_corr(@var{dataset_})
%! @anchor{compute_corr}
%! This function computes covariance matrix of the sample (possibly with missing observations).
%!
%! @strong{Inputs}
%! @table @var
%! @item dataset_
%! Dynare structure describing the dataset, built by @ref{initialize_dataset}
%! @end table
%!
%! @strong{Outputs}
%! @table @var
%! @item dataset_
%! Dynare structure describing the dataset, built by @ref{initialize_dataset}
%! @end table
%!
%! @strong{This function is called by:}
%! @ref{descriptive_statistics}.
%!
%! @strong{This function calls:}
%! @ref{ndim}, @ref{compute_cova}.
%!
%! @strong{Remark 1.} On exit, a new field is appended to the structure: @code{dataset_.descriptive.corr} is a
%! @tex{n\times n} vector (where @tex{n} is the number of observed variables as defined by @code{dataset_.info.nvobs}).
%!
%! @strong{Remark 2.} If @code{dataset_.descriptive.cova} does not exist, the covariance matrix is computed prior to the
%! computation of the correlation matrix.
%!
%! @end deftypefn
%@eod:
% Copyright (C) 2011 Dynare Team
% stephane DOT adjemian AT univ DASH lemans DOT fr
%
% 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/>.
if ~isfield(dataset_.descriptive,'cova')
dataset_ = compute_cova(dataset_);
end
normalization_matrix = diag(1./sqrt(diag(dataset_.descriptive.cova)));
dataset_.descriptive.corr = normalization_matrix*dataset_.descriptive.cova*normalization_matrix;
\ No newline at end of file
function dataset_ = compute_cova(dataset_)
% Computes the covariance matrix of the sample (possibly with missing observations).
%@info:
%! @deftypefn {Function File} {@var{dataset_} =} compute_corr(@var{dataset_})
%! @anchor{compute_corr}
%! This function computes covariance matrix of the sample (possibly with missing observations).
%!
%! @strong{Inputs}
%! @table @var
%! @item dataset_
%! Dynare structure describing the dataset, built by @ref{initialize_dataset}
%! @end table
%!
%! @strong{Outputs}
%! @table @var
%! @item dataset_
%! Dynare structure describing the dataset, built by @ref{initialize_dataset}
%! @end table
%!
%! @strong{This function is called by:}
%! @ref{descriptive_statistics}.
%!
%! @strong{This function calls:}
%! @ref{ndim}, @ref{demean}, @ref{nandemean}.
%!
%! @strong{Remark 1.} On exit, a new field is appended to the structure: @code{dataset_.descriptive.cova} is a
%! @tex{n\times n} vector (where @tex{n} is the number of observed variables as defined by @code{dataset_.info.nvobs}).
%!
%! @end deftypefn
%@eod:
% Copyright (C) 2011 Dynare Team
% stephane DOT adjemian AT univ DASH lemans DOT fr
%
% 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/>.
dataset_.descriptive.cova = zeros(dataset_.nvobs);
data = transpose(dataset_.data);
for i=1:dataset_.info.nvobs
for j=i:dataset_.info.nvobs
if dataset_.missing.state
dataset_.descriptive.cova(i,j) = nanmean(nandemean(data(:,i)).*nandemean(data(:,j)));
else
dataset_.descriptive.cova(i,j) = mean(demean(data(:,i)).*demean(data(:,j)));
end
if j>i
dataset_.descriptive.cova(j,i) = dataset_.descriptive.cova(i,j);
end
end
end
\ No newline at end of file
function dataset_ = compute_stdv(dataset_)
% Compute the standard deviation for each observed variable (possibly with missing observations).
%@info:
%! @deftypefn {Function File} {@var{dataset_} =} compute_stdv(@var{dataset_})
%! @anchor{compute_stdv}
%! This function computes the standard deviation of the observed variables (possibly with missing observations).
%!
%! @strong{Inputs}
%! @table @var
%! @item dataset_
%! Dynare structure describing the dataset, built by @ref{initialize_dataset}
%! @end table
%!
%! @strong{Outputs}
%! @table @var
%! @item dataset_
%! Dynare structure describing the dataset, built by @ref{initialize_dataset}
%! @end table
%!
%! @strong{This function is called by:}
%! @ref{descriptive_statistics}.
%!
%! @strong{This function calls:}
%! @ref{ndim}, @ref{demean}, @ref{nandemean}.
%!
%! @strong{Remark 1.} On exit, a new field is appended to the structure: @code{dataset_.descriptive.stdv} is a
%! @tex{n\times 1} vector (where @tex{n} is the number of observed variables as defined by @code{dataset_.info.nvobs}).
%!
%! @end deftypefn
%@eod:
% Copyright (C) 2011 Dynare Team
% stephane DOT adjemian AT univ DASH lemans DOT fr
%
% 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/>.
if dataset_.missing.state
dataset_.descriptive.stdv = sqrt(nanmean(bsxfun(@power,nandemean(transpose(dataset_.data)),2)));
else
dataset_.descriptive.stdv = sqrt(mean(bsxfun(@power,demean(transpose(dataset_.data)),2)));
end
\ No newline at end of file
function [i,n,s,j] = describe_missing_data(data)
% This function reads the dataset and determines the location of the missing observations (defined by NaNs)
%@info:
%! @deftypefn {Function File} {[@var{i}, @var{n}, @var{s}, @var{j} ] =} describe_missing_data (@var{data}, @var{gend}, @var{nvarobs})
%! This function reads the dataset and determines where are the missing observations.
%!
%! @strong{Inputs}
%! @table @var
%! @item data
%! Real matrix (T-by-N) for the dataset.
%! @end table
%!
%! @strong{Outputs}
%! @table @var
%! @item i
%! cell array (1-by-T). Each element is a @math{p_t\times 1} column vector of indices targeting the non-NaN variables at time t.
%! @item n
%! Integer scalar. The effective number of observations:
%! @math(n=\sum_{t=1}^T p_t)
%! @item s
%! Integer scalar. The value of the time index such that @math(p_t=p_s) for all @math(t\geq s).
%! @item j
%! cell array (1-by-N). Each element is a column vector targeting to the non-NaN observations of a variable.
%! @end table
%!
%! @end deftypefn
%@eod:
% Copyright (C) 2008-2011 Dynare Team
% stephane DOT adjemian AT univ DASH lemans DOT fr
%
% 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/>.
[variable_index,observation_index] = find(~isnan(data));
[N,T] = size(data);
i = cell(1,T);
j = cell(1,N);
missing_observations_counter = NaN(T,1);
for obs=1:T
idx = find(observation_index==obs);
tmp = variable_index(idx);
missing_observations_counter(obs,1) = N-length(tmp);
if rows(tmp(:))
i(obs) = { tmp(:) };
else
i(obs) = { [] };
end
end
missing_observations_counter = cumsum(missing_observations_counter);
n = length(variable_index);
if ~missing_observations_counter
s = 1;
else
tmp = find(missing_observations_counter>=(T*N-n));
s = tmp(1)+1;
end
if nargout>3
for var=1:N
idx = find(variable_index==var);
tmp = observation_index(idx);
j(var) = { tmp(:) };
end
end
%@test:1
%$ % Define a data set.
%$ A = [ 1 1 ; ...
%$ 1 NaN ; ...
%$ NaN 1 ; ...
%$ 1 1 ; ...
%$ NaN NaN ; ...
%$ 1 NaN ; ...
%$ 1 NaN ; ...
%$ 1 1 ; ...
%$ 1 1 ; ...
%$ 1 1 ; ...
%$ 1 1 ];
%$
%$ % Define expected results.
%$ eB = cell(1,11);
%$ eB(1) = { transpose(1:2) };
%$ eB(2) = { 1 };
%$ eB(3) = { 2 };
%$ eB(4) = { transpose(1:2)};
%$ eB(5) = { [] };
%$ eB(6) = { 1 };
%$ eB(7) = { 1 };
%$ eB(8) = { transpose(1:2) };
%$ eB(9) = { transpose(1:2) };
%$ eB(10) = { transpose(1:2) };
%$ eB(11) = { transpose(1:2) };
%$ eC = 16;
%$ eD = 8;
%$ eE = cell(1,2);
%$ eE(1) = { [1; 2; 4; transpose(6:11)] };
%$ eE(2) = { [1; 3; 4; transpose(8:11)] };
%$
%$ % Call the tested routine.
%$ [B,C,D,E] = describe_missing_data(transpose(A));
%$
%$ % Check the results.
%$ t(1) = dyn_assert(B,eB);
%$ t(2) = dyn_assert(C,eC);
%$ t(3) = dyn_assert(D,eD);
%$ t(4) = dyn_assert(E,eE);
%$ T = all(t);
%@eof:1
\ No newline at end of file
function dataset_ = descriptive_statistics(dataset_,statistic,varagin)
% Computes various descriptive statistics for the sample and stores them in the structure dataset_.
%@info:
%! @deftypefn {Function File} {@var{dataset_} =} descriptive_statistics(@var{dataset_},@var{statistic})
%! @deftypefn {Function File} {@var{dataset_} =} descriptive_statistics(@var{dataset_},@var{statistic},nlags)
%! @anchor{compute_corr}
%! This function computes various descriptive statistics on the sample (possibly with missing observations).
%!
%! @strong{Inputs}
%! @table @var
%! @item dataset_
%! Dynare structure describing the dataset, built by @ref{initialize_dataset}
%! @item statistic
%! String. The name of the statistic to be computed. Admissible values are:
%! @table @var
%! @item 'stdv'
%! Computes the standard deviation of each observed variable.
%! @item 'cova'
%! Computes the covariance matrix of the sample.
%! @item 'corr'
%! Computes the correlation matrix of the sample.
%! @item 'acov'
%! Computes the (multivariate) auto-covariance function of the sample. In this case a third argument (@code{nlags}) defining the
%! maximum number of lags is mandatory.
%! @end table
%! @item nlags
%! Integer scalar. The maximum number of lags when computing the autocovariance function.
%! @end table
%!
%! @strong{Outputs}
%! @table @var
%! @item dataset_
%! Dynare structure describing the dataset, built by @ref{initialize_dataset}
%! @end table
%!
%! @strong{This function is called by:}
%! none.
%!
%! @strong{This function calls:}
%! @ref{compute_stdv}, @ref{compute_cova}, @ref{compute_corr}, @ref{compute_acov}.
%!
%! @strong{Remark 1.} On exit, a new field containing the computed statistics is appended to the structure.
%!
%! @end deftypefn
%@eod:
% Copyright (C) 2011 Dynare Team
% stephane DOT adjemian AT univ DASH lemans DOT fr
%
% 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/>.
if strcmpi(statistic,'stdv')
dataset_ = compute_std(dataset_)
end
if strcmpi(statistic,'cova')
dataset_ = compute_cova(dataset_)
end
if strcmpi(statistic,'corr')
dataset_ = compute_cova(dataset_)
end
if strcmpi(statistic,'acov')
if nargin==2
nlag = 10;
else
nlag = varargin{1};
end
dataset_ = compute_acov(dataset_,nlag);
end
\ No newline at end of file
function dataset_ = initialize_dataset(datafile,varobs,first,nobs,transformation,prefilter,xls)
% Initializes a structure describing the dataset.
% Copyright (C) 2011 Dynare Team
% stephane DOT adjemian AT univ DASH lemans DOT fr
%
% 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/>.
if isempty(datafile)
error('Estimation:: You have to declare a dataset file!')
end
if isempty(varobs)
error('Estimation:: You have to declare a set of observed variables')
end
% Get raw data.
rawdata = read_variables(datafile,varobs,[],xls.sheet,xls.range);
% Get the (default) number of observations.
if isempty(nobs)
nobs = rows(rawdata)-first+1;
end
% Get the (default) prefilter option.
if isempty(prefilter)
prefilter = 0;
end
% Fill the dataset structure
dataset_.info.ntobs = nobs;
dataset_.info.nvobs = rows(varobs);
dataset_.info.varobs = varobs;
rawdata = rawdata(first:(first+dataset_.info.ntobs-1),:);
% Take the log (or anything else) of the variables if needed
if isempty(transformation)
dataset_.rawdata = rawdata;
else
dataset_.rawdata = arrayfun(transformation,rawdata);
end
% Test if the observations are real numbers.
if ~isreal(dataset_.rawdata)
error('Estimation:: There are complex values in the data! Probably a wrong (log) transformation...')
end
% Test for missing observations.
dataset_.missing.state = any(any(isnan(dataset_.rawdata)));
if dataset_.missing.state
[i,n,s,j] = describe_missing_data(dataset_.rawdata);
dataset_.missing.aindex = i;
dataset_.missing.vindex = j;
dataset_.missing.number_of_observations = n;
dataset_.missing.no_more_missing_observations = s;
else
dataset_.missing.aindex = [];
dataset_.missing.vindex = [];
dataset_.missing.number_of_observations = [];
dataset_.missing.no_more_missing_observations = [];
end
% Compute the empirical mean of the observed variables..
dataset_.descriptive.mean = nanmean(dataset_.rawdata);
% Prefilter the data if needed.
if prefilter == 1
dataset_.data = transpose(bsxfun(@minus,dataset_.rawdata,dataset_.descriptive.mean));
else
dataset_.data = transpose(dataset_.rawdata);
end
\ No newline at end of file
function c = demean(x)
% Removes the mean of each column of a matrix.
%@info:
%! @deftypefn {Function File} {@var{c} =} demean (@var{x})
%! @anchor{demean}
%! This function removes the mean of each column of a matrix.
%!
%! @strong{Inputs}