I’m trying my hand at estimation via indirect inference, and created this script to experiment. I am wondering what is the best way to parallelize the computation. I think the critical functions are `simulate`

and the line that computes `θ̃`

inside the function `obj`

. I don’t have experience in parallel computing, so I am open to any suggestions.

```
using Optim
using Statistics
using Distributions
using Random
Random.seed!(23578)
# true dgp
# y = exp(β ⋅ x) + u
# u ~ 𝒩(0, σ²)
function model(x, β)
exp.(x * β)
end
# number of simulations
const M = 10
# size of data
const n = 1_000
const K = 5
# generate data
const σ² = 1.5
const β = rand(K)
const udgp = rand(Normal(0, σ²), n)
const x = hcat(fill(1.0, n), rand(Normal(), (n, K - 1)))
const ydgp = model(x, β) + udgp
# auxiliary model
# y = θ ⋅ x + ε
# ε ~ 𝒩(0, σε²)
# estimator of auxiliary model
function ols(x, y)
xt = transpose(x)
inv(xt * x) * xt * y
end
# generate simulations
const u0 = [randn(n) for i in 1:M]
function simulate(x, β; u0=u0, M=M)
[model(x, β) + u0[i] for i in 1:M]
end
# estimate of auxiliary model
const θ̂ = ols(x, ydgp)
# criterion to be minimized
criterion(θ̃; θ̂=θ̂) = sum((θ̂ - θ̃).^2)
# indirect inference objective function
function obj(β)
ys = simulate(x, β)
θ̃ = mean([ols(x, ys[i]) for i in 1:M])
return criterion(θ̃)
end
# initial values of parameters of true model
β0 = ones(K)
# optimization
opt = optimize(obj, β0, method=ConjugateGradient(), autodiff=:forward)
# indirect inference estimates (of the true model)
β̂ = Optim.minimizer(opt)
```