function y = makestationary(Phi)
% make VAR mean-reversion matrix stationary -- ensure largest eigenvalue does not exceed 0.99
% like Kilian (1998, REStat)
step = 0.001;
maxeigen = 0.99;
delta = 1;
Phinew = Phi;
while max(abs(eig(Phinew))) > maxeigen
    delta = delta - step;
    Phinew = delta*Phi;
end
y = Phinew;

