Towards Data Science has a long guide to Bayesian and Markov networks by Sean Moran, and the part that stuck isn’t the algebra. In his wet grass example, seeing wet grass lifts the probability of rain from a prior of 0.2 to about 0.69, and learning it rained then drops the sprinkler from 0.36 to 0.11. Evidence supports two causes, and then one confirmation retires the other. Flat fraud scores can’t do that, and Bayesian network inference is the reasonable way to get it when a store asks me to triage orders.
Why a flat score fails
The request usually looks the same everywhere: send the order to an LLM or a scoring endpoint, get a number back, hold anything above a threshold. The source makes the limitation plain. A classifier answers one fixed question, P(Y | X). A network represents the joint distribution over all the variables and lets evidence enter anywhere. I’ve written before that a trained model still isn’t a risk score on its own; the flat LLM version is worse, because at least a model has features you can audit.
A risky-looking order has more than one cause: a stolen card, an account takeover, or someone fat-fingering the billing address. A flat scorer can say an order is risky. It can’t tell you which explanation is doing the work, and it can’t back off one explanation once another is confirmed. The article calls this explaining away. Wet grass is evidence for both rain and sprinkler, right up until rain is settled.
Where the graph earns its keep
A Bayesian network is a joint distribution split into local pieces: P(fraud), then P(card_bin_mismatch | fraud), then P(billing_mismatch | fraud), and so on. Each variable depends only on its parents, so every number in the model is a countable slice of your own order history. There’s no training run or model artifact, and any drift is drift you caused yourself.
Two useful parents for fraud are booleans WooCommerce already knows: whether the card’s issuing country matches the billing country, and whether billing and shipping match. Fraud as the parent of both is about the smallest net I’d ship. The arrow direction matters, because backwards queries are what make triage useful, and a graph with the arrows reversed still multiplies fine but stops meaning much. The source is careful about this and I agree with it.
Bayesian network inference in a PHP endpoint
Here is the whole model as a REST route. The counts should come from your own orders, not from my placeholders, and they should live in a transient so a nightly WP-CLI job can refresh them.
<?php
add_action( 'rest_api_init', function () {
register_rest_route( 'bbioon/v1', '/order-triage', [
'methods' => 'POST',
'callback' => 'bbioon_order_triage',
'permission_callback' => '__return_true', // put real auth here
] );
} );
function bbioon_order_triage( WP_REST_Request $request ) {
$tables = get_transient( 'bbioon_triage_tables' );
$bin_mismatch = (bool) $request->get_param( 'card_bin_mismatch' );
$billing_mismatch = (bool) $request->get_param( 'billing_mismatch' );
$p_evidence = 0.0;
$p_fraud_and_evidence = 0.0;
foreach ( [ false, true ] as $fraud ) {
$p_world = $tables['p_fraud'][ $fraud ]
* $tables['p_bin_mismatch'][ $fraud ][ $bin_mismatch ]
* $tables['p_billing_mismatch'][ $fraud ][ $billing_mismatch ];
$p_evidence += $p_world;
if ( $fraud ) {
$p_fraud_and_evidence = $p_world;
}
}
if ( $p_evidence <= 0 ) {
return new WP_Error(
'bbioon_empty_evidence',
'Unseen evidence combination',
[ 'status' => 500 ]
);
}
return rest_ensure_response( [
'p_fraud' => round( $p_fraud_and_evidence / $p_evidence, 4 ),
] );
}With the tables loaded the endpoint answers more than one question, which is the point. Return both conditionals and you get the why-was-this-flagged answer the flat score never gave anyone. Query the same tables forwards and you can generate realistic flagged orders for staff training. One WooCommerce caveat while you hold orders: since 11.0, orders that move to failed restore their reduced stock, so a triage flow that flips orders between statuses has inventory consequences. Read the developer blog advisory before you wire statuses together.
Costs
Enumeration is exponential. The article does the arithmetic: n binary variables need 2 to the n states, so twenty variables already pass a million. Its junction-tree section notes that a single 20-variable clique costs 1,048,576 states on its own. Around ten variables is the ceiling I’d put on hand-rolled Bayesian network inference. Past that, variable elimination or a library like pgmpy is the honest answer, and pgmpy is Python, which means an external service rather than in-process PHP. Exact inference scales with treewidth, so keep the graph sparse or the math stops being cheap.
Parameters still come from counting. An empty parent configuration divides by zero, so smooth the counts, and keep parent sets small so the tables don’t starve on thin slices of data. Log every score with its inputs, because the day a merchant disputes a held order is the day you need to show which entry moved.
If you want this on your store, it’s the kind of work I do: a small inference endpoint, a review queue for held orders, and the nightly job that keeps the tables honest against real sales.
One more thing about the collider: once staff start manually reviewing flagged orders, the flag becomes evidence in your own data, and conditioning on a shared effect creates dependence that wasn’t there before. Keep the triage result out of the feature tables and store it in a separate log column, or your counts quietly stop measuring fraud.