# Numerical Jacobian

**URL:** <https://discourse.julialang.org/t/numerical-jacobian/30455>\
**Category:** New to Julia\
**Created:** [October 29, 2019, 6:06pm UTC](https://discourse.julialang.org/t/numerical-jacobian/30455 "2019-10-29T18:06:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmcastro2109](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmcastro2109/32/38427_2.png) [@jmcastro2109](https://discourse.julialang.org/u/jmcastro2109)\
**Post date:** [October 29, 2019, 6:06pm UTC](https://discourse.julialang.org/t/numerical-jacobian/30455/1 "2019-10-29T18:06:35Z")

</div>

Hello!

I am looking for the best package to compute a numerical Jacobian.

Best,  
Juan

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [October 29, 2019, 8:12pm UTC](https://discourse.julialang.org/t/numerical-jacobian/30455/2 "2019-10-29T20:12:35Z")

</div>

For 𝑌 = g(𝑋), `CoupledFields.gradvecfield([a b], X, Y, kernelpars)` returns 𝑛 gradient matrices, for 𝑛 random points in 𝑋.  
For parameters [𝑎 𝑏]: 𝑎 is a smoothness parameter, and 𝑏 is a ridge parameter

```julia
using CoupledFields
g(x,y,z) = x .* exp.(-x.^2 - y.^2 - z.^2)
X = -2 .+ 4*rand(100, 3)
Y = g.(X[:,1], X[:,2], X[:,3])

 kernelp.ars = GaussianKP(X)
 ∇g = gradvecfield([0.5 -7], X, Y[:,1:1], kernelpars)

```

Also `CoupledFields` doesn’t require a closed-form function, it can be used if you only have the observed fields 𝑋 and 𝑌.

---

<div class="post-metadata">

**Author:** ![lstagner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lstagner/32/448_2.png) [@lstagner](https://discourse.julialang.org/u/lstagner)\
**Post date:** [October 29, 2019, 8:52pm UTC](https://discourse.julialang.org/t/numerical-jacobian/30455/3 "2019-10-29T20:52:13Z")

</div>

Your best bet is to use [ForwardDiff.jl](https://github.com/JuliaDiff/ForwardDiff.jl) which uses automatic differentiation

```julia
julia> using ForwardDiff

julia> ForwardDiff.jacobian(x->exp.(x), rand(2))
2×2 Array{Float64,2}:
 2.33583 0.0
 0.0 1.59114

```

---

<div class="post-metadata">

**Author:** ![jmcastro2109](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmcastro2109/32/38427_2.png) [@jmcastro2109](https://discourse.julialang.org/u/jmcastro2109)\
**Post date:** [October 29, 2019, 9:05pm UTC](https://discourse.julialang.org/t/numerical-jacobian/30455/4 "2019-10-29T21:05:21Z")

</div>

Yeah that’s what I’ve been trying to use. However it seems to me that if I have a function that does not have a closed form ForwardDiff does not worj. Or at least it is not working for me.

Obtener [Outlook para iOS](https://aka.ms/o0ukef)

---

<div class="post-metadata">

**Author:** ![lstagner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lstagner/32/448_2.png) [@lstagner](https://discourse.julialang.org/u/lstagner)\
**Post date:** [October 29, 2019, 9:11pm UTC](https://discourse.julialang.org/t/numerical-jacobian/30455/5 "2019-10-29T21:11:05Z")

</div>

You could also use [DiffEqDiffTools.jl](https://github.com/JuliaDiffEq/DiffEqDiffTools.jl) to calculate the jacobian via finite differencing.

Also if you post your function and the error message we could figure out why ForwardDiff isn’t working.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [October 29, 2019, 10:50pm UTC](https://discourse.julialang.org/t/numerical-jacobian/30455/6 "2019-10-29T22:50:23Z")

</div>

FiniteDIfferences.jl to calculate via fiinite differencing.

[https://www.juliadiff.org/FiniteDifferences.jl/latest/pages/api/#FiniteDifferences.jacobian](https://www.juliadiff.org/FiniteDifferences.jl/latest/pages/api/#FiniteDifferences.jacobian)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 30, 2019, 6:39am UTC](https://discourse.julialang.org/t/numerical-jacobian/30455/7 "2019-10-30T06:39:44Z")

</div>

@jmcastro2109: when you asked via e-mail you also mentioned that you are using these for indirect inference. The problem with that is that for discrete-choice problems, derivatives may not be easy to approximate using (finite) samples, so I would consider using a derivative-free optimization method.
