सामग्री पर जाएँ

लेसन 21 · Pitfalls and Checks

Sanity Checks and Uncertainty

Validate your analysis with reconciliation, invariants, assertions and second methods, then use the bootstrap to show how uncertain your estimates are.

Before you share a number, try to break it. A few cheap checks catch many errors: a join that duplicated rows, a filter that removed too much or a unit that changed. After that, ask a second question: how much could this number move if you had slightly different data?

Reconcile totals

Reconciliation means comparing your result with an independent source: a finance report, a database count or a manual tally. Also check that parts add up to the whole after each step.

Python Track rows through every step

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In this tiny example the two filters are exact opposites, so the checks must pass. In a real pipeline, with many joins and transformations, the same checks can catch a lost or duplicated row.

Write invariants as checks

An invariant is a rule that must always be true: keys are unique, shares sum to 100%, quantities are not negative, an end date is not before a start date. Turn each into a check that runs every time the analysis runs.

Python A reusable check report

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Compare decimal numbers with a tolerance, as above, because floating-point arithmetic is not exact. An empty table would pass every "all" check, so the non-empty check matters. For important pipelines, stop with an error on failure. Python skips assert statements when it runs with the -O option, so use an explicit if and raise in production code. See testing DataFrames.

Use a second method

Compute a headline number two ways: with pandas and with SQL, or as a grand total and as the sum of its groups. Check a few rows by hand, from raw record to final result. When the two methods disagree, you often learn something about missing values or duplicates.

SQL Groups must add up to the total

Press Run (or Ctrl + Enter) to see the result here.

If you report only north and south, 20 units silently disappear. Show an "unknown" row.

Order-of-magnitude checks

Ask whether the number is plausible. If a small cafe appears to sell 40,000 coffees a day, something is wrong. Rough estimates, such as "about 200 orders a day times about 10 per order", are powerful tests.

Uncertainty from sampling

If your data is a sample from a larger population, a different sample would give a somewhat different result. The bootstrap estimates this variation: resample your rows with replacement many times, compute the statistic each time and look at the spread.

Python A percentile bootstrap interval

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The interval shows that 15 values do not pin down the median precisely. Learn more in bootstrap and permutation tests.

Limits of the bootstrap

The bootstrap cannot fix

Biased sample It repeats the bias
Dependent rows Resample users or blocks
Tiny samples Too little to learn from
Bad measurement Errors stay in
1 / 2

If several rows belong to one user, they are not independent: resample users, not rows. For time series, ordinary resampling destroys the order. If your data covers a whole population, sampling error may not be the main concern, but measurement error and change over time still are.

Stability checks

Rerun your main result with reasonable alternatives: another time window, a different outlier rule, with and without incomplete rows. If the conclusion survives, you can say so. If it flips, report that it is fragile.

A narrow interval does not mean a correct answer. It describes sampling variation under your assumptions, not bias, errors or wrong definitions.

What to do when a check fails

Do not silence a failing check by loosening it until it passes. First find the rows that break the rule and look at them. Then decide: is the data wrong, is your rule wrong or did the world change? A new product line with negative adjustments, for example, can make a "quantity is positive" rule outdated. Update the rule with a note that explains why.

A useful check report shows how many rows failed and a few example keys, so that someone can investigate quickly. Take care that error messages and logs do not copy personal data.

Try it yourself

Extend the check report so that each failing check also prints the IDs of the rows that broke it. Then repeat the bootstrap with a sample twice as long (use the same values twice) and notice that the interval can look narrower. Explain why copying rows does not add real information.

Say how sure you are

Combine the results of your checks into a plain statement. You might write that the total matches the independent source, that the pattern holds in both halves of the period and that the sample is too small to pin down the size of the difference. This tells the reader which part is solid and which is not. A single label such as "validated" hides that difference.

How much checking is enough?

Match the effort to the stakes. A quick internal question may need only a row count and a plausibility check. A number that will guide spending, appear in public or affect people deserves reconciliation, a second method and a review by another person.

A common mistake

Do not check a result only against a copy of itself. If two reports use the same faulty join, they will agree and both be wrong. A useful check takes a different route, such as a source system total or a hand count of raw records.

Write checks early

Write your first checks right after loading the data, not only at the end. A failed check at the start saves you from building an hour of work on a broken table.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What is reconciliation?

    It confirms that nothing was lost or duplicated unexpectedly.
  2. 2. How does the bootstrap create new samples?

    Each resample has the same size, and rows can repeat.
  3. 3. Each user has many rows. What should you resample?

    Rows from one user are related, so the user is the sampling unit.
  4. 4. What does a narrow bootstrap interval not protect against?

    A biased sample can give a precise but wrong estimate.

Summary

  • Reconcile totals and track rows through each step.
  • Encode invariants as checks that run with the analysis.
  • Verify key results with a second method and a rough plausibility estimate.
  • The bootstrap shows sampling variation, but not bias or measurement error.
  • Test whether conclusions survive reasonable alternative choices.

Next: Charts That Communicate Findings

आपकी प्रगति इसी ब्राउज़र में सेव होती है। खाते की ज़रूरत नहीं है।