A three-second baseline for file transfers

Fit a constant on two transfer times, then measure its error on a transfer the fit never saw.

Last week I separated what I know before a transfer from what I measure afterwards. I want to keep the same rows for this example so there is only one new thing to work out. How do I tell whether a prediction is any good?

The three sample transfers are 1 MB in 2 seconds, 2 MB in 4 seconds and 3 MB in 6 seconds. Set aside the third row before fitting. The first two are the training set. The row I set aside is the test set for this exercise.

A baseline gives me something to beat. This one predicts the same time for every file. Fit it by averaging the two training durations, (2 + 4) / 2 = 3 seconds. Freeze that prediction before looking at the test duration. The test transfer took 6 seconds, so the absolute error is the distance between 6 and 3, which is 3 seconds.

The print statement below returns the prediction and its absolute error in that order. Try changing only test to 8. The prediction should stay at 3 and the error should grow to 5. If your prediction moves when you change the test target, inspect where that value enters the fit.

One transfer gives me one error. I would keep the errors from more transfers before comparing two models. If I use a set of results to choose the model, I need a separate set for the final check. Next week I can fit a line and compare it with this constant.

Companion exercises from the daily series

Run each example independently using Python 3. The inputs are sample transfers.

Fit the simplest baseline

Set the third file transfer aside. Use only the first two observed durations to make one constant prediction. Compare it with the untouched answer.

The training mean is (2 + 4) / 2 = 3 seconds. The held-out transfer took 6, so the miss is 3 seconds.

from statistics import mean
train = [2, 4]
prediction = mean(train)
print(prediction, abs(6 - prediction))

Expected output

3 3

Reference: https://docs.python.org/3/library/statistics.html

Keep the direction of a prediction error

Choose a sign convention before logging errors. Subtract the observed time from the prediction. A negative result means the transfer took longer than predicted.

With a prediction of 3 seconds and actual times of 2 and 6, the signed errors are +1 and −3 seconds.

predicted = 3
actuals = [2, 6]
print([predicted - actual for actual in actuals])

Expected output

[1, -3]

Reference: https://scikit-learn.org/stable/modules/model_evaluation.html

Keep the individual errors

Before you publish an average, keep the miss for each held-out transfer. The list tells you which rows improved when you compare a second model.

Errors of 3 and 5 seconds average to 4. The larger miss is still 5 seconds.

from statistics import mean
errors = [abs(y - 3) for y in [6, 8]]
print(errors, mean(errors))

Expected output

[3, 5] 4

Reference: https://docs.python.org/3/library/statistics.html

Watch signed errors cancel

Use signed errors to see direction. Use their absolute values when you need the size of a typical miss. Two large misses can cancel in a signed average.

A two-second overshoot and a two-second undershoot average to zero. Their mean absolute error is 2 seconds.

from statistics import mean
errors = [2, -2]
print(mean(errors), mean(map(abs, errors)))

Expected output

0 2

Reference: https://docs.python.org/3/library/statistics.html

An outlier moves a mean more than a median

Take the same three training times and change just the longest. Compare how much the mean and median constants move before deciding which error matters to you.

For 2, 4 and 6 both are 4 seconds. Replacing 6 with 24 moves the mean to 10; the median stays 4.

from statistics import mean, median
for times in [[2, 4, 6], [2, 4, 24]]:
    print(mean(times), median(times))

Expected output

4 4
10 4

Reference: https://docs.python.org/3/library/statistics.html

Compare a baseline on a later transfer

Fit on earlier transfers and test on a later one. Record the time boundary before choosing a model. Keep the later answer out of the training mean.

Early times of 2 and 4 give a prediction of 3 seconds. The next transfer takes 8, so the miss is 5.

from statistics import mean
train = [2, 4]
new_time = 8
print(mean(train), abs(new_time-mean(train)))

Expected output

3 5

Reference: https://scikit-learn.org/stable/modules/model_evaluation.html

Compare models on the same rows

Use the same observed times for both predictors. Report each error before comparing the averages so a harder test batch cannot be mistaken for a worse model.

For actual times 6 and 8, constant 3 has errors 3 and 5. Predictions 5 and 7 have errors 1 and 1.

from statistics import mean
actual = [6, 8]
for pred in [[3, 3], [5, 7]]:
    err = [abs(p-y) for p, y in zip(pred, actual, strict=True)]
    print(err, mean(err))

Expected output

[3, 5] 4
[1, 1] 1

Reference: https://scikit-learn.org/stable/modules/model_evaluation.html

Work through the mathematics

Mean training time = (2 + 4) / 2 = 3 seconds. Absolute error on the held-out transfer = |6 − 3| = 3 seconds.

Run in Python 3

train = [2, 4]
prediction = sum(train) / len(train)
test = 6
print(prediction, abs(test - prediction))

Expected output

3.0 3.0

The team guide · My original notes on testing coding agents · Working with Lightbulb Partners