Lesson 03
Calculus for AI — Derivatives, Partial Derivatives & Chain Rule
Learn calculus for machine learning: limits, derivatives, partial derivatives, and the chain rule with AI and real-estate pricing examples.
What You Will Learn
- Functions
- Continuity
- Limits
- Derivatives
- Partial Derivatives
- Chain Rule
Functions
A function is the foundation of Calculus and Machine Learning.
A function is a rule that takes an input and produces exactly one output.
Think of it as a machine.
- Input →
x - Function → Rule
- Output →
y
What is a function?
A function connects input and output.
Read it as:
y is a function of x
Example:
This means:
- Take x
- Multiply by 2
- Add 1
Step-by-step example
If:
Find the output when x = 3
Answer: 7
| Input (x) | Output f(x) |
|---|---|
| 1 | 3 |
| 2 | 5 |
| 3 | 7 |
| 4 | 9 |
The rule never changes.
Visual graph
Every input has one unique output.
Why are functions important in ML?
Every Machine Learning model is a function.
The model receives data and returns a prediction.
The model is learning the best function from features to a prediction.
Types of Functions (Calculus)
| Function | Example | ML relevance |
|---|---|---|
| Constant | f(x) = 5 | Baselines |
| Linear | f(x) = 2x + 3 | Linear Regression |
| Polynomial | x² + 2x + 1 | Polynomial Regression |
| Quadratic | x² | Optimization / loss |
| Exponential | eˣ | Growth, ML math |
| Logarithmic | log x | Losses, transformations |
| Sigmoid | 1 / (1 + e⁻ˣ) | Classification |
| ReLU | max(0, x) | Neural Networks |
| Softmax | — | Multiclass classification |
Python Example
# Function definition
def f(x):
return 2 * x + 1
print(f(3))
print(f(10))Output:
7
21f() is literally a Python function implementing the mathematical function.
Summary
| Term | Meaning |
|---|---|
| Input | x |
| Function | Rule / Model |
| Output | y |
| Notation | y = f(x) |
| ML Meaning | Model maps features → prediction |
Memory Trick
A Machine Learning model is simply a mathematical function that learns the best rule to convert inputs into outputs.
This is the foundation you'll need before moving to Lesson 2: Limits, where Calculus begins analyzing how functions behave as inputs change.
Interview question
Q: Why is every Machine Learning model a function?
Answer: Because a model maps inputs (features) to a single output (prediction). Training means finding the best function f(x) that minimizes error on real data.
Continuity
Before derivatives, you must understand continuity.
A function is continuous if you can draw its graph without lifting your pen.
In simple words:
No breaks. No holes. No jumps.
Intuition: Road without breaks
Imagine driving on a road.
You can drive from start to finish without interruption.
Now imagine a broken bridge.
You must stop.
That gap is called a discontinuity.
Mathematical definition
A function is continuous at x = a if these three conditions are true:
-
f(a) exists
-
limₓ → ₐ f(x) exists
-
limₓ → ₐ f(x) = f(a)
Don't memorize yet—understand the meaning.
| Condition | Meaning |
|---|---|
| f(a) exists | The function has a value |
| Limit exists | Left = Right |
| Both equal | No gap |
If all three are true → Continuous ✅
Example
Function:
Find continuity at x = 3.
Step 1
Step 2
Step 3
Both are equal.
Therefore: Continuous
Graph
One smooth line.
No breaks.
Python Example
def price(area):
return 5000 * area
print(price(1000))
print(price(1001))Output:
5000000
5005000A tiny input change produces a tiny output change.
This is continuity.
Summary Table
| Type | Continuous? | Example |
|---|---|---|
| Straight line | ✅ | y = 2x + 1 |
| Smooth curve | ✅ | y = x² |
| Hole | ❌ | Missing point |
| Jump | ❌ | Stair function |
| Vertical asymptote | ❌ | 1/(x−2) |
Memory Trick
Limit asks: What value are we approaching?
Continuity asks: Can I reach that value without any break?
Interview Question
Q: Why is continuity important in Machine Learning?
Answer: Most optimization algorithms (like Gradient Descent) assume the loss function changes smoothly. Continuous functions allow gradients to guide the model toward better solutions without unpredictable jumps or breaks.
Next lesson: Derivatives.
Limits
A limit is the first real idea in calculus. Before learning derivatives, you must understand what happens when a value gets very close to another value.
Limit = The value a function approaches as the input gets closer to a specific number.
Think of it as approaching, not necessarily reaching.
Intuition: A car approaching a traffic signal
Imagine a car moving toward a traffic light at 100 m.
The car reaches:
- 80 m
- 90 m
- 95 m
- 99 m
- 99.9 m
It is getting closer and closer to 100.
That idea is exactly what a limit means.
Mathematical definition
If a function is:
We write the limit as:
Read it as:
The limit of f(x) as x approaches 3
Notice it says approaches, not equals.
Step-by-step example
Function:
Let's approach 3.
| x | f(x) |
|---|---|
| 2.5 | 6 |
| 2.9 | 6.8 |
| 2.99 | 6.98 |
| 2.999 | 6.998 |
As x gets closer to 3, the output gets closer to 7.
Therefore:
Even before reaching 3, we already know the answer.
Visual graph
The red circle shows the value the function approaches.
Left-hand limit (LHL)
Approaching from the left.
Think:
Examples:
- 2.5
- 2.8
- 2.99
All move toward 3.
Notation:
Right-hand limit (RHL)
Approaching from the right.
Examples:
- 3.5
- 3.1
- 3.01
Notation:
When does a limit exist?
A limit exists only when the left-hand limit and the right-hand limit are equal.
If both sides approach 5, then the limit is 5.
When does a limit NOT exist?
If the left side approaches 5 and the right side approaches 8, there is no single value.
That break is a jump discontinuity.
Python Example
def f(x):
return 2 * x + 1
values = [2.9, 2.99, 2.999]
for x in values:
print(x, f(x))Output:
2.9 6.8
2.99 6.98
2.999 6.998Notice the outputs approach 7.
Summary Table
| Concept | Meaning |
|---|---|
| Limit | Value a function approaches |
| LHL | Approach from the left |
| RHL | Approach from the right |
| Limit Exists | LHL = RHL |
| No Limit | LHL ≠ RHL |
Memory Trick
Function asks: "What is the output?" Limit asks: "What output are we approaching?"
This idea is the bridge to Lesson 3: Continuity, where you'll learn when functions have no breaks or jumps.
Interview question
Q: When does a limit exist?
Answer: A limit exists only when the left-hand limit equals the right-hand limit. If the two sides approach different values, the overall limit does not exist.
Derivatives
This is the most important calculus concept for Machine Learning. Every ML model learns using derivatives.
Derivative = How fast the output changes when the input changes.
In simple words:
Derivative measures the rate of change (slope).
Intuition: Speedometer of a car
Imagine you're driving.
The speedometer doesn't tell where you are.
It tells how fast you're changing position.
A derivative does exactly the same thing:
- Position → Function
- Speed → Derivative
What does “rate of change” mean?
Suppose salary depends on experience.
| Experience (Years) | Salary (Lakh) |
|---|---|
| 1 | 4 |
| 2 | 5 |
| 3 | 6 |
| 4 | 7 |
Every extra year increases salary by 1 lakh.
The derivative is:
Meaning:
Salary increases 1 lakh for every 1 year.
Graph intuition
A derivative is the slope of the curve.
The red tangent line touches the curve at one point.
Its slope is the derivative.
Mathematical definition
If:
Derivative is written as:
Or:
Both mean the same thing.
Read it as:
The rate of change of y with respect to x.
Easy example
Function:
Let's see how it changes.
| x | y = x² |
|---|---|
| 1 | 1 |
| 2 | 4 |
| 3 | 9 |
| 4 | 16 |
Notice:
- 1 → 4 = +3
- 4 → 9 = +5
- 9 → 16 = +7
The change is increasing.
Derivative tells exactly how steep the curve becomes.
The derivative of x² is:
At x = 3:
Slope = 6
Gradient Descent Formula
This is the most important ML equation.
| Symbol | Meaning |
|---|---|
| w | Weight |
| α | Learning rate |
| dL/dw | Derivative of loss |
The model updates weights using derivatives.
Python Example
import sympy as sp
x = sp.symbols('x')
f = x**2
print(sp.diff(f, x))Output:
2*xdiff() computes the derivative.
Real-world intuition
| Situation | Derivative Means |
|---|---|
| Car | Speed |
| Salary | Growth per year |
| House Price | Change per sq.ft |
| Loss Function | Direction to reduce error |
| ChatGPT | How to update weights |
Summary
| Concept | Meaning |
|---|---|
| Function | Maps input → output |
| Limit | Approaches a value |
| Continuity | No breaks |
| Derivative | Rate of change / slope |
Memory Trick
Function tells what the output is.
Derivative tells how fast the output changes.
Interview Question
Q: Why are derivatives essential in Machine Learning?
Answer: Derivatives measure how the loss changes with respect to model parameters. Gradient Descent uses these derivatives to update weights in the direction that reduces prediction error, enabling the model to learn from data.
Next lesson: Partial Derivatives.
Partial Derivatives
You've learned derivatives for functions with one input. But most ML models have many inputs (BHK, Area, Parking, etc.). Partial derivatives solve that problem.
Partial Derivative = How the output changes when only ONE input changes, while all other inputs stay constant.
This is one of the most important concepts behind Gradient Descent, Neural Networks, and ChatGPT training.
Intuition: House price depends on multiple features
A house price is affected by many variables:
Inputs:
- BHK
- Area
- Parking
- Floor
Output:
Price
Now ask a specific question:
If only Area increases, how much does Price change?
That is a partial derivative.
Why “partial”?
Normal derivative:
- One input → One output
Partial derivative:
- Multiple inputs → One output
Now we can change x or y independently.
Example Function
Suppose:
Where:
- x = Bedrooms
- y = Area (hundreds of sq.ft.)
We want to know:
- Effect of Bedrooms
- Effect of Area
Separately.
Partial derivative with respect to x
Notation:
Rule:
Treat y as constant.
Derivative:
Meaning:
Every additional bedroom increases the score by 2 units.
Notice we ignored y.
Partial derivative with respect to y
Now differentiate only y.
Meaning:
Every additional 100 sq.ft increases the score by 3 units.
Visual intuition
Imagine a 3D hill.
The hill represents the function.
You can move:
- Left ↔ Right (x)
- Forward ↔ Backward (y)
Each direction has its own slope.
Those slopes are partial derivatives.
Gradient = Collection of Partial Derivatives
If a function has many variables:
The gradient is:
Think of it as a vector of slopes.
| Variable | Partial Derivative |
|---|---|
| BHK | 10 |
| Area | 0.04 |
| Parking | 5 |
Gradient:
This vector tells the optimizer how each feature influences the output.
Python Example
import sympy as sp
x, y = sp.symbols('x y')
f = 2 * x + 3 * y
print(sp.diff(f, x)) # ∂f/∂x
print(sp.diff(f, y)) # ∂f/∂yOutput:
2
3Summary Table
| Concept | Meaning |
|---|---|
| Derivative | Change of a function with one variable |
| Partial Derivative | Change of one variable while others stay constant |
| Symbol | ∂ |
| Gradient | Vector of all partial derivatives |
Memory Trick
Derivative asks: "How does the output change?" (1 input)
Partial Derivative asks: "How does the output change if ONLY this one input changes?" (Many inputs)
Interview Question
Q: Why are partial derivatives essential in neural networks?
Answer: Neural networks have millions or billions of parameters. Partial derivatives compute how the loss changes with respect to each parameter individually, allowing backpropagation and gradient descent to update every weight correctly.
Next lesson: Chain Rule.
Chain Rule
The Chain Rule is the calculus concept that makes deep learning possible. Every neural network—including ChatGPT—learns through repeated application of the chain rule during backpropagation.
Chain Rule = Find how the final output changes by multiplying the changes through every intermediate step.
A simple way to remember it:
If A affects B, and B affects C, then A indirectly affects C.
Intuition: Domino effect
Imagine three dominoes.
- x changes the hidden value u
- u changes the output y
- Therefore, x changes y
The chain rule measures this indirect effect.
Step 1: One function inside another
Suppose:
Then:
This is actually one combined function:
But we'll solve it step by step.
Step 2: Differentiate each step
First derivative:
Second derivative:
Now apply the chain rule:
Substitute:
Answer: The derivative is 6x.
Why do we multiply?
Think of water flowing through pipes.
The influence passes through every stage.
Each stage contributes its own derivative.
So we multiply them.
Why is it called “Chain”?
Because derivatives are connected like a chain.
If A changes D:
- A affects B
- B affects C
- C affects D
The total effect equals:
This is the chain rule.
Python Example
Using SymPy:
import sympy as sp
x = sp.symbols('x')
u = x**2
y = 3 * u
print(sp.diff(y, x))Output:
6*xThe derivative automatically applies the chain rule.
Summary Table
| Concept | Meaning |
|---|---|
| Derivative | Change of one function |
| Partial Derivative | Change of one variable in many variables |
| Chain Rule | Change through multiple connected functions |
| Main ML Use | Backpropagation |
Memory Trick
Derivative = One slope
Partial Derivative = One variable's slope
Chain Rule = Multiply slopes through every layer
Interview Question
Q: Why is the chain rule essential in deep learning?
Answer: Neural networks are composed of many nested functions (linear layers, activations, attention, etc.). The chain rule computes how the final loss changes with respect to each parameter by multiplying derivatives through every intermediate layer, enabling backpropagation and weight updates.