Algorithm Theory: Neural ODE for Cellular Dynamics
Zaoqu Liu
2026-01-26
Source:vignettes/algorithm-theory.Rmd
algorithm-theory.RmdIntroduction
CellODE implements a deep generative model that combines Variational Autoencoders (VAE) with Neural Ordinary Differential Equations (Neural ODE) to infer continuous cellular dynamics from single-cell RNA sequencing data.
This vignette provides a detailed explanation of the mathematical foundations and algorithmic principles underlying CellODE.
Mathematical Framework
Problem Formulation
Given a single-cell gene expression matrix where is the number of cells and is the number of genes, we aim to:
- Infer a pseudotime for each cell
- Learn a latent representation capturing cellular state
- Model the continuous dynamics in latent space
Model Architecture
The TNODE (Time Neural ODE) model consists of three main components:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CellODE Model β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββ β
β β Input β X β β^(NΓG) β
β β Expression β β
β ββββββββ¬βββββββ β
β β β
β βΌ β
β βββββββββββββββββββββββββββββββββββββββββββ β
β β Encoder Network β β
β β q(t, z | x) = q(t|x) Β· q(z|x) β β
β βββββββββββββββββββ¬ββββββββββββββββββββββββ β
β β β
β βββββββββββ΄ββββββββββ β
β βΌ βΌ β
β ββββββββββββββ ββββββββββββββ β
β β Time t β β Latent z β β
β β (sigmoid) β β (ΞΌ, ΟΒ²) β β
β βββββββ¬βββββββ ββββββββ¬ββββββ β
β β β β
β ββββββββββββββ¬ββββββββ β
β βΌ β
β βββββββββββββββββββββββββββββββββββββββββββ β
β β Neural ODE Solver β β
β β dz/dt = f_ΞΈ(z, t) β β
β β β β
β β z(tβ) = z(tβ) + β«_{tβ}^{tβ} f_ΞΈ(z,t)dt β β
β βββββββββββββββββββ¬ββββββββββββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββββββββββββββββββββββββββββββ β
β β Decoder Network β β
β β p(x | z) ~ NB(ΞΌ(z), ΞΈ) β β
β βββββββββββββββββββββββββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Encoder Network
The encoder maps gene expression to time and latent space:
Neural ODE
Continuous Dynamics
The core innovation is modeling latent dynamics as a continuous-time process:
where is a neural network parameterized by .
Integration
Given initial state at time , the state at any time is:
CellODE uses the Euler method for numerical integration:
ODE Function Architecture
The latent ODE function is implemented as a simple MLP:
Input: z β β^d
β
βΌ
βββββββββββββββ
β Linear(dβh) β
βββββββ¬ββββββββ
β
βΌ
βββββββββββββββ
β ELU β
βββββββ¬ββββββββ
β
βΌ
βββββββββββββββ
β Linear(hβd) β
βββββββββββββββ
β
βΌ
Output: dz/dt β β^d
Decoder Network
Time Direction Determination
The model may learn pseudotime in either direction. CellODE automatically determines the correct direction using the correlation between inferred time and number of detected genes:
If , the time is reversed since more mature cells typically have fewer detected genes.
Demonstration
Letβs visualize how the Neural ODE models dynamics:
library(torch)
# Create a simple ODE function
ode_func <- torch::nn_module(
initialize = function() {
self$fc1 <- torch::nn_linear(2, 32)
self$fc2 <- torch::nn_linear(32, 2)
},
forward = function(t, z) {
out <- torch::nnf_elu(self$fc1(z))
self$fc2(out)
}
)
# Initialize
func <- ode_func()
torch::torch_manual_seed(42)
# Create initial points on a circle
n_points <- 20
theta <- seq(0, 2*pi, length.out = n_points + 1)[1:n_points]
z0 <- torch::torch_stack(list(
torch::torch_tensor(0.5 * cos(theta)),
torch::torch_tensor(0.5 * sin(theta))
), dim = 2)
# Time points
t <- torch::torch_linspace(0, 1, 20)
# Simple Euler integration
func$eval()
trajectories <- list()
torch::with_no_grad({
for (i in 1:n_points) {
z <- z0[i, ]
traj <- matrix(0, nrow = 20, ncol = 2)
traj[1, ] <- as.numeric(z)
for (j in 2:20) {
dt <- (t[j] - t[j-1])$item()
dz <- func(t[j-1], z)
z <- z + dt * dz
traj[j, ] <- as.numeric(z)
}
trajectories[[i]] <- traj
}
})
# Plot trajectories
plot(NULL, xlim = c(-1.5, 1.5), ylim = c(-1.5, 1.5),
xlab = "z1", ylab = "z2", main = "Neural ODE Trajectories")
colors <- rainbow(n_points)
for (i in 1:n_points) {
lines(trajectories[[i]], col = colors[i], lwd = 1.5)
points(trajectories[[i]][1, 1], trajectories[[i]][1, 2],
pch = 19, col = colors[i], cex = 1)
points(trajectories[[i]][20, 1], trajectories[[i]][20, 2],
pch = 17, col = colors[i], cex = 1)
}
legend("topright", legend = c("Start", "End"), pch = c(19, 17), bty = "n")
Key Innovations
1. Automatic Time Inference
Unlike methods requiring specification of root cells, CellODE infers pseudotime directly from the data through the encoder network.
References
Li, S. et al.Β (2023). scTour: a deep learning architecture for robust inference and accurate prediction of cellular dynamics. Genome Biology, 24, 149.
Chen, R.T.Q. et al.Β (2018). Neural Ordinary Differential Equations. NeurIPS.
Kingma, D.P. & Welling, M. (2014). Auto-Encoding Variational Bayes. ICLR.
Lopez, R. et al.Β (2018). Deep generative modeling for single-cell transcriptomics. Nature Methods, 15, 1053-1058.