| Title: | Monte Carlo Solution of First Order Differential Equations |
|---|---|
| Description: | Functions for simulating the solution of initial value problems of the form g'(x) = G(x, g) with g(x0) = g0. One is an acceptance-rejection method. The others are based on the Mean Value Theorem, with options for antithetic variates, replicated solutions and confidence intervals, and systems of equations. |
| Authors: | W.J. Braun [aut, cre] |
| Maintainer: | W.J. Braun <[email protected]> |
| License: | GPL (>= 2) |
| Version: | 1.2 |
| Built: | 2026-11-22 01:11:40 UTC |
| Source: | https://github.com/cran/mcODE |
Monte Carlo solution of first order differential equations and confidence intervals for the solutions.
ODE.MVT can solve any first order differential equation
of the form g' = F(x, g) with initial condition g(x0) = g0, provided conditions are
satisfied for existence and uniqueness of the solution. The method is based on the mean
value theorem (MVT). ODE.MVT.anti adds antithetic variates, which reduce the
random error by several orders of magnitude for non-stiff problems.
ODE.MVT.sys solves systems of equations, such as the Lotka-Volterra and SIR
models. Confidence intervals for the solution can be obtained through use of
ODE.MVT.agg. An alternate method due to Akhtar et al is implemented in
ODE.ADA.
Author: W.J. Braun
Maintainer: W.J. Braun
Akhtar, M. N., Durad, M. H., and Ahmed, A. (2015). Solving initial value ordinary differential equations by Monte Carlo method. Proc. IAM, 4:149-174.
Braun, W. J. (2026). Monte Carlo integration of initial value problems. Preprint.
deSolve
# Solve g' = F(x, g) on (0, 1] with g(0) = 0 G <- function(x, g) { -1000*g + 3000 - 2000*exp(-x) } T <- 1 x0 <- 0 g0 <- 0 nMVT <- 5000 ghat <- ODE.MVT(G, initvalue = g0, endpoint = T, initpoint = x0, Niter = 2, npoints = nMVT) plot(ghat, type = "l") # exact solution curve(3 - (997/999)*exp(-1000*x) - (2000/999)*exp(-x), add = TRUE, col = 2, lty = 2)# Solve g' = F(x, g) on (0, 1] with g(0) = 0 G <- function(x, g) { -1000*g + 3000 - 2000*exp(-x) } T <- 1 x0 <- 0 g0 <- 0 nMVT <- 5000 ghat <- ODE.MVT(G, initvalue = g0, endpoint = T, initpoint = x0, Niter = 2, npoints = nMVT) plot(ghat, type = "l") # exact solution curve(3 - (997/999)*exp(-1000*x) - (2000/999)*exp(-x), add = TRUE, col = 2, lty = 2)
Given g' = G(x, g) and g(x0) = g0 satisfying conditions for existence and uniqueness of solution, a Monte Carlo approximation to the solution is found. The method is essentially a rejection method.
ODE.ADA(G, initvalue, endpoint, X0 = 0, npoints = 1000, M, R, N = 1e+05)ODE.ADA(G, initvalue, endpoint, X0 = 0, npoints = 1000, M, R, N = 1e+05)
G |
a function having two numeric vector arguments. |
initvalue |
a numeric initial value |
endpoint |
a numeric interval endpoint value. |
X0 |
a numeric interval starting point value. |
npoints |
an integer value specifying the number of equally spaced mesh points
(that is, |
M |
a numeric value specifying an upper bound for G(x, g); it must be at least the maximum of G along the solution. |
R |
a non-negative numeric value; |
N |
an integer value specifying the number of uniform random variates. A single
set of |
A list consisting of
x |
a vector of length npoints, consisting of the x-coordinate of the solution. |
y |
a vector of length npoints, consisting of the y-coordinate of the solution, i.e. g(x). |
Akhtar, M. N., Durad, M. H., and Ahmed, A. (2015). Solving initial value ordinary differential equations by Monte Carlo method. Proc. IAM, 4:149-174.
G <- function(x, g) { -1000*g + sin(x) } gTrue <- function(x) (1000*sin(x) - cos(x))/1000001 xF <- 1; x0 <- 0 g0 <- -1/1000001 # initial value NAkhtar <- 800 nAkhtar <- 2500 MAkhtar <- 1e-3 RAkhtar <- 1e-6 gAkhtar <- ODE.ADA(G, initvalue = g0, endpoint = xF, X0 = x0, npoints = nAkhtar, M=MAkhtar, R = RAkhtar, N = NAkhtar) plot(gAkhtar, type = "l")G <- function(x, g) { -1000*g + sin(x) } gTrue <- function(x) (1000*sin(x) - cos(x))/1000001 xF <- 1; x0 <- 0 g0 <- -1/1000001 # initial value NAkhtar <- 800 nAkhtar <- 2500 MAkhtar <- 1e-3 RAkhtar <- 1e-6 gAkhtar <- ODE.ADA(G, initvalue = g0, endpoint = xF, X0 = x0, npoints = nAkhtar, M=MAkhtar, R = RAkhtar, N = NAkhtar) plot(gAkhtar, type = "l")
Given g' = G(x, g) and g(x0) = g0 satisfying conditions for existence and uniqueness of solution, a Monte Carlo approximation to the solution is found. The method is essentially a Monte Carlo version of Euler and Runge-Kutta type methods.
ODE.MVT(G, initvalue, endpoint, initpoint = 0, Niter = 2, npoints = 1000)ODE.MVT(G, initvalue, endpoint, initpoint = 0, Niter = 2, npoints = 1000)
G |
a function of two numeric arguments, |
initvalue |
a numeric initial value |
endpoint |
a numeric interval endpoint value. |
initpoint |
a numeric interval starting point value. |
Niter |
an integer value |
npoints |
an integer value specifying the number of equally spaced mesh points
(that is, |
If is uniformly distributed on , then
is an unbiased estimator of . The unknown
is approximated by applying the same argument recursively, with
= Niter nested uniform variates, starting from the crude prediction
. The predictor is applied on each subinterval of an equally spaced mesh with
spacing , using a single realization per subinterval.
The bias per subinterval is of order , with leading term
(where is the partial derivative of
with respect to ), so the accumulated bias over a fixed interval is of order
. The bias is driven by : for stiff problems, where this
is not small, more iterations or a finer mesh are needed. The accumulated random error is
of order , regardless of ; for non-stiff problems, 2 or 3
iterations are therefore usually sufficient. The random error can be reduced with
antithetic variates (ODE.MVT.anti), and replicated solutions give confidence
bands (ODE.MVT.agg).
A list consisting of
x |
a vector of length npoints, consisting of the x-coordinate of the solution. |
y |
a vector of length npoints, consisting of the y-coordinate of the solution, i.e. g(x). |
Braun, W.J.
Braun, W.J. (2026). Monte Carlo integration of initial value problems. Preprint.
ODE.MVT.anti, ODE.MVT.agg, ODE.MVT.sys
# Initial Value Problem: G <- function(x, g) { -1000*g + sin(x) } g0 <- -1/1000001; x0 <- 0 # initial condition xF <- 1 # interval endpoint nMVT <- 1000 # number of subintervals used # Monte Carlo solution based on one iteration: ghat1 <- ODE.MVT(G, initvalue = g0, endpoint = xF, initpoint = x0, Niter = 1, npoints = nMVT) # Monte Carlo solution based on five iterations: ghat5 <- ODE.MVT(G, initvalue = g0, endpoint = xF, initpoint = x0, Niter = 5, npoints = nMVT) gTrue <- function(x) (1000*sin(x) - cos(x))/1000001 # true solution oldpar <- par(mfrow=c(1,3)) curve(gTrue(x)) # lines(ghat1, col = 2, lty = 2, ylab="g(x)") plot(abs(gTrue(ghat1$x) - ghat1$y) ~ ghat1$x, xlab = "x", ylab = "Absolute Error", type = "l", ylim = c(0, 2e-6), main="1 Iteration") plot(abs(gTrue(ghat5$x) - ghat5$y) ~ ghat5$x, xlab = "x", ylab = "Absolute Error", type = "l", ylim = c(0, 2e-6), main="5 Iterations") par(oldpar)# Initial Value Problem: G <- function(x, g) { -1000*g + sin(x) } g0 <- -1/1000001; x0 <- 0 # initial condition xF <- 1 # interval endpoint nMVT <- 1000 # number of subintervals used # Monte Carlo solution based on one iteration: ghat1 <- ODE.MVT(G, initvalue = g0, endpoint = xF, initpoint = x0, Niter = 1, npoints = nMVT) # Monte Carlo solution based on five iterations: ghat5 <- ODE.MVT(G, initvalue = g0, endpoint = xF, initpoint = x0, Niter = 5, npoints = nMVT) gTrue <- function(x) (1000*sin(x) - cos(x))/1000001 # true solution oldpar <- par(mfrow=c(1,3)) curve(gTrue(x)) # lines(ghat1, col = 2, lty = 2, ylab="g(x)") plot(abs(gTrue(ghat1$x) - ghat1$y) ~ ghat1$x, xlab = "x", ylab = "Absolute Error", type = "l", ylim = c(0, 2e-6), main="1 Iteration") plot(abs(gTrue(ghat5$x) - ghat5$y) ~ ghat5$x, xlab = "x", ylab = "Absolute Error", type = "l", ylim = c(0, 2e-6), main="5 Iterations") par(oldpar)
Given g' = G(x, g) and g(x0) = g0 satisfying conditions for existence and uniqueness
of solution, a Monte Carlo approximation to the solution is found. The method is
essentially a Monte Carlo version of Euler and Runge-Kutta type methods.
Repeated calls are made to the ODE.MVT function (or ODE.MVT.anti) to obtain
replicated estimates of the solution.
ODE.MVT.agg(G, initvalue, endpoint, initpoint = 0, Niter = 2, npoints = 1000, nsims = 10, antithetic = FALSE)ODE.MVT.agg(G, initvalue, endpoint, initpoint = 0, Niter = 2, npoints = 1000, nsims = 10, antithetic = FALSE)
G |
a function having two numeric vector arguments. |
initvalue |
a numeric initial value |
endpoint |
a numeric interval endpoint value. |
initpoint |
a numeric interval starting point value. |
Niter |
an integer value |
npoints |
an integer value specifying the number of equally spaced mesh points
(that is, |
nsims |
an integer value specifying the number of replicated solutions required. |
antithetic |
logical; if |
Approximate pointwise confidence intervals are given by
y stdev/sqrt(nsims). Strictly, these are confidence
intervals for the expected value of the Monte Carlo solution; they are confidence intervals
for only to the extent that the bias of the solution is negligible compared with
stdev/sqrt(nsims). The more replicates are used, the narrower the intervals, and the
more iterations are needed to keep the bias small relative to their width, especially for
stiff problems.
A list consisting of
x |
a vector of length npoints, consisting of the x-coordinate of the solution. |
y |
a vector of length npoints, consisting of the average of replicated y-coordinates of the solution, i.e. g(x). |
stdev |
a vector of length npoints, consisting of the standard deviation of the
replicated solutions at each point; the standard error of |
W.J. Braun
Braun, W.J. (2026). Monte Carlo integration of initial value problems. Preprint.
G <- function(x, g) { -1000*g + sin(x) } g0 <- -1/1000001; x0 <- 0 # initial condition xF <- 1 # interval endpoint nMVT <- 1000 # number of subintervals used # 10 reps of Monte Carlo solution based on two iterations: ghat2 <- ODE.MVT.agg(G, initvalue = g0, endpoint = xF, initpoint = x0, Niter = 2, npoints = nMVT, nsims = 10) # 10 reps of Monte Carlo solution based on three iterations: ghat3 <- ODE.MVT.agg(G, initvalue = g0, endpoint = xF, initpoint = x0, Niter = 3, npoints = nMVT, nsims = 10) gTrue <- function(x) (1000*sin(x) - cos(x))/1000001 # true solution oldpar <- par(mfrow=c(1,2)) curve(gTrue(x), from = 0.3, to = 0.3015) # graph of solution on small interval lines(ghat2, col = 4, lty = 4, ylab="g(x)", lwd = 2) # estimated MC solution - 2 iterations lines(ghat2$y - 1.96*ghat2$stdev/sqrt(10) ~ ghat2$x, lty = 3, col = 4) # lower conf. limits lines(ghat2$y + 1.96*ghat2$stdev/sqrt(10) ~ ghat2$x, lty = 3, col = 4) # upper conf. limits curve(gTrue(x), from = 0.3, to = 0.3015) # graph of solution on small interval lines(ghat3, col = 4, lty = 4, ylab="g(x)", lwd = 2) # estimated MC solution - 3 iterations lines(ghat3$y - 1.96*ghat3$stdev/sqrt(10) ~ ghat3$x, lty = 3, col = 4) # lower conf. limits lines(ghat3$y + 1.96*ghat3$stdev/sqrt(10) ~ ghat3$x, lty = 3, col = 4) # upper conf. limits par(oldpar)G <- function(x, g) { -1000*g + sin(x) } g0 <- -1/1000001; x0 <- 0 # initial condition xF <- 1 # interval endpoint nMVT <- 1000 # number of subintervals used # 10 reps of Monte Carlo solution based on two iterations: ghat2 <- ODE.MVT.agg(G, initvalue = g0, endpoint = xF, initpoint = x0, Niter = 2, npoints = nMVT, nsims = 10) # 10 reps of Monte Carlo solution based on three iterations: ghat3 <- ODE.MVT.agg(G, initvalue = g0, endpoint = xF, initpoint = x0, Niter = 3, npoints = nMVT, nsims = 10) gTrue <- function(x) (1000*sin(x) - cos(x))/1000001 # true solution oldpar <- par(mfrow=c(1,2)) curve(gTrue(x), from = 0.3, to = 0.3015) # graph of solution on small interval lines(ghat2, col = 4, lty = 4, ylab="g(x)", lwd = 2) # estimated MC solution - 2 iterations lines(ghat2$y - 1.96*ghat2$stdev/sqrt(10) ~ ghat2$x, lty = 3, col = 4) # lower conf. limits lines(ghat2$y + 1.96*ghat2$stdev/sqrt(10) ~ ghat2$x, lty = 3, col = 4) # upper conf. limits curve(gTrue(x), from = 0.3, to = 0.3015) # graph of solution on small interval lines(ghat3, col = 4, lty = 4, ylab="g(x)", lwd = 2) # estimated MC solution - 3 iterations lines(ghat3$y - 1.96*ghat3$stdev/sqrt(10) ~ ghat3$x, lty = 3, col = 4) # lower conf. limits lines(ghat3$y + 1.96*ghat3$stdev/sqrt(10) ~ ghat3$x, lty = 3, col = 4) # upper conf. limits par(oldpar)
Given g' = G(x, g) and g(x0) = g0 satisfying conditions for existence and uniqueness
of solution, a Monte Carlo approximation to the solution is found using the
Niter-step mean value theorem (MVT) predictor of ODE.MVT,
optionally with antithetic variates.
ODE.MVT.anti(G, initvalue, endpoint, initpoint = 0, Niter = 2, npoints = 1000, method = c("anti", "plain", "pair"))ODE.MVT.anti(G, initvalue, endpoint, initpoint = 0, Niter = 2, npoints = 1000, method = c("anti", "plain", "pair"))
G |
a function of two numeric arguments, |
initvalue |
a numeric initial value. |
endpoint |
a numeric interval endpoint value. |
initpoint |
a numeric interval starting point value. |
Niter |
an integer value specifying the number of iterations, i.e. the number
of nested uniform variates and evaluations of |
npoints |
an integer value specifying the number of equally spaced mesh points
(that is, |
method |
|
On each subinterval of length , the nested uniform
variates are written as ,
, where are independent
. The antithetic predictor averages the predictor computed from
with that computed from .
Both predictors have the same distribution, so the antithetic predictor has the same bias
as the plain one, at twice the number of evaluations of G. The leading term of the
random error cancels, so that the variance of each step is reduced from order
to order , and the accumulated random error over a fixed
interval from order to order . Since the accumulated
bias is of order , at least 3 iterations are needed to benefit fully;
for stiff problems more may be required.
A list consisting of
x |
a vector of length npoints, consisting of the x-coordinate of the solution. |
y |
a vector of length npoints, consisting of the y-coordinate of the solution, i.e. g(x). |
Braun, W.J.
Braun, W.J. (2026). Monte Carlo integration of initial value problems. Preprint.
# Riccati equation g' = 1 + x^2 - 2xg + g^2, g(0) = 1, with solution x - 1/(x - 1) G <- function(x, g) 1 + x^2 - 2*x*g + g^2 gTrue <- function(x) x - 1/(x - 1) ghat <- ODE.MVT.anti(G, initvalue = 1, endpoint = 0.5, initpoint = 0, Niter = 3, npoints = 1000, method = "plain") ghatAnti <- ODE.MVT.anti(G, initvalue = 1, endpoint = 0.5, initpoint = 0, Niter = 3, npoints = 1000, method = "anti") plot(ghat$x, log10(abs(ghat$y - gTrue(ghat$x)) + 1e-16), type = "l", ylim = c(-16, 0), xlab = "x", ylab = "log10 absolute error") lines(ghatAnti$x, log10(abs(ghatAnti$y - gTrue(ghatAnti$x)) + 1e-16), col = 2) legend("topleft", legend = c("plain", "antithetic"), col = 1:2, lty = 1, bty = "n")# Riccati equation g' = 1 + x^2 - 2xg + g^2, g(0) = 1, with solution x - 1/(x - 1) G <- function(x, g) 1 + x^2 - 2*x*g + g^2 gTrue <- function(x) x - 1/(x - 1) ghat <- ODE.MVT.anti(G, initvalue = 1, endpoint = 0.5, initpoint = 0, Niter = 3, npoints = 1000, method = "plain") ghatAnti <- ODE.MVT.anti(G, initvalue = 1, endpoint = 0.5, initpoint = 0, Niter = 3, npoints = 1000, method = "anti") plot(ghat$x, log10(abs(ghat$y - gTrue(ghat$x)) + 1e-16), type = "l", ylim = c(-16, 0), xlab = "x", ylab = "log10 absolute error") lines(ghatAnti$x, log10(abs(ghatAnti$y - gTrue(ghatAnti$x)) + 1e-16), col = 2) legend("topleft", legend = c("plain", "antithetic"), col = 1:2, lty = 1, bty = "n")
Given a system g' = G(x, g), where g is a vector, and g(x0) = g0 satisfying conditions
for existence and uniqueness of solution, a Monte Carlo approximation to the solution is
found using the mean value theorem (MVT) predictor of ODE.MVT.
ODE.MVT.sys(G, initvalue, endpoint, initpoint = 0, Niter = 2, npoints = 1000)ODE.MVT.sys(G, initvalue, endpoint, initpoint = 0, Niter = 2, npoints = 1000)
G |
a function of two arguments, a numeric value |
initvalue |
a numeric vector of initial values; its names, if any, are used as column names of the result. |
endpoint |
a numeric interval endpoint value. |
initpoint |
a numeric interval starting point value. |
Niter |
an integer value specifying the number of iterations, i.e. the number
of nested uniform variates and evaluations of |
npoints |
an integer value specifying the number of equally spaced mesh points
(that is, |
The method is that of ODE.MVT, applied without modification to a vector
valued solution: on each subinterval, the same uniform variates are used for all
components, and all components of each intermediate prediction are updated
simultaneously from the same previous estimates. For a single equation, the result is
identical to that of ODE.MVT with the same random number seed.
Higher order equations, and some integro-differential equations, can be solved by first rewriting them as first order systems.
A list consisting of
x |
a vector of length npoints, consisting of the x-coordinate of the solution. |
y |
a matrix with npoints rows and one column for each component of the solution. |
Braun, W.J.
Braun, W.J. (2026). Monte Carlo integration of initial value problems. Preprint.
# Lotka-Volterra predator-prey model LV <- function(t, g) { c(0.6*g[1] - 0.3*g[1]*g[2], -0.4*g[2] + 0.65*g[1]*g[2]) } out <- ODE.MVT.sys(LV, initvalue = c(prey = 2, predator = 1), endpoint = 50, Niter = 2, npoints = 5000) oldpar <- par(mfrow = c(1, 2)) matplot(out$x, out$y, type = "l", lty = 1, xlab = "t", ylab = "population") plot(out$y, type = "l", main = "phase plane") par(oldpar) # SIR epidemic model SIR <- function(t, g, beta = 0.5, gamma = 0.15, N = 1000) { c(-beta*g[1]*g[2]/N, beta*g[1]*g[2]/N - gamma*g[2], gamma*g[2]) } out <- ODE.MVT.sys(SIR, initvalue = c(S = 990, I = 10, R = 0), endpoint = 52) matplot(out$x, out$y, type = "l", lty = 1, xlab = "t", ylab = "number")# Lotka-Volterra predator-prey model LV <- function(t, g) { c(0.6*g[1] - 0.3*g[1]*g[2], -0.4*g[2] + 0.65*g[1]*g[2]) } out <- ODE.MVT.sys(LV, initvalue = c(prey = 2, predator = 1), endpoint = 50, Niter = 2, npoints = 5000) oldpar <- par(mfrow = c(1, 2)) matplot(out$x, out$y, type = "l", lty = 1, xlab = "t", ylab = "population") plot(out$y, type = "l", main = "phase plane") par(oldpar) # SIR epidemic model SIR <- function(t, g, beta = 0.5, gamma = 0.15, N = 1000) { c(-beta*g[1]*g[2]/N, beta*g[1]*g[2]/N - gamma*g[2], gamma*g[2]) } out <- ODE.MVT.sys(SIR, initvalue = c(S = 990, I = 10, R = 0), endpoint = 52) matplot(out$x, out$y, type = "l", lty = 1, xlab = "t", ylab = "number")