ANALYSIS TOOL BOX

Simulations

SimulateCountUntilFirstSuccess

Simulate the number of independent trials required to achieve the first success.

monte-carlosimulationstochasticuncertainty

Introduction

"How many cold calls until the first sale, how many days until the first signal detection, how many inspected units until the first defect" are all the same underlying question: given a constant per-trial success probability, how long is the wait for the first success? SimulateCountUntilFirstSuccess runs a Monte Carlo simulation of this Geometric-distribution scenario, repeatedly simulating Bernoulli trials until the first success lands for each Monte Carlo run, and returning the full distribution of wait times rather than a single expected count.

Teaching Note

Geometric wait-time simulations are essential for:

  • Intelligence Analysis: Modeling the number of days or collection attempts until a high-value signal is detected.
  • Healthcare: Simulating the number of treatment rounds or diagnostic tests until a patient achieves remission or a definitive result.
  • Sales & Marketing: Estimating the number of cold calls or touchpoints required to secure a first-time customer.
  • Quality Engineering: Predicting the number of production units inspected until the first defective item is identified.
  • Cybersecurity: Modeling the number of brute-force login attempts required to breach a test account during a security audit.
  • Human Resources: Estimating the number of candidates screened or interviewed until a qualified hire is made.
  • Engineering: Simulating the number of cycles or operations until the first instance of component failure or fatigue.
  • Scientific Research: Predicting the number of experimental trials required until a specific reaction or observation occurs.

Parameters

ParameterTypeDefaultDescription
probability_of_successrequiredfloatThe constant probability of success for each individual trial (must be between 0 and 1).
number_of_trialsint10000The number of stochastic simulations (Monte Carlo trials) to run. Defaults to 10000.
simulated_variable_namestr'Count Until First Success'The label for the simulated count variable in the output and plot. Defaults to 'Count Until First Success'.
random_seedint412The seed for the random number generator to ensure replicability. Defaults to 412.
return_formatstr'dataframe'The format of the returned data: 'dataframe' (pd.DataFrame) or 'array' (np.ndarray). Defaults to 'dataframe'.
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 wait time as a vertical dashed line. Defaults to True.
show_medianboolTrueWhether to display the median wait time 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 distribution of the count until first success'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 trials until the first success across all Monte Carlo runs — a pandas DataFrame or a NumPy array, depending on return_format.

Example

python
from analysistoolbox.simulations import SimulateCountUntilFirstSuccess

# Sales: estimating calls needed for first conversion (10% success rate)
calls_sim = SimulateCountUntilFirstSuccess(
    probability_of_success=0.10,
    simulated_variable_name='Calls to Close',
    title_for_plot='Sales Conversion Wait Time'
)