← All projects

Probabilistic graphical modelling · 2026

Bayesian Network Structure Learning & MPG Inference

A Bayesian-network study comparing an expert-defined vehicle dependency graph with a BIC-learned structure, then using conditional probability tables for MPG inference and classification.

PythonbnlearnBayesian networksHill climbingBICProbabilistic inference
AUTO MPG / BAYESIAN NETWORKPROBABILISTIC MODEL
In-sample accuracy58.7%392 vehicle records
Expert graph8directed edges
Learned graph5after pruning
Agreement3common edges
DisplacementWeight
DisplacementHorsepower
HorsepowerAcceleration
26.65% Largest posterior at weight 3,0006 MPG categoriesInference with uncertainty visible

Central question

How closely does a data-driven Bayesian network agree with an expert vehicle model, and what can the fitted network infer about fuel-economy categories?

01

Project overview

This project uses the Auto MPG dataset to model relationships among fuel economy, cylinders, displacement, horsepower, weight, acceleration, model year and origin. Continuous variables were converted into ordered categories so that discrete Bayesian-network structures and conditional probability tables could be estimated.

Two modelling views were compared. The expert graph encoded plausible vehicle relationships in advance, while hill climbing with the Bayesian Information Criterion searched for a smaller structure supported by the observed data.

392vehicle records

8 model variables, no missing values

3common directed edges

Expert and learned structures

26.65%largest posterior

MPG 17.65 to 21.3 at weight 3,000

58.67%in-sample accuracy

Not an out-of-sample estimate

Evidence summary

From vehicle data to a probabilistic dependency model

Six evidence sections compare the graph structures, demonstrate posterior inference and assess the fitted network's reconstruction performance.

01 · Data and discretisation

The network models eight variables across 392 complete vehicle records

Continuous measurements were converted into ordered categories before discrete structure and parameter learning.
Bar chart showing the six observed MPG categories after discretisation, with frequencies ranging from 37 to 103 vehicles.
Dataset and target audit
MeasureResultInterpretation
Vehicle records392All rows used in the fitted model
Variables8MPG plus seven vehicle characteristics
Missing values0No imputation required
Duplicate rows0No exact repeats
MPG categories6Ordered fuel-economy intervals

What it showsThe dataset contains 392 rows, eight variables, no missing values and no duplicate rows. MPG was divided into six categories containing between 37 and 103 vehicles.

Why it mattersA discrete Bayesian network requires clearly defined states. Showing the resulting class balance makes the later classification metrics and confusion matrix easier to interpret.

LimitationDiscretisation removes differences within each interval. Alternative cut-points could change class balance, learned edges, conditional probabilities and predictions.

02 · Structure comparison

Expert knowledge produced a richer graph than BIC-based hill climbing

The expert structure encoded eight directed relationships, while the pruned learned structure retained five.
Network diagram comparing common, expert-only and learned-only directed relationships among MPG and vehicle characteristics.

What it showsThree directed edges were shared: displacement to weight, displacement to horsepower, and horsepower to acceleration. The remaining five expert edges and two learned edges did not match exactly.

Why it mattersThe comparison reveals where domain reasoning and empirical conditional dependence agree. It also identifies relationships that require closer review rather than treating one graph as automatically authoritative.

LimitationA different edge direction can represent a similar set of conditional independencies within a Markov-equivalence class. Directional disagreement is not, by itself, proof that a relationship is absent.

03 · Edge agreement and pruning

The learned graph was smaller and did not retain a direct MPG edge

Independence testing removed the learned weight-to-MPG edge, leaving five supported relationships in the pruned graph.
Bar chart showing three common directed edges, five expert-only edges and two learned-only edges.
Directed-edge comparison
RelationshipStatusReading
Displacement to weightCommonExpert and learned graphs agree
Displacement to horsepowerCommonExpert and learned graphs agree
Horsepower to accelerationCommonExpert and learned graphs agree
Displacement to originLearned onlySelected from the observed dependencies
Weight to MPGExpert onlyPresent in the expert parameter model

What it showsHill climbing first selected six directed edges. After pruning, displacement remained connected to cylinders, weight, horsepower and origin, while horsepower remained connected to acceleration. All five retained pairs rejected independence at the selected rule.

Why it mattersPruning prevents every search-selected edge from being treated as equally supported. The final data-driven graph is therefore more compact than both the initial learned structure and the expert graph.

LimitationThe result depends on the hill-climbing search, BIC score and pruning threshold. The expert structure was used for the parameter-learning and MPG-inference stage, so these graphs answer different parts of the analysis.

04 · Posterior inference

A 3,000-pound vehicle produced a broad MPG probability distribution

The conditional probability tables were queried using the weight interval containing 3,000 pounds.
Bar chart of posterior probabilities for six MPG categories given a vehicle weight of 3,000 pounds, led by the 17.65 to 21.3 MPG category at 26.65 percent.
Posterior MPG probabilities given weight = 3,000
MPG categoryPosterior probabilityRank
17.65 to 21.326.65%1
15.25 to 17.6516.01%2
21.3 to 25.6515.40%3
8.624 to 15.2515.10%4
28.9 to 46.613.58%5
25.65 to 28.913.27%6

