Home > atmlab > covmat > covmat_partstat_corr.m

covmat_partstat_corr

PURPOSE ^

COVMAT_PARTSTAT_CORR Covariance matrix for multidimensional data

SYNOPSIS ^

function S = covmat_partstat_corr(si,varargin)

DESCRIPTION ^

 COVMAT_PARTSTAT_CORR   Covariance matrix for multidimensional data

    Sets up a covariance matrix for data spanning several dimensions and with
    partial stationary correlation. That is, it is assumed that there is no
    dependency between the correlation in one dimension with the other data
    dimensions. The correlation is allowed to vary along each dimension, but
    this independently between the data dimensions. A higher degree of
    freedom for the correlation is accepted by *covmat3d_from_cfun*.

    Note that the standard deviation can vary freely.

    The data are assumed to be stored with dimension 1 as innermost "loop",
    dimension 2 next etc. For 3D case, the data order is as follows:
 
       [(x1,y1,z1) (x2,y1,z2) ...(xn,y1,z1) (x1,y2,z) ... (x1,y1,z2) ...]'

    Correlation coefficient matrix 1 (C1) describes then the correlation
    for x=[x1,x2,...xn]', and C2 the same thing for y, etc. The correlation
    along dimensions 1 depends thus only on x, no dependency on y, z ...

 FORMAT   S = covmat_partstat_corr( si, C1 [, C2, C3 ...] )
        
 OUT   S    Covariance matrix.
 IN    si   Standard deviations: given as 
              1. A vector with a value for each data point. Length must
                 then be the product of dimension sizes.
              2. A scalar. This value is then applied for all data points.
              3. Empty. A constant standard deviation of 1 is then applied, 
                 and the overall correlation matrix is obtained.
       C1   Correlation coefficients along dimension 1.
 OPT   C2   Correlation coefficients along dimension 2.
       C3   Correlation coefficients along dimension 3.
       ...

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

covmat_partstat_corr.m

SOURCE CODE ^

0001 % COVMAT_PARTSTAT_CORR   Covariance matrix for multidimensional data
0002 %
0003 %    Sets up a covariance matrix for data spanning several dimensions and with
0004 %    partial stationary correlation. That is, it is assumed that there is no
0005 %    dependency between the correlation in one dimension with the other data
0006 %    dimensions. The correlation is allowed to vary along each dimension, but
0007 %    this independently between the data dimensions. A higher degree of
0008 %    freedom for the correlation is accepted by *covmat3d_from_cfun*.
0009 %
0010 %    Note that the standard deviation can vary freely.
0011 %
0012 %    The data are assumed to be stored with dimension 1 as innermost "loop",
0013 %    dimension 2 next etc. For 3D case, the data order is as follows:
0014 %
0015 %       [(x1,y1,z1) (x2,y1,z2) ...(xn,y1,z1) (x1,y2,z) ... (x1,y1,z2) ...]'
0016 %
0017 %    Correlation coefficient matrix 1 (C1) describes then the correlation
0018 %    for x=[x1,x2,...xn]', and C2 the same thing for y, etc. The correlation
0019 %    along dimensions 1 depends thus only on x, no dependency on y, z ...
0020 %
0021 % FORMAT   S = covmat_partstat_corr( si, C1 [, C2, C3 ...] )
0022 %
0023 % OUT   S    Covariance matrix.
0024 % IN    si   Standard deviations: given as
0025 %              1. A vector with a value for each data point. Length must
0026 %                 then be the product of dimension sizes.
0027 %              2. A scalar. This value is then applied for all data points.
0028 %              3. Empty. A constant standard deviation of 1 is then applied,
0029 %                 and the overall correlation matrix is obtained.
0030 %       C1   Correlation coefficients along dimension 1.
0031 % OPT   C2   Correlation coefficients along dimension 2.
0032 %       C3   Correlation coefficients along dimension 3.
0033 %       ...
0034 
0035 % 2007-05-25   Created by Patrick Eriksson.
0036 
0037 
0038 function S = covmat_partstat_corr(si,varargin)
0039 
0040 
0041 nc = length(varargin);
0042 %
0043 if nc < 1
0044   error( 'At least one correlation matrix must be given.' )
0045 end
0046 %
0047 n = 1;
0048 for i = 1 : nc
0049   if ~( dimens(varargin{i})~=2 | size(varargin{i},1)==size(varargin{i},2) )
0050     error( sprintf( 'Correlation matrix %d is not a square matrix', i ) );
0051   end
0052   n = n * size(varargin{i},1);
0053 end
0054 %
0055 if ~( isempty(si)  |  isscalar(si)  |  n == length(si) )
0056   error( ...
0057    'Inconsistency between length of *si* and sizes of correlation matrices.' );
0058 end
0059 %
0060 clear n
0061 
0062 
0063 S = varargin{1};
0064 
0065 
0066 for i = 2 : nc
0067 
0068   nold = size( S, 1 );
0069   nnew = size( varargin{i}, 1 );
0070   
0071   [irow0,icol0,s0] = find( S );
0072   %
0073   ns   = length( s0 );
0074   irow = repmat( irow0, nnew^2, 1 );
0075   icol = repmat( icol0, nnew^2, 1 );
0076   s    = repmat( s0, nnew^2, 1 );  
0077 
0078   for ir = 1:nnew
0079     for ic = 1:nnew
0080       %
0081       ind_s   = (1:ns) + ((ic-1)*nnew+ir-1)*ns;
0082       %
0083       irow(ind_s) = irow0 + (ir-1)*nold;
0084       icol(ind_s) = icol0 + (ic-1)*nold;
0085       s(ind_s)    = varargin{i}(ir,ic) * s0;
0086     end
0087   end
0088   
0089   ind = find(s);
0090 
0091   if i < nc  |  isempty(si)
0092     S = sparse( irow(ind), icol(ind), s(ind), nold*nnew, nold*nnew );  
0093   else
0094     % If last dimension, include si
0095     if isscalar(si)
0096       S = sparse( irow(ind), icol(ind), (si*si)*s(ind), nold*nnew, nold*nnew );
0097     else
0098       si = vec2col(si);
0099       S = sparse( irow(ind), icol(ind), ...
0100                   si(irow(ind)).*si(icol(ind)).*s(ind), nold*nnew, nold*nnew );
0101     end
0102   end
0103 end
0104 
0105 % *si* not included if nc == 1
0106 %
0107 if nc == 1  &  ~isempty(si)
0108   if isscalar(si)
0109     S = (si*si) * S;
0110   else
0111     si = vec2col(si);
0112     S  = (si*si') .* S;
0113   end
0114 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005