Using two independent variables in an ode solver

I am trying to solve an ode using integrate_ode_rk45 function that uses two different independent variables, which have paired values.

The model is:
y'(t) = \theta(t - d) - y(t)

I want to estimate y at time ‘t’ which has the source \theta feeding in from time t-d. At time t0, y = y0.

Right now, I am trying to feed the values of d as an input using int data argument in the integrate_ode_rk45 function. For each value of t[i] I want to use the paired value of d[i] to solve for y[i]. Not sure how to that.

Attaching the simple stan code and rstan script with this. Let me know if anyone can help me with this.

stan_ode_2vars.R (309 Bytes)

stan_ode_2vars.stan (865 Bytes)

What is the function \theta here? Do you have it in analytic form? It looks kind of like a delay differential equation, but I think you should be able to solve it with the built in solvers if you can evaluate \theta easily.

\theta is an exponenential function of form \theta_0 * exp(m *t) .
So \theta(t-d) = \theta_0 * exp(m * (t-d)) where \theta_0 and m are predefined,

I am not sure how to use ode solvers in stan to solve such equations. Any help would be greatly appreciated.

Thanks :)

In that case you can just use either of the ODE solvers in Stan. Chapter 48 of the Stan manual talks about how to do that. @Bob_Carpenter’s Lotka-Volterra case study is also useful for getting the hang of fitting ODE models.

Basically in your ode function takes in a parameter t that you’ll call in the body of the function setting

theta = theta_0 * exp(m*(t-d))

The uknown parameters to be estimated will have to be passed in through the theta parameter of that function.

Hi Arya, Thanks for your response. May be I was not very clear in formulating this question.

In my equation both ‘t’ and ‘d’ are variables and are paired.
I know how to solve the ODEs using solvers in stan. I am not just sure how to feed the information for t and d simultaneously to the ode solver such that for each time point t[i], \theta(t[i] -d[i]) is used as the source in the solution for y[t]. Hope this is clear.

As @arya points out, this is a delay equation and not a standard ODE, which means that it cannot be immediately fit using Stan’s standard ODE solver. If you can analytically solve for one of the states then you can manipulate the delay equation into an ordinary differential equation and implement the model in Stan but otherwise it’s not currently feasible.

1 Like

Hi @arya and @betanalpha I was able to hack the Stan ode solver to solve y[i] at each time t[i] with a delay d[i].

I did NOT solve the ODE analytically. Simple modification in stan code. May be the way I posed the problem made it confusing. Sorry for that.

If still curious have a look at attached files.

trial_ode_expose.R (2.2 KB)
Trial_ode_expose.stan (1.2 KB)