# Help in Optim/optimize - structure of code and help

**URL:** <https://discourse.julialang.org/t/help-in-optim-optimize-structure-of-code-and-help/93656>\
**Category:** Optimization (Mathematical)\
**Created:** [January 27, 2023, 7:01pm UTC](https://discourse.julialang.org/t/help-in-optim-optimize-structure-of-code-and-help/93656 "2023-01-27T19:01:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Outeirom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/outeirom/32/43141_2.png) [@Outeirom](https://discourse.julialang.org/u/Outeirom)\
**Post date:** [January 27, 2023, 7:01pm UTC](https://discourse.julialang.org/t/help-in-optim-optimize-structure-of-code-and-help/93656/1 "2023-01-27T19:01:03Z")

</div>

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

```julia
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

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [January 30, 2023, 10:10pm UTC](https://discourse.julialang.org/t/help-in-optim-optimize-structure-of-code-and-help/93656/2 "2023-01-30T22:10:26Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![tmigot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tmigot/32/23914_2.png) [@tmigot](https://discourse.julialang.org/u/tmigot)\
**Post date:** [February 1, 2023, 3:55am UTC](https://discourse.julialang.org/t/help-in-optim-optimize-structure-of-code-and-help/93656/3 "2023-02-01T03:55:49Z")

</div>

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.
