%% Generate Different Simulation Scenarios for Glucose-Insulin Response
%
% Load the model of glucose-insulin response. For details about the model, see the Background section in Simulating the Glucose-Insulin Response.

sbioloadproject('insulindemo','m1');

%
% The model contains different parameter values and initial conditions that represents different insulin impairments (such as Type 2 diabetes, low insulin sensitivity, and so on) stored in five variants.

variants = getvariant(m1)

%
% Suppress an informational warning that is issued during simulations.

warnSettings = warning('off','SimBiology:DimAnalysisNotDone_MatlabFcn_Dimensionless');

%
% Select a dose that represents a single meal of 78 grams of glucose.

singleMeal = sbioselect(m1,'Name','Single Meal');

%
% Create a Scenarios object to represent different initial conditions combined with the dose. That is, create a scenario object where each variant is paired (or combined) with the dose, for a total of five simulation scenarios.

sObj = SimBiology.Scenarios;
add(sObj,'cartesian','variants',variants);
add(sObj,'cartesian','dose',singleMeal)

%
% sObj contains two entries. Use the generate function to combine the entries and generate five scenarios. The function returns a scenarios table, where each row represents a scenario and each column represents an entry of the Scenarios object.

scenariosTbl = generate(sObj)

%
% Change the entry name of the first entry.

rename(sObj,1,'Insulin Impairements')

%
% Create a SimFunction object to simulate the generated scenarios. Use the Scenarios object as the input and specify the plasma glucose and insulin concentrations as reponses (outputs of the function to be plotted). Specify [] for the dose input argument since the Scenarios object already has the dosing information.

f = createSimFunction(m1,sObj,{'[Plasma Glu Conc]','[Plasma Ins Conc]'},[])

%
% Simulate the model for 24 hours and plot the simulation data. The data contains five runs, where each run represents a scenario in the Scenarios object.

sd = f(sObj,24);
sbioplot(sd)

%
% If you have Statistics and Machine Learning Toolbox™, you can also draw sample values for model quantities from various probability distributions. For instance, suppose that the parameters Vmx and kp3, which are known for the low and high insulin sensitivity, follow the lognormal distribution. You can generate sample values for these parameters from such a distribution, and perform a scan to explore model behavior.
%
% Define the lognormal probability distribution object for Vmx.

pd_Vmx = makedist('lognormal')

%
% By definition, the parameter mu is the mean of logarithmic values. To vary the parameter value around the base (model) value of the parameter, set mu to log(model_value). Set the standard deviation (sigma) to 0.2. For a small sigma value, the mean of a lognormal distribtion is approximately equal to log(model_value). For details, see Lognormal Distribution.

Vmx = sbioselect(m1,'Name','Vmx');
pd_Vmx.mu = log(Vmx.Value);
pd_Vmx.sigma = 0.2

%
% Similarly define the probability distribution for kp3.

pd_kp3 = makedist('lognormal');
kp3 = sbioselect(m1,'Name','kp3');
pd_kp3.mu = log(kp3.Value);
pd_kp3.sigma = 0.2

%
% Now define a joint probability distribution to draw sample values for Vmx and kp3, with a rank correlation to specify some correlation between these two parameters. Note that this correlation assumption is for the illustration purposes of this example only and may not be biologically relevant.
%
% First remove the variants entry (entry 1) from sObj.

remove(sObj,1)

%
% Add an entry that defines the joint probability distribution with a rank correlation matrix.

add(sObj,'cartesian',["Vmx","kp3"],[pd_Vmx, pd_kp3],'RankCorrelation',[1,0.5;0.5,1])

%
% By default, the number of samples to draw from the joint distribution is set to 2. Increase the number of samples.

updateEntry(sObj,2,'Number',50)

%
% Verify that the Scenarios object can be simulated with the model. The verify function throws an error if any entry does not resolve uniquely to an object in the model or the entry contents have inconsistent lengths (sample sizes). The function throws a warning if multiple entries resolve to the same object in the model.

verify(sObj,m1)

%
% Generate the simulation scenarios. Plot the sample values using plotmatrix. You can see the value of Vmx is varied around its model value 0.047 and that of kp3 around 0.009.

sTbl = generate(sObj);
[s,ax,bigax,h,hax] = plotmatrix([sTbl.Vmx,sTbl.kp3]);
ax(1,1).YLabel.String = "Vmx";
ax(2,1).YLabel.String = "kp3";
ax(2,1).XLabel.String = "Vmx";
ax(2,2).XLabel.String = "kp3";

%
% Simulate the scenarios using the same SimFunction you created previously. You do not need to create a new SimFunction object even though the Scenarios object has been updated.

sd2 = f(sObj,24);
sbioplot(sd2);

%
% By default, SimBiology uses the random sampling method. You can change it to the Latin hypercube sampling (or sobol or halton) for a more systematic space-filling approach.

entry2struct = getEntry(sObj,2)
entry2struct.SamplingMethod = 'lhs'

%
% You can now use the updated structure to modify entry 2.

updateEntry(sObj,2,entry2struct)

%
% Visualize the sample values.

sTbl2 = generate(sObj);
[s,ax,bigax,h,hax] = plotmatrix([sTbl2.Vmx,sTbl2.kp3]);
ax(1,1).YLabel.String = "Vmx";
ax(2,1).YLabel.String = "kp3";
ax(2,1).XLabel.String = "Vmx";
ax(2,2).XLabel.String = "kp3";

%
% Simulate the scenarios.

sd3 = f(sObj,24);
sbioplot(sd3);

%
% Restore warning settings.

warning(warnSettings);

% Copyright 2019 The MathWorks, Inc.