Section 14.13

Set versus multiset

Definition

A set is a collection of distinct elements: each possible element is either in it or not, and adding an element that is already present changes nothing. A multiset, also called a bag, also records how many times each element occurs, a number called its multiplicity. Neither keeps an order. The difference that matters: a set answers which values occurred, while a multiset also answers how often, so counts, totals and averages over all occurrences need a multiset or a list.

The idea

A program logs how long each of three requests took: 1 millisecond (ms), 1 ms and 9 ms. Someone removes duplicate entries before computing statistics, and the log becomes the two values 1 and 9. But the two requests that took 1 ms were different requests that happened to take the same time. Removing one deletes a real observation, and the average duration moves from 11/3≈3.6711 / 3 \approx 3.67 ms to 10/2=510 / 2 = 5 ms.

Picture three levels of record keeping. A list remembers every observation and its order. A multiset forgets the order but keeps how often each value occurred. A set also forgets how often. No later computation can recover what a level has forgotten. Deduplicating, meaning removing duplicate entries, turns a multiset into a set. That is correct only when the duplicates are copies of one observation, not separate observations that happen to be equal.

How it works

A set supports membership (is xx present?), insertion (with no effect if xx is present), union, intersection and difference (the elements in either set, in both, or in the first only), and size, the number of distinct elements. A hash table §14.7 makes the average cost of a membership test independent of the size; a balanced search tree §14.10 makes it grow with the logarithm of the size and keeps the elements sorted.

A multiset is stored as a map from each element to its multiplicity, a positive whole number; an absent element has multiplicity 0. Inserting xx adds 1 to its multiplicity, removing one copy subtracts 1, and the size is the sum of the multiplicities. Combining two multisets has several meanings: the sum adds multiplicities, the union takes the larger and the intersection the smaller. Python’s §12.23 collections.Counter is such a map. The C++ container std::multiset instead stores every copy separately, which keeps any data that the comparison ignores.

Database tables are multisets by default. In SQL (Structured Query Language), UNION ALL keeps duplicate rows and UNION removes them; COUNT(*) counts rows and COUNT(DISTINCT x) distinct non-null values.

To deduplicate measurement records correctly:

  1. Give each record an identity, a key that names the event it describes, such as (source device, sequence number), where the source increases the sequence number for every event.

  2. Group records by identity, not by value. Records with equal values but different identities are separate events, and all of them stay.

  3. Within one identity, check that the copies agree. Identical copies, such as a log line written twice after a retried transfer, collapse to one record. Copies that disagree are a conflict to report, not a duplicate to drop.

The records that remain give the denominator, the count that a statistic such as an average divides by. The right denominator follows from the estimand §11.2, the quantity the study sets out to estimate: for the mean duration per request it counts requests; for the share of devices that ever failed it counts devices, and many records from one device count once.

Worked example

A log holds four rows, each giving an event identity and a duration:

The estimand is the mean duration per event, and there are three events.

  1. Keep every row. The total is 1+1+1+9=121 + 1 + 1 + 9 = 12 ms over 4 rows, a mean of 12/4=312 / 4 = 3 ms. Event e2 counts twice, so the denominator 4 is one too large.

  2. Deduplicate by identity. The rows for e1, e2 and e3 remain. The total is 1+1+9=111 + 1 + 9 = 11 ms, a mean of 11/3≈3.6711 / 3 \approx 3.67 ms. This is the intended answer.

  3. Deduplicate by value. The set {1,9}\{1, 9\} remains. The total is 1+9=101 + 9 = 10 ms, a mean of 10/2=510 / 2 = 5 ms. Events e1 and e2 merged because their durations were equal, so the denominator 2 is one too small.

As a multiset, the correct sample is m(1)=2m(1) = 2 and m(9)=1m(9) = 1, where m(x)m(x) is the multiplicity of the value xx. The number of observations is 2+1=32 + 1 = 3, and the total is 2×1+1×9=112 \times 1 + 1 \times 9 = 11 ms, as in method 2. The set {1,9}\{1, 9\} keeps only the fact that both multiplicities are positive.

Other statistics move too. The share of observations at or below 1 ms, which is the value at 1 ms of the empirical cumulative distribution function §11.17 (ECDF), is 2/3≈0.672 / 3 \approx 0.67 for the correct sample, 3/4=0.753 / 4 = 0.75 with every row kept, and 1/2=0.51 / 2 = 0.5 for the set. The median §11.13, the middle value, is 1 ms for the correct sample 1,1,91, 1, 9, but (1+9)/2=5(1 + 9) / 2 = 5 ms for the set.

Stated precisely

Let UU be the set of possible values. A multiset over UU is a function mm that gives each xx in UU a multiplicity m(x)m(x) in {0,1,2,…}\{0, 1, 2, \dots\}. Its support {x:m(x)>0}\{x : m(x) > 0\} is the set of values that occur. The number of observations is N=∑xm(x)N = \sum_{x} m(x), and the number of distinct values DD is the size of the support. Deduplicating by value replaces every positive m(x)m(x) by 1, so it changes NN into DD. The two are equal exactly when no value occurs more than once.

For numbers, the mean of the multiset is ∑xxm(x)/N\sum_{x} x \, m(x) / N, and the mean after deduplicating by value is ∑x:m(x)>0x/D\sum_{x : m(x) > 0} x / D. The two means are equal whenever all positive multiplicities are equal; otherwise they are equal only for particular values, by coincidence.

Deduplicating records by identity is correct under one condition: all records with the same identity describe the same event and carry the same values. The result then holds exactly one record per event, so every statistic per event uses the intended denominator.

Limits and common mistakes

Equal values do not mean the same event, and one event can appear with different values. Deduplicating by value merges separate events; deduplicating whole rows keeps two copies of one event that differ only in a field added on the way, such as the time the line was logged. Only an identity key settles which records describe one event.

Deduplicating by a hash of each record, a short value computed from its content, merges two different records whenever their hashes are equal, which is a hash collision §14.8.

Deduplication does not create independence. One device that reports ten events gives ten events but only one device. When the study’s question is about devices, the experimental unit §11.3, the unit that the sample size counts, is the device. Treating the ten events as ten independent observations of devices is pseudoreplication §11.6.

A set needs an equality rule. The texts 1.0 and 1.00 are different strings but the same number, so a set of strings keeps both and a set of numbers keeps one.

Sets and multisets keep no order; when order or timing matters, keep the list.

Questions answered

Does deduplicating records change the intended scientific denominator?

Deduplicating records changes the intended denominator, the count a statistic divides by, whenever it removes a separate observation; removing only extra copies of one observation restores it instead. That denominator counts the units the study is about, such as events, programs or devices. Deduplicating by value treats the sample as a set, which keeps each value once, and drops separate events that happened to be equal: durations of 1, 1 and 9 ms become 1 and 9, the count falls from 3 to 2, and the mean rises from about 3.67 ms to 5 ms. Deduplicating by an identity key, such as source and sequence number, removes only repeated copies of one event. So define the unit and its identity first, deduplicate by identity, keep repeated values, and report copies that share an identity but disagree as conflicts.

Related entries: Estimand and estimator (§11.2), Empirical cumulative distribution function (ECDF) (§11.17), Hash collision versus cryptographic collision (§14.8).