Hi, I’m trying to write my own cost function for parameter estimation of an ode model.
I am trying to weight the loss by the time t.
Based on the documentation I came up with the following, but I’m unsure about the correct syntax:
function optimization(model,data,constants,dens,options)
# fit function - given model, data, densities, options get best parameters
@unpack lb,ub,tspan = options
f = (du,u,p,t) -> model(du,u,10 .^ p,t,dens,constants) # optimize in log space
prob = ODEProblem(f,u₀,tspan,p)
# cost_function = build_loss_objective(prob,Tsit5(),L2Loss(t,data),
# maxiters=10000,verbose=false)
cost_function = build_loss_objective(prob,Tsit5(),my_loss_function(sol,t,data),
maxiters=10000,verbose=false)
# lb = [-10., -10.] # from 1e-10 to 1e10
# ub = [5., 5.]
result = optimize(cost_function,lb,ub,[0.,0.], Fminbox(BFGS()))
end
function my_loss_function(sol,t,data)
tot_loss = 0.0
if any((s.retcode != :Success for s in sol))
tot_loss = Inf
else
# calculatisolon for the loss here
# n = length(t)
exp = sol(t)
tot_loss = sum( ((exp .- data) .^ 2) .* t ) # time weighted loss
end
tot_loss
end
fit = optimization(model_name,data,constants,dens,c)
Thanks