Thanks Elrod. I tried it but it didn’t work.
function model(V::Vector) ### figure out dimensions to put all params in V
parnum = getnumElem(d); parnum = Int64.(parnum); udo = parnum + d;
par2 = 2*udo;
bn = size(X,2)*d;
M = zeros(eltype(V), d,d)
M[tril!(trues(size(M)), -1)] .= V[1:parnum]
M[diagind(M)] .= 1.
sigmae = exp.(V[(parnum+1):udo])
Ve = Diagonal(sigmae) * M * Diagonal(sigmae)
Mu = zeros(eltype(V), d,d)
Mu[tril!(trues(size(Mu)), -1)] .= V[(udo+1):udo+parnum]
Mu[diagind(Mu)] .= 1.
sigmau = exp.(V[(udo+parnum+1):par2])
Vg = Diagonal(sigmau) * Mu * Diagonal(sigmau)
Vgchol = cholesky(Positive, Vg).L
beta = V[(par2 + 1):(par2 + bn)];
beta = reshape(beta, :, d)
In = one(rand(Float64, size(Achol))) #### change
gcov = kron(Vg, Achol)
rcov = kron(Ve, In)
mu = vec(X*beta);
Rcov = cholesky(Positive, gcov+rcov).L
a = AltMvNormal(Val(:L), mu, Rcov)
llk = logpdf(a, vec(Y))
loglik = llk
logpri = sum(logpdf.(Cauchy(2.5), sigmau)) + sum(logpdf.(Cauchy(2.5), sigmae)) +
logpdf(LKJL(d), LowerTriangular(Mu)) + logpdf(LKJL(d), LowerTriangular(M)) + loglikelihood(Normal(0.,1000.), beta)
logprob = loglik + logpri
return(-logprob)
end
julia> using ForwardDiff
julia> ForwardDiff.gradient(vars -> model(vars[1:nvar]), init)
ERROR: MethodError: no method matching floattype(::Type{ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol("##3#4")),Float64},Float64,12}})
Closest candidates are:
floattype(::Type{T<:AbstractFloat}) where T<:AbstractFloat at /home/ubuntu/.julia/packages/PositiveFactorizations/VybOm/src/cholesky.jl:264
floattype(::Type{T<:Integer}) where T<:Integer at /home/ubuntu/.julia/packages/PositiveFactorizations/VybOm/src/cholesky.jl:265
Stacktrace:
[1] default_δ(::Array{ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol("##3#4")),Float64},Float64,12},2}) at /home/ubuntu/.julia/packages/PositiveFactorizations/VybOm/src/cholesky.jl:269
[2] default_tol(::Array{ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol("##3#4")),Float64},Float64,12},2}) at /home/ubuntu/.julia/packages/PositiveFactorizations/VybOm/src/cholesky.jl:270
[3] cholesky(::Type{Positive}, ::Array{ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol("##3#4")),Float64},Float64,12},2}, ::Type) at /home/ubuntu/.julia/packages/PositiveFactorizations/VybOm/src/cholesky.jl:7 (repeats 2 times)
[4] model(::Array{ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol("##3#4")),Float64},Float64,12},1}) at ./REPL[11]:20
[5] (::getfield(Main, Symbol("##3#4")))(::Array{ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol("##3#4")),Float64},Float64,12},1}) at ./REPL[18]:1
[6] chunk_mode_gradient(::getfield(Main, Symbol("##3#4")), ::Array{Float64,1}, ::ForwardDiff.GradientConfig{ForwardDiff.Tag{getfield(Main, Symbol("##3#4")),Float64},Float64,12,Array{ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol("##3#4")),Float64},Float64,12},1}}) at /home/ubuntu/.julia/packages/ForwardDiff/N0wMF/src/gradient.jl:140
[7] gradient(::Function, ::Array{Float64,1}, ::ForwardDiff.GradientConfig{ForwardDiff.Tag{getfield(Main, Symbol("##3#4")),Float64},Float64,12,Array{ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol("##3#4")),Float64},Float64,12},1}}, ::Val{true}) at /home/ubuntu/.julia/packages/ForwardDiff/N0wMF/src/gradient.jl:19
[8] gradient(::Function, ::Array{Float64,1}, ::ForwardDiff.GradientConfig{ForwardDiff.Tag{getfield(Main, Symbol("##3#4")),Float64},Float64,12,Array{ForwardDiff.Dual{ForwardDiff.Tag{getfield(Main, Symbol("##3#4")),Float64},Float64,12},1}}) at /home/ubuntu/.julia/packages/ForwardDiff/N0wMF/src/gradient.jl:15 (repeats 2 times)
[9] top-level scope at none:0