Help in Optim/optimize - structure of code and help

I have a code taht is more or less like this:

fixed parameters \alpha, \beta... that never changes

#then I have vector of parameters I want to calibrate (theta) and target (mom_data)
theta = [ *vector of parameters to calibrate*]
mom_data[ *vector of moments in the real data* ]

#a function to solve the model:
#it uses \alpha and \beta, but in order to simplify, I put it just in function of vector theta

function solve_model(theta)
return mom_model = [ *vector of moments in the model given theta*]
end

#and a function that calibrate it:
function calibrate(theta, mom_data)
model_target =  model(theta)
error = (model_target - mom_data)/mom_data

return error'*I(length(error))*error #whete I(n) is identity matrix
end

#finally, I want to calibrate
r = optimize(x -> calibrate(x, mom_data), initial_guess)



So, first of all, I would like to know if this structure is correct to you guys, or is there anything I can do to improve it.

However, the important question I would like to do is this: I have a parameter \rho that I want to estimate, so, it is inside vector theta. However, I want to set a target to it as well, so it does not deviate a lot from the value I want. I mean, the point is that I want to set \rho = 1, but in order to improve calibration in other dimensions, I would let it vary a bit.

How can I do this? I am struggling because this \rho would appear as a part of theta (since I am estimating it), but as a target, since I woul be trying to set it equals to 1

I would like to know if this structure is correct to you guys

The structure looks reasonable. Itâ€™s hard to say more without a reproducible example containing the details.

I mean, the point is that I want to set \rho = 1, but in order to improve calibration in other dimensions, I would let it vary a bit.

You could either add a constraint, or add (rho - target)^2 as a penalty term in the objective.

Hi @Outeirom !

Agreed with @odow that it is hard to give advice without an example. If your error vector is very large, it could be interesting to use solvers/models designed for nonlinear least squares.