Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Beginner45 min

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.

Diagram
  • Input → x
  • Function → Rule
  • Output → y

What is a function?

A function connects input and output.

Equation y = f(x)

Read it as:

y is a function of x

Example:

Equation f(x) = 2x + 1

This means:

  1. Take x
  2. Multiply by 2
  3. Add 1

Step-by-step example

If:

Equation f(x) = 2x + 1

Find the output when x = 3

Equation f(3) = 2(3) + 1
Equation = 6 + 1 = 7

Answer: 7

Input (x) Output f(x)
1 3
2 5
3 7
4 9

The rule never changes.

Visual graph

Diagram

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.

Diagram

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

python
# Function definition
def f(x):
    return 2 * x + 1

print(f(3))
print(f(10))

Output:

text
7
21

f() 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.

Diagram

You can drive from start to finish without interruption.

Now imagine a broken bridge.

Diagram

You must stop.

That gap is called a discontinuity.

Mathematical definition

A function is continuous at x = a if these three conditions are true:

  1. f(a) exists

  2. limₓ → ₐ f(x) exists

  3. 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:

Equation f(x) = 2x + 1

Find continuity at x = 3.

Step 1

Equation f(3) = 7

Step 2

Equation limx → ₃ f(x) = 7

Step 3

Both are equal.

Equation 7 = 7

Therefore: Continuous

Graph

Diagram

One smooth line.

No breaks.

Python Example

python
def price(area):
    return 5000 * area

print(price(1000))
print(price(1001))

Output:

text
5000000
5005000

A 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.

Diagram

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:

Equation f(x) = 2x + 1

We write the limit as:

Equation limx → ₃ f(x)

Read it as:

The limit of f(x) as x approaches 3

Notice it says approaches, not equals.

Step-by-step example

Function:

Equation f(x) = 2x + 1

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:

Equation limx → ₃ (2x + 1) = 7

Even before reaching 3, we already know the answer.

Visual graph

Diagram

The red circle shows the value the function approaches.

Left-hand limit (LHL)

Approaching from the left.

Equation x → 3^-

Think:

Diagram

Examples:

  • 2.5
  • 2.8
  • 2.99

All move toward 3.

Notation:

Equation limx → ₃^- f(x)

Right-hand limit (RHL)

Approaching from the right.

Equation x → 3^+

Examples:

  • 3.5
  • 3.1
  • 3.01
Diagram

Notation:

Equation limx → ₃^+ f(x)

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

python
def f(x):
    return 2 * x + 1

values = [2.9, 2.99, 2.999]

for x in values:
    print(x, f(x))

Output:

text
2.9    6.8
2.99   6.98
2.999  6.998

Notice 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.

Diagram

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:

Equation (dy)/(dx) = 1

Meaning:

Salary increases 1 lakh for every 1 year.

Graph intuition

A derivative is the slope of the curve.

Diagram

The red tangent line touches the curve at one point.

Its slope is the derivative.

Mathematical definition

If:

Equation y = f(x)

Derivative is written as:

Equation (dy)/(dx)

Or:

Equation f'(x)

Both mean the same thing.

Read it as:

The rate of change of y with respect to x.

Easy example

Function:

Equation f(x) = x²

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:

Equation (d)/(dx)(x²) = 2x

At x = 3:

Equation 2(3) = 6

Slope = 6

Gradient Descent Formula

This is the most important ML equation.

Equation weight update w_new = w_old − α · dL/dw
Symbol Meaning
w Weight
α Learning rate
dL/dw Derivative of loss

The model updates weights using derivatives.

Python Example

python
import sympy as sp

x = sp.symbols('x')

f = x**2

print(sp.diff(f, x))

Output:

text
2*x

diff() 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:

Diagram

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
Equation y = f(x)

Partial derivative:

  • Multiple inputs → One output
Equation z = f(x, y)

Now we can change x or y independently.

Example Function

Suppose:

Equation f(x, y) = 2x + 3y

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:

Equation (∂f)/(∂x)

Rule:

Treat y as constant.

Equation f(x, y) = 2x + 3y

Derivative:

Equation (∂f)/(∂x) = 2

Meaning:

Every additional bedroom increases the score by 2 units.

Notice we ignored y.

Partial derivative with respect to y

Now differentiate only y.

Equation (∂f)/(∂y) = 3

Meaning:

Every additional 100 sq.ft increases the score by 3 units.

Visual intuition

Imagine a 3D hill.

Diagram

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:

Equation f(x, y, z)

The gradient is:

Equation ∇ f = ( (∂f)/(∂x), (∂f)/(∂y), (∂f)/(∂z) )

Think of it as a vector of slopes.

Variable Partial Derivative
BHK 10
Area 0.04
Parking 5

Gradient:

Equation (10, 0.04, 5)

This vector tells the optimizer how each feature influences the output.

Python Example

python
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/∂y

Output:

text
2
3

Summary 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.

Diagram
  • 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:

Equation u = x²

Then:

Equation y = 3u

This is actually one combined function:

Equation y = 3x²

But we'll solve it step by step.

Step 2: Differentiate each step

First derivative:

Equation (du)/(dx) = 2x

Second derivative:

Equation (dy)/(du) = 3

Now apply the chain rule:

Equation (dy)/(dx) = (dy)/(du) × (du)/(dx)

Substitute:

Equation 3 × 2x = 6x

Answer: The derivative is 6x.

Why do we multiply?

Think of water flowing through pipes.

Diagram

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.

Diagram

If A changes D:

  • A affects B
  • B affects C
  • C affects D

The total effect equals:

Equation (dD)/(dA) = (dD)/(dC) × (dC)/(dB) × (dB)/(dA)

This is the chain rule.

Python Example

Using SymPy:

python
import sympy as sp

x = sp.symbols('x')

u = x**2
y = 3 * u

print(sp.diff(y, x))

Output:

text
6*x

The 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.