AI & Machine Learning Master Course — 015. Derivatives as local sensitivity
Smartyfy AI & Machine Learning Master Course
Course ID: smartyfy-ai-ml-master-200
Lesson 015: Derivatives as local sensitivity
Prerequisites: Lessons 011–014 and arithmetic with squares. Learning outcome: Estimate and interpret slopes for simple prediction and loss functions.
1. Ask how an output responds to a small change
A prediction function maps an input to an output. A loss function measures how far a prediction is from a target according to a chosen rule. In machine learning, we often need to know how either output changes when we adjust one input or parameter slightly. A derivative describes this local sensitivity.
Start with a simple packing-time prediction f(x) = 2x + 1, where x is package mass in kilograms and the output is minutes. Increasing mass from 3 to 3.1 kilograms raises the prediction from 7 to 7.2 minutes. The output change divided by the input change is 0.2 ÷ 0.1 = 2 minutes per kilogram.
This straight-line rule has the same slope everywhere. A curved function behaves differently: its slope depends on where we evaluate it. Derivatives let us summarize that changing slope at a particular point. They describe the mathematical function, however, and do not by themselves establish a causal relationship in the real world.
2. Move from average change to local slope
For a function f, the difference quotient at x with a nonzero step h is [f(x + h) − f(x)] ÷ h. It measures average change over the interval between x and x + h. Geometrically, it is the slope of the line joining two points on the graph.
Consider f(x) = x squared at x = 3. A step h = 1 gives (16 − 9) ÷ 1 = 7. A step of 0.1 gives (9.61 − 9) ÷ 0.1 = 6.1. A step of 0.01 gives (9.0601 − 9) ÷ 0.01 = 6.01. These slopes approach 6 as the step approaches zero.
The derivative, written f prime of x or df/dx, is the limit of this quotient when that limit exists. We let h approach zero; we do not substitute zero into a denominator. The publisher’s OpenStax introduction to the derivative explains the connection between limiting slopes and tangent lines.
For the square function, expand (x + h) squared into x squared + 2xh + h squared. Subtract x squared, divide by h, and obtain 2x + h. As h approaches zero, the remaining value is 2x. Therefore f prime of 3 equals 6, while f prime of −2 equals −4. One function can have different local sensitivities at different inputs.
3. Use a derivative for a local approximation
When a function is differentiable, a sufficiently small change Δx gives the approximation f(x + Δx) ≈ f(x) + f prime of x times Δx. At x = 3 for the square function, a change of 0.1 predicts 9 + 6 times 0.1 = 9.6. The actual value is 9.61, so the approximation misses by 0.01.
The missing amount here is the squared step. A change of 0.01 misses by only 0.0001. This illustrates why a derivative is local: it uses the tangent slope, while a curved function keeps bending. Large changes may make the approximation poor even when the derivative itself is calculated correctly.
The derivative’s sign gives a local direction of change. A positive slope means a sufficiently small positive step increases the function when the derivative is strictly positive; a negative slope means it decreases it. A zero derivative means the first-order approximation has no change. It does not prove a minimum: x cubed has derivative zero at zero but keeps increasing through that point.

