Iteration with companion matrix to make future estimates

Hello everyone, I have just started to learn Julia, and Julia is the only language with which I’ve worked for more than 3 months, so I am new to programming in general.

I need to make 46 future quarterly estimates of interest rate data. After having estimated the coefficients of an autoregressive AR(5), I found its companion matrix to get

Xt+1 = Companion(Φ(5)) * Xt

in order to compute the future 46 Xt+1.

I tried to do some research on how to come up with a loop that saves my next 40 estimates in an array, but I’m not sure how to do this. Other topics do not seem related to the collection of iterations, hence, I hope you can help me and that this is not too silly, thank you for your attention.

What I have tried to do up to now is:

maxiter = 46
oldΔp = pastΔp[:, 1]       #past interest rates, column 1 for country 1
iter = 1
while iter <= maxiter
    futΔp = βcomp_US * oldΔp           #Companion(Φ(5)) * Xt
    #replace and continue
    oldΔp = futΔp
    iter = iter + 1
end     
#=if I do return(futΔp) I only get a 5x1 matrix with only one 
new observation at the top
I don't know how to index each result, or just save the 
new observations for each iteration in a new array =#

Alternatively, following Sargent QuantEcon example, to create a function for fixed points

iv = pastΔp[:, 1]    #past interest rates, column 1 for country 1
maxiter = 46
function futurevalues(f::typeof(f); iv::Vector{Float64}, maxiter::Int64)
    #setup the algorithm, literally identical to Sargent's one
    x_old = iv
    iter = 1
    while iter <= maxiter
        x_new = f(x_old)
        x_old = x_new
        iter = iter + 1
    end
    return (x_old, iter) 
end

f(v) = βcomp_US * v   #Companion(Φ(5)) * Xt
v_initial = pastΔp[:, 1]   #past interest rates, column 1 for country 1
maxiter = 46
println(futurevalues(f, v_initial, maxiter))   #probably nonsense

To make it easier for people to help, you could try to use the same words in your description of what you are trying to do as in the code. Also make sure that the code that you post can run, so if it needs any data, then either provide it, or include code that generates random data. That way the people who want to help don’t have to guess.

For example, here’s how to generate some data:

X1 = rand(5) # A random vector
Companion = rand(5,5) # A random matrix

Now, what I think you want to do is add new vectors to the end of a list. This is done with the push! function in Julia.

maxiter = 46;
Xt = [X1] # A list of vectors, containing just one vector to start with. 
for i = 1:maxiter
    push!(Xt, Companion*last(Xt))
end

After running this code, the list Xt will contain 47 vectors.

Another way of doing it would be to store the vectors side-by-side in a matrix. (Let’s call it M, to make it different from Xt.)

M = zeros(length(X1), maxiter+1) # Create the matrix filled with zeros to start with
M[:,1] = X1 # Set the first column of the matrix
for i=1:maxiter
    M[:,i+1] = Companion*M[:,i]
end
1 Like