# BYOM, byom_bioconc_start.m, a quick example

BYOM is a General framework for simulating model systems in terms of ordinary differential equations (ODEs). The model itself needs to be specified in derivatives.m, and call_deri.m may need to be modified to the particular problem as well. The files in the engine directory are needed for fitting and plotting. Results are shown on screen but also saved to a log file (results.out).

The model: An organism is exposed to a chemical in its surrounding medium. The animal accumulates the chemical according to standard one-compartment first-order kinetics, specified by a bioconcentration factor (Piw) and an elimination rate (ke). The chemical degrades at a certain rate (kd). When the external concentration reaches a certain concentration (Ct), degradation stops. This is useful to demonstrate the events function in call_deri.m, which catches this discontuity graciously.

This script: byom_bioconc_start provides a quick and clean file for your analyses. For a more detailed example, with more explanation and more options, consult byom_bioconc_extra.m or download the BYOM manual from here.

## Initial things

Make sure that this script is in a directory somewhere below the BYOM folder.

clear, clear global % clear the workspace and globals
global DATA W X0mat % make the data set and initial states global variables
global glo          % allow for global parameters in structure glo
global pri zvd      % global structures for optional priors and zero-variate data
diary off           % turn of the diary function (if it is accidentaly on)
% set(0,'DefaultFigureWindowStyle','docked'); % collect all figure into one window with tab controls
set(0,'DefaultFigureWindowStyle','normal'); % separate figure windows

pathdefine % set path to the BYOM/engine directory
glo.basenm  = mfilename; % remember the filename for THIS file for the plots
glo.saveplt = 0; % save all plots as (1) Matlab figures, (2) JPEG file or (3) PDF (see all_options.txt)


## The data set

Data are entered in matrix form, time in rows, scenarios (exposure concentrations) in columns. First column are the exposure times, first row are the concentrations or scenario numbers. The number in the top left of the matrix indicates how to calculate the likelihood:

• -1 for multinomial likelihood (for survival data)
• 0 for log-transform the data, then normal likelihood
• 0.5 for square-root transform the data, then normal likelihood
• 1 for no transformation of the data, then normal likelihood
% observed external concentrations (two series per concentration)
DATA{1} = [ 1  10 10 20 20 30 30
0  11 12 20 19 28 29
10  6  8 13 12 20 18
20  4  5  9  7 10 12
30  5  7  6  4  6  7
40  4  5  6  5  4  3];

% weight factors (number of replicates per observation in DATA{1})
W{1} = [10 10 10 10 10 10
10 10 10 10 10 10
8  8  8  8  8  9
8  7  7  8  8  9
6  6  6  6  6  7];

% observed internal concentrations (two series per concentration)
DATA{2} = [0.5   10  10   20   20   50   50
0    0   0    0    0    0    0
10 570 600 1150 1100 2500 3000
20 610 650 1300 1210 2700 2800
25 590 620  960  900 2300 2200
40 580 560  920  650 1200 1700];

% if weight factors are not specified, ones are assumed in start_calc.m


## Initial values for the state variables

Initial states, scenarios in columns, states in rows. First row are the 'names' of all scenarios.

X0mat = [10 20 30 50    % the scenarios (here nominal concentrations)
9 18 27 47    % initial values state 1 (actual external concentrations)
0  0  0  0];  % initial values state 2 (internal concentrations)


## Initial values for the model parameters

Model parameters are part of a 'structure' for easy reference.

% syntax: par.name = [startvalue fit(0/1) minval maxval];
par.kd    = [0.1  1 0 100];  % degradation rate constant, d-1
par.ke    = [0.2  1 0 100];  % elimination rate constant, d-1
par.Piw   = [200  1 0 1e6];  % bioconcentration factor, L/kg
par.Ct    = [5    0 0 1e6];  % threshold external concentration where degradation stops, mg/L


## Time vector and labels for plots

Specify what to plot. If time vector glo.t is not specified, a default is used, based on the data set

