Home > matlab > sample_autocovariance.m

sample_autocovariance

PURPOSE ^

Computes the autocovariance function associated to a time series.

SYNOPSIS ^

function [autocov,autocor] = sample_autocovariance(data,q)

DESCRIPTION ^

 Computes the autocovariance function associated to a time series.
 

 INPUTS 

   data            [double]       T*1 vector of data.
   q               [integer]      Order of the autocovariance function. 
    
 OUTPUTS 
   autocov         [double]       (q+1)*1 vector, autocovariance function (first scalar is the variance).  
   autocor         [double]       (q+1)*1 vector, autocorrelation function (first scalar is equal to one).

 SPECIAL REQUIREMENTS

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [autocov,autocor] = sample_autocovariance(data,q)
0002 % Computes the autocovariance function associated to a time series.
0003 %
0004 %
0005 % INPUTS
0006 %
0007 %   data            [double]       T*1 vector of data.
0008 %   q               [integer]      Order of the autocovariance function.
0009 %
0010 % OUTPUTS
0011 %   autocov         [double]       (q+1)*1 vector, autocovariance function (first scalar is the variance).
0012 %   autocor         [double]       (q+1)*1 vector, autocorrelation function (first scalar is equal to one).
0013 %
0014 % SPECIAL REQUIREMENTS
0015 
0016 % Copyright (C) 2003-2008 Dynare Team
0017 %
0018 % This file is part of Dynare.
0019 %
0020 % Dynare is free software: you can redistribute it and/or modify
0021 % it under the terms of the GNU General Public License as published by
0022 % the Free Software Foundation, either version 3 of the License, or
0023 % (at your option) any later version.
0024 %
0025 % Dynare is distributed in the hope that it will be useful,
0026 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0027 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0028 % GNU General Public License for more details.
0029 %
0030 % You should have received a copy of the GNU General Public License
0031 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0032 
0033 autocov = zeros(q+1,1);
0034 
0035 demeaned_data = data(:) - mean(data(:));
0036 sample_size = length( data(q+1:end) );
0037 lagged_indices = transpose(0:-1:-q);
0038 
0039 for t = 1:sample_size
0040     tt = t+q;
0041     autocov = autocov + demeaned_data(tt)*demeaned_data(tt+lagged_indices);
0042 end
0043 
0044 autocov = autocov/sample_size ;
0045 
0046 if nargout>1
0047     autocor = autocov / autocov(1);
0048 end

Generated on Tue 22-May-2012 02:40:23 by m2html © 2005