How to fix this error when I use JUMP to maximize a function?

I’m trying to optimize a likelihood function a little different from the traditional ones. I’m going to call this likelihood function of mine “faux”. To show my problem, first see how well this example (I took this example from the Jump tutorial) works:

using Distributions
using JuMP, Ipopt

n = 1_000
data = randn(n)
model = Model(Ipopt.Optimizer)
set_silent(model)

@variable(model, μ, start = 0.0)
@variable(model, σ >= 0.0, start = 1.0)
@NLobjective( model , Max, n / 2 * log(1 / (2 * π * σ^2)) -sum((data[i] - μ)^2 for i in 1:n) / (2 * σ^2) )
optimize!(model)   
# declare solution
@show value(μ)
@show value(σ)

As I said, my function is a little more complicated to the point of writing it directly like the example I’m giving. So, as I have faux built out and want to maximize it using the same script as above. For this, assume that

faux(μ,σ) = n / 2 * log(1 / (2 * π * σ^2)) -sum((data[i] - μ)^2 for i in 1:n) / (2 * σ^2)

But, when I do this

n = 1_000
data = randn(n)
model = Model(Ipopt.Optimizer)
set_silent(model)

@variable(model, μ, start = 0.0)
@variable(model, σ >= 0.0, start = 1.0)
@NLobjective( model , Max, faux(μ,σ) )
optimize!(model)   

I have an error: Unrecognized function “faux” used in nonlinear expression.
Why is this happening?
Any solution?

What version of JuMP are you using? You should have received the warning:

julia> faux(μ,σ) = n / 2 * log(1 / (2 * π * σ^2)) -sum((data[i] - μ)^2 for i in 1:n) / (2 * σ^2)
faux (generic function with 1 method)

julia> @NLobjective( model , Max, faux(μ,σ) )
┌ Warning: Function faux automatically registered with 2 arguments.
│ 
│ Calling the function with a different number of arguments will result in an
│ error.
│ 
│ While you can safely ignore this warning, we recommend that you manually
│ register the function as follows:
│ ```Julia
│ model = Model()
│ register(model, :faux, 2, faux; autodiff = true)
│ ```
└ @ MathOptInterface.Nonlinear ~/.julia/packages/MathOptInterface/RbvtA/src/Nonlinear/operators.jl:430

You need to use a registered function Nonlinear Modeling · JuMP

using JuMP, Ipopt, Distributions
n = 1_000
data = randn(n)
model = Model(Ipopt.Optimizer)
set_silent(model)
@variable(model, μ, start = 0.0)
@variable(model, σ >= 0.0, start = 1.0)
faux(μ,σ) = n / 2 * log(1 / (2 * π * σ^2)) -sum((data[i] - μ)^2 for i in 1:n) / (2 * σ^2)
register(model, :faux, 2, faux; autodiff = true)
@NLobjective(model , Max, faux(μ,σ) )
optimize!(model)
1 Like