% specify the y-axis labels for each state variable
glo.ylab{1} = 'external concentration (mg/L)';
glo.ylab{2} = 'internal concentration (mg/kg)';
% specify the x-axis label (same for all states)
glo.xlab    = 'time (days)';
glo.leglab1 = 'conc. '; % legend label before the 'scenario' number
glo.leglab2 = 'mg/L';   % legend label after the 'scenario' number

prelim_checks % script to perform some preliminary checks and set things up
% Note: prelim_checks also fills all the options (opt_...) with defauls, so
% modify options after this call, if needed.


## Calculations and plotting

Here, the functions are called that will do the calculation and the plotting.

Options for the plotting can be set using opt_plot (see prelim_checks.m). Options for the optimsation routine can be set using opt_optim. Options for the ODE solver are part of the global glo. For the demo, the iterations are turned off (opt_optim.it = 0).

opt_optim.fit = 1; % fit the parameters (1), or don't (0)
opt_optim.it  = 0; % show iterations of the optimisation (1, default) or not (0)

% optimise and plot (fitted parameters in par_out)
par_out = calc_optim(par,opt_optim); % start the optimisation
calc_and_plot(par_out,opt_plot); % calculate model lines and plot them

Provisional goodness-of-fit measures for each data set
0.9612    0.9892

=================================================================================
Results of the parameter estimation with BYOM version 4.0
=================================================================================
Filename      : byom_bioconc_start
Analysis date : 27-Apr-2017 (11:51)
Data entered  :
data state 1: 5x6, continuous data, no transf.
data state 2: 5x6, continuous data, power 0.50 transf.
Search method: Nelder-Mead simplex direct search, 2 rounds.
The optimisation routine has converged to a solution
Total 195 simplex iterations used to optimise.
Minus log-likelihood has reached the value 223.307 (AIC=452.614).
=================================================================================
kd        0.04381 (fit: 1, initial: 0.1)
ke         0.1099 (fit: 1, initial: 0.2)
Piw         116.9 (fit: 1, initial: 200)
Ct              5 (fit: 0, initial:  5)
=================================================================================
Time required: 3.8 secs
Plots result from the optimised parameter values.


## Profiling the likelihood

By profiling you make robust confidence intervals for one or more of your parameters. Use the name of the parameter as it occurs in your parameter structure par above. You do not need to run the entire script before you can make a profile.

Note: for more post-calculations, see byom_bioconc_extra.m

Options for profiling can be set using opt_prof (see prelim_checks.m).

% run a profile for selected parameters ...
calc_proflik(par_out,'kd',opt_prof); % uncomment to calculate a profile
calc_proflik(par_out,'Piw',opt_prof); % uncomment to calculate a profile

% UNCOMMENT LINE(S) TO CALCULATE
% % or run profiles for all fitted parameters.
% all_profiles(par_out,opt_prof);


95% confidence interval from the profile
=================================================================================
kd         interval:    0.04027 - 0.04814

Time required: 28.8 secs

95% confidence interval from the profile
=================================================================================
Piw        interval:      109.1 - 125.7

Time required: 43.2 secs


## Other files: derivatives

To archive analyses, publishing them with Matlab is convenient. To keep track of what was done, the file derivatives.m can be included in the published result.

%% BYOM function derivatives.m (the model in ODEs)
%
%  Syntax: dX = derivatives(t,X,par,c)
%
% This function calculates the derivatives for the model system. It is
% linked to the script files <byom_bioconc_extra.html byom_bioconc_extra.m>
% and <byom_bioconc_start.html byom_bioconc_start.m>. As input,
% it gets:
%
% * _t_   is the time point, provided by the ODE solver
% * _X_   is a vector with the previous value of the states
% * _par_ is the parameter structure
% * _c_   is the external concentration (or scenario number)
%
% Time _t_ and scenario name _c_ are handed over as single numbers by
% <call_deri.html call_deri.m> (you do not have to use them in this
% function). Output _dX_ (as vector) provides the differentials for each
% state at _t_.
%
% * Author: Tjalling Jager
% * Date: April 2017
% * Web support: <http://www.debtox.info/byom.html>
% * Back to index <walkthrough_byom.html>

