Hello guys,
I am a very recent Julia programmer coming from matlab in search of performance. Right now, I am building an economics model which involves solving different problems for different agents in different circumstances. For that reason, and in order to contain the size of the code and improve readability, my strategy is to use a single function which takes the characteristics of the agent in question (his income, savings, etc), sets up his problem conditional on its characteristics, and solves it. In particular, I built a function which calculates consumption, which is used in all problems:
function budget(p,b::Float64,bp::Float64,Ļµ::Float64,ht::Float64,h::Float64,hp::Float64,
retired::Bool)
"""
b : starting quantity of bonds
bp : chosen quantity of bonds for next period
iĻµ : position on the labor productivity grid
ht : size of house rented
h : size of house at start of period
hp : size of house at end of period
retired : agent is retired (==1)
"""
if retired == 1 && (h != hp || h == 0.0)
# retired renter. chooses to keep renting
# retired renter. chooses to own house
# retired homeowner. chooses to sell house and rent
# retired homeowner. chooses to sell house and buy another
@unpack w,q_b,p_h,Ī“_h,Ļ_h,Īŗ_h,Ļ = p
return Ļµ*w + b + p_h*h*(1-Ī“_h-Ļ_h-Īŗ_h) - q_b*bp - p_h*hp - Ļ*ht
elseif retired == 0 && (h != hp || h == 0.0)
# active renter. chooses to keep renting
# active renter. chooses to own house
# active homeowner. chooses to sell house and rent
# active homeowner. chooses to sell house and buy another
@unpack w,q_b,p_h,Ī“_h,Ļ_h,Īŗ_h,Ļ = p
return Ļµ*w + b + p_h*h*(1-Ī“_h-Ļ_h-Īŗ_h) - q_b*bp - p_h*hp - Ļ*ht
elseif retired == 1 && h == hp && h > 0.0
# retired homeowner. chooses to keep his house
@unpack w,q_b,p_h,Ī“_h,Ļ_h,Ļ = p
return Ļµ*w + b - p_h*h*(Ī“_h+Ļ_h) - q_b*bp
else
# active homeowner. chooses to keep his house
@unpack w,q_b,p_h,Ī“_h,Ļ_h,Ļ = p
return Ļµ*w + b - p_h*h*(Ī“_h+Ļ_h) - q_b*bp
end
end
The logic of the code is very simple: if agent == characteristic X => consumption for agent type X. Now, compared to a case where I simply make a different function for every different type of agent, this is twice as slow. Also, notice I am passing the parameters through object p (from Parameters.jl), which is also very costly in terms of performance. I do this so I dont have to be setting 24 different parameters inside each function everytime I want to call it, thus avoiding bugs and miscalculations (which is also the reason I prefer to use the same budget function for every problem). So my question is: is what I am doing best practice in terms of performance? Is there a way to increase it?
Thanks