What it showsThe 17.65 to 21.3 MPG category had the largest posterior probability at 26.65%. Every other category retained between 13.27% and 16.01% probability.

Why it mattersBayesian inference preserves uncertainty. The network does not only return a class label; it shows how strongly each MPG range remains plausible under the supplied evidence.

LimitationThis is one illustrative evidence query based on discretised weight. It should not be interpreted as a calibrated prediction for every vehicle weighing 3,000 pounds.

05 · Fitted-model performance

Accuracy was moderate, while macro metrics exposed uneven class performance

The fitted network used weight, horsepower and displacement as evidence for MPG-category classification.
Bar chart showing in-sample accuracy of 0.587, macro precision of 0.403, macro recall of 0.503 and macro F1 of 0.438.
In-sample MPG classification metrics
MetricResultWhat it captures
Accuracy58.67%Overall correct classifications
Macro precision40.28%Average class-level positive predictive value
Macro recall50.28%Average class-level sensitivity
Macro F143.84%Balanced class-level precision and recall

What it showsThe network reproduced 58.67% of observed MPG categories. Macro precision was 40.28%, macro recall was 50.28%, and macro F1 was 43.84%.

Why it mattersMacro averaging gives every MPG category equal weight. The gap between overall accuracy and macro F1 shows that the model performed much better for some categories than others.

LimitationThese metrics are in-sample. The same 392 records informed parameter estimation and evaluation, so the scores do not estimate performance on unseen vehicles.

06 · Classification errors

Two MPG categories were never selected as predictions

The confusion matrix explains why the macro scores remained below the overall accuracy.
Six-class confusion matrix for MPG prediction showing no predictions in two middle categories and stronger performance in the lowest and highest MPG categories.
Selected class-level results
MPG categoryPrecisionRecallF1
8.624 to 15.2565.59%88.41%75.31%
17.65 to 21.358.23%63.01%60.53%
21.3 to 25.6538.10%77.42%51.06%
28.9 to 46.679.79%72.82%76.14%
15.25 to 17.650.00%0.00%0.00%
25.65 to 28.90.00%0.00%0.00%

What it showsThe model predicted zero vehicles in the 15.25 to 17.65 and 25.65 to 28.9 MPG categories. Recall was strongest for the lowest MPG group at 88.41%, followed by the 21.3 to 25.65 group at 77.42% and the highest MPG group at 72.82%.

Why it mattersA model can reach moderate accuracy by concentrating predictions in a subset of classes. The missing categories make the current hard classifications unsuitable for claims of balanced multiclass performance.

LimitationOut-of-sample validation, probability calibration and alternative class boundaries are required before deciding whether the error pattern is stable.

02

Expert knowledge and structure learning

The expert graph contained eight directed edges. Hill climbing initially selected six edges, and independence testing reduced the learned graph to five. Three directed relationships appeared in both structures: displacement to weight, displacement to horsepower, and horsepower to acceleration.

The differences are informative rather than evidence that one graph is automatically correct. The learned model reversed the cylinders-displacement direction, added displacement to origin, and did not retain the expert model's direct weight-to-MPG relationship after pruning.

03

Probabilistic inference

The fitted conditional probability tables translate the network into a probabilistic model. For a vehicle weighing 3,000 pounds, the evidence falls in the 2,959.5 to 3,657.5 weight category. The largest posterior probability was 26.65% for the 17.65 to 21.3 MPG category.

The remaining probability was spread across all other MPG categories, with individual probabilities between 13.27% and 16.01%. The example therefore demonstrates uncertainty-aware inference rather than a single certain classification.

04

Classification result

Using weight, horsepower and displacement as evidence, the fitted network reproduced 58.67% of the 392 observed MPG categories. Macro precision was 40.28%, macro recall was 50.28%, and macro F1 was 43.84%.

Performance varied sharply across categories. The model classified the lowest and highest MPG groups comparatively well, yet it never predicted the 15.25 to 17.65 or 25.65 to 28.9 categories. This explains why accuracy alone gives an incomplete picture of model quality.

05

Interpretation and limitations

The prediction metrics are in-sample: the same 392 observations informed parameter learning and evaluation. They measure how well the fitted network reconstructs its training data, not how reliably it will classify unseen vehicles.

The learned edges describe conditional dependence and should not be interpreted as causal effects without additional assumptions. Results also depend on discretisation cut-points, hill-climbing search, BIC scoring, pruning rules and edge direction within potentially equivalent graph structures.

  • Use a train-test split or cross-validation for a genuine generalisation estimate.
  • Test alternative discretisation rules and compare structural stability.
  • Report posterior probabilities alongside hard class predictions.
  • Treat the graph as a dependency model unless causal identification is established.

People behind the project

Project contributors

Project lead and analyst

Toni Cata

Next project

Bitcoin NLP × Volatility

View report