Skip to content
svgshare-compare

Real vs synthetic category shares

Does the synthetic data keep the category proportions of the real data?

Demo data · seed 20261101 · 12 rows · default settings · 640 × 360SVG, lightSVG, darkDemo CSV

How to read it

  1. Compare the two bars per category.
  2. Sorted by the real share.
  3. Read total variation distance in the legend: lower is closer.
  1. Callout 1: A close match.
  2. Callout 2: Small categories: often lost or over-sampled.
  3. Callout 3: The overall distance.

Watch out

  • Rare categories matter most for privacy and fairness; check them directly.

Defaults

No settings: the chart draws one way.

Pinned in the code

  • Draws the run's numbers as given (computed by the Python/R engine or the host's scorecard); the chart computes nothing statistical.
  • Total variation distance = ½ Σ |p − q| over all categories of the first two sources (1 − TVD is the scorecard's TV complement); at most 20 categories are drawn.
# Real vs synthetic category shares — Codesplash Chart Lab (recipe "share-compare")
# controls: defaults
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

try:
    import codesplash as cs  # optional: the app's chart theme
except ImportError:
    cs = None
if cs:
    cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]

df = pd.read_csv("share-compare-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["source"] = pd.Categorical(df["source"].astype("string"), categories=["real", "synthetic (CART)"])

def levels_of(s):
    """Declared level order (categoricals), else order of first appearance."""
    if isinstance(s.dtype, pd.CategoricalDtype):
        return [v for v in s.cat.categories if (s == v).any()]
    return list(dict.fromkeys(s.dropna().astype(str)))
L = "employment"
SH = "share"
S = "source"
sources = levels_of(df[S])
wide = df.pivot_table(index=L, columns=S, values=SH, aggfunc="sum", observed=False).reindex(columns=sources).fillna(0)
wide = wide.sort_values(sources[0]).tail(20)  # largest real share on top
h = 0.8 / len(sources)
fig, ax = plt.subplots(figsize=(8, 4.5))
y = np.arange(len(wide))
for j, s in enumerate(sources):
    ax.barh(y + (len(sources) - 1 - j) * h - 0.4 + h / 2, wide[s], height=h * 0.95, color=PALETTE[j % len(PALETTE)], label=str(s))
ax.set_yticks(y, wide.index.astype(str))
if len(sources) >= 2:
    ax.set_title(f"total variation distance {0.5 * (wide[sources[0]] - wide[sources[1]]).abs().sum():.3f}", fontsize=9, loc="left")
ax.set_xlabel(SH)
ax.legend(fontsize=8)
plt.show()

Needs pandas, numpy, matplotlib · reads share-compare-demo.csv · app theme from the optional codesplash helper