--- title: "Synthetic data from a profile" author: "Richard Aubrey White" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Synthetic data from a profile} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r setup, include = FALSE} # error = FALSE stops the build at an unexpected error. The chunks that show a # refusal set error = TRUE for themselves, because the error is what they show. knitr::opts_chunk$set( collapse = TRUE, comment = "#>", error = FALSE ) ``` csdb makes synthetic data for one table in two steps. A profile summarises the real table with aggregate SQL, under disclosure control. A simulator then draws new rows from the profile. A `SyntheticTable` holds the contract of the table and a catalogue of fixed datasets, so that a test can name the data it uses. This vignette works at the level of one table. Two other vignettes build on it: - cs9, [`vignette("synthetic-data", package = "cs9")`](https://niphr.github.io/cs9/articles/synthetic-data.html): synthetic tables, synthetic mode and switchable sources in a surveillance system. - norsyss.cs9, `vignettes/synthetic-development.Rmd` in the private repository `niphr/norsyss.cs9`: one late task of a real pipeline, run on synthetic inputs alone. Every chunk here runs on SQLite. ## A table to profile The table holds 600 made-up consultations. Each row is one visit of one person. ```{r table} library(csdb) library(data.table) set.seed(1) visits <- data.table( id = sample(1:150, 600, replace = TRUE), date = as.Date("2024-01-01") + sample(0:120, 600, replace = TRUE), sex = sample(c("female", "male"), 600, replace = TRUE), age = sample(0:95, 600, replace = TRUE), consultations = rpois(600, 3), municipality = sample(sprintf("m%03d", 1:40), 600, replace = TRUE) ) visits[1:3, date := as.Date("2024-09-30")] tab <- DBTable_v9$new( dbconfig = list(driver = "SQLite", db = tempfile(fileext = ".sqlite")), table_name = "visits", field_types = c( id = "INTEGER", date = "DATE", sex = "TEXT", age = "INTEGER", consultations = "INTEGER", municipality = "TEXT" ), keys = NULL, validator_field_types = validator_field_types_blank, validator_field_contents = validator_field_contents_blank ) invisible(tab$create_table()) tab$insert_data(visits) ``` Three rows move to 2024-09-30, a week with no other visit. Disclosure control has to deal with them below. ## Sensitivity levels Each column gets one of four sensitivity levels. A column that `columns` does not name is `schema_only`. | Level | What the profile publishes | |---|---| | `full` | the values and their counts; above `max_levels`, bins for a number and `n_distinct` for text | | `coarsen` | a date by ISO week, a number by bins | | `schema_only` | the type and the share of missing values | | `exclude` | nothing beyond the field type | `profile_table()` reads the query, the field types and the keys from the table. `profile_query()` does the same for any SQL query. ```{r profile} p <- profile_table( tab, columns = c( id = "exclude", date = "full", sex = "full", age = "coarsen", consultations = "full" ), k = 5, max_levels = 10, group_sizes = list(rows_per_id = "id") ) p$columns$sex p$columns$municipality ``` `municipality` is `schema_only`, because `columns` does not name it. The profile holds no value of it. ## Suppression by merge The profile never writes a count below `k`. On an ordered dimension, a cell below `k` merges with its neighbours instead of being removed: - Days merge into ISO weeks, then calendar months, then calendar quarters. The column takes the finest resolution at which every cell holds `k`. - Bins of a number merge into runs of adjacent bins. A merged bin keeps the outer breaks of its run. - Group sizes merge into ranges of row counts. No exact smallest or largest row count is published. The three late visits are the only visits in their week, their month and their quarter. Each of those cells holds 3, below `k = 5`. So `date` comes out by quarter, and the third quarter joins the second in one run: ```{r merge} names(p$columns$date) data.table( from = p$columns$date$quarters, to = p$columns$date$quarters_to, n = p$columns$date$n ) ``` `consultations` has more than `max_levels` distinct values, so it is published as bins. A number column also records `nonnegative`, which is `TRUE` when its minimum is at least 0. The simulator then draws no value below 0. ```{r bins} p$columns$consultations$breaks p$columns$consultations$nonnegative p$group_sizes$rows_per_id ``` An unordered cell, such as a level of a `TEXT` column, cannot merge. A cell below `k` is removed, and the removed counts are summed into `suppressed_n` when that sum is at least `k`. ## The profile refuses a torn read `profile_query()` reads the data with several aggregate queries, in one read snapshot where the driver gives one. It then checks that the counts of each column, plus its missing values, add up to `n_rows`. A profile that fails the check is torn, and the function refuses it. A concurrent write without a snapshot tears a profile. This chunk simulates one. It mocks the internal reader of the missing counts, so that 10 rows are inserted through the same connection just after that read. Every later read then sees 610 rows, and `n_rows` is 600: ```{r torn} con <- tab$dbconnection$autoconnection real_read <- csdb:::sql_missing_counts torn <- testthat::with_mocked_bindings( sql_missing_counts = function(con, sql, columns) { out <- real_read(con, sql, columns) DBI::dbExecute(con, "INSERT INTO visits SELECT * FROM visits LIMIT 10") return(out) }, tryCatch( profile_query( con, "SELECT * FROM visits", columns = c(sex = "full"), field_types = c(sex = "TEXT") ), error = function(e) conditionMessage(e) ), .package = "csdb" ) stopifnot(grepl("Torn profile", torn)) cat(torn, " ") DBI::dbGetQuery(con, "SELECT COUNT(*) AS n FROM visits")$n ``` The function ends its transaction with a rollback, so the 10 inserted rows are gone again. ## Write and read the profile A profile is a YAML file. `read_profile()` gives back an identical object. ```{r yaml} f <- write_profile(p, tempfile(fileext = ".yaml")) cat(readLines(f)[1:16], sep = "\n") identical(read_profile(f), p) ``` The provenance names the query, the driver, `k`, `noise`, `max_levels` and the csdb version. It holds no user name. ## What a profile may contain A profile of real data is a release of aggregates. It MUST have the approval of the data owner before it leaves the restricted environment. That holds for a profile in a package directory, in a test fixture and in a git repository. Suppression keeps every published count at `k` or above. It does not decide whether the set of aggregates is safe to publish. That decision is the data owner's. With `noise = list(epsilon = e)`, the profile adds Laplace noise of scale `1 / e` to each count before suppression. ## Simulate and compare `simulate_from_profile()` draws new rows from the published summaries. It reads the profile only, never the profiled rows. ```{r simulate} sim <- simulate_from_profile(p, n = 600, seed = 1) head(sim) ``` `profile_compare()` checks how far the simulation is from the real profile. Profile the simulation with the same `columns`, `k` and `max_levels` first: ```{r compare} s <- profile_data( sim, field_types = p$field_types, columns = c( id = "exclude", date = "full", sex = "full", age = "coarsen", consultations = "full" ), k = 5, max_levels = 10, group_sizes = list(rows_per_id = "id") ) profile_compare(p, s) ``` Each row has its own rule for the threshold: - A table of counts, such as `levels`, `quarters`, `bins` or `group_sizes`: the function draws `n_boot` multinomial tables of the synthetic size from the real shares. It merges each draw with the `k` of `real`. The threshold is the 95th percentile of their distances, plus `margin`. - `missing_share`: `n_boot` binomial draws of `synth$n_rows` rows at the real share. The threshold is the 95th percentile of their distances, plus `margin`. A share of `"suppressed"` on either side gives no row. - `n_distinct`: the distance is 0 inside the range that `synth$n_rows` uniform draws from the real values can reach. The threshold is `margin`. - `dependency`: the distance is 0 when both profiles agree on `holds`, otherwise 1. The threshold is 0. - A summary of `real` that `synth` lacks, or holds in a form that does not align: distance 1, threshold `margin`. For a table of counts, a smaller simulation gets a wider threshold. No draw models the noise that `noise` adds to a profile. ## A SyntheticTable A `SyntheticTable` holds the contract of one table: its field types, its keys, `validator_field_contents` and named invariants. Every data.table that it returns passed `$validate()`. An invariant is a `function(d)` or a `function(d, world)`. It returns `TRUE`, or a message that says why `d` fails. ```{r st} dir <- tempfile("visits-") dir.create(dir) write_profile(p, file.path(dir, "profile.yaml")) ft <- p$field_types invariants <- list( age_nonnegative = function(d) { if (all(d$age >= 0, na.rm = TRUE)) TRUE else "age is negative." } ) st <- synthetic_table_from_dir( dir, name = "visits", field_types = ft, invariants = invariants ) print(st$random(n = 5, seed = 3)) ``` `$random()` with `seed = NULL` draws a seed and reports it in a message, so a failure can be replayed. ## Named datasets A test SHOULD name the dataset it uses. `$add_dataset()` stores a recipe in `datasets.yaml`. A recipe holds `n`, `seed`, `overrides`, the md5 of the world as `world_md5`, the row count as `rows`, and the md5 of the data. The name `default` is the generator output at `seed = 1` and `n = 1000`. A generator that chooses its own size uses `n = NULL` there. ```{r recipe} world <- list(sex = c("female", "male")) st$add_dataset( "default", n = 1000, seed = 1, description = "The generator output at seed 1.", world = world ) st$add_dataset( "all_female", n = 200, seed = 2, description = "Every row is female.", overrides = list(sex = "female"), world = world ) cat(readLines(file.path(dir, "datasets.yaml")), sep = "\n") ``` A handmade dataset is a CSV file in `handmade/`. Each column is read as text and converted to its field type. An empty cell is `NA`. ```{r handmade} dir.create(file.path(dir, "handmade")) writeLines( c( "id,date,sex,age,consultations,municipality", "1,2024-01-01,female,40,2,m001", "2,2024-01-02,male,,1,m002" ), file.path(dir, "handmade", "two_visits.csv") ) st$list_datasets() print(st$dataset("two_visits")) ``` `$dataset()` regenerates a recipe. When the md5 of the world equals `world_md5`, it checks the row count against `rows` and the md5 against `md5`: ```{r dataset} nrow(st$dataset("all_female", world = world)) st$check_hashes(world = world) ``` ## The hash check after a generator change A change of the generator changes the data of every recipe. The hash check then stops, and the error names the table and the dataset. Here a new generator draws 1 to 10 consultations in place of the profile: ```{r hash, error = TRUE} changed <- function(profile, n, seed, world) { d <- simulate_from_profile(profile, n = n, seed = seed, world = world) d[, consultations := sample(1:10, .N, replace = TRUE)] return(d) } st2 <- synthetic_table_from_dir( dir, name = "visits", field_types = ft, invariants = invariants, generator = changed ) st2$dataset("all_female", world = world) ``` After a deliberate change, delete the recipe from `datasets.yaml` and add it again with `$add_dataset()`. A test that reads the dataset then sees the new data. For a `world` other than the recipe's, `$dataset()` validates the data, does not check the md5, and says so in a message. ```{r cleanup, include = FALSE} tab$dbconnection$disconnect() unlink(dir, recursive = TRUE) ```