I’m retaking a program I did some time ago, trying to speed it up. I am using ComponentArrays
because it is very easy to organize data and parameters with it. Consider this:
using ComponentArrays
using Parameters: @unpack
const homecode = 6
function agecat(a)
(19 <= a <= 24) && return 1
(25 <= a <= 29) && return 2
(30 <= a <= 34) && return 3
(35 <= a <= 39) && return 4
(40 <= a <= 44) && return 5
(45 <= a <= 49) && return 6
(50 <= a <= 54) && return 7
(55 <= a <= 59) && return 8
(60 <= a <= 64) && return 9
end
const data = ComponentArray(
wẘ = rand(2, 9, 2, 2, 5, 44),
P_1 = rand(5, 44),
ē = rand(5, 44)
)
const θ = ComponentArray(
α0 = rand(2, 2, 5),
α = rand(3),
α̃h = rand(2, 2),
α̃ga = rand(2, 9)
)
function Uk(g, a, e, k, t; data=data, θ=θ)
@unpack α0, α = θ
@unpack wẘ, P_1, ē = data
w = wẘ[:, :, :, 1, :, :]
α0[g,e,k] + α[1] * w[g, agecat(a), e, k, t] + α[2] * (e - ē[k, t])^2 + α[3] * P_1[k, t]
end
function α̃0(g, a, e, t; data=data, θ=θ)
@unpack α̃h, α̃ga = θ
α̃h[1,g] + α̃h[2,g] * t + α̃ga[g, agecat(a)]
end
function U0(g, a, e, t; data=data, θ=θ)
α̃0(g, a, e, t; data=data, θ=θ)
end
function Probk(g,a,e,k,t; data=data, θ=θ)
uk = ( k == homecode ? U0(g, a, e, t; data=data, θ=θ) : Uk(g, a, e, k, t; data=data, θ=θ) )
1/(sum(exp(Uk(g, a, e, kk, t; data=data, θ=θ)-uk) for kk=1:5) + exp(U0(g, a, e, t; data=data, θ=θ)-uk))
end
Notice how I pass data
and θ
from Probk
to U0
and Uk
. Is that good practice? I’m worried about allocations.
Ultimately, I will have to optimize a function over θ
and I am trying squeeze as much speed as possible from the program.