# Fit ODE parameters with different weights for each variable

**URL:** https://discourse.julialang.org/t/fit-ode-parameters-with-different-weights-for-each-variable/56296
**Category:** General Usage
**Tags:** diffeq, ode, optimization
**Created:** [March 2, 2021, 12:52am UTC](https://discourse.julialang.org/t/fit-ode-parameters-with-different-weights-for-each-variable/56296 "2021-03-02T00:52:35Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Rodrigo\_Zepeda](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rodrigo_zepeda/32/17935_2.png) [@Rodrigo\_Zepeda](https://discourse.julialang.org/u/Rodrigo_Zepeda)
#### Post date: [March 2, 2021, 12:52am UTC](https://discourse.julialang.org/t/fit-ode-parameters-with-different-weights-for-each-variable/56296/1 "2021-03-02T00:52:35Z")

</div>

Hi! I am trying to optimize parameters for an ODE giving different weights to each entry of the solution `y` inside the `L2Loss` function. That is all `y[1]`s should have some weights `w1` while all `y[3]`s should have some different weights `w3`.  
however: 1) I am uncertain on how to pass those weights using the `data_weight` parameter and 2) I am uncertain on how to combine with `save_idx` as I haven’t observed all the entries of `y`. Here is a toy example:

```julia
using DifferentialEquations, DiffEqParamEstim, RecursiveArrayTools, BlackBoxOptim

#This is a toy example
function f(du,u,p,t)
  du[1] = dx = p[1]*u[1] - u[2]
  du[2] = dy = -3*u[2] + u[3]
  du[3] = dz = -u[3] + u[1]
end

u0 = [1.0;1.0;1.0]
tspan = (0.0,10.0)
p = [1.5]
prob = ODEProblem(f,u0,tspan,p)
sol = solve(prob, Tsit5())
t = collect(range(0,stop=10,length=200))
randomized = VectorOfArray([(sol(t[i]) + .01randn(3)) for i in 1:length(t)])
data = convert(Array,randomized)[[1,3],:] #Keep only 1st and third for example

#Here I am uncertain on whether which way to structure the weights is correct
w1 = 0.5 #weights for u[1]
w3 = 100.0 #weights for u[3]
weights1 = [repeat([w1], size(data)[2]); repeat([w3], size(data)[2])]
weights2 = hcat(repeat([w1], size(data)[2]), repeat([w3], size(data)[2]))'

#This seems to work
loss = build_loss_objective(prob, Tsit5(), L2Loss(t,data; data_weight = weights1),
                              save_idxs = [1,3])
prob_bb = bboptimize(loss, SearchRange = (0.0,100.0), NumDimensions = 1)

#This ALSO seems to work however returns a different fitness
loss = build_loss_objective(prob, Tsit5(), L2Loss(t,data; data_weight = weights2),
                              save_idxs = [1,3])
prob_bb = bboptimize(loss, SearchRange = (0.0,100.0), NumDimensions = 1)

```

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [March 2, 2021, 1:09am UTC](https://discourse.julialang.org/t/fit-ode-parameters-with-different-weights-for-each-variable/56296/2 "2021-03-02T01:09:24Z")

</div>

Just use DiffEqFlux.jl for this. More flexibility in the loss function that way.
