Passing struct as argument to optimizer

structural
Wed, 24 Jan 2024 20:02:26 +0000
General Usage

Is it possible to pass a struct of parameters as an argument to an optimizer? The example below estimates an OLS model in which the intercept and slope are passed a vector p.

function ssr(y,x,p)
    return sum((y .- (p[1] .+ p[2] .* x)).^2)
end

x1 = cumsum(ones(10))
y1 = 2 .+ 3 .* x1 .+ rand(10)

Optim.minimizer(optimize(p->ssr(y1,x1,p), ones(2)))

Is it possible to instead pass the intercept and slope as a struct?

struct ols_struct
   intercept::Float64
   slope::Float64
end
p = ols_struct(1,1)

3 posts - 3 participants