ANALYSIS TOOL BOX

Simulations

SimulateCountOfSuccesses

Simulate the number of successes in a series of independent Bernoulli trials (Binomial Distribution).

monte-carlosimulationstochasticuncertainty

Introduction

"Given a fixed number of independent attempts and a constant per-attempt success rate, how many successes should we actually expect — and how much could that vary?" is the Binomial question, and it shows up everywhere from clinical trial responders to defective units in a manufacturing lot. SimulateCountOfSuccesses runs a Monte Carlo simulation of exactly that using numpy.random.binomial, returning the full distribution of possible success counts across repeated trials rather than just a single expected value.

Teaching Note

Binomial count simulations are essential for:

  • Epidemiology: Modeling the potential number of infections in a cohort given a transmission probability.
  • Healthcare: Simulating the number of patients expected to respond positively to a clinical treatment.
  • Intelligence Analysis: Estimating the number of successful signal interceptions in a batch of collection trials.
  • Quality Engineering: Predicting the number of defective units likely to be found in a manufacturing lot.
  • Marketing: Simulating the number of customers who convert (purchase) from a target email campaign.
  • Cybersecurity: Modeling the expected number of successful intrusion attempts over a specific observation window.
  • Finance: Estimating the number of profitable trades in a large-scale algorithmic trading strategy.
  • Ecology: Simulating the survival count of offspring in a population given a baseline survival rate.

Parameters

ParameterTypeDefaultDescription
probability_of_successrequiredfloatThe constant probability of success for each individual trial (must be between 0 and 1).
sample_size_per_trialrequiredintThe total number of trials conducted in each simulation (e.g., total patients or attempts).
number_of_trialsint10000The number of stochastic simulations (Monte Carlo trials) to run. Defaults to 10000.
return_formatstr'dataframe'The format of the returned data: 'dataframe' (pd.DataFrame) or 'array' (np.ndarray). Defaults to 'dataframe'.
simulated_variable_namestr'Count'The label for the simulated count variable. Defaults to 'Count'.
random_seedint412The seed for the random number generator to ensure replicability. Defaults to 412.
plot_simulation_resultsboolTrueWhether to display a histogram of the simulation outcomes. Defaults to True.
fill_colorstr'#999999'The hex color code for the histogram bars. Defaults to '#999999'.
fill_transparencyfloat0.6The transparency level (0-1) for the histogram plot. Defaults to 0.6.
figure_sizetuple(8, 6)The size of the plot figure in inches (width, height). Defaults to (8, 6).
show_meanboolTrueWhether to display the mean success count as a vertical dashed line. Defaults to True.
show_medianboolTrueWhether to display the median success count as a vertical dotted line. Defaults to True.
title_for_plotstr'Simulation Results'The main title for the distribution plot. Defaults to 'Simulation Results'.
subtitle_for_plotstr'Showing the simulated number of successes'The descriptive subtitle for the plot.
caption_for_plotstrNoneOptional caption text displayed at the bottom of the plot. Defaults to None.
data_source_for_plotstrNoneOptional data source identification text. Defaults to None.
show_y_axisboolFalseWhether to display the frequency/density scale on the y-axis. Defaults to False.
title_y_indentfloat1.1Vertical position for the title text. Defaults to 1.1.
subtitle_y_indentfloat1.05Vertical position for the subtitle text. Defaults to 1.05.
caption_y_indentfloat-0.15Vertical position for the caption text. Defaults to -0.15.

Returns

The simulated counts of successes across all trials — a pandas DataFrame or a NumPy array, depending on return_format.

Example

python
from analysistoolbox.simulations import SimulateCountOfSuccesses

# Healthcare: predicting patient response in a 50-person trial (20% success rate)
response_sim = SimulateCountOfSuccesses(
    probability_of_success=0.20,
    sample_size_per_trial=50,
    simulated_variable_name='Patients Responded',
    title_for_plot='Simulated Treatment Successes'
)