# Simple optimizer?

**URL:** https://discourse.julialang.org/t/simple-optimizer/121744
**Category:** Optimization (Mathematical)
**Tags:** question
**Created:** [October 25, 2024, 10:56am UTC](https://discourse.julialang.org/t/simple-optimizer/121744 "2024-10-25T10:56:53Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 25, 2024, 10:56am UTC](https://discourse.julialang.org/t/simple-optimizer/121744/1 "2024-10-25T10:56:53Z")

</div>

I have the following equation:

```julia
res = c1 * v_app * u_s + c2/v_app * sin(psi) * cos(beta) - psi_dot

```

and I want to determine the constants c1 and c2 based on measurements such that the the sum of res² over all measurements reaches a minimum.

I have vectors of v\_app, u\_s, psi, beta and psi\_dot with about 1000 elements.

Which package would you suggest?

---

<div class="post-metadata">

### Author: ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)
#### Post date: [October 25, 2024, 11:13am UTC](https://discourse.julialang.org/t/simple-optimizer/121744/2 "2024-10-25T11:13:23Z")

</div>

Re-express as a linear system Ac = b where c=[c\_1, c\_2] is a vector of the unknown coefficients, A is a 1000 \times 2 matrix whose columns are v\_{app} u\_s and \sin(\psi) \cos(\beta) /v\_{app}, and b is the vector `psi_dot`.

Then solve with `c = A\b`. That’ll get you a least-squares solution to the problem, the one that minimizes sum res^2, using QR decomposition.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 25, 2024, 11:15am UTC](https://discourse.julialang.org/t/simple-optimizer/121744/3 "2024-10-25T11:15:52Z")

</div>

Very nice idea! No package needed at all!

Works nicely:

```julia
function calc_c1_c2(v_app, psi, beta, psi_dot, steering)
    col1 = v_app .* steering
    col2 = sin.(psi) .* cos.(beta) ./ v_app
    A = [col1 col2]
    c = A \ psi_dot
    return c[1], c[2]
end

```

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 25, 2024, 11:47am UTC](https://discourse.julialang.org/t/simple-optimizer/121744/4 "2024-10-25T11:47:20Z")

</div>

Extra question: How can I obtain the standard deviation of c1 and c2 ?

---

<div class="post-metadata">

### Author: ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)
#### Post date: [October 25, 2024, 12:14pm UTC](https://discourse.julialang.org/t/simple-optimizer/121744/5 "2024-10-25T12:14:22Z")

</div>

You can use the `StatsBase` package’s `std(c1)` function.

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 25, 2024, 3:10pm UTC](https://discourse.julialang.org/t/simple-optimizer/121744/6 "2024-10-25T15:10:54Z")

</div>

Not really. If I calculate:

```julia
c = A \ psi_dot

```

I get a two element vector of c1 and c2, both scalars. And I cannot calculate the standard deviation of a scalar.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 25, 2024, 3:32pm UTC](https://discourse.julialang.org/t/simple-optimizer/121744/7 "2024-10-25T15:32:51Z")

</div>

> [@ufechner7](#):
>
> Extra question: How can I obtain the standard deviation of c1 and c2 ?

The package LsqFit.jl has a `standard_error()` of the fit parameters.  
[See the tutorial section here](https://julianlsolvers.github.io/LsqFit.jl/latest/tutorial/#Goodness-of-Fit-1).

---

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [October 25, 2024, 3:58pm UTC](https://discourse.julialang.org/t/simple-optimizer/121744/8 "2024-10-25T15:58:43Z")

</div>

But isn’t `LsqFit` intended for non-linear models, and here we have a linear model?

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 25, 2024, 4:03pm UTC](https://discourse.julialang.org/t/simple-optimizer/121744/9 "2024-10-25T16:03:06Z")

</div>

Absolutely not, performing linear regressions with LsqFit.jl is perfectly fine, and it is probably when the estimated standard deviations are the most accurate.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [October 25, 2024, 4:33pm UTC](https://discourse.julialang.org/t/simple-optimizer/121744/10 "2024-10-25T16:33:37Z")

</div>

I think we are actually talking about a standard error of a parameter estimate here, not a standard deviation. You first should think about conceptually what you are actually trying to estimate here, as the docs of LsqFit say there are some assumptions being made

> Assume the errors in each sample are independent, normal distributed with zero mean and same variance, i.e. ϵ∼N(0,σ2I)

which might or might not make sense (especially homoskedasticity is often a problematic assumption on real world data at least in the social sciences, I’m not a kite power plant modelling expert so YMMV).

But indeed your model is linear so you can just estimate it with GLM or FixedEffectModels and get heteroskedasiticty-robust standard errors as well if that matters to you.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 25, 2024, 4:40pm UTC](https://discourse.julialang.org/t/simple-optimizer/121744/11 "2024-10-25T16:40:07Z")

</div>

Some [terminology reference](https://en.wikipedia.org/wiki/Standard_error).

---

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [October 25, 2024, 9:54pm UTC](https://discourse.julialang.org/t/simple-optimizer/121744/12 "2024-10-25T21:54:48Z")

</div>

what if you replace the terms in your `A` and `psi_dot` arrays for `Measurements.measurement` types?
