Course outline

Data Analysis / Advanced: Defensible and Reproducible Decisions / Lesson 17

Scale and Numerical Reliability

Preserve analytical meaning while reducing memory or computation and audit precision, overflow, and aggregation boundaries.

About 5 min readContent reviewed
On this page

Lesson goals

  • Distinguish floating approximation from data uncertainty.
  • Combine chunk summaries using sufficient information.
  • Review dtype, alignment, and performance assumptions.

A faster wrong metric is still wrong

Optimization begins with a correct metric contract and a representative workload. Changing join order, deduplication scope, or missing-value treatment can alter the answer even if execution becomes faster. Keep a small trusted reference calculation and reconciliation checks before adding vectorization, chunking, or another storage format.

Measure memory, runtime, and output equivalence separately. A benchmark on three rows does not establish behavior for a large skewed key distribution. Allocation pressure can come from intermediate tables or a join explosion rather than a slow arithmetic loop. Fix the analytical grain and algorithm before tuning individual expressions.

Numerical approximation is not sampling uncertainty

Floating-point operations round finite representations. The first program uses a deliberately simple sequential accumulator where a small value is lost between very large opposite values, then compares math.fsum on the same inputs. The more accurate sum is one. This is a numerical issue, separate from whether the observations represent the target population.

Do not assume the built-in sum has the same algorithm across Python releases; Python3.12 changed floating summation behavior. The example explicitly implements the naive loop so its teaching point is clear. Exact decimal or rational representations can suit a domain, but they do not repair bad units, measurement error, or an invalid statistical design.

python Compare a naive loop with a compensated library sum
from math import fsum

values = [1e16, 1.0, -1e16]
naive = 0.0
for value in values:
    naive += value
print(f"naive={naive:.1f} accurate={fsum(values):.1f}")
Expected output
naive=0.0 accurate=1.0

The explicit loop loses the unit-sized term; math.fsum retains a result of1.0 for these fixed values.

Keep counts when combining partial means

The second program processes two chunks using count and total, then derives the overall mean. Averaging chunk means equally would give2.75 instead of3 because the chunks contain different numbers of observations. Streaming can preserve this metric with sufficient information; a median or arbitrary quantile generally needs a different exact or approximate strategy.

Chunk boundaries must not define new analytical groups accidentally. Deduplicating within each chunk misses duplicates across chunks, and joining each chunk to a duplicate-key lookup can still multiply rows. Validate global keys and totals under a design that covers the entire dataset rather than assuming local checks imply global correctness.

python A correctly weighted chunk aggregate
chunks = [[1, 2], [3, 4, 5]]
count = 0
total = 0
for chunk in chunks:
    count += len(chunk)
    total += sum(chunk)
if count == 0:
    raise ValueError("no observations")
print(f"count={count} mean={total / count:.2f}")
Expected output
count=5 mean=3.00

The combined total15 and count5 produce mean3.00 independent of these chunk sizes.

Array and table semantics affect arithmetic

NumPy fixed-width integer arrays can overflow even though Python integers have arbitrary precision. Choose an adequate dtype before the risky operation; converting an already overflowed result is too late. Broadcasting can also produce a larger array or a different calculation than intended. Assert shapes and units before trusting a vectorized result.

pandas aligns labeled data in many operations, while a raw NumPy array uses positional axes. Combining differently indexed Series can introduce missing values or unintended matches. A faster conversion to arrays may remove labels that were providing correctness. Document whether the operation is label-based or positional and test reordered input.

Tolerances must come from the domain

Floating comparisons can need absolute and relative tolerances. NumPy isclose uses a documented formula with a reference operand and defaults that may be unsuitable near zero. Choose tolerances from measurement precision and acceptable computational error, not a universal constant. Never use tolerance to conceal a unit conversion or denominator mistake.

Profile representative runs, retain intermediate row counts and checks, and compare outputs against the reference implementation under meaningful tolerances. Report approximations such as sketches or sampling when exact computation is replaced. Reproducibility should record the algorithm and dtype as well as the package version, because both influence numerical results.

Common errors and fixes

Casting to a wider dtype fails to restore a large value.

Why it happens
Overflow occurred before conversion.
How to fix it
Choose the accumulator/operand dtype before arithmetic.
Prevent it next time
Test boundary magnitudes and intermediate ranges.

A chunked mean differs from the full-data mean.

Why it happens
Partial means were averaged without counts.
How to fix it
Combine totals and valid counts.
Prevent it next time
Test unequal chunk sizes and missing values.

A reordered table changes a positional calculation.

Why it happens
Labels and positions were mixed.
How to fix it
Align by validated keys or enforce the intended order.
Prevent it next time
Test permuted input and unmatched labels.

Check your understanding

1. Two chunks have means2 and10 from counts2 and8. What is the full-data mean, and which information is essential?

Show worked solution

Recover totals4 and80 and divide their sum84 by the combined count10, giving8.4. The equal mean of the two means is6 and answers a different equal-chunk question. Keep valid observation counts, not merely raw row counts if measurements can be missing. The counts are essential for recombination.

2. Why can a tolerance of0.01 be appropriate for one metric and unacceptable for another?

Show worked solution

It depends on scale, units, measurement resolution, and the decision sensitivity. A hundredth of a unit may be negligible for a large count but material for a small rate or calibrated measurement. Use a documented absolute/relative policy and test near-zero values. First rule out structural errors such as mismatched denominators or units; approximate equality cannot validate those.

Key takeaways

  • Numerical error, sampling uncertainty, and measurement error are different.
  • Chunk summaries need enough information to preserve the target metric.
  • Dtypes, shapes, alignment, and tolerances are part of correctness.

Official references