4. Differentiate simple formulas
A constant has derivative zero because changing x does not change its value. A straight line ax + b has derivative a. For a positive integer n, the power rule gives the derivative of x to the power n as n times x to the power n − 1. Sums can be differentiated term by term, and constant multipliers remain in place.
For example, g(x) = 3x squared − 4x + 7 has derivative 6x − 4. At x = 2, the slope is 8. The value of g itself is 11, which is a different quantity. Distinguishing function value from derivative value prevents a common mistake: a large loss and a steep loss are not the same claim. See OpenStax differentiation rules for the underlying rules.
5. Interpret a loss derivative with one parameter
Hold an example’s input fixed at 2 and its target fixed at 7. Define a prediction by p(w) = 2w + 1, where w is the adjustable parameter. Squared-error loss is L(w) = [p(w) − 7] squared. Substituting the prediction gives (2w − 6) squared, which expands to 4w squared − 24w + 36.
Differentiating the expanded expression gives L prime of w = 8w − 24. At w = 2, the prediction is 5, the loss is 4, and the loss derivative is −8. A small increase in w should therefore reduce the loss. A change of 0.05 predicts an approximate loss change of −8 times 0.05 = −0.4.
Check the actual result: at w = 2.05, prediction is 5.1 and loss is (5.1 − 7) squared = 3.61. The actual change is −0.39, close to −0.4. This explains why slope information is useful for parameter adjustment. It does not yet specify a safe step size or establish performance on unseen data.
Notice which variable changes. The prediction slope with respect to w is 2, while the loss slope at w = 2 is −8. They answer different questions. We held the input, target, and intercept fixed throughout. Later lessons extend this reasoning to functions with many adjustable parameters.
6. Check derivatives numerically on a CPU
A centered difference uses [f(x + h) − f(x − h)] ÷ (2h). For sufficiently smooth functions it often gives a more accurate estimate than a one-sided difference at the same step size. This Python standard-library example compares finite differences with exact formulas. The math.isclose documentation describes the tolerance-based comparison used in its assertions.
import math
def centered_difference(f, x, h=1e-4):
if h <= 0:
raise ValueError("Choose a positive step")
return (f(x + h) - f(x - h)) / (2 * h)
def loss(w):
return (2 * w + 1 - 7) ** 2
square_slope = centered_difference(lambda x: x ** 2, 3)
loss_slope = centered_difference(loss, 2)
cubic_slope = centered_difference(lambda x: x ** 3, 2)
assert math.isclose(square_slope, 6, abs_tol=1e-8)
assert math.isclose(loss_slope, -8, abs_tol=1e-8)
assert math.isclose(cubic_slope, 12, abs_tol=1e-6)
assert math.isclose(loss(2.05), 3.61, abs_tol=1e-12)
print("square slope:", round(square_slope, 6))
print("loss slope:", round(loss_slope, 6))
print("loss change:", round(loss(2.05) - loss(2), 6))
h = 1e-20
tiny_step_slope = ((3 + h) ** 2 - 3 ** 2) / h
print("overly tiny step:", tiny_step_slope)
print("abs centered at zero:", centered_difference(abs, 0))
The first outputs are 6, −8, and −0.39. The overly tiny step produces zero in the tested floating-point environment because adding it does not change the stored value of 3. Smaller is therefore not always better. The final centered estimate is also zero, but absolute value has no derivative at zero: its left slope is −1 and right slope is 1. Numerical estimates are checks, not proofs of differentiability.
Practice: four questions with worked answers
1. Differentiate f(x) = 5x − 2 and interpret its slope.
Answer: The derivative is 5 everywhere. Increasing x by 0.2 changes the output by 1. Because the function is a straight line, this calculation is exact.
2. Approximate the change in x squared when x moves from −2 to −1.99.
Answer: The derivative at −2 is −4 and the step is 0.01. The approximate change is −0.04. The actual change is 3.9601 − 4 = −0.0399.
3. For our loss, find its value and derivative at w = 3.
Answer: Prediction is 7, matching the target, so loss is zero. Its derivative is 8 times 3 − 24 = 0. Here the squared loss reaches its minimum possible value.
4. Why not always choose the smallest representable finite-difference step?
Answer: Nearby function values can round to the same stored number, and subtracting them can lose significant digits. Compare estimates at reasonable steps rather than assuming tiny steps guarantee accuracy.
Knowledge check: five explained MCQs
1. A derivative describes: A. Local rate of change. B. Dataset size. C. Guaranteed causation.
Correct: A. It concerns sensitivity of a mathematical function at a point.
2. The derivative of x squared at x = 4 is: A. 16. B. 8. C. 2.
Correct: B. The derivative formula is 2x, while 16 is the function value.
3. A negative loss derivative suggests a small positive parameter step will: A. Increase loss locally. B. Leave every loss unchanged. C. Decrease loss locally.
Correct: C. The first-order change is derivative times step, which is negative here.
4. Does a zero derivative always prove a minimum? A. No. B. Yes. C. Yes, when the derivative is estimated numerically.
Correct: A. A flat slope can also occur at a maximum or another stationary point.
5. Why can absolute value’s centered estimate at zero be misleading? A. Its values are unavailable. B. Symmetry hides different one-sided slopes. C. The centered estimate uses only one side of zero.
Correct: B. The centered numerator cancels even though the derivative does not exist there.
Common pitfalls and summary
Keep the changing variable explicit, distinguish function values from slopes, and attach appropriate units to sensitivity. Do not extrapolate a local approximation across an arbitrary distance or treat a finite difference as a proof. Derivatives turn small changes into interpretable local predictions and prepare us to reason about gradients and optimization.