Hello!
I am trying to solve a big model and at the core I have the following loop which essentially solves a Backward Induction Problem. In what follows I provide a MWE.
In my model, I am solving this problem many many times and therefore I would like to push its performance to the max, however, at this point I do not know how to improve it further.
I believe I can make improvements both in terms of memory usage and computation speed. Any suggestions are welcome.
using Distributed
using BenchmarkTools
using ProgressMeter
using BitOperations
using Profile
using Traceur
addprocs(11, exeflags="--project=.")
@everywhere J = 100
@everywhere T = 200
@everywhere Î = 10*rand(J,T)
@everywhere Dᾢ = rand([0],J,1)
@everywhere Sj = 20*rand(J,T)
@everywhere Fj = 20*rand(J,T)
@everywhere Î =Î .-Sj
@everywhere function myfun1(Π,Fj,Sj,Dᾢ,β,J,T)
D = zeros(Int64,2,T,J)
v = zeros(Float64,2,T,J)
Dstar = zeros(Int64,J,T+1)
Dstar[:,1]=Dᾢ
S = [0 1]
Î star=0
@inbounds for t=0:T-1, j=1:J, i=1:2
if t==0
payoff = Î [j,T-t]+Sj[j,T-t]*S[i]
D[i,T-t,j]=(payoff>0)
v[i,T-t,j]=D[i,T-t,j]*payoff
else
payoff_enter = Π[j,T-t]+Sj[j,T-t]*S[i]+β*v[2,T-t+1,j]
payoff_exit = β*v[1,T-t+1,j]
d=(payoff_enter>payoff_exit)
D[i,T-t,j] = d
v[i,T-t,j]=d*(payoff_enter)+(1-d)*payoff_exit
end
end
#println(v)
@inbounds for t=2:T+1, j=1:J
Dstar[j,t]=D[Dstar[j,t-1]+1,t-1,j]
end
Dstar = Dstar[:,2:end]
return Dstar
end
println("TIMES")
f1 = @btime myfun1(Π,Fj,Sj,Dᾢ,0.9,J,T)
@profile myfun1(Π,Fj,Sj,Dᾢ,0.9,J,T)
@trace myfun1(Π,Fj,Sj,Dᾢ,0.9,J,T)
Any suggestion will be very much appreciated!