-term mean, no constant vector
end

Y = Y(lag+1:end,:); %matrix of combined time series (first p terms removed, with constant vector) 
Y_= Y-LTM(ones(N-lag,1),:); %matrix in terms of difference from long-term mean, no constant vector

s.B_=inv(X_'*X_)*X_'*Y_;    %compute VAR coefficients 


%% 3. Adjust VAR(p) coefficients for exogenous variables, if applicable
for idx = exo
  s.B_(:,idx)=0;    %other lagged variables have no effect on exogenous variable
  s.B_(idx,idx)=inv(X_(:,idx)'*X_(:,idx))*X_(:,idx)'*