Validate and communicateLesson 55 of 76 · 7 min

Test a small case you can solve

Verify model arithmetic with boundary cases and independent references.

Verification asks whether the implementation follows the stated model. A small exact case is often more revealing than a large plausible-looking simulation. Choose inputs that force zero, certain or easily calculated outcomes.

For a stocking-loss function, demand equal to stock should create neither shortage nor waste. Zero demand should leave all stock unused. Demand above stock should incur shortage cost only for the missing units. These cases test signs, boundaries and units without relying on random variation.

Keep reference calculations independent of the implementation being checked. Copying the same expression into an expected-answer cell can preserve the same bug. Once the deterministic rule passes, test its use across random inputs and retain every failed case while investigating.

Solve a boundary case by hand
  1. No demand, stock 5
    L(0,5)=10L(0,5)=10
  2. Exact match
    L(5,5)=0L(5,5)=0
  3. Demand 8, stock 5
    L(8,5)=30L(8,5)=30
Reference illustration. These exact checks follow the authored shortage penalty 10 and leftover penalty 2. Boundary checks can catch accounting errors; they do not validate the demand distribution.

Can a plausible simulated mean conceal a sign error in a per-trial loss?

Write a prediction (optional)

Seeded teaching simulation

Select Run Python below with the code unchanged.

Tools
About Python ↗

The code is editable. Press Ctrl+Enter or ⌘+Enter to run Python from the editor.

Published example output

This result comes from the original Python. Your runs appear above. Build environment ↗

demand, stock, actual, expected: 0 5 10.0 10
demand, stock, actual, expected: 5 5 0.0 0
demand, stock, actual, expected: 8 5 30.0 30
All exact boundary cases passed.

Read the first result

The assertions check specific model properties. They do not establish that the penalty values describe real consequences.

Try an edit

Calculate the penalty by hand for demand 2 and stock 0, and for demand 5.25 and stock 5. Append (2, 0, 20), (5.25, 5, 2.5) to the cases list. Select Run Python again. Read the new demand, stock, actual, expected rows. Does each actual value match your hand calculation? What would an integer conversion do to the 2.5 result?

Back to the Python editor

Scope. A finite set of tests can miss untested cases and cannot establish empirical validity.

Try a fresh case

What does a passing implementation test establish?

Choose an answer
Need a hint?
Hint 1

Separate the code rule from the real-world mechanism.

Hint 2

Name exactly what the test compares.

Your notes