%% Start

function dX = derivatives(t,X,par,c)

global glo   % allow for global parameters in structure glo (handy for switches)

%% Unpack states
% The state variables enter this function in the vector _X_. Here, we give
% them a more handy name.

Cw = X(1); % state 1 is the external concentration at previous time point
Ci = X(2); % state 2 is the internal concentration at previous time point

%% Unpack parameters
% The parameters enter this function in the structure _par_. The names in
% the structure are the same as those defined in the byom script file.
% The 1 between parentheses is needed as each parameter has 5 associated
% values.

kd   = par.kd(1);   % degradation rate constant, d-1
ke   = par.ke(1);   % elimination rate constant, d-1
Piw  = par.Piw(1);  % bioconcentration factor, L/kg
Ct   = par.Ct(1);   % threshold external concentration that stops degradation, mg/L

% % Optionally, include the threshold concentration as a global (see script)
% Ct   = glo.Ct;      % threshold external concentration that stops degradation, mg/L

%% Calculate the derivatives
% This is the actual model, specified as a system of two ODEs:
%
% $$\frac{d}{dt}C_w=-k_d C_w \quad \textrm{as long as } C_w>C_t$$
%
% $$\frac{d}{dt}C_i=k_e(P_{iw}C_w-C_i)$$

if Cw > Ct % if we are above the critical internal concentration ...
dCw = -kd * Cw; % let the external concentration degrade
else           % otherwise ...
dCw = 0; % make the change in external concentration zero
end

dCi = ke * (Piw*Cw-Ci); % first order bioconcentration

dX = [dCw;dCi]; % collect both derivatives in one vector dX



## Other files: call_deri

To archive analyses, publishing them with Matlab is convenient. To keep track of what was done, the file call_deri.m can be included in the published result.

%% BYOM function call_deri.m (calculates the model output)
%
%  Syntax: [Xout TE] = call_deri(t,par,X0v)
%
% This function calls the ODE solver to solve the system of differential
% equations specified in <derivatives.html derivatives.m>, or the explicit
% function(s) in <simplefun.html simplefun.m>. As input, it gets:
%
% * _t_   the time vector
% * _par_ the parameter structure
% * _X0v_   a vector with initial states and one concentration (scenario number)
%
% The output _Xout_ provides a matrix with time in rows, and states in
% columns. This function calls <derivatives.html derivatives.m>. The
% optional output _TE_ is the time at which an event takes place (specified
% using the events function). The events function is set up to catch
% discontinuities. It should be specified according to the problem you are
% simulating. If you want to use parameters that are (or influence) initial
% states, they have to be included in this function.
%
% * Author: Tjalling Jager
% * Date: April 2017
% * Web support: <http://www.debtox.info/byom.html>
% * Back to index <walkthrough_byom.html>

%% Start

function [Xout, TE] = call_deri(t,par,X0v)

global glo   % allow for global parameters in structure glo
global zvd   % global structure for zero-variate data

%% Initial settings
% This part extracts optional settings for the ODE solver that can be set
% in the main script (defaults are set in prelim_checks). The useode option
% decides whether to calculate the model results using the ODEs in
% <derivatives.html derivatives.m>, or the analytical solution in
% <simplefun.html simplefun.m>. Using eventson=1 turns on the events
% handling. Also modify the sub-function at the bottom of this function!
% Further in this section, initial values can be determined by a parameter
% (overwrite parts of X0), and zero-variate data can be calculated. See the

useode   = glo.useode; % calculate model using ODE solver (1) or analytical solution (0)
eventson = glo.eventson; % events function on (1) or off (0)
stiff    = glo.stiff; % set to 1 to use a stiff solver instead of the standard one

% Unpack the vector X0v, which is X0mat for one scenario
X0 = X0v(2:end); % these are the intitial states for a scenario
% % if needed, extract parameters from par that influence initial states in X0
% Ci0   = par.Ci0(1); % example: parameter for the internal concentration
% X0(2) = Ci0; % put this parameter in the correct location of the initial vector

% if needed, calculate model values for zero-variate data from parameter set
if ~isempty(zvd)
zvd.ku(3) = par.Piw(1) * par.ke(1); % add model prediction as third value
end

%% Calculations
% This part calls the ODE solver (or the explicit model in <simplefun.html
% simplefun.m>) to calculate the output (the value of the state variables
% over time). There is generally no need to modify this part. The solver
% ode45 generally works well. For stiff problems, the solver might become
% very slow; you can try ode15s instead.

c  = X0v(1);     % the concentration (or scenario number)
t  = t(:);       % force t to be a row vector (needed when useode=0)

% specify options for the ODE solver
if eventson == 1
options = odeset(options, 'Events',@eventsfun); % add an events function
end
options = odeset(options, 'RelTol',1e-4,'AbsTol',1e-7); % specify tightened tolerances
% options = odeset(options,'InitialStep',max(t)/1000,'MaxStep',max(t)/100); % specify smaller stepsize

TE = 0; % dummy for time of events

if useode == 1 % use the ODE solver to calculate the solution
% call the ODE solver (use ode15s for stiff problems, escpecially for pulses)
if isempty(options.Events) % if no events function is specified ...
switch stiff
case 0
[~,Xout] = ode45(@derivatives,t,X0,options,par,c);
case 1
[~,Xout] = ode113(@derivatives,t,X0,options,par,c);
case 2
[~,Xout] = ode15s(@derivatives,t,X0,options,par,c);
end
else % with an events functions ... additional output arguments for events:
% TE catches the time of an event, YE the states at the event, and IE the number of the event
switch stiff
case 0
[~,Xout,TE,YE,IE] = ode45(@derivatives,t,X0,options,par,c);
case 1
[~,Xout,TE,YE,IE] = ode113(@derivatives,t,X0,options,par,c);
case 2
[~,Xout,TE,YE,IE] = ode15s(@derivatives,t,X0,options,par,c);
end
end
else % alternatively, use an explicit function provided in simplefun!
Xout = simplefun(t,X0,par,c);
end

if isempty(TE) || all(TE == 0) % if there is no event caught
TE = +inf; % return infinity
end

%% Output mapping
% _Xout_ contains a row for each state variable. It can be mapped to the
% data. If you need to transform the model values to match the data, do it
% here.

% Xout(:,1) = Xout(:,1).^3; % e.g., do something on first column, like cube it ...

% % To obtain the output of the derivatives at each time point. The values in
% % dXout might be used to replace values in Xout, if the data to be fitted
% % are the changes (rates) instead of the state variable itself.
% % dXout = zeros(size(Xout)); % initialise with zeros
% for i = 1:length(t) % run through all time points
%     dXout(i,:) = derivatives(t(i),Xout(i,:),par,c);
%     % derivatives for each stage at each time
% end

%% Events function
% Modify this part of the code if _eventson_=1.
% This subfunction catches the 'events': in this case, it looks for the
% external concentration where degradation stops. This function should be
% adapted to the problem you are modelling (this one matches the
% byom_bioconc_... files). You can catch more events by making a vector out
% of _values_.
%
% Note that the eventsfun has the same inputs, in the same sequence, as
% <derivatives.html derivatives.m>.

function [value,isterminal,direction] = eventsfun(t,X,par,c)

Ct      = par.Ct(1); % threshold external concentration where degradation stops
nevents = 1;         % number of events that we try to catch

value       = zeros(nevents,1); % initialise with zeros
value(1)    = X(1) - Ct;        % thing to follow is external concentration (state 1) minus threshold
isterminal  = zeros(nevents,1); % do NOT stop the solver at an event
direction   = zeros(nevents,1); % catch ALL zero crossing when function is increasing or decreasing