Hi!
I am trying to make the following function faster. Any suggestion would be very much appreciated.
using Random
using LinearAlgebra
using BenchmarkTools
###############################################################
# Loglikelihood
###############################################################
const J = 75 # This is greek letter \Iota
const Ι = 200
const N = 10000
const Mat1 = ones(Int8,Ι,N)
const Mat2 = ones(Int8,J,N)
const Mat3 = rand(Ι,J,N) .>0.9
NumberParams = Ι
const τ = rand(Ι,J)
#NumberParams = 2
@views function Log_Likelihood(N,Ι,J,τ,Mat1,Mat2,Mat3,params)
llike = 0.0
θ = params[1]
γ = params[2:end]
γ = vcat(γ,1.0)
#llike_test = zeros(Real,Ι,J,N)
vals = zeros(Real,size(Mat3)[1:2])
log_vals = zeros(Real,size(Mat3)[1:2])
vals .= exp.(-θ.*τ .+ γ)
log_vals = log.(vals)
cond= sum(Mat1,dims=1)'
@fastmath @inbounds for n = 1 : N
if cond[n]>1
for j = 1 : J
denom_index = 0.0 :: Real
if Mat2[j,n] == 1
denom_index = Mat1[:,n]'*vals[:,j]
for i = 1 : Ι
llike += Mat3[i,j,n]*Mat2[j,n]*Mat1[i,n]*(log_vals[i,j] - log(denom_index))
#llike_test[i,j,n] = Mat3[i,j,n]*Mat2[j,n]*Mat1[i,n]*(log_vals[i,j] - log(denom_index))
end
end
end
end
end
llike = -llike
return llike
end
XXX = @btime Log_Likelihood(N,Ι,J,τ,Mat1,Mat2,Mat3,vcat(7.0,ones(NumberParams-1)))
77.749866 seconds (3.60 G allocations: 62.597 GiB, 3.56% gc time)
1.0246133571164802e8