Pure functions and experiment configuration
Quick answer
A pure function produces the same result for the same inputs and has no observable side effects. In ML experiments, pure calculations make it easier to test a prediction separately from reading data or saving results. A configuration is a collection of settings, such as a fixed intercept and slope. These settings are not necessarily learned parameters; here we supply them directly.
A prediction with explicit parameters
A toy parcel duration rule uses 4 minutes of preparation plus 2 minutes per kilometer. def declares a function, return gives its result to the caller, and the three parameters make the assumptions visible. Distances are nonnegative integers in this example.
def predict(distance, intercept, slope): return intercept + slope * distance
print(predict(3, 4, 2))print(predict(3, 4, 2))Expected output:
1010Both calls return 10 minutes. No variable outside the function changes.
Compare two configurations
A dictionary groups named settings. Square brackets retrieve a value by its key. Passing the configuration lets us change an assumption without rewriting the formula.
def predict(distance, config): return config["intercept"] + config["slope"] * distance
first = {"intercept": 4, "slope": 2}second = {"intercept": 4, "slope": 3}print(predict(5, first))print(predict(5, second))print(first["slope"])Expected output:
14192The same distance has two predictions because the configurations differ. The first configuration remains unchanged. This comparison alone does not establish which model is better.
Common mistakes
Reading a changing global setting or modifying the input dictionary makes the result depend on hidden state. Pass settings explicitly and keep the function read-only. Printing inside a function is also a side effect; return the value and print in the surrounding experiment instead.
Practice and next steps
Predict the time for distance 0 with each configuration, then change only the intercept. Explain why identical inputs should produce identical results. The next lesson organizes multiple samples into records.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Pure functions and experiment configuration — knowledge check
Apply the lesson to small reproducible experiment scenarios.
Which is a pure prediction function?
With intercept 4, slope 3 and distance 2, what is the prediction?
Why pass a configuration dictionary?
What should a prediction function do with a supplied configuration?
Where should a display-only print normally go?