Is the general consensus that using loops is “more Julian” than storing multiple time series in a matrix?
Context: If I want to generate and plot 3 time series, my Python-ian instinct is to store them in a matrix. But Quant Econ uses loops. Does this approach generalize? Is this because array dimensions are a bit unintuitive in Julia?
αs = [0.0, 0.8, 0.98]
n = 200
p = plot() # naming a plot to add to
for α in αs
x = zeros(n + 1)
x[1] = 0.0
for t in 1:n
x[t+1] = α * x[t] + randn()
end
plot!(p, x, label = “alpha = $α”) # add to plot p
end
p # display plot
My first thought was to instead do something like:
function plot_corr_ts(α,N)
numseries = length(α)
x = zeros(N+1,numseries)
for s in 1:numseries
for t in axes(x,1)
ε = randn()
println(α*x[t,s] + ε)
end
end
plot(x, seriestype = :line, title = "A Correlated Time Series")
end
plot_corr_ts([0,.8,.98],3)
The truly Julian approach is to define a custom type that has the desired behavior. Does TimeSeries.jl work for your application? Plotting · TimeSeries.jl
1 Like
Yes it looks like it does. Thanks!
Storing in a matrix is fine. Just create the matrix first and then fill it. Sure, you could do more sophisticated things and they may pay off in very large computations. For simple stuff, keep it simple. For instance, x[t+1,s]
is just fine.
1 Like