From f0addc9e3993eb666aba832d032074d90edeb30d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?= Date: Thu, 23 Jun 2011 21:46:41 +0200 Subject: [PATCH] Added routines from dynare-breaks branch. --- matlab/utilities/dataset/compute_acov.m | 74 ++++++++++ matlab/utilities/dataset/compute_corr.m | 58 ++++++++ matlab/utilities/dataset/compute_cova.m | 66 +++++++++ matlab/utilities/dataset/compute_stdv.m | 55 ++++++++ .../utilities/dataset/describe_missing_data.m | 128 ++++++++++++++++++ .../dataset/descriptive_statistics.m | 86 ++++++++++++ matlab/utilities/dataset/initialize_dataset.m | 85 ++++++++++++ matlab/utilities/general/demean.m | 54 ++++++++ matlab/utilities/general/nandemean.m | 54 ++++++++ matlab/utilities/general/ndim.m | 48 +++++++ matlab/utilities/tests/dyn_assert.m | 62 +++++++++ matlab/utilities/tests/test.m | 82 +++++++++++ 12 files changed, 852 insertions(+) create mode 100644 matlab/utilities/dataset/compute_acov.m create mode 100644 matlab/utilities/dataset/compute_corr.m create mode 100644 matlab/utilities/dataset/compute_cova.m create mode 100644 matlab/utilities/dataset/compute_stdv.m create mode 100644 matlab/utilities/dataset/describe_missing_data.m create mode 100644 matlab/utilities/dataset/descriptive_statistics.m create mode 100644 matlab/utilities/dataset/initialize_dataset.m create mode 100644 matlab/utilities/general/demean.m create mode 100644 matlab/utilities/general/nandemean.m create mode 100644 matlab/utilities/general/ndim.m create mode 100644 matlab/utilities/tests/dyn_assert.m create mode 100644 matlab/utilities/tests/test.m diff --git a/matlab/utilities/dataset/compute_acov.m b/matlab/utilities/dataset/compute_acov.m new file mode 100644 index 000000000..8e3bec348 --- /dev/null +++ b/matlab/utilities/dataset/compute_acov.m @@ -0,0 +1,74 @@ +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 . + +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 diff --git a/matlab/utilities/dataset/compute_corr.m b/matlab/utilities/dataset/compute_corr.m new file mode 100644 index 000000000..3d56adb3b --- /dev/null +++ b/matlab/utilities/dataset/compute_corr.m @@ -0,0 +1,58 @@ +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 . + +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 diff --git a/matlab/utilities/dataset/compute_cova.m b/matlab/utilities/dataset/compute_cova.m new file mode 100644 index 000000000..12e5cb18a --- /dev/null +++ b/matlab/utilities/dataset/compute_cova.m @@ -0,0 +1,66 @@ +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 . + +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 diff --git a/matlab/utilities/dataset/compute_stdv.m b/matlab/utilities/dataset/compute_stdv.m new file mode 100644 index 000000000..2111e0056 --- /dev/null +++ b/matlab/utilities/dataset/compute_stdv.m @@ -0,0 +1,55 @@ +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 . + +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 diff --git a/matlab/utilities/dataset/describe_missing_data.m b/matlab/utilities/dataset/describe_missing_data.m new file mode 100644 index 000000000..7b7fedf4a --- /dev/null +++ b/matlab/utilities/dataset/describe_missing_data.m @@ -0,0 +1,128 @@ +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 . + +[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 diff --git a/matlab/utilities/dataset/descriptive_statistics.m b/matlab/utilities/dataset/descriptive_statistics.m new file mode 100644 index 000000000..06afbdf40 --- /dev/null +++ b/matlab/utilities/dataset/descriptive_statistics.m @@ -0,0 +1,86 @@ +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 . + + +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 diff --git a/matlab/utilities/dataset/initialize_dataset.m b/matlab/utilities/dataset/initialize_dataset.m new file mode 100644 index 000000000..e83d94d97 --- /dev/null +++ b/matlab/utilities/dataset/initialize_dataset.m @@ -0,0 +1,85 @@ +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 . + +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 diff --git a/matlab/utilities/general/demean.m b/matlab/utilities/general/demean.m new file mode 100644 index 000000000..cfa386f5f --- /dev/null +++ b/matlab/utilities/general/demean.m @@ -0,0 +1,54 @@ +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} +%! @table @var +%! @item x +%! Matlab matrix (T-by-N). +%! @end table +%! +%! @strong{Outputs} +%! @table @var +%! @item c +%! Matlab matrix (T-by-N). The demeaned x matrix. +%! @end table +%! +%! @strong{This function is called by:} +%! @ref{compute_cova}, @ref{compute_acov}, @ref{compute_std}. +%! +%! @strong{This function calls:} +%! @ref{ndim}, +%! +%! @end deftypefn +%@eod: + +% Copyright (C) 2011 Dynare Team +% stephane DOT adjemian AT ens 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 . + +if ndim(x)==1 + c = x-mean(x); +elseif ndim(x)==2 + c = bsxfun(@minus,x,mean(x)); +else + error('descriptive_statistics::demean:: This function is not implemented for arrays with dimension greater than two!') +end \ No newline at end of file diff --git a/matlab/utilities/general/nandemean.m b/matlab/utilities/general/nandemean.m new file mode 100644 index 000000000..d7561516f --- /dev/null +++ b/matlab/utilities/general/nandemean.m @@ -0,0 +1,54 @@ +function c = nandemean(x) +% Removes the mean of each column of a matrix with some NaNs. + +%@info: +%! @deftypefn {Function File} {@var{c} =} nandemean (@var{x}) +%! @anchor{nandemean} +%! This function removes the mean of each column of a matrix with some NaNs. +%! +%! @strong{Inputs} +%! @table @var +%! @item x +%! Matlab matrix (T-by-N). +%! @end table +%! +%! @strong{Outputs} +%! @table @var +%! @item c +%! Matlab matrix (T-by-N). The demeaned x matrix. +%! @end table +%! +%! @strong{This function is called by:} +%! @ref{compute_cova}, @ref{compute_acov}, @ref{compute_std}. +%! +%! @strong{This function calls:} +%! @ref{ndim}, +%! +%! @end deftypefn +%@eod: + +% Copyright (C) 2011 Dynare Team +% stephane DOT adjemian AT ens 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 . + +if ndim(x)==1 + c = x-nanmean(x); +elseif ndim(x)==2 + c = bsxfun(@minus,x,nanmean(x)); +else + error('descriptive_statistics::nandemean:: This function is not implemented for arrays with dimension greater than two!') +end \ No newline at end of file diff --git a/matlab/utilities/general/ndim.m b/matlab/utilities/general/ndim.m new file mode 100644 index 000000000..836d217bf --- /dev/null +++ b/matlab/utilities/general/ndim.m @@ -0,0 +1,48 @@ +function n = ndim(x) +% Report the number of non singleton dimensions of a matlab array. + +%@info: +%! @deftypefn {Function File} {@var{n} =} ndim (@var{x}) +%! @anchor{ndim} +%! This function reports the number of non singleton dimensions of a matlab array. +%! +%! @strong{Inputs} +%! @table @var +%! @item x +%! Matlab array. +%! @end table +%! +%! @strong{Outputs} +%! @table @var +%! @item n +%! Integer scalar. The number of non singleton dimensions of a matlab array. +%! @end table +%! +%! @strong{This function is called by:} +%! @ref{demean}, @ref{nandemean}. +%! +%! @strong{This function calls:} +%! none. +%! +%! @end deftypefn +%@eod: + +% Copyright (C) 2011 Dynare Team +% stephane DOT adjemian AT ens 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 . + +n = sum(size(x)>1); \ No newline at end of file diff --git a/matlab/utilities/tests/dyn_assert.m b/matlab/utilities/tests/dyn_assert.m new file mode 100644 index 000000000..a1c26f648 --- /dev/null +++ b/matlab/utilities/tests/dyn_assert.m @@ -0,0 +1,62 @@ +function t = dyn_assert(A,B,tol) +% This function tests the equality of two objects. + +% 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 . + +if ( (nargin<3) || isempty(tol) ) + use_isequal_matlab_builtin = 1; +else + use_isequal_matlab_builtin = 0; +end + +[nA,mA] = size(A); +[nB,mB] = size(B); + +if nA-nB + error('assert:: Dimensions of compared objects A and B don''t match!') +end + +if mA-mB + error('assert:: Dimensions of compared objects A and B don''t match!') +end + +if isstruct(B) && ~isstruct(A) + error('assert:: Compared objects are not of the same type!') +end + +if iscell(B) && ~iscell(A) + error('assert:: Compared objects are not of the same type!') +end + +if use_isequal_matlab_builtin + t = isequal(A,B); + if ~t + t = isequalwithequalnans(A,B); + end +else + t = 1; + if ~(isstruct(B) || iscell(B)) + if abs(A(:)-B(:))>tol + t = 0; + end + else + % not yet implemented + t = NaN; + end +end \ No newline at end of file diff --git a/matlab/utilities/tests/test.m b/matlab/utilities/tests/test.m new file mode 100644 index 000000000..6b502bc98 --- /dev/null +++ b/matlab/utilities/tests/test.m @@ -0,0 +1,82 @@ +function check = test(fname,fpath) +% Extract test sections from matlab's routine executes the test and report errors. + +% 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 . + +% Default answer (no problem). +check = 1; + +% Open the matlab file. +fid = fopen([fpath '/' fname '.m'],'r'); + +% Read the matlab file. +file = textscan(fid,'%s','delimiter','\n'); +file = file{1}; + +% Close the matlab file. +fclose(fid); + +% Locate the test blocks. +b1 = find(strncmp(file,'%@test:',7))+1; +b2 = find(strncmp(file,'%@eof:',6))-1; +nn = length(b2); + +if length(b1)-length(b2) + error('test:: There is a problem with the test blocks definition!') +end + +% Perform the tests. +for i=1:nn + % Write the temporary test routine. + tid = fopen([fname '_test_' int2str(i) '.m'],'w'); + fprintf(tid,['function [T,t,log] = ' fname '_test_' int2str(i) '()\n']); + fprintf(tid,['try\n']); + for j=b1(i):b2(i) + str = file{j}; + fprintf(tid,[str(4:end) '\n']); + end + fprintf(tid,['log = NaN;\n']); + fprintf(tid,['catch exception\n']); + fprintf(tid,['log = getReport(exception,''extended'');\n']); + fprintf(tid,['T = NaN;\n']); + fprintf(tid,['t = NaN;\n']); + fprintf(tid,['end\n']); + fclose(tid); + % Call the temporary test routine. + [TestFlag,TestDetails,log] = feval([fname '_test_' int2str(i)]); + if isnan(TestFlag) + fprintf(['\n']) + fprintf(['Call to ' fname ' test routine n°' int2str(i) ' failed (' datestr(now) ')!\n']) + fprintf(['\n']) + disp(log) + check = 0; + continue + end + if ~TestFlag + fprintf(['Test n°' int2str(i) ' for routine ' fname ' failed (' datestr(now) ')!\n']); + for j=1:length(TestDetails) + if ~TestDetails(j) + fprintf(['Output argument n°' int2str(j) ' didn''t give the expected result.\n']); + end + end + check = 0; + else + delete([fname '_test_' int2str(i) '.m']) + end +end \ No newline at end of file