MultiSim#
- class MultiSim(sims=None, base_sim=None, label=None, initialize=False, **kwargs)[source]#
Bases:
FlexPretty
Class for running multiple copies of a simulation. The parameter n_runs controls how many copies of the simulation there will be, if a list of sims is not provided. This is the main class that’s used to run multiple versions of a simulation (e.g., with different random seeds).
- Parameters:
sims (Sim/list) – a single sim or a list of sims
base_sim (Sim) – the sim used for shared properties; if not supplied, the first of the sims provided
label (str) – the name of the multisim
initialize (bool) – whether or not to initialize the sims (otherwise, initialize them during run)
kwargs (dict) – stored in run_args and passed to run()
- Returns:
a MultiSim object
- Return type:
msim
Examples:
sim = cv.Sim() # Create the sim msim = cv.MultiSim(sim, n_runs=5) # Create the multisim msim.run() # Run them in parallel msim.combine() # Combine into one sim msim.plot() # Plot results sim = cv.Sim() # Create the sim msim = cv.MultiSim(sim, n_runs=11, noise=0.1, keep_people=True) # Set up a multisim with noise msim.run() # Run msim.reduce() # Compute statistics msim.plot() # Plot sims = [cv.Sim(beta=0.015*(1+0.02*i)) for i in range(5)] # Create sims for sim in sims: sim.run() # Run sims in serial msim = cv.MultiSim(sims) # Convert to multisim msim.plot() # Plot as single sim
Methods
- init_sims(**kwargs)[source]#
Initialize the sims, but don’t actually run them. Syntax is the same as MultiSim.run(). Note: in most cases you can just call run() directly, there is no need to call this separately.
- Parameters:
kwargs (dict) – passed to multi_run()
- run(reduce=False, combine=False, **kwargs)[source]#
Run the actual sims
- Parameters:
reduce (bool) – whether or not to reduce after running (see reduce())
combine (bool) – whether or not to combine after running (see combine(), not compatible with reduce)
kwargs (dict) – passed to multi_run(); use run_args to pass arguments to sim.run()
- Returns:
None (modifies MultiSim object in place)
Examples:
msim.run() msim.run(run_args=dict(until='2020-0601', restore_pars=False))
- shrink(**kwargs)[source]#
Not to be confused with reduce(), this shrinks each sim in the msim; see sim.shrink() for more information.
- Parameters:
kwargs (dict) – passed to sim.shrink() for each sim
- reduce(quantiles=None, use_mean=False, bounds=None, output=False)[source]#
Combine multiple sims into a single sim statistically: by default, use the median value and the 10th and 90th percentiles for the lower and upper bounds. If use_mean=True, then use the mean and ±2 standard deviations for lower and upper bounds.
- Parameters:
quantiles (dict) – the quantiles to use, e.g. [0.1, 0.9] or {‘low : ‘0.1, ‘high’ : 0.9}
use_mean (bool) – whether to use the mean instead of the median
bounds (float) – if use_mean=True, the multiplier on the standard deviation for upper and lower bounds (default 2)
output (bool) – whether to return the “reduced” sim (in any case, modify the multisim in-place)
Example:
msim = cv.MultiSim(cv.Sim()) msim.run() msim.reduce() msim.summarize()
- mean(bounds=None, **kwargs)[source]#
Alias for reduce(use_mean=True). See reduce() for full description.
- Parameters:
bounds (float) – multiplier on the standard deviation for the upper and lower bounds (default, 2)
kwargs (dict) – passed to reduce()
- median(quantiles=None, **kwargs)[source]#
Alias for reduce(use_mean=False). See reduce() for full description.
- Parameters:
quantiles (list or dict) – upper and lower quantiles (default, 0.1 and 0.9)
kwargs (dict) – passed to reduce()
- combine(output=False)[source]#
Combine multiple sims into a single sim with scaled results.
Example:
msim = cv.MultiSim(cv.Sim()) msim.run() msim.combine() msim.summarize()
- compare(t=None, sim_inds=None, output=False, do_plot=False, **kwargs)[source]#
Create a dataframe compare sims at a single point in time.
- Parameters:
t (int/str) – the day (or date) to do the comparison; default, the end
sim_inds (list) – list of integers of which sims to include (default: all)
output (bool) – whether or not to return the comparison as a dataframe
do_plot (bool) – whether or not to plot the comparison (see also plot_compare())
kwargs (dict) – passed to plot_compare()
- Returns:
a dataframe comparison
- Return type:
df (dataframe)
- plot(to_plot=None, inds=None, plot_sims=False, color_by_sim=None, max_sims=5, colors=None, labels=None, alpha_range=None, plot_args=None, show_args=None, **kwargs)[source]#
Plot all the sims – arguments passed to Sim.plot(). The behavior depends on whether or not combine() or reduce() has been called. If so, this function by default plots only the combined/reduced sim (which you can override with plot_sims=True). Otherwise, it plots a separate line for each sim.
Note that this function is complex because it aims to capture the flexibility of both sim.plot() and scens.plot(). By default, if combine() or reduce() has been used, it will resemble sim.plot(); otherwise, it will resemble scens.plot(). This can be changed via color_by_sim, together with the other options.
- Parameters:
to_plot (list) – list or dict of which results to plot; see cv.get_default_plots() for structure
inds (list) – if not combined or reduced, the indices of the simulations to plot (if None, plot all)
plot_sims (bool) – whether to plot individual sims, even if combine() or reduce() has been used
color_by_sim (bool) – if True, set colors based on the simulation type; otherwise, color by result type; True implies a scenario-style plotting, False implies sim-style plotting
max_sims (int) – maximum number of sims to use with color-by-sim; can be overridden by other options
colors (list) – if supplied, override default colors for color_by_sim
labels (list) – if supplied, override default labels for color_by_sim
alpha_range (list) – a 2-element list/tuple/array providing the range of alpha values to use to distinguish the lines
plot_args (dict) – passed to sim.plot()
show_args (dict) – passed to sim.plot()
kwargs (dict) – passed to sim.plot()
- Returns:
Figure handle
- Return type:
fig
Examples:
sim = cv.Sim() msim = cv.MultiSim(sim) msim.run() msim.plot() # Plots individual sims msim.reduce() msim.plot() # Plots the combined sim
- plot_result(key, colors=None, labels=None, *args, **kwargs)[source]#
Convenience method for plotting – arguments passed to sim.plot_result()
- plot_compare(t=-1, sim_inds=None, log_scale=True, **kwargs)[source]#
Plot a comparison between sims, using bars to show different values for each result. For an explanation of other available arguments, see Sim.plot().
- Parameters:
t (int) – index of results, passed to compare()
sim_inds (list) – which sims to include, passed to compare()
log_scale (bool) – whether to plot with a logarithmic x-axis
kwargs (dict) – standard plotting arguments, see Sim.plot() for explanation
- Returns:
Figure handle
- Return type:
fig
- save(filename=None, keep_people=False, **kwargs)[source]#
Save to disk as a gzipped pickle. Load with cv.load(filename) or cv.MultiSim.load(filename).
- Parameters:
filename (str) – the name or path of the file to save to; if None, uses default
keep_people (bool) – whether or not to store the population in the Sim objects (NB, very large)
kwargs (dict) – passed to
sc.makefilepath()
- Returns:
the validated absolute path to the saved file
- Return type:
scenfile (str)
Example:
msim.save() # Saves to an .msim file
- static load(msimfile, *args, **kwargs)[source]#
Load from disk from a gzipped pickle.
- Parameters:
msimfile (str) – the name or path of the file to load from
kwargs – passed to cv.load()
- Returns:
the loaded MultiSim object
- Return type:
msim (MultiSim)
Example:
msim = cv.MultiSim.load('my-multisim.msim')
- static merge(*args, base=False)[source]#
Convenience method for merging two MultiSim objects.
- Parameters:
args (MultiSim) – the MultiSims to merge (either a list, or separate)
base (bool) – if True, make a new list of sims from the multisim’s two base sims; otherwise, merge the multisim’s lists of sims
- Returns:
a new MultiSim object
- Return type:
msim (MultiSim)
Examples:
mm1 = cv.MultiSim.merge(msim1, msim2, base=True) mm2 = cv.MultiSim.merge([m1, m2, m3, m4], base=False)
- split(inds=None, chunks=None)[source]#
Convenience method for splitting one MultiSim into several. You can specify either individual indices of simulations to extract, via inds, or consecutive chunks of indices, via chunks. If this function is called on a merged MultiSim, the chunks can be retrieved automatically and no arguments are necessary.
- Parameters:
inds (list) – a list of lists of indices, with each list turned into a MultiSim
chunks (int or list) – if an int, split the MultiSim into that many chunks; if a list return chunks of that many sims
- Returns:
A list of MultiSim objects
Examples:
m1 = cv.MultiSim(cv.Sim(label='sim1'), initialize=True) m2 = cv.MultiSim(cv.Sim(label='sim2'), initialize=True) m3 = cv.MultiSim.merge(m1, m2) m3.run() m1b, m2b = m3.split() msim = cv.MultiSim(cv.Sim(), n_runs=6) msim.run() m1, m2 = msim.split(inds=[[0,2,4], [1,3,5]]) mlist1 = msim.split(chunks=[2,4]) # Equivalent to inds=[[0,1], [2,3,4,5]] mlist2 = msim.split(chunks=2) # Equivalent to inds=[[0,1,2], [3,4,5]]
- disp(output=False)[source]#
Display a verbose description of a multisim. See also multisim.summarize() (medium length output) and multisim.brief() (short output).
- Parameters:
output (bool) – if true, return a string instead of printing output
Example:
msim = cv.MultiSim(cv.Sim(verbose=0), label='Example multisim') msim.run() msim.disp() # Displays detailed output
- summarize(output=False)[source]#
Print a moderate length summary of the MultiSim. See also multisim.disp() (detailed output) and multisim.brief() (short output).
- Parameters:
output (bool) – if true, return a string instead of printing output
Example:
msim = cv.MultiSim(cv.Sim(verbose=0), label='Example multisim') msim.run() msim.summarize() # Prints moderate length output
- brief(output=False)[source]#
Print a compact representation of the multisim. See also multisim.disp() (detailed output) and multisim.summarize() (medium length output).
- Parameters:
output (bool) – if true, return a string instead of printing output
Example:
msim = cv.MultiSim(cv.Sim(verbose=0), label='Example multisim') msim.run() msim.brief() # Prints one-line output