# Forward Diff and Matrix Operations

**URL:** <https://discourse.julialang.org/t/forward-diff-and-matrix-operations/97042>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 4, 2023, 3:34am UTC](https://discourse.julialang.org/t/forward-diff-and-matrix-operations/97042 "2023-04-04T03:34:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JHall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jhall/32/206240_2.png) [@JHall](https://discourse.julialang.org/u/JHall)\
**Post date:** [April 4, 2023, 3:34am UTC](https://discourse.julialang.org/t/forward-diff-and-matrix-operations/97042/1 "2023-04-04T03:34:02Z")

</div>

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]^T_Weighting matrix_[distance between parameter implied moments and sample moments]

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

```julia
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:

```julia
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!

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [April 4, 2023, 8:03am UTC](https://discourse.julialang.org/t/forward-diff-and-matrix-operations/97042/2 "2023-04-04T08:03:40Z")

</div>

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](https://github.com/SciML/PreallocationTools.jl) to find some easy solutions to this problem.

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [April 4, 2023, 9:29am UTC](https://discourse.julialang.org/t/forward-diff-and-matrix-operations/97042/3 "2023-04-04T09:29:10Z")

</div>

You could also do something like the following

```julia
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
