Virtual Population: Stochastic Optimization
PumasQSP provides the methodological framework to create and use Virtual Populations (VP) in Julia. A schematical overview of a potential use case of VP generation is presented in the figure below.
To create VPs with PumasQSP, three main ingredients are needed:
- the QSP model, i.e. the differential equations which describe the system
- the clinical trial data associated with different instantiations of the system
- some cost specifications
In this example we will showcase how to take these ingredients and generate a virutal population with parallelized stochastic optimization.
Julia environment
For this tutorial we will need the following packages:
Module | Description |
---|---|
PumasQSP | PumasQSP is used to formulate the QSP workflow. |
ModelingToolkit | The symbolic modeling environment |
DifferentialEquations | The numerical differential equation solvers |
DataSets | We will load our experimental data from datasets on JuliaHub. |
DataFrames | Handling tabular data manipulation |
Plots and StatsPlots | The plotting and visualization libraries used for this tutorial. |
First we prepare the environment by listing the packages we are using within the example.
# Packages used for this example
using PumasQSP
using ModelingToolkit
using DifferentialEquations
using DataSets
using DataFrames
using Plots
using StatsPlots
Model setup
As the example model we are using the Hires model with eight variables. Their dynamics are described by eight equations and default values are set for the variables and parameters. We use the ModelingTookit.jl
package to specify the model.
# Model states and their default values
@variables begin
t
y1(t) = 1.0
y2(t) = 0.0
y3(t) = 0.0
y4(t) = 0.0
y5(t) = 0.0
y6(t) = 0.0
y7(t) = 0.0
y8(t) = 0.0057
end
# Model parameters and their default values
@parameters begin
k1 = 1.71
k2 = 280.0
k3 = 8.32
k4 = 0.69
k5 = 0.43
k6 = 1.81
end
# Differential operator
D = Differential(t)
# Differential equations
eqs = [
D(y1) ~ (-k1*y1 + k5*y2 + k3*y3 + 0.0007),
D(y2) ~ (k1*y1 - 8.75*y2),
D(y3) ~ (-10.03*y3 + k5*y4 + 0.035*y5),
D(y4) ~ (k3*y2 + k1*y3 - 1.12*y4),
D(y5) ~ (-1.745*y5 + k5*y6 + k5*y7),
D(y6) ~ (-k2*y6*y8 + k4*y4 + k1*y5 - k5*y6 + k4*y7),
D(y7) ~ (k2*y6*y8 - k6*y7),
D(y8) ~ (-k2*y6*y8 + k6*y7)
]
# Model definition
@named model = ODESystem(eqs)
\[ \begin{align} \frac{\mathrm{d} \mathrm{y1}\left( t \right)}{\mathrm{d}t} =& 0.0007 - k1 \mathrm{y1}\left( t \right) + k3 \mathrm{y3}\left( t \right) + k5 \mathrm{y2}\left( t \right) \\ \frac{\mathrm{d} \mathrm{y2}\left( t \right)}{\mathrm{d}t} =& - 8.75 \mathrm{y2}\left( t \right) + k1 \mathrm{y1}\left( t \right) \\ \frac{\mathrm{d} \mathrm{y3}\left( t \right)}{\mathrm{d}t} =& 0.035 \mathrm{y5}\left( t \right) - 10.03 \mathrm{y3}\left( t \right) + k5 \mathrm{y4}\left( t \right) \\ \frac{\mathrm{d} \mathrm{y4}\left( t \right)}{\mathrm{d}t} =& - 1.12 \mathrm{y4}\left( t \right) + k1 \mathrm{y3}\left( t \right) + k3 \mathrm{y2}\left( t \right) \\ \frac{\mathrm{d} \mathrm{y5}\left( t \right)}{\mathrm{d}t} =& - 1.745 \mathrm{y5}\left( t \right) + k5 \mathrm{y7}\left( t \right) + k5 \mathrm{y6}\left( t \right) \\ \frac{\mathrm{d} \mathrm{y6}\left( t \right)}{\mathrm{d}t} =& k1 \mathrm{y5}\left( t \right) + k4 \mathrm{y7}\left( t \right) + k4 \mathrm{y4}\left( t \right) - k5 \mathrm{y6}\left( t \right) - k2 \mathrm{y8}\left( t \right) \mathrm{y6}\left( t \right) \\ \frac{\mathrm{d} \mathrm{y7}\left( t \right)}{\mathrm{d}t} =& - k6 \mathrm{y7}\left( t \right) + k2 \mathrm{y8}\left( t \right) \mathrm{y6}\left( t \right) \\ \frac{\mathrm{d} \mathrm{y8}\left( t \right)}{\mathrm{d}t} =& k6 \mathrm{y7}\left( t \right) - k2 \mathrm{y8}\left( t \right) \mathrm{y6}\left( t \right) \end{align} \]
We are considering two clinical conditions. The first trial computes a steady state which can be used as the initial condition of the second trial. First we read data and define all parameters of the first trial.
# Load data using DataSets packages
data1 = dataset("pumasqsptutorials/hires_trial_1")
trial1 = SteadyStateTrial(data1, model;
params = [k6 => 20.0],
alg = DynamicSS(QNDF()),
abstol = 1e-10,
reltol = 1e-10
)
SteadyStateTrial with parameters:
k6 => 20.0.
The second condition has altered parameters trial2_params
and uses the steady state solution of trial1
as its initial value.
# Load data using DataSets package
data2 = dataset("pumasqsptutorials/hires_trial_2")
# Clinical condition 2
trial2_params = [k3 => 4.0, k4 => 0.4, k6 => 8.0]
trial2_tspan = (0.0, 20.0)
# Create the Trial object for trial 2
trial2 = Trial(data2, model;
forward_u0 = true,
params = trial2_params,
tspan = trial2_tspan,
abstol = 1e-10,
reltol = 1e-10
)
Trial with parameters:
k3 => 4.0
k4 => 0.4
k6 => 8.0
and initial conditions obtained from another trial.
timespan: (0.0, 20.0)
Analysis
The cost function associated to the virtual population optimization problem is specified using the PumasQSP object InverseProblem
. Next to the model and the trials, the user needs to specify a search space. This is to constrain the optimizer in which values to consider as potential values for the parameter combinations.
# Define a search space
example_ss = [k1 => (1.0, 3.0),
k2 => (200.0, 400.0),
y1 => (0.0, 3.0)
]
# Create sequence of Steady State Trials
trials = SteadyStateTrials(trial1, trial2)
# Create a InverseProblem object
prob = InverseProblem(trials, model, example_ss)
InverseProblem for model with 2 trials optimizing 3 parameters and states.
Once the QSP model and the trials are defined, the package can generate plausible patients, i.e. find parameter combinations for the provided model, which describe the trial data sufficiently well. In each optimization step, a new parameter combination is selected, the model is simulated (i.e. the ODE problem is solved) and the ODE solution is compared to the trial data to assess its fit.
# Generate potential population
vp = vpop(prob,
StochGlobalOpt(maxiters = 750);
population_size = 10
)
Virtual population of size 10 computed in 1 minute and 30 seconds.
┌─────────────┬─────────┬─────────┐
│ y1(t) │ k1 │ k2 │
├─────────────┼─────────┼─────────┤
│ 0.000646902 │ 1.73366 │ 323.008 │
│ 0.000761034 │ 1.9386 │ 291.955 │
│ 0.000690683 │ 2.17294 │ 306.44 │
│ 0.000577928 │ 1.58192 │ 274.337 │
│ 0.000629252 │ 1.6703 │ 274.187 │
│ 0.000496333 │ 1.81874 │ 305.213 │
│ 0.000614264 │ 1.76393 │ 335.703 │
│ 0.000702894 │ 1.80694 │ 214.53 │
│ 0.000875135 │ 1.71845 │ 287.06 │
│ 0.00080012 │ 2.43613 │ 370.642 │
└─────────────┴─────────┴─────────┘
Visualization
We can conveniently analyze and visualize the results by relying on the ploting recipes for PumasQSP.
# Viusalization (1/2): Simulation results for Condition 2
plot(vp, trials[2], legend = :outerright, grid = "off")
# Viusalization (2/2): Parameters
params = DataFrame(vp)[!,"y1(t)"]
plt = violin(params, grid = :off, legend = :outerright, label = "VP", ylabel = "y1", color = "purple")
Simulating Virtual Trials
Once we have created and validated our VP, we can use it to explore the system for new clinical conditions. For this, we can use the virtual_trial
function.
# Creating virtual trial
vt = virtual_trial(trials[2], model;
u0 = [y3 =>2.1],
params = [k1 => 1.9],
tspan = (0, 100))
# Visualize simulation results
plot(vp, vt, legend = :outerright, grid = "off")