source: kdnuggets: 5 scipy.stats tricks for simulating ‘what if’ scenarios
level: technical
freezing distributions turns parameter sets into reusable objects. instead of passing dictionaries of parameters like mean and standard deviation through every function, you create a frozen random variable with stats.norm(loc=800, scale=250). this object holds all distribution details and lets you call methods like .rvs(), .cdf(), or .ppf() directly. it separates model assumptions from execution logic, making it easy to swap scenarios or change distribution types without touching downstream code.
monte carlo simulation with .rvs() replaces static point estimates with full probability distributions. by drawing many samples from frozen distributions for inputs like traffic, conversion rate, and order value, you can propagate them through a business formula using vectorized numpy operations. this reveals the actual range of outcomes, such as the 5th and 95th percentiles of revenue, exposing risks that simple averages hide. the approach avoids slow python loops and gives a clear picture of joint uncertainty.
sensitivity analysis uses .ppf() to compute exact percentiles without simulation. by sweeping parameters like volatility and calling .ppf(0.95) on frozen distributions, you instantly get the 95th percentile value. this is much faster than running full monte carlo simulations for each parameter combination. it helps identify how changes in input assumptions affect downside risk, such as the probability of exceeding a budget threshold. for heavy-tailed data, fitting distributions like student's t with .fit() and using .sf() gives accurate tail risk estimates that normal distributions miss.
why it matters: these tricks let data scientists build fast, accurate simulations for risk assessment and decision-making without relying on heavy external libraries.
source: kdnuggets: 5 scipy.stats tricks for simulating ‘what if’ scenarios