AI Glossary / Mathematics and optimization / Beginner
Stochastic algorithm
Definition
A stochastic algorithm uses random choices as part of its execution. Even with the same input, different random draws can produce different intermediate steps, running times or outputs, according to the algorithm's design.
What it means
Randomness can determine which training examples are examined, how a model is initialized or which candidate solution is tried next. The randomness is part of the procedure, rather than merely uncertainty in the input data.
For example, a deterministic formula applied to a randomly sampled dataset is not automatically a stochastic algorithm once that dataset is fixed. Conversely, an algorithm that randomly selects rows from a fixed table uses a stochastic mechanism even though the table itself no longer changes.
In software, random choices are often generated by a pseudorandom number generator. Its sequence is determined by its internal state. Recording a seed can help reproduce that sequence under compatible conditions, but a seed alone does not guarantee identical results across every library version, device and execution order.
How it works
The designer specifies what is sampled, its probability distribution and how the sampled choice affects the next step. Those details determine the algorithm, not simply the instruction to “choose randomly.” Uniform and weighted sampling can lead to different behavior.
Performance may be described through expected running time, a probability of error or an expected optimization result. The relevant quantity must be named. An average running-time guarantee does not say that every run takes the same time.
Some randomized algorithms always return a correct answer but have variable running time. Others allow a controlled chance of error or produce an approximation. Stochastic optimization often studies the distribution and convergence of iterates rather than a simple correct-or-incorrect answer.
Evaluation should therefore include repeated runs where randomness can materially affect conclusions. Record the sampling procedure, seeds and stopping rules, and report variability instead of selecting only the most favorable run.
Three practical examples
- A training algorithm selects a random mini-batch to estimate a gradient from a large fixed dataset.
- A clustering experiment uses randomized initialization and compares several resulting solutions.
- A simulation samples possible future demand paths to estimate how often a proposed inventory rule would exhaust stock.
Think of it this way
Think of exploring a maze by occasionally drawing a direction from a specified lottery. The lottery may help explore alternatives, but its rules and the stopping condition determine what the exploration achieves.
The analogy does not mean useful algorithms move without structure. Their random choices are embedded in carefully designed procedures, and their guarantees depend on those procedures.
Worked example
Estimating a fixed average by random sampling
A table contains values 2, 4, 6 and 8. Its exact mean is 5. An algorithm samples two positions independently and uniformly with replacement, then averages the two selected values.
One run might draw 2 and 8 and return 5. Another might draw 2 twice and return 2. The table is unchanged; the random position choices cause the different results.
For one draw X, the expectation is (2 + 4 + 6 + 8)/4 = 5. Its variance is [(2 − 5)² + (4 − 5)² + (6 − 5)² + (8 − 5)²]/4 = 5. The average of two independent draws still has expectation 5, but variance 5/2 = 2.5.
With twenty independent draws under the same scheme, the average has variance 5/20 = 0.25. More sampling reduces this variance but costs more work. Sampling without replacement would have a different variance calculation. The example illustrates an unbiased estimator, not a promise that every returned average equals the exact answer.
Illustrative case study
A fictional product-search team compares two randomized clustering methods. The first report uses one run per method and declares a winner based on a small difference in a quality score.
A repeat study shows that both scores vary with initialization. The team runs a defined set of seeds, records failures and compares distributions of scores and running times. It also checks that both methods receive equivalent computational budgets.
The revised report explains whether the observed advantage persists across runs and how much variation remains. The lesson is not that randomness makes comparison impossible. It means the evaluation design must account for the algorithm's random mechanism rather than treating one fortunate execution as its typical behavior.
Common misconceptions and mistakes
- Stochastic means uncontrolled. The sampling distribution can be precisely specified.
- Randomness guarantees better exploration or accuracy. The effect depends on the algorithm and problem.
- A fixed seed removes all nondeterminism. Parallel execution and implementation details may still matter.
- Expected performance describes every run. An expectation summarizes a distribution.
- Repeating until a good result appears is neutral evaluation. Selective reporting can make performance look better than it is.
Business use
Stochastic algorithms can trade exact computation for affordable estimates, diversify search or make large training problems practical. Their business value depends on the quality and reliability achieved for a given resource budget.
Require a clear statement of what varies and how that variation is assessed. For reproducibility, preserve configuration, data version and software environment alongside seeds. For operational use, decide whether occasional poor runs are acceptable and how they will be detected. Comparing average quality, variability and completion cost usually provides a more useful picture than reporting one best score.
People also ask
Can a stochastic algorithm produce the same output repeatedly?
Yes. Random paths can lead to the same result, or a fixed generator state may reproduce choices.
Are all machine-learning algorithms stochastic?
No. Some fitting procedures are deterministic under a specified implementation and input.
Does pseudorandom mean unsuitable?
No. Suitable pseudorandom generators are widely used for simulation and sampling; requirements depend on the application.
Check your understanding
Why does the sample-average algorithm remain stochastic when its input table is fixed?
Answer: It randomly chooses which table positions to inspect. The source of variation is an internal sampling step, not a change to the table.
Related terms
- Stochastic gradient descent
- Variance
- Randomized algorithm
- pseudorandom generator
- random seed
- Monte Carlo method
Learn next
Read sampling and Monte Carlo estimation. Repeat the worked experiment using draws with and without replacement. Compare what is guaranteed, what is true only on average and what additional information a single run cannot establish.
More in Mathematics and optimization
- Absolute error loss - Absolute error loss measures a numerical prediction's error as the absolute difference between its predicted and reference values: L(y,y-hat)=|y-y-hat|.
- Active-set method - An active-set method is an optimization approach that works with a changing subset of constraints believed to be tight at the solution.
- Adjoint state method - The adjoint state method computes how a chosen objective changes when the parameters of a mathematical model change.
- Σ-algebra
- Algebraic connectivity - Algebraic connectivity is the second-smallest eigenvalue of a graph's Laplacian matrix.
- Algorithm IMED - Algorithm IMED, short for Indexed Minimum Empirical Divergence, is a method for choosing actions in a stochastic multi-armed bandit problem.
Sources
Aalto Dictionary of Machine Learning, stochastic algorithm: https://aaltodictionaryofml.github.io/ADictML_Main.pdf. Random-mechanism definition.
NumPy, Random sampling: https://numpy.org/doc/stable/reference/random/index.html. Generator framework. The fixed-table calculation and fictional comparison are original teaching examples, not benchmark results.
Previous: Stixel Next: Stochastic beam search