# Solving an SDE with time-dependent parameter in R

I am trying to solve a system of SDEs in R using the Diffeqr package.

Let's reduce the system to a simple ODE:

# Packages
library(tidyverse)
library(diffeqr)
library(JuliaCall)
diffeq_setup()

# Drift function
f <- function(u,p,t){
du1 = p
return(c(du1))
}

# Diffusion function
g <- function(u,p,t){
du1 = 0 # note that there is currenlty no noise
}

u0 = 1
tspan <- list(0.0, 100)
p = 1
sol = sde.solve(f, g, u0, seed = 1, tspan, p=p, saveat=0.05)
udf=as.data.frame(sol$$u) udf <- udf %>% rownames_to_column(var = "time") udf <- udf %>% rename(y=sol$$u)
plot(udf$$time, udf$$y, type = "l", xlim = c(0,400))


I was wondering if it is possible to alter the parameters time-dependently? I tried to replace p=1 with p=c( c(1,2,3,4,5) ) (as an example), but that doesn't work.

Or are there other solutions to solve systems of SDEs with time-dependent parameters in R?

• If you pass a function as the parameter, you should be able to call it as p(t). – Chris Rackauckas Oct 24 at 14:45
• Hehe it works! I was thinking it would work. The fact that it works wasn't directly coded into the package, but it's really the result of JuliaCall being awesome! – Chris Rackauckas Oct 25 at 7:24

The parameter can be any type, so here I pass in a time-dependent function for p and use it in the differential equation:

# Packages
library(tidyverse)
library(diffeqr)
library(JuliaCall)
diffeq_setup()

# Drift function
f <- function(u,p,t){
du1 = p(t)
return(c(du1))
}

# Diffusion function
g <- function(u,p,t){
du1 = 0 # note that there is currenlty no noise
}

u0 = 1
tspan <- list(0.0, 100)
p <- function(t){
t
}
sol = sde.solve(f, g, u0, tspan, p=p, saveat=0.05)
udf=as.data.frame(sol$$u) udf <- udf %>% rownames_to_column(var = "time") udf <- udf %>% rename(y=sol$$u)
plot(udf$$time, udf$$y, type = "l", xlim = c(0,400))

• Nice! I guess it will also work to add the function itself inside a vector of some independent variables and a time-dependent variable? Or multiple time-dependent variables in different functions? Or would you, in that case, need one function that returns the complete parameter vector? – user213544 Oct 25 at 7:51
• I think a vector of functions (or other things) should just work. – Chris Rackauckas Oct 25 at 8:22