ANALYSIS TOOL BOX

Simulations

CreateSLURPDistributionFromLogisticRegression

Generate a SLURP distribution for predicted probabilities using a fitted Logistic Regression model.

monte-carlosimulationstochasticuncertainty

Introduction

A logistic regression's predicted probability for a specific applicant or case comes with a confidence interval — but most workflows discard that interval and pass only the point estimate downstream. CreateSLURPDistributionFromLogisticRegression keeps the uncertainty: it extracts the confidence interval for a predicted probability from a fitted statsmodels logit or probit model at a given set of predictor values, fits a flexible Metalog distribution bounded to [0, 1] over that interval, and returns stochastic probability samples ready for a larger Monte Carlo risk model.

Teaching Note

SLURP distributions from logistic regression are essential for:

  • Healthcare: Simulating the probability of disease presence based on clinical diagnostic markers.
  • Finance: Estimating the likelihood of loan default based on credit history and income levels.
  • Intelligence Analysis: Modeling the probability of a specific threat or event based on signal data.
  • Marketing: Simulating customer churn probability for a cohort based on engagement metrics.
  • Epidemiology: Estimating the individual probability of infection based on exposure risk factors.
  • Cybersecurity: Modeling the likelihood of a system breach based on detected vulnerabilities.
  • Legal Analysis: Estimating the probability of a favorable verdict based on case characteristics.
  • Environmental Science: Assessing the likelihood of extreme weather events or wildfires.

Parameters

ParameterTypeDefaultDescription
logistic_regression_modelrequiredstatsmodels.discrete.discrete_model.BinaryResultsWrapperA fitted logit or probit model from the statsmodels library.
list_of_prediction_valuesrequiredlistA list of values for each predictor in the model. If the model includes a constant, it will be automatically handled.
number_of_trialsint10000The number of stochastic trials to generate from the probability uncertainty. Defaults to 10000.
prediction_intervalfloat0.95The confidence level for the predicted probability interval (between 0 and 1). Defaults to 0.95.
lower_boundfloat0A logical lower limit for the probability (clamped to 0). Defaults to 0.
upper_boundfloat1A logical upper limit for the probability (clamped to 1). Defaults to 1.
learning_ratefloat0.01The step length for the Metalog fitting algorithm. Defaults to 0.01.
term_maximumint3The maximum number of terms in the Metalog expansion (up to 9). Higher terms increase flexibility. Defaults to 3.
term_minimumint2The minimum number of terms allowed. Defaults to 2.
term_for_random_sampleintNoneThe specific number of terms used for generating samples. If None, the term_limit from the fit is used. Defaults to None.
show_summaryboolFalseWhether to print the summary of the Metalog distribution coefficients. Defaults to False.
return_formatstr'dataframe'The format of the output: 'dataframe' (pd.DataFrame) or 'array' (np.ndarray). Defaults to 'dataframe'.
show_distribution_plotboolTrueWhether to display a histogram of the generated probability samples. Defaults to True.
figure_sizetuple(8, 6)The size of the plot figure in inches (width, height). Defaults to (8, 6).
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.
show_meanboolTrueWhether to display the mean probability as a vertical dashed line. Defaults to True.
show_medianboolTrueWhether to display the median probability as a vertical dotted line. Defaults to True.
show_y_axisboolFalseWhether to display the frequency/density scale on the y-axis. Defaults to False.
title_for_plotstrNoneThe main title for the distribution plot. Defaults to None.
subtitle_for_plotstrNoneThe descriptive subtitle for the plot. Defaults to None.
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.
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 generated probability samples representing the uncertainty of the model's prediction — a pandas DataFrame or a NumPy array, depending on return_format.

Example

python
from analysistoolbox.simulations import CreateSLURPDistributionFromLogisticRegression
import statsmodels.api as sm

# Finance: simulating the probability of loan default for an applicant
logit_model = sm.Logit(y, X).fit()
default_prob_sim = CreateSLURPDistributionFromLogisticRegression(
    logistic_regression_model=logit_model,
    list_of_prediction_values=[720, 50000],  # Credit score and Income
    title_for_plot="Uncertainty in Default Probability"
)