Monte Carlo forecasting for WooCommerce stock levels

A forecast by Ari Joury on Towards Data Science gives Spain a 16% chance of winning the 2026 World Cup. The model behind that number is almost embarrassingly small: one Elo rating per team, independent Poisson goal counts split around a 2.7 goal baseline, and 10,000 simulated tournaments, which is Monte Carlo forecasting with no decoration. I like it for the same reason the author does. Every number traces back to an assumption you can argue about, and the answer comes out as a distribution instead of the single confident figure clients keep asking me for.

What Monte Carlo forecasting buys you

The favorite sits at 16%, not 50%, because variance in a low-scoring sport compounds across seven knockout matches. The author also points out that the numbers land close to heavyweight statistical systems, which suggests a transparent pipeline recovers most of the signal and stays explainable. A store owner can plan around “16%” but not around “Spain”, and that difference shows up in every forecast I write for a client.

You also get a number for the noise in the forecast itself. A simulated probability estimated from N tournaments has a standard error of sqrt(p(1-p)/N). So a 15% favorite at 10,000 runs wobbles by about 0.36 percentage points between runs; at 500 runs it’s roughly 1.6 points, enough to reshuffle the midfield. Whenever a report regenerates and the numbers shift slightly, I want to know how much of that shift is sampling noise and how much is a real change.

The other side has a point too, and the article is honest about it. Every simplification is listed as a dial: hosts get no home boost, which the author prices at roughly 50 to 100 Elo; ratings stay frozen once the tournament starts; the Dixon-Coles low-score correction is skipped; and the knockout bracket is seeded rather than FIFA’s exact map, which matters for specific paths even though title odds barely move. A model with squad data and injury news will beat this one. For the decisions I make, I’d rather turn a labeled dial than audit a black box, because I can explain a dial to the person paying the bill.

The same three steps on a WooCommerce store

The pipeline maps cleanly. Rate the thing you forecast: average daily units per SKU. Put a distribution on it: order counts are discrete and fairly rare, so Poisson is the same reasonable starting point here. Then simulate thousands of future weeks and read off the level that covers 95% of them. That last number, not the mean, is what stock planning needs, because the gap between the two is what safety stock costs. It’s the same argument I keep making about small data machine learning on WooCommerce stores: a model you can rebuild in an afternoon often gets you most of the way.

A WP-CLI script does it. wc_get_orders works the same under HPOS, so this runs on either setup:

<?php
// wp eval-file stock-cover.php 123
function bbioon_poisson( float $lambda ): int {
	$l = exp( -$lambda );
	$k = 0;
	$p = 1.0;
	do {
		$k++;
		$p *= mt_rand() / mt_getrandmax();
	} while ( $p > $l );
	return $k - 1;
}

$product = wc_get_product( (int) ( $args[0] ?? 0 ) );
if ( ! $product ) {
	WP_CLI::error( 'Product not found.' );
}

$orders = wc_get_orders( array(
	'limit'        => -1,
	'status'       => array( 'processing', 'completed' ),
	'date_created' => '>=' . ( time() - 90 * DAY_IN_SECONDS ),
) );

$units = 0;
foreach ( $orders as $order ) {
	foreach ( $order->get_items() as $item ) {
		if ( (int) $item->get_product_id() === $product->get_id() ) {
			$units += (int) $item->get_quantity();
		}
	}
}

$daily_mean = $units / 90;
$weeks      = array();
for ( $i = 0; $i < 10000; $i++ ) {
	$week_units = 0;
	for ( $d = 0; $d < 7; $d++ ) {
		$week_units += bbioon_poisson( $daily_mean );
	}
	$weeks[] = $week_units;
}
sort( $weeks );

$cover = $weeks[ (int) floor( 0.95 * ( count( $weeks ) - 1 ) ) ];
WP_CLI::line( sprintf( 'Mean daily: %.2f, weekly 95%% cover: %d units', $daily_mean, $cover ) );

The caveats are the same shape as the World Cup’s. Demand is treated as stationary, which is the store version of frozen ratings, and Poisson tails are thin. A product that sells three units a day except during launches or coupon pushes will see a real tail longer than the simulated one, the same way the soccer model underestimates 0-0 draws without the Dixon-Coles correction. When the 95th percentile looks wrong, widen the assumption instead of trusting it. On stores doing tens of thousands of orders in 90 days, aggregate the counts first rather than loading every order object. (I haven’t tested this against bundle discounts, so check line item quantities against your own reports before you reorder on it.)

If this grows beyond a throwaway script, it’s worth registering it as a proper command instead, and I wrote about a better way to write WP-CLI commands a while back.

If you want a real version of this on your store, with lead times and per-SKU reorder points instead of a one-off script, that’s work I take on. Send me the SKUs that matter and I’ll tell you what the numbers say.

This week I’d run the script over the twenty SKUs that drive most of a site’s revenue, compare the 95% cover against stock on hand, and hand over a reorder list with the main assumption stated at the top. If the simple version holds up, it stays. If not, I’ll know which dial to turn before I try anything heavier.

author avatar
Ahmad Wael
I'm a WordPress and WooCommerce developer with 15+ years of experience building custom e-commerce solutions and plugins. I specialize in PHP development, following WordPress coding standards to deliver clean, maintainable code. Currently, I'm exploring AI and e-commerce by building multi-agent systems and SaaS products that integrate technologies like Google Gemini API with WordPress platforms, approaching every project with a commitment to performance, security, and exceptional user experience.

Leave a Comment