Forward Diff and Matrix Operations

Hi all,

I am new to ForwardDiff and coding up a simple example of minimum distance estimation. I would ideally like to be able to set up the problem in the traditional way of:

[distance between parameter implied moments and sample moments]^TWeighting matrix[distance between parameter implied moments and sample moments]

Unfortunately, when I try to do this, I trigger the following error:

MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{typeof(MDE_Criterion), Float64}, Float64, 2})
Closest candidates are:
  (::Type{T})(::Real, !Matched::RoundingMode) where T<:AbstractFloat at rounding.jl:200
  (::Type{T})(::T) where T<:Number at boot.jl:772
  (::Type{T})(!Matched::AbstractChar) where T<:Union{AbstractChar, Number} at char.jl:50

If I specify (implicitly) that I am using the identity (by squaring the difference between moments), then there is no trouble, but when I let the function use an arbitrary weighting matrix it does not work.

Full code included below:

function OneStep(θ,SampleMoments,T,W)
    #ρ_guess = θ[1]
    #σ_guess = θ[2]
    #(θ[2]^2)/(1-θ[1]^2) = σ^2/(1-ρ^2)

    moments = copy(SampleMoments)
    count = 1
    for t in 1:T
        for j in t:T
            moments[count] = ((θ[1]^(j-t))*(θ[2]^2)/(1-θ[1]^2) - SampleMoments[count])
            count += 1 
        end
    end

    return diff'*W*moments
end

Any help would be much appreciated!

I’m guessing that you are differentiating over θ. In that case, you have the problem that moments is a Float64 type and θ is a Dual type, and so you can’t store the the results of a Dual computation in a Float64 array. (Or something like that, since you haven’t provided a runnable example I’m guessing what you are doing.)

Take a look at PreallocationTools.jl to find some easy solutions to this problem.

2 Likes

You could also do something like the following

function OneStep(θ,SampleMoments,T,W)
    #ρ_guess = θ[1]
    #σ_guess = θ[2]
    #(θ[2]^2)/(1-θ[1]^2) = σ^2/(1-ρ^2)

    moments = [
        ((θ[1]^(j-t))*(θ[2]^2)/(1-θ[1]^2) - SampleMoments[count])
        for t in 1:T for j in 1:T
    ]

    return diff'*W*moments
end

That way the vector moments you create has the right type from the start

2 Likes