Copula, trees or mixture: choosing a generator
Sequential trees keep interactions but copy real values. Mixtures handle numeric clusters. See what each generator gains and gives up, using recorded runs on the same tables.
What runs in CodeSplash
- Synthetic data — sequential CART Verified in Python and R
- Synthetic data — Gaussian mixture Verified in Python and R
- Synthetic data — Gaussian copula Verified in Python and R
You will be able to
- Explain how sequential trees and Gaussian mixtures generate rows
- Predict which generator copies more real values, and why
- Choose a generator for a table and a purpose
Read first
Three ideas of a table
Read| Generator | Its idea of your table | Good at | Weak at |
|---|---|---|---|
| Gaussian copula | Column shapes, plus one set of correlations | Speed, privacy by default | Interactions, clusters |
| Sequential trees (CART) | Each column depends on the ones before it | Interactions, odd shapes | Copies real values |
| Gaussian mixture | A few overlapping numeric groups | Clusters, several modes | Numeric columns only |
No generator wins on every score, so you choose by the table and the job.
Sequential trees
ReadThe trees work one column at a time, in the order you list them.
- The first column is resampled from its real values.
- For each later column, a tree learns to predict it from the columns before it.
- Each synthetic row falls into a leaf of that tree, and takes the value of a real row from that leaf.
Step 3 is the catch. Every value is a real value from a donor row, which is how it keeps odd shapes so well. It's also why order matters: put the columns that others depend on first.
Which copies more?
PredictPredict first
Both generators run on the same four customer columns, with no holdout, and both use only values that occur in the data. Which one produces a larger share of exact copies of training rows?
Reveal
Sequential trees. They produce 64 exact copies in 300 rows, about 21%. The copula produces 13 in 250, about 5%. The copula draws each value separately, while each tree takes a value from a donor that resembles the row so far, so whole rows can come back.
More columns
In the appNow look at the same generator on all eight columns.
320 synthetic rows over 8 columns from 320 training rows with sequential classification and regression trees. Numeric columns differ from the data by a mean Kolmogorov–Smirnov distance of 0.051 and categories by a mean total variation of 0.042.
- Synthetic rows
- 320
- Mean KS distance
- 0.0511
- Mean TV distance
- 0.0424
- Rows copied from the data
- 0
Plots
View data
| Density | Source | age |
|---|---|---|
| 0.0230 | real | 18.950 |
| 0.0016 | real | 20.850 |
| 0.0049 | real | 22.750 |
| 0.0148 | real | 24.650 |
| 0.0148 | real | 26.550 |
| 0.0181 | real | 28.450 |
| 0.0197 | real | 30.350 |
| 0.0296 | real | 32.250 |
| 0.0362 | real | 34.150 |
| 0.0115 | real | 36.050 |
| 0.0362 | real | 37.950 |
| 0.0313 | real | 39.850 |
| 0.0296 | real | 41.750 |
| 0.0280 | real | 43.650 |
| 0.0280 | real | 45.550 |
| 0.0181 | real | 47.450 |
| 0.0164 | real | 49.350 |
| 0.0197 | real | 51.250 |
| 0.0362 | real | 53.150 |
| 0.0148 | real | 55.050 |
| 0.0164 | real | 56.950 |
| 0.0181 | real | 58.850 |
| 0.0082 | real | 60.750 |
| 0.0164 | real | 62.650 |
| 0.0049 | real | 64.550 |
| 0.0115 | real | 66.450 |
| 0.0066 | real | 68.350 |
| 0.0049 | real | 70.250 |
| 0.0016 | real | 72.150 |
| 0.0049 | real | 74.050 |
| 0.0164 | synthetic | 18.950 |
| 0.0049 | synthetic | 20.850 |
| 0.0082 | synthetic | 22.750 |
| 0.0132 | synthetic | 24.650 |
| 0.0066 | synthetic | 26.550 |
| 0.0280 | synthetic | 28.450 |
| 0.0247 | synthetic | 30.350 |
| 0.0263 | synthetic | 32.250 |
| 0.0214 | synthetic | 34.150 |
| 0.0132 | synthetic | 36.050 |
| 0.0296 | synthetic | 37.950 |
| 0.0247 | synthetic | 39.850 |
| 0.0395 | synthetic | 41.750 |
| 0.0395 | synthetic | 43.650 |
| 0.0296 | synthetic | 45.550 |
| 0.0115 | synthetic | 47.450 |
| 0.0230 | synthetic | 49.350 |
| 0.0132 | synthetic | 51.250 |
| 0.0345 | synthetic | 53.150 |
| 0.0214 | synthetic | 55.050 |
| 0.0115 | synthetic | 56.950 |
| 0.0132 | synthetic | 58.850 |
| 0.0115 | synthetic | 60.750 |
| 0.0181 | synthetic | 62.650 |
| 0.0148 | synthetic | 64.550 |
| 0.0066 | synthetic | 66.450 |
| 0.0049 | synthetic | 68.350 |
| 0.0082 | synthetic | 70.250 |
| 0.0033 | synthetic | 72.150 |
| 0.0049 | synthetic | 74.050 |
View data
| Density | Source | annual income |
|---|---|---|
| 3.010e-6 | real | 6851.93 |
| 1.605e-5 | real | 10141.74 |
| 2.107e-5 | real | 13431.55 |
| 2.408e-5 | real | 16721.36 |
| 3.511e-5 | real | 20011.17 |
| 4.113e-5 | real | 23300.98 |
| 2.809e-5 | real | 26590.79 |
| 2.408e-5 | real | 29880.60 |
| 2.508e-5 | real | 33170.41 |
| 1.605e-5 | real | 36460.22 |
| 1.605e-5 | real | 39750.03 |
| 1.505e-5 | real | 43039.84 |
| 1.003e-5 | real | 46329.65 |
| 5.016e-6 | real | 49619.46 |
| 6.019e-6 | real | 52909.27 |
| 4.013e-6 | real | 56199.07 |
| 4.013e-6 | real | 59488.88 |
| 4.013e-6 | real | 62778.69 |
| 1.003e-6 | real | 66068.50 |
| 1.003e-6 | real | 69358.31 |
| 1.003e-6 | real | 72648.12 |
| 0 | real | 75937.93 |
| 1.003e-6 | real | 79227.74 |
| 1.003e-6 | real | 82517.55 |
| 0 | real | 85807.36 |
| 0 | real | 89097.17 |
| 0 | real | 92386.98 |
| 0 | real | 95676.79 |
| 0 | real | 98966.60 |
| 1.003e-6 | real | 102256.41 |
| 8.133e-6 | synthetic | 6851.93 |
| 1.423e-5 | synthetic | 10141.74 |
| 2.135e-5 | synthetic | 13431.55 |
| 3.457e-5 | synthetic | 16721.36 |
| 3.355e-5 | synthetic | 20011.17 |
| 3.152e-5 | synthetic | 23300.98 |
| 3.253e-5 | synthetic | 26590.79 |
| 1.932e-5 | synthetic | 29880.60 |
| 3.253e-5 | synthetic | 33170.41 |
| 1.830e-5 | synthetic | 36460.22 |
| 1.423e-5 | synthetic | 39750.03 |
| 1.017e-5 | synthetic | 43039.84 |
| 1.220e-5 | synthetic | 46329.65 |
| 4.066e-6 | synthetic | 49619.46 |
| 1.017e-6 | synthetic | 52909.27 |
| 3.050e-6 | synthetic | 56199.07 |
| 4.066e-6 | synthetic | 59488.88 |
| 3.050e-6 | synthetic | 62778.69 |
| 1.017e-6 | synthetic | 66068.50 |
| 1.017e-6 | synthetic | 69358.31 |
| 2.033e-6 | synthetic | 72648.12 |
| 0 | synthetic | 75937.93 |
| 0 | synthetic | 79227.74 |
| 1.017e-6 | synthetic | 82517.55 |
| 0 | synthetic | 85807.36 |
| 0 | synthetic | 89097.17 |
| 0 | synthetic | 92386.98 |
| 0 | synthetic | 95676.79 |
| 0 | synthetic | 98966.60 |
| 1.017e-6 | synthetic | 102256.41 |
View data
| Density | Source | tenure_months |
|---|---|---|
| 0.0938 | real | 0.7667 |
| 0.0224 | real | 2.300 |
| 0.0183 | real | 3.833 |
| 0.0306 | real | 5.367 |
| 0.0204 | real | 6.900 |
| 0.0265 | real | 8.433 |
| 0.0204 | real | 9.967 |
| 0.0530 | real | 11.500 |
| 0.0183 | real | 13.033 |
| 0.0510 | real | 14.567 |
| 0.0183 | real | 16.100 |
| 0.0367 | real | 17.633 |
| 0.0224 | real | 19.167 |
| 0.0448 | real | 20.700 |
| 0.0469 | real | 22.233 |
| 0.0082 | real | 23.767 |
| 0.0306 | real | 25.300 |
| 0.0061 | real | 26.833 |
| 0.0183 | real | 28.367 |
| 0.0041 | real | 29.900 |
| 0.0143 | real | 31.433 |
| 0.0061 | real | 32.967 |
| 0.0183 | real | 34.500 |
| 0.0020 | real | 36.033 |
| 0.0041 | real | 37.567 |
| 0 | real | 39.100 |
| 0.0061 | real | 40.633 |
| 0.0041 | real | 42.167 |
| 0.0020 | real | 43.700 |
| 0.0041 | real | 45.233 |
| 0.0917 | synthetic | 0.7667 |
| 0.0061 | synthetic | 2.300 |
| 0.0143 | synthetic | 3.833 |
| 0.0306 | synthetic | 5.367 |
| 0.0265 | synthetic | 6.900 |
| 0.0346 | synthetic | 8.433 |
| 0.0183 | synthetic | 9.967 |
| 0.0428 | synthetic | 11.500 |
| 0.0204 | synthetic | 13.033 |
| 0.0448 | synthetic | 14.567 |
| 0.0245 | synthetic | 16.100 |
| 0.0367 | synthetic | 17.633 |
| 0.0204 | synthetic | 19.167 |
| 0.0510 | synthetic | 20.700 |
| 0.0510 | synthetic | 22.233 |
| 0.0122 | synthetic | 23.767 |
| 0.0326 | synthetic | 25.300 |
| 0.0061 | synthetic | 26.833 |
| 0.0224 | synthetic | 28.367 |
| 0.0102 | synthetic | 29.900 |
| 0.0143 | synthetic | 31.433 |
| 0.0041 | synthetic | 32.967 |
| 0.0143 | synthetic | 34.500 |
| 0.0041 | synthetic | 36.033 |
| 0.0020 | synthetic | 37.567 |
| 0 | synthetic | 39.100 |
| 0.0020 | synthetic | 40.633 |
| 0.0061 | synthetic | 42.167 |
| 0.0020 | synthetic | 43.700 |
| 0.0061 | synthetic | 45.233 |
View data
| Density | Source | signup |
|---|---|---|
| 0.0005 | real | 17582.23 |
| 0.0007 | real | 17654.70 |
| 0.0005 | real | 17727.17 |
| 0.0005 | real | 17799.63 |
| 0.0004 | real | 17872.10 |
| 0.0005 | real | 17944.57 |
| 0.0006 | real | 18017.03 |
| 0.0004 | real | 18089.50 |
| 0.0005 | real | 18161.97 |
| 0.0005 | real | 18234.43 |
| 0.0001 | real | 18306.90 |
| 0.0009 | real | 18379.37 |
| 0.0006 | real | 18451.83 |
| 0.0003 | real | 18524.30 |
| 0.0005 | real | 18596.77 |
| 0.0004 | real | 18669.23 |
| 0.0002 | real | 18741.70 |
| 0.0005 | real | 18814.17 |
| 0.0006 | real | 18886.63 |
| 0.0005 | real | 18959.10 |
| 0.0005 | real | 19031.57 |
| 0.0005 | real | 19104.03 |
| 0.0003 | real | 19176.50 |
| 0.0006 | real | 19248.97 |
| 0.0004 | real | 19321.43 |
| 0.0003 | real | 19393.90 |
| 0.0002 | real | 19466.37 |
| 0.0005 | real | 19538.83 |
| 0.0003 | real | 19611.30 |
| 0.0004 | real | 19683.77 |
| 0.0002 | synthetic | 17582.23 |
| 0.0004 | synthetic | 17654.70 |
| 0.0006 | synthetic | 17727.17 |
| 0.0006 | synthetic | 17799.63 |
| 0.0004 | synthetic | 17872.10 |
| 0.0006 | synthetic | 17944.57 |
| 0.0007 | synthetic | 18017.03 |
| 8.625e-5 | synthetic | 18089.50 |
| 0.0010 | synthetic | 18161.97 |
| 0.0006 | synthetic | 18234.43 |
| 0.0003 | synthetic | 18306.90 |
| 0.0009 | synthetic | 18379.37 |
| 0.0008 | synthetic | 18451.83 |
| 0.0003 | synthetic | 18524.30 |
| 0.0003 | synthetic | 18596.77 |
| 0.0003 | synthetic | 18669.23 |
| 0.0001 | synthetic | 18741.70 |
| 0.0004 | synthetic | 18814.17 |
| 0.0005 | synthetic | 18886.63 |
| 0.0006 | synthetic | 18959.10 |
| 0.0005 | synthetic | 19031.57 |
| 0.0002 | synthetic | 19104.03 |
| 0.0004 | synthetic | 19176.50 |
| 0.0007 | synthetic | 19248.97 |
| 0.0005 | synthetic | 19321.43 |
| 0.0004 | synthetic | 19393.90 |
| 0.0001 | synthetic | 19466.37 |
| 0.0005 | synthetic | 19538.83 |
| 0.0003 | synthetic | 19611.30 |
| 0.0003 | synthetic | 19683.77 |
View data
| region | Share | Source |
|---|---|---|
| east | 0.2848 | real |
| north | 0.2718 | real |
| south | 0.3042 | real |
| west | 0.1392 | real |
| east | 0.3151 | synthetic |
| north | 0.2637 | synthetic |
| south | 0.2958 | synthetic |
| west | 0.1254 | synthetic |
View data
| plan | Share | Source |
|---|---|---|
| basic | 0.3563 | real |
| plus | 0.3969 | real |
| premium | 0.2469 | real |
| basic | 0.4281 | synthetic |
| plus | 0.3688 | synthetic |
| premium | 0.2031 | synthetic |
View data
| churned | Share | Source |
|---|---|---|
| false | 0.7688 | real |
| true | 0.2313 | real |
| false | 0.7438 | synthetic |
| true | 0.2562 | synthetic |
View data
| Column | Column | Difference |
|---|---|---|
| age | age | 0 |
| annual income | age | −0.0433 |
| tenure_months | age | −0.0104 |
| signup | age | −0.0279 |
| visits | age | 0.0164 |
| age | annual income | −0.0433 |
| annual income | annual income | 0 |
| tenure_months | annual income | 0.0559 |
| signup | annual income | 0.0337 |
| visits | annual income | 0.0579 |
| age | tenure_months | −0.0104 |
| annual income | tenure_months | 0.0559 |
| tenure_months | tenure_months | 0 |
| signup | tenure_months | −0.0118 |
| visits | tenure_months | 0.1094 |
| age | signup | −0.0279 |
| annual income | signup | 0.0337 |
| tenure_months | signup | −0.0118 |
| signup | signup | 0 |
| visits | signup | 0.0628 |
| age | visits | 0.0164 |
| annual income | visits | 0.0579 |
| tenure_months | visits | 0.1094 |
| signup | visits | 0.0628 |
| visits | visits | 0 |
Real vs synthetic, column by column
| Column | Kind | Real | Synthetic | Distance | Measure |
|---|---|---|---|---|---|
| age | numeric | 43.14 ± 13.1 | 43.84 ± 13.2 | 0.0531 | Kolmogorov–Smirnov D |
| region | categorical | south 30.4% | south 29.6% | 0.0303 | total variation |
| plan | categorical | plus 39.7% | plus 36.9% | 0.0719 | total variation |
| annual income | numeric | 2.929e+04 ± 1.43e+04 | 2.81e+04 ± 1.4e+04 | 0.0615 | Kolmogorov–Smirnov D |
| tenure_months | numeric | 15 ± 10.7 | 15.59 ± 10.6 | 0.0375 | Kolmogorov–Smirnov D |
| churned | boolean | false 76.9% | false 74.4% | 0.0250 | total variation |
| signup | temporal | 2020-11-05 ± 621 days | 2020-11-10 ± 604 days | 0.0469 | Kolmogorov–Smirnov D |
| visits | numeric | 3.394 ± 2.09 | 3.2 ± 2.13 | 0.0563 | Kolmogorov–Smirnov D |
Trees (synthesis order)
| Step | Column | Drawn by |
|---|---|---|
| 1 | age | resampling its values |
| 2 | region | a tree on the 1 column before it (36 leaves) |
| 3 | plan | a tree on the 2 columns before it (46 leaves) |
| 4 | annual income | a tree on the 3 columns before it (48 leaves) |
| 5 | tenure_months | a tree on the 4 columns before it (46 leaves) |
| 6 | churned | a tree on the 5 columns before it (35 leaves) |
| 7 | signup | a tree on the 6 columns before it (47 leaves) |
| 8 | visits | a tree on the 7 columns before it (46 leaves) |
- 80 of 400 rows (20%) were held out of training so privacy and utility can be checked against rows the generator never saw.
The exact R script · R R version 4.5.1 (2025-06-13)
# Synthetic data — sequential CART
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method synth.cart 1.0.2 · spec 1d9728a4b0fe66db
# Dataset: g1_customers (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr)
# %% Data
COLUMNS <- c("age", "region", "plan", "annual income", "tenure_months", "churned", "signup", "visits")
KINDS <- list(
age = "numeric",
region = "categorical",
plan = "categorical",
`annual income` = "numeric",
tenure_months = "numeric",
churned = "boolean",
signup = "temporal",
visits = "numeric"
)
LEVELS <- list(
region = c("east", "north", "south", "west"),
plan = c("basic", "plus", "premium")
)
SEED <- 42
loaded <- cs_synth_load("tests/parity/fixtures/g1_customers.parquet", COLUMNS, KINDS, LEVELS)
cols <- loaded$cols; n_input <- loaded$n
for (c in cols) if (c$kind == "numeric") cs_check_finite(c$values, c$name) # infinite values stop the run (registry/errors.yaml)
rng <- cs_rng(SEED) # shared stream: the same holdout and the same synthetic rows in Python and R
sp <- cs_synth_split(n_input, 0.2, rng)
train_rows <- sp$train; holdout_rows <- sp$holdout
train <- lapply(cols, cs_synth_take, rows = train_rows)
N_OUT <- length(train_rows)
warnings <- list()
fit_values <- list()
extra_tables <- list()
model_note <- ""
if (length(holdout_rows)) {
warnings <- c(warnings, sprintf("%s of %s rows (%.0f%%) were held out of training so privacy and utility can be checked against rows the generator never saw.",
format(length(holdout_rows), big.mark = ","), format(n_input, big.mark = ","), 100 * length(holdout_rows) / n_input))
}
for (c in train) if (mean(cs_synth_missing(c)) == 1) cs_error("CS_E_ALL_MISSING", column = c$name) # no values in the training rows
# %% Generate
# Sequential CART (twin: codesplash.synth.cart; classic equivalent: synthpop::syn(method = "cart")).
# Columns in the order listed; each later column is drawn from donors in its tree's leaf.
fit <- cs_synth_cart(train, N_OUT, rng, min_leaf = 5L, max_depth = NULL)
synth <- fit$cols
for (j in seq_along(fit$leaves)) fit_values[[sprintf("leaves[%s]", COLUMNS[j + 1L])]] <- fit$leaves[j]
model_note <- " with sequential classification and regression trees"
trows <- list(list(step = 1L, column = COLUMNS[1], how = "resampling its values"))
for (j in seq_along(fit$leaves)) {
trows[[j + 1L]] <- list(step = j + 1L, column = COLUMNS[j + 1L],
how = sprintf("a tree on the %d column%s before it (%d leaves)", j, if (j > 1L) "s" else "", fit$leaves[j]))
}
extra_tables <- list(list(
key = "trees", title = "Trees (synthesis order)",
columns = list(list(key = "step", label = "Step", type = "integer"), list(key = "column", label = "Column", type = "string"),
list(key = "how", label = "Drawn by", type = "string")),
rows = trows))
synth <- cs_synth_add_missing(synth, train, rng, "keep_rate") # each column's missing share, at random
# %% Write and compare
out <- cs_ml_out_dir("target/academy-parity/run-953/synth.cart/customers_all/out-r")
path <- cs_synth_write(file.path(out, "synthetic.parquet"), synth)
split <- cs_synth_write_split(file.path(out, "split.parquet"), n_input, train_rows, holdout_rows)
sm <- cs_synth_summarize(train, synth, length(train_rows), n_input, length(holdout_rows), fit_values)
values <- sm$values
summary <- sprintf("%s synthetic rows over %d column%s from %s training rows%s.",
format(values$n_rows, big.mark = ","), length(COLUMNS), if (length(COLUMNS) != 1) "s" else "",
format(length(train_rows), big.mark = ","), model_note)
if (!is.nan(values$fidelity_ks_mean)) {
summary <- paste0(summary, sprintf(" Numeric columns differ from the data by a mean Kolmogorov–Smirnov distance of %.3f", values$fidelity_ks_mean),
if (!is.nan(values$fidelity_tv_mean)) sprintf(" and categories by a mean total variation of %.3f.", values$fidelity_tv_mean) else ".")
} else if (!is.nan(values$fidelity_tv_mean)) {
summary <- paste0(summary, sprintf(" Categories differ from the data by a mean total variation of %.3f.", values$fidelity_tv_mean))
}
if (values$exact_matches > 0) {
summary <- paste0(summary, sprintf(" %s synthetic row%s identical to a training row.", format(values$exact_matches, big.mark = ","),
if (values$exact_matches != 1) "s are" else " is"))
}
cs_synth_publish(
"Synthetic data — sequential CART",
cs_engine_info("nanoparquet", "nanoparquet"),
summary, values, sm$table, sm$charts, warnings,
list(list(key = "synthetic", path = path, format = "parquet", title = "Synthetic rows"),
list(key = "split", path = split, format = "parquet", title = "Training and holdout rows")),
list(kind = "generator", columns = I(COLUMNS), params = list(
n_rows = NULL,
min_leaf = 5,
max_depth = NULL,
missing = "keep_rate",
holdout = 0.2
)),
extra_tables
)
320 synthetic rows over 8 columns from 320 training rows with sequential classification and regression trees. Numeric columns differ from the data by a mean Kolmogorov–Smirnov distance of 0.051 and categories by a mean total variation of 0.042.
- Synthetic rows
- 320
- Mean KS distance
- 0.0511
- Mean TV distance
- 0.0424
- Rows copied from the data
- 0
To keep this page light, the plots are shown once, under the R tab.
Real vs synthetic, column by column
| Column | Kind | Real | Synthetic | Distance | Measure |
|---|---|---|---|---|---|
| age | numeric | 43.14 ± 13.1 | 43.84 ± 13.2 | 0.0531 | Kolmogorov–Smirnov D |
| region | categorical | south 30.4% | south 29.6% | 0.0303 | total variation |
| plan | categorical | plus 39.7% | plus 36.9% | 0.0719 | total variation |
| annual income | numeric | 2.929e+04 ± 1.43e+04 | 2.81e+04 ± 1.4e+04 | 0.0615 | Kolmogorov–Smirnov D |
| tenure_months | numeric | 15 ± 10.7 | 15.59 ± 10.6 | 0.0375 | Kolmogorov–Smirnov D |
| churned | boolean | false 76.9% | false 74.4% | 0.0250 | total variation |
| signup | temporal | 2020-11-05 ± 621 days | 2020-11-10 ± 604 days | 0.0469 | Kolmogorov–Smirnov D |
| visits | numeric | 3.394 ± 2.09 | 3.2 ± 2.13 | 0.0563 | Kolmogorov–Smirnov D |
Trees (synthesis order)
| Step | Column | Drawn by |
|---|---|---|
| 1 | age | resampling its values |
| 2 | region | a tree on the 1 column before it (36 leaves) |
| 3 | plan | a tree on the 2 columns before it (46 leaves) |
| 4 | annual income | a tree on the 3 columns before it (48 leaves) |
| 5 | tenure_months | a tree on the 4 columns before it (46 leaves) |
| 6 | churned | a tree on the 5 columns before it (35 leaves) |
| 7 | signup | a tree on the 6 columns before it (47 leaves) |
| 8 | visits | a tree on the 7 columns before it (46 leaves) |
- 80 of 400 rows (20%) were held out of training so privacy and utility can be checked against rows the generator never saw.
The exact Python script · Python Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Synthetic data — sequential CART
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method synth.cart 1.0.2 · spec 1d9728a4b0fe66db
# Dataset: g1_customers (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import os
import numpy as np
import codesplash as cs
from codesplash.errors import check_finite
from codesplash.ml.run import out_dir
from codesplash.stats.rng import Rng
from codesplash.synth import common as sy
from codesplash.synth import cart
# %% Data
COLUMNS = ["age", "region", "plan", "annual income", "tenure_months", "churned", "signup", "visits"]
KINDS = {
"age": "numeric",
"region": "categorical",
"plan": "categorical",
"annual income": "numeric",
"tenure_months": "numeric",
"churned": "boolean",
"signup": "temporal",
"visits": "numeric",
}
LEVELS = {
"region": ["east", "north", "south", "west"],
"plan": ["basic", "plus", "premium"],
}
SEED = 42
cols, n_input = sy.load("tests/parity/fixtures/g1_customers.parquet", COLUMNS, KINDS, LEVELS)
for c in cols:
if c.kind == "numeric":
check_finite(c.values, c.name) # infinite values stop the run (registry/errors.yaml)
rng = Rng(SEED) # shared stream: the same holdout and the same synthetic rows in Python and R
train_rows, holdout_rows = sy.split_holdout(n_input, 0.2, rng)
train = [c.take(train_rows) for c in cols]
N_OUT = None or len(train_rows)
warnings = []
fit_values = {}
extra_tables = []
model_note = ""
if len(holdout_rows):
warnings.append(f"{len(holdout_rows):,} of {n_input:,} rows ({len(holdout_rows) / n_input:.0%}) were held out of training so privacy and utility can be checked against rows the generator never saw.")
for c in train:
share = float(np.mean(c.missing()))
if share == 1.0:
cs.cs_error("CS_E_ALL_MISSING", column=c.name) # no values in the training rows
# %% Generate
# Sequential CART (twin: codesplashr::cs_synth_cart; classic equivalent: synthpop::syn(method = "cart")).
# Columns in the order listed; each later column is drawn from donors in its tree's leaf.
synth, leaves = cart.synthesize(train, N_OUT, rng, min_leaf=5, max_depth=None)
for name, n_leaves in zip(COLUMNS[1:], leaves):
fit_values[f"leaves[{name}]"] = n_leaves
model_note = " with sequential classification and regression trees"
extra_tables.append({
"key": "trees",
"title": "Trees (synthesis order)",
"columns": [{"key": "step", "label": "Step", "type": "integer"}, {"key": "column", "label": "Column", "type": "string"},
{"key": "how", "label": "Drawn by", "type": "string"}],
"rows": [{"step": 1, "column": COLUMNS[0], "how": "resampling its values"}]
+ [{"step": i + 2, "column": c, "how": f"a tree on the {i + 1} column{'s' if i else ''} before it ({n} leaves)"} for i, (c, n) in enumerate(zip(COLUMNS[1:], leaves))],
})
sy.add_missing(synth, train, rng, "keep_rate") # each column's missing share, at random
# %% Write and compare
out = out_dir("target/academy-parity/run-953/synth.cart/customers_all/out-python")
path = sy.write(os.path.join(out, "synthetic.parquet"), synth)
split = sy.write_split(os.path.join(out, "split.parquet"), n_input, train_rows, holdout_rows)
values, table, charts = sy.summarize(train, synth, len(train_rows), n_input, len(holdout_rows), fit_values)
summary = (
f"{values['n_rows']:,} synthetic rows over {len(COLUMNS)} column{'s' if len(COLUMNS) != 1 else ''} from "
f"{len(train_rows):,} training rows{model_note}."
)
if not np.isnan(values["fidelity_ks_mean"]):
summary += f" Numeric columns differ from the data by a mean Kolmogorov–Smirnov distance of {values['fidelity_ks_mean']:.3f}"
summary += f" and categories by a mean total variation of {values['fidelity_tv_mean']:.3f}." if not np.isnan(values["fidelity_tv_mean"]) else "."
elif not np.isnan(values["fidelity_tv_mean"]):
summary += f" Categories differ from the data by a mean total variation of {values['fidelity_tv_mean']:.3f}."
if values["exact_matches"]:
summary += f" {values['exact_matches']:,} synthetic row{'s are' if values['exact_matches'] != 1 else ' is'} identical to a training row."
sy.publish(
"Synthetic data — sequential CART",
cs.engine_info("numpy", "pyarrow", "numpy", "pyarrow"),
summary, values, table, charts, warnings,
[{"key": "synthetic", "path": path, "format": "parquet", "title": "Synthetic rows"},
{"key": "split", "path": split, "format": "parquet", "title": "Training and holdout rows"}],
{"kind": "generator", "columns": COLUMNS, "params": {
"n_rows": None,
"min_leaf": 5,
"max_depth": None,
"missing": "keep_rate",
"holdout": 0.2,
}},
extra_tables,
)
It has 0 exact copies in 320 rows, because more columns make a full match harder. On penguins, with six columns, it has 9 in 258.
Don't read zero as safe. A row can match in all but one column. That's why the privacy lesson measures distance, not only exact copies.
Mixtures
ExploreA mixture says each row came from one of overlapping normal groups. To generate a row, it picks a group by its weight, then draws from that group's multivariate normal.
AExplorable · Gaussian mixtures by EM
How can you recover overlapping groups you never saw labeled?
The data come from three groups, this far apart. How many components will BIC choose?
Commit your prediction above to see the result
Make your prediction to see the result.
Step through the fit, and then change how far apart the groups are. Notice how BIC's choice of follows the data.
With Choose components by BIC on and an upper limit of 4, the customer table gets = 3. Mixtures take numbers and dates only, so categories need one of the other two.
Clusters and correlations
PredictPredict first
Penguins come in three species with different body shapes, so the numeric columns form clusters. Which generator will miss the correlations among them by more?
Reveal
The copula: its mean gap in correlation is 0.117, against 0.010 for the trees. One correlation matrix can't describe three clusters with different relationships inside them. The two runs use slightly different columns, so read this as a difference in kind. Check it on your own table.
Choose for your table
ReflectThink of a table you'd like to synthesize.
- Is it mostly numbers, or does it have important categories?
- Would an interaction matter, such as an effect that only appears in one region?
- Who sees the result?
Then pick a first generator, and say what you'd check on the scorecard to know you chose badly. Comparing two generators on the same table is cheap, so plan on that.
Check your understanding
CheckpointCheck yourself
You're using sequential trees. income depends on age and region. Which column order makes sense?
Show the answer
Answer: a
Each column is predicted from the ones before it, so put the columns that others depend on first.
Check yourself
You raise min_leaf for the trees. What do you expect?
Show the answer
Answer: a
Larger leaves pool more real rows, so any one person's values matter less, at some cost in detail.
Check yourself
Your table has a categorical plan column you need to keep. Which generators can use it?
Show the answer
Answer: a
The mixture models numbers and dates. Switching to it in the studio drops categorical columns, and the app tells you which ones.
Do it on your own data
In the appIn the studio, generate with two generators on one table, and compare their scorecards. This code shows sequential trees at work. It uses each library's own trees, so its rows differ from the app's, and R's synthpop package offers a full version.
import numpy as np
import pandas as pd
from sklearn.tree import DecisionTreeRegressor
df = pd.read_csv("customers.csv")[["age", "tenure_months", "visits"]].dropna()
rng = np.random.default_rng(1)
synth = pd.DataFrame({"age": rng.choice(df["age"], len(df))})
for i, col in enumerate(df.columns[1:], start=1):
before = list(df.columns[:i])
tree = DecisionTreeRegressor(min_samples_leaf=5, random_state=1).fit(df[before], df[col])
real_leaf, new_leaf = tree.apply(df[before]), tree.apply(synth[before])
values = df[col].to_numpy()
synth[col] = [rng.choice(values[real_leaf == leaf]) for leaf in new_leaf]
print(synth.head())library(rpart)
df <- na.omit(read.csv("customers.csv")[, c("age", "tenure_months", "visits")])
set.seed(1)
synth <- data.frame(age = sample(df$age, nrow(df), replace = TRUE))
for (i in 2:ncol(df)) {
col <- names(df)[i]
fit <- rpart(reformulate(names(df)[seq_len(i - 1)], col), df, minbucket = 5)
real_leaf <- predict(fit, df)
synth[[col]] <- vapply(predict(fit, synth), function(leaf) {
donors <- df[[col]][real_leaf == leaf]
donors[sample.int(length(donors), 1)]
}, numeric(1))
}
head(synth)Recap
Recap- A copula is fast and rarely copies, but one set of correlations can't describe clusters or interactions.
- Sequential trees keep interactions and shapes by taking real values from donors, so check privacy.
- A mixture suits numeric tables with clusters.
- Compare two generators on your own table; the scorecard decides, not the name.
Concepts in this lesson