# How to incorporate time dependent interpolation functions while solving ensemble parameter estimation problem?

**URL:** <https://discourse.julialang.org/t/how-to-incorporate-time-dependent-interpolation-functions-while-solving-ensemble-parameter-estimation-problem/115121>\
**Category:** New to Julia\
**Created:** [June 3, 2024, 1:50pm UTC](https://discourse.julialang.org/t/how-to-incorporate-time-dependent-interpolation-functions-while-solving-ensemble-parameter-estimation-problem/115121 "2024-06-03T13:50:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Piyush](https://avatars.discourse-cdn.com/v4/letter/p/ecb155/32.png) [@Piyush](https://discourse.julialang.org/u/Piyush)\
**Post date:** [June 3, 2024, 1:50pm UTC](https://discourse.julialang.org/t/how-to-incorporate-time-dependent-interpolation-functions-while-solving-ensemble-parameter-estimation-problem/115121/1 "2024-06-03T13:50:19Z")

</div>

Hi everyone,

Data interpolation functions/ objects are not part of the ODE function in my code and also not the part of the parameter list.

Example:

```julia
function V(du, u, p, t)
    du[1] = Rate(t)
    du[2] = p[1]*u[1] + (Rate(t) / u[1]) *u[2]
end

p0 = [1.]
u0 = [1.0, 0.2]
N = 8
prob = ODEProblem(V, u0, (0., 12.), p0)

function prob_func(prob, i , repeat)
    Rate = ConstantInterpolation(y[i], x[i], extrapolate=true)
    tspan = (tspan_data[i][1], tspan_data[i][2])
    remake(prob; u0 = initial_conditions[i], tspan = (tspan_data[i][1], tspan_data[i][2]), p = prob.p)
end

enprob = EnsembleProblem(prob, prob_func = prob_func)

sim = solve(enprob, Tsit5(), trajectories = N)

```

I would like to estimate the parameter set (p[1] in this example) with different initial conditions, different sample time-points, different tspan and different Rate functions. @ChrisRackauckas kindly help

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [June 4, 2024, 9:35am UTC](https://discourse.julialang.org/t/how-to-incorporate-time-dependent-interpolation-functions-while-solving-ensemble-parameter-estimation-problem/115121/2 "2024-06-04T09:35:31Z")

</div>

It is unclear (to me) what you want and your code does not run because many things are missing. Please clarify (and edit your code to reflect):

1. What is `ConstantInterpolation`? Where does it come from?
2. In `prob_func` what are `x` and `y`? They are some global variables not defined here. Same thing with `tspan_data`.
3. Please note that your usage of globals is very confusing and likely introduces a bug. You define `Rate` in line 1, then reuse it inside `V`. It seems that you try to set it in `prob_func` but that just creates a new local variable and does not touch the global `Rate`. I think you should just put that `Rate` thing into the parameters of the solver.
4. You talk about estimation of something but your code does not show any comparison to some baseline. Please provide the bigger picture
5. Why do you need the `EnsembleProblem`? From your `prob_func`, it seems you want to do something piecewise?

---

<div class="post-metadata">

**Author:** ![Piyush](https://avatars.discourse-cdn.com/v4/letter/p/ecb155/32.png) [@Piyush](https://discourse.julialang.org/u/Piyush)\
**Post date:** [June 4, 2024, 10:13am UTC](https://discourse.julialang.org/t/how-to-incorporate-time-dependent-interpolation-functions-while-solving-ensemble-parameter-estimation-problem/115121/3 "2024-06-04T10:13:25Z")

</div>

Hi @abraemer, thank you for your response.

1. `ConstantInterpolation` is a function from the library `DataInterpolations`. It is used to make a continuous function between `x` and `y`, where `x` is time and `y` is volume. So `Rate(t)` is a flowrate.

1. x and y are both vectors for different flowrate for different experimental runs.

2. Yes, that is correct. I can remove the first line in the code for clarity.

3. I would like to estimate the parameter `p[1]` for different experimental runs and hence using `EnsembleProblem`.

---

<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:** [June 4, 2024, 11:35am UTC](https://discourse.julialang.org/t/how-to-incorporate-time-dependent-interpolation-functions-while-solving-ensemble-parameter-estimation-problem/115121/4 "2024-06-04T11:35:47Z")

</div>

Just put it in the parameters.

```julia
function V(du, u, _p, t)
    p, Rate = _p
    du[1] = Rate(t)
    du[2] = p[1]*u[1] + (Rate(t) / u[1]) *u[2]
end

p0 = [1.]
u0 = [1.0, 0.2]
N = 8
prob = ODEProblem(V, u0, (0., 12.), p0)

function prob_func(prob, i , repeat)
    Rate = ConstantInterpolation(y[i], x[i], extrapolate=true)
    tspan = (tspan_data[i][1], tspan_data[i][2])
    remake(prob; u0 = initial_conditions[i], tspan = (tspan_data[i][1], tspan_data[i][2]), p = (prob.p, Rate))
end

enprob = EnsembleProblem(prob, prob_func = prob_func)

sim = solve(enprob, Tsit5(), trajectories = N)

```

---

<div class="post-metadata">

**Author:** ![Piyush](https://avatars.discourse-cdn.com/v4/letter/p/ecb155/32.png) [@Piyush](https://discourse.julialang.org/u/Piyush)\
**Post date:** [June 5, 2024, 4:37am UTC](https://discourse.julialang.org/t/how-to-incorporate-time-dependent-interpolation-functions-while-solving-ensemble-parameter-estimation-problem/115121/5 "2024-06-05T04:37:21Z")

</div>

@ChrisRackauckas Thank you for the help
