Thank you for your reply. I have modified the question according to your suggestion. The above script is all I have and I verified that after your suggestion, I have the error message even before ForwardDiff.jacobian:
using Parameters, ForwardDiff
@with_kw struct Primitives{T}
β::T # discount rate
end
function u(θ; M,A,E,N,B,W,j)
δ_1, δ_2 = θ
u = (δ_1^2*A) + δ_2^2*A^2+j
return u
end
function Z(θ)
Z1 = zeros(typeof(θ), 3)
@unpack β = Primitives()
for a=22:24
Z1[a-21] = (u(θ,M=0,A=a+0,E=1,N=0,B=0,W=0,j=1)+β*u(θ,M=0,A=a+0,E=1,N=0,B=0,W=0,j=2))
end
return Z1
end
Z([1.0,1.0])
θ_init = [1.0,1.0]
ForwardDiff.jacobian(θ->Z(θ), θ_init)
Z([1.0,1.0])
returns
MethodError: no method matching zero(::Type{Array{Float64,1}})
Closest candidates are:
zero(!Matched::Type{Missing}) at missing.jl:103
zero(!Matched::Type{LibGit2.GitHash}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\LibGit2\src\oid.jl:220
zero(!Matched::Type{Pkg.Resolve.VersionWeight}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\Pkg\src\Resolve\versionweights.jl:15
...
zeros(::Type{Array{Float64,1}}, ::Tuple{Int64}) at array.jl:505
zeros(::Type{Array{Float64,1}}, ::Int64) at array.jl:500
Z(::Array{Float64,1}) at experimentHouse.jl:38
top-level scope at experimentHouse.jl:45
Also, ForwardDiff.jacobian(θ->Z(θ), θ_init)
returnst the following:
MethodError: no method matching zero(::Type{Array{ForwardDiff.Dual{ForwardDiff.Tag{var"#21#22",Float64},Float64,2},1}})
Closest candidates are:
zero(!Matched::Type{Missing}) at missing.jl:103
zero(!Matched::Type{LibGit2.GitHash}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\LibGit2\src\oid.jl:220
zero(!Matched::Type{Pkg.Resolve.VersionWeight}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\Pkg\src\Resolve\versionweights.jl:15
...
zeros(::Type{Array{ForwardDiff.Dual{ForwardDiff.Tag{var"#21#22",Float64},Float64,2},1}}, ::Tuple{Int64}) at array.jl:505
zeros(::Type{Array{ForwardDiff.Dual{ForwardDiff.Tag{var"#21#22",Float64},Float64,2},1}}, ::Int64) at array.jl:500
Z(::Array{ForwardDiff.Dual{ForwardDiff.Tag{var"#21#22",Float64},Float64,2},1}) at experimentHouse.jl:38
(::var"#21#22")(::Array{ForwardDiff.Dual{ForwardDiff.Tag{var"#21#22",Float64},Float64,2},1}) at experimentHouse.jl:48
vector_mode_dual_eval(::var"#21#22", ::Array{Float64,1}, ::ForwardDiff.JacobianConfig{ForwardDiff.Tag{var"#21#22",Float64},Float64,2,Array{ForwardDiff.Dual{ForwardDiff.Tag{var"#21#22",Float64},Float64,2},1}}) at apiutils.jl:37
vector_mode_jacobian(::var"#21#22", ::Array{Float64,1}, ::ForwardDiff.JacobianConfig{ForwardDiff.Tag{var"#21#22",Float64},Float64,2,Array{ForwardDiff.Dual{ForwardDiff.Tag{var"#21#22",Float64},Float64,2},1}}) at jacobian.jl:140
jacobian(::Function, ::Array{Float64,1}, ::ForwardDiff.JacobianConfig{ForwardDiff.Tag{var"#21#22",Float64},Float64,2,Array{ForwardDiff.Dual{ForwardDiff.Tag{var"#21#22",Float64},Float64,2},1}}, ::Val{true}) at jacobian.jl:17
jacobian(::Function, ::Array{Float64,1}, ::ForwardDiff.JacobianConfig{ForwardDiff.Tag{var"#21#22",Float64},Float64,2,Array{ForwardDiff.Dual{ForwardDiff.Tag{var"#21#22",Float64},Float64,2},1}}) at jacobian.jl:15
jacobian(::Function, ::Array{Float64,1}) at jacobian.jl:15
top-level scope at experimentHouse.jl:48
As I am new to the Dual
number types, I am more lost here… why do we use write curly brackets in the end of Primitives{T}
? Does it free \beta to change its types when needed?