function y = Con_Var(X, win)

% calculate conditional variance-covariance of variables in X
% apply an VAR(1) to X using win samples,then use the variance-covariance 
% matrix of residuals as the conditional var-cov for X
%
% Input:    X, TxN  variables
%           win, rolling window
%
% Output:   y, conditioanl var-cov of X

[T,N] = size(X);
var_cov = nan(T-win,(N*N+N)/2);
mdl = varm(N,1);
for i = 1:T-win
    xinput = X(i:i+win-1,:);
    EstMdl = estimate(mdl,xinput);
    var_cov(i,:) = [diag(EstMdl.Covariance); EstMdl.Covariance(1,2)];
end

y = var_cov;



end
