•
17 min read
Calculus for Machine Learning

At its core, calculus is how neural networks learn. Think of a model with millions of weights as a massive control panel of knobs. Calculus tells the model exactly which direction to turn each knob to reduce error—finding the “downhill” path to optimization.

To understand how it works under the hood, here are the 5 core concepts broken down:

  1. The Infinity Principle:
    Break complex structures into infinitely small parts to study them.
  • Example: Finding the area of a circle by slicing it up and arranging those slices into a rectangle.

The Infinity Principle

  1. Approximation (“Tending To”):
  • Example: X→2X \to 2 means X is extremely close to 2 (like 1.999999 or 2.000001), not necessarily the exact integer.
  1. Geometry Meets Algebra:
    Every algebraic equation has a twin—a geometric shape represented visually on a graph.

Algebra-parabola

  1. The Idea of the Slope:
    Slope measures steepness (Vertical Distance / Horizontal Distance). Depending on the graph, slopes can be constant (straight lines) or variable (curves).

  2. Mathematical Representation:
    The formal formula for slope is expressed as: Slope=ΔyΔx\text{Slope} = \frac{\Delta y}{\Delta x}

1. Introduction to Differentiation

1.0 Core Concept: Finding the slope of a graph.

  • Differentiation: The mathematical process of calculating a derivative.
  • Derivative: The resulting function

1.1 Slope of a Line

  • Basic Formula: slope=ΔyΔx=y2−y1x2−x1\text{slope} = \frac{\Delta y}{\Delta x} = \frac{y_2 - y_1}{x_2 - x_1}
  • Using Function Notation (f(x)f(x)): Replacing yy with f(x)f(x) gives the slope function between two points: slope=f(x2)−f(x1)x2−x1\text{slope} = \frac{f(x_2) - f(x_1)}{x_2 - x_1}
  • Secant Line: This represents the slope of a secant line connecting two points on a curve.

1.2. From Secant to Tangent (Derivatives)

  • Approximation: On a curve, a secant line approximates the slope between two points.
  • Tangent Line: When the distance between the two points approaches zero (h→0h \to 0), the secant line becomes a tangent line.
  • The slope of this tangent line represents the instantaneous rate of change at a single point, which is the derivative (f′(x)f'(x)).

1.3. The Derivative Formula:

f′(x)=lim⁡h→0f(x+h)−f(x)hf'(x) = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h}

1.4 Example: Finding the Derivative of f(x)=x2f(x) = x^2:

Problem: Find the slope of the tangent line to f(x)=x2f(x) = x^2 at any point xx.

Step-by-step Solution using the limit definition:

Set up the formula:

f′(x)=lim⁡h→0(x+h)2−x2hf'(x) = \lim_{h \to 0} \frac{(x+h)^2 - x^2}{h}

Expand the numerator:

f′(x)=lim⁡h→0x2+2xh+h2−x2hf'(x) = \lim_{h \to 0} \frac{x^2 + 2xh + h^2 - x^2}{h}

Simplify terms:

f′(x)=lim⁡h→02xh+h2h=lim⁡h→0(2x+h)f'(x) = \lim_{h \to 0} \frac{2xh + h^2}{h} = \lim_{h \to 0} (2x + h)

Evaluate the limit as h→0h \to 0:

f′(x)=2xf'(x) = 2x

(The slope of the tangent line to f(x)=x2f(x) = x^2 at any point xx is 2x2x.)

  • Secant line: Line passing through 2 points on curve.
  • Tangent line: Line passing thriugh the curve at one point without crossing it.

1.5 Derivative Rules:
Derivative rules help make calculations easier without always needing the long limit definition.

A. Power Rule & Constant Rules:

  • Power Rule:
ddx(xn)=nxn−1\frac{d}{dx}(x^n) = n x^{n-1}
  • Constant & Linear Rules:
ddx[cx]=c\frac{d}{dx}[cx] = c ddx[x]=1\frac{d}{dx}[x] = 1 ddx[c]=0\frac{d}{dx}[c] = 0

Examples with Special Powers:

  • Negative Power: ddx(x−2)=−2x−3\frac{d}{dx}(x^{-2}) = -2x^{-3}

  • Fractional Power: ddx(x4/3)=43x43−1=43x1/3\frac{d}{dx}(x^{4/3}) = \frac{4}{3}x^{\frac{4}{3} - 1} = \frac{4}{3}x^{1/3}

  • Polynomial Example: If f(x)=x4+2x3+x2+4x−1f(x) = x^4 + 2x^3 + x^2 + 4x - 1, then f′(x)=4x3+6x2+2x+4f'(x) = 4x^3 + 6x^2 + 2x + 4

B. Product Rule:
Used when finding the derivative of the product of two functions (uu and vv).

  • Formula:ddx(u⋅v)=u⋅dvdx+v⋅dudx\frac{d}{dx}(u \cdot v) = u \cdot \frac{dv}{dx} + v \cdot \frac{du}{dx}

  • Example: Find derivative of f(x)=(x2+1)(x3+x+5)f(x) = (x^2 + 1)(x^3 + x + 5)
    Apply rule: f′(x)=(x2+1)(3x2+1)+(x3+x+5)(2x)f'(x) = (x^2 + 1)(3x^2 + 1) + (x^3 + x + 5)(2x)
    Expand: f′(x)=3x4+3x2+x2+1+2x4+2x2+10xf'(x) = 3x^4 + 3x^2 + x^2 + 1 + 2x^4 + 2x^2 + 10x
    Simplify: f′(x)=5x4+6x2+10x+1f'(x) = 5x^4 + 6x^2 + 10x + 1

C. Quotient Rule:
Used when finding the derivative of a division of two functions (uv\frac{u}{v}).

  • Formula: ddx(uv)=vdudx−udvdxv2(where v≠0)\frac{d}{dx}\left(\frac{u}{v}\right) = \frac{v \frac{du}{dx} - u \frac{dv}{dx}}{v^2} \quad (\text{where } v \neq 0)

  • Example: Find derivative of f(x)=x2+13x−2f(x) = \frac{x^2 + 1}{3x - 2}
    Apply rule: f′(x)=(3x−2)(2x)−(x2+1)(3)(3x−2)2f'(x) = \frac{(3x - 2)(2x) - (x^2 + 1)(3)}{(3x - 2)^2}
    Simplify numerator: 6x2−4x−3x2−3(3x−2)2=3x2−4x−3(3x−2)2\frac{6x^2 - 4x - 3x^2 - 3}{(3x - 2)^2} = \frac{3x^2 - 4x - 3}{(3x - 2)^2}

D. Chain Rule (Outside-Inside Rule):

  • Formula: dydx=(derivative of outside function, keeping inside alone)⋅(derivative of inside function)\frac{dy}{dx} = \left(\text{derivative of outside function, keeping inside alone}\right) \cdot \left(\text{derivative of inside function}\right)

  • Example: Find derivative of y=(3x+7)7y = (3x + 7)^7
    Apply rule: dydx=7(3x+7)6⋅(3)\frac{dy}{dx} = 7(3x + 7)^6 \cdot (3)
    Simplify: dydx=21(3x+7)6\frac{dy}{dx} = 21(3x + 7)^6

2. Partial Derivatives

  • When a function has more than one input, like f(x,y)f(x, y), it describes a surface rather than a curve. Hill analogy: the steepness you feel depends on which way you walk.

  • Partial derivatives give the slope in two specific directions:

  • ∂f∂x\frac{\partial f}{\partial x}: slope when you walk only along the xx-axis (yy stays fixed).
    ∂f∂y\frac{\partial f}{\partial y}: slope when you walk only along the yy-axis (xx stays fixed)
  • The core rule, Pick one variable, freeze the rest, and differentiate as if it were a single-variable problem.

2.0 Example: f(x,y)=x2+3xy+y2f(x, y) = x^2 + 3xy + y^2

  • Derivative with respect to xx (treat yy as a constant):

x2→2xx^2 \to 2x
3xy→3y3xy \to 3y, because 3y3y is just a constant multiplier on xx, like the 5 in 5x5x
y2→0y^2 \to 0, because it contains no xx, so it’s a constant and constants differentiate to 0.

∂f∂x=2x+3y\frac{\partial f}{\partial x} = 2x + 3y

  • Derivative with respect to yy (treat xx as a constant):

x2→0x^2 \to 0
3xy→3x3xy \to 3x
y2→2yy^2 \to 2y

∂f∂y=3x+2y\frac{\partial f}{\partial y} = 3x + 2y

2.1 Application: Neural Networks

  • “If we change one weight, how does the loss change?”

  • A neural network has a loss function that measures how wrong its predictions are, and that loss depends on thousands or millions of weights w1,w2,…w_1, w_2, \dots. The partial derivative ∂L∂wi\frac{\partial L}{\partial w_i} tells you how much the loss changes if you tweak just that one weight.

  • Collecting all of these partial derivatives gives the gradient. Training (gradient descent) repeatedly nudges each weight in the direction that lowers the loss.

3. Numerical vs Analytical derivative

  • There are 2 ways to compute derivaties:
    1. Analytical: Derive the exact formula by hand using calculus rules.
    2. Numerical: Calculate, using the formula with a tiny h (e.g. h = 0.001)

Here is a clear and structured explanation of your notes, which provide a fantastic intuitive breakdown of the Hessian matrix, second-order derivatives, and how they apply to optimization and Machine Learning.


4. Hessian Matrix

  • Gradient (∇f(x)\nabla f(x)): Represents the 1st derivative. It tells you the slope of the function at any given point (the steepness and the direction of the steepest ascent/descent).

  • Hessian Matrix (HH): Represents the 2nd derivative (fxxf_{xx}). It tells you the shape of the landscape (curvature)—how the slope itself is changing.

4.1. The Hessian Matrix Structure (For 2 Variables: xx and yy)

For a function of two variables, f(x,y)f(x, y), the Hessian is a 2×22 \times 2 matrix of second-order partial derivatives:

H=[fxxfxyfyxfyy]H = \begin{bmatrix} f_{xx} & f_{xy} \\ fyx & f_{yy} \end{bmatrix}

  • fxxf_{xx}: Rate of change of the slope in the xx-direction.

  • fyyf_{yy}: Rate of change of the slope in the yy-direction.

  • fxyf_{xy} (and fyxf_{yx}): Mixed partial derivatives—how the slope in the xx-direction changes as you move in the yy-direction (and vice versa).

4.2 Example Analysis (“The Bowl”):

Looking at your example function f(x,y)=x2+y2f(x, y) = x^2 + y^2:

  1. First derivatives: fx=2xf_x = 2x, fy=2yf_y = 2y

  2. Second derivatives: fxx=2f_{xx} = 2, fyy=2f_{yy} = 2, fxy=0f_{xy} = 0

  3. Hessian Matrix:

H=[2002]H = \begin{bmatrix} 2 & 0 \\ 0 & 2 \end{bmatrix}

Because both diagonal entries (22 and 22) are positive and constant, the surface curves upward in all directions like a bowl (a shape representing a local minimum).

4.3. Classifying Critical Points (The Determinant Test) :

At a point where the gradient is zero (flat spot), the Hessian helps determine what kind of point it is by computing the determinant DD:

D=fxx⋅fyy−(fxy)2D = f_{xx} \cdot f_{yy} - (f_{xy})^2

ConditionResult / ShapePoint Type
D>0D > 0 and fxx>0f_{xx} > 0BowlLocal Minimum
D>0D > 0 and fxx<0f_{xx} < 0DomeLocal Maximum
D<0D < 0SaddleSaddle Point

4.4. Why Derivatives Matter & Why We Use Higher Orders

  • 1st Derivative: Tells you the slope.

  • 2nd Derivative: Tells you the curvature and helps with optimization (knowing how fast you need to accelerate or brake).

  • 3rd Derivative: Used in fields like mechanical engineering (e.g., designing smooth elevator transitions so passengers don’t experience abrupt jerks in acceleration, known as jerk).

4.5. Why the Hessian Matters in Machine Learning

Standard Gradient Descent (GD) only looks at the slope (1st derivative). Newton’s Method, however, leverages the Hessian matrix to optimize weights more intelligently:

  • Standard Gradient Descent update:

wnew=wold−learning rate×gradientw_{\text{new}} = w_{\text{old}} - \text{learning rate} \times \text{gradient}

  • Newton’s Method update:

wnew=wold−H−1×gradientw_{\text{new}} = w_{\text{old}} - H^{-1} \times \text{gradient}

Key Intuition on Curvature:

  • In steep directions (high curvature/large Hessian), Newton’s method takes smaller steps to avoid overshooting.

  • In flat directions (low curvature/small Hessian), it takes larger steps to move faster through flat regions.

Production Reality:

While Newton’s method is mathematically powerful, computing and inverting a massive Hessian matrix for millions of neural network weights is computationally expensive. That is why Adam (Adaptive Moment Estimation) is typically used as the default optimizer in deep learning, striking a balance by approximating adaptive learning rates efficiently.

Final summary note highlights this evolution:

ADAM>Newton>GD\text{ADAM} > \text{Newton} > \text{GD}

5. Chain Rule:

  • The Chain Rule is a method in calculus that helps us find the derivative of a function that is inside another function.

  • A simple example - Real-life intuition: Imagine this:

  • You press the accelerator in a car.
  • The engine speed (RPM) increases.
  • The car’s speed increases because of the engine speed. Here, one thing affects another thing, which affects something else.

The chain rule helps us calculate how changes pass through a chain of dependent variables.

  • Why is this important in Machine Learning?

Input(x) -> layer1 -> layer2 -> layer3 -> Final output

A neural network has multiple layers. Each layer depends on the previous layer.

During training, we need to understand how changing a weight affects the final error (loss). The chain rule allows us to calculate this

6. Taylor Series Approximation:

Imagine you have a complicated mathematical function, and you want to calculate its value near a point. Instead of calculating the complicated function directly, you approximate it using a simple polynomial. That is the main idea behind a Taylor Series Approximation.

  1. example: Suppose you want to calculate (exe^x), where (ee) is Euler’s number.

We know that (exe^x) can be complicated to calculate by hand. Taylor series lets us approximate it using:

ex≈1+x+x22!+x33!e^x \approx 1+x+\frac{x^2}{2!}+\frac{x^3}{3!}

Here, 2!=2×1=22! = 2 \times 1 = 2, and 3!=3×2×1=63! = 3 \times 2 \times 1 = 6.

Let’s calculate (e0.1e^{0.1}).

Substitute (x=0.1): e0.1≈1+0.1+0.122+0.136e^{0.1}\approx 1+0.1+\frac{0.1^2}{2}+\frac{0.1^3}{6}

=1+0.1+0.005+0.000167=1+0.1+0.005+0.000167

e0.1≈1.105167\boxed{e^{0.1}\approx1.105167}

The actual value is approximately (1.105171). Our approximation is very close!

6.1. Why This Matters

In fields like computer vision, and machine learning optimization, computers often have to deal with complex loss functions or non-linear models where exact calculations are too computationally expensive. By using Taylor expansions (especially first-order linear approximations or second-order quadratic approximations like the Hessian), we can locally linearize or simplify complex landscapes to make gradient descent and numerical solving feasible!

These notes provide a clear, step-by-step introduction to integral calculus, moving from the basic geometric intuition of area under a curve to solving both definite and indefinite integrals using the power rule.

Here is a comprehensive breakdown of the concepts covered across your notes:


7. Integral?

  • Core Concept: An integral is essentially a continuous summation. If you slice an area under a curve into infinitely thin vertical strips (dxdx) and add them all up, you get the exact area under the curve.

  • Notation: Represented by the symbol ∫\int, the area under a function y=f(x)y = f(x) from a starting point to an ending point is written as:

Area=∫y dx\text{Area} = \int y \, dx

7.1. Area Without vs. With Integrals

  • Straight-Line Graphs (No Integral Needed): For simple linear functions like f(x)=xf(x) = x, you can calculate the area under the curve using basic geometry formulas (like the area of a triangle: Area=12×base×height\text{Area} = \frac{1}{2} \times \text{base} \times \text{height}).

  • Curved Graphs (Integral Needed): When a graph curves, geometric formulas no longer work directly because the top boundary is not a straight slope. This is where calculus and integration become necessary to sum up the infinite thin strips.

7.2. Definite vs. Indefinite Integrals

Your notes distinguish between two main types of integrals:

  • Definite Integrals:

  • Have lower & upper limits (numbers attached to the integral sign, e.g., ∫04\int_{0}^{4}).

  • Result: Evaluates to a specific numerical value (representing an exact area).

  • Example : Evaluating ∫04x dx\int_{0}^{4} x \, dx yields a final area of 88.

  • Indefinite Integrals:

  • Do not have limits.

  • Result: Yields a general function rather than a single number. Because differentiation of a constant is zero, integration of a function leaves an unknown vertical shift, so we must add a constant of integration (+C+ C) where C∈RC \in \mathbb{R} (real numbers).

7.3. Key Integration Rules & Examples Solved

The Power Rule for Integration:

∫xn dx=xn+1n+1+C(for n≠−1)\int x^n \, dx = \frac{x^{n+1}}{n+1} + C \quad (\text{for } n \neq -1)

Walkthrough of Examples:

  1. Definite Integral Example :

∫04x dx=[12x2]04\int_{0}^{4} x \, dx = \left[ \frac{1}{2}x^2 \right]_{0}^{4}

  • Plugging in the upper limit (44) and lower limit (00):

=(12(4)2)−(12(0)2)=8−0=8= \left(\frac{1}{2}(4)^2\right) - \left(\frac{1}{2}(0)^2\right) = 8 - 0 = 8

  1. Indefinite Integral with a Coefficient :

∫4x6 dx=4x6+16+1+C=47x7+C\int 4x^6 \, dx = \frac{4x^{6+1}}{6+1} + C = \frac{4}{7}x^7 + C

  1. Polynomial Integration :

∫(x2+2x−7) dx\int (x^2 + 2x - 7) \, dx

  • Integrate term by term:

  • ∫x2 dx=x33\int x^2 \, dx = \frac{x^3}{3}

  • ∫2x dx=2x22=x2\int 2x \, dx = \frac{2x^2}{2} = x^2

  • ∫−7 dx=−7x\int -7 \, dx = -7x (using the rule ∫c dx=cx\int c \, dx = cx)

  • Final Answer: 13x3+x2−7x+C\frac{1}{3}x^3 + x^2 - 7x + C

  1. Integration with Constants as Parameters :

∫(8ax4+17x3)dx\int \left(8ax^4 + \frac{1}{7}x^3\right) dx

  • Treating aa as a real number constant:

  • ∫8ax4 dx=8ax55\int 8ax^4 \, dx = \frac{8a x^5}{5}

  • ∫17x3 dx=17⋅x44=128x4\int \frac{1}{7}x^3 \, dx = \frac{1}{7} \cdot \frac{x^4}{4} = \frac{1}{28}x^4

  • Final Answer: 8a5x5+128x4+C\frac{8a}{5}x^5 + \frac{1}{28}x^4 + C (where a,C∈Ra, C \in \mathbb{R})

8. Multivariate Chain Rule — Explained Simply

The multivariate chain rule is a method in calculus for finding how a function changes when it depends on multiple variables, and those variables are also changing.

Think of it as: When one thing changes, it affects multiple things, which together affect the final result.

8.1. First, understand the normal chain rule

Suppose:

y=x2y=x^2

and

x=3x=3

Here, yy depends on xx, and xx depends on tt.

Algebra-parabola

To calculate how yy changes with tt:

dydt=dydx×dxdt\frac{dy}{dt} = \frac{dy}{dx} \times \frac{dx}{dt}

We know:

dydx=2x,dxdt=3\frac{dy}{dx} = 2x, \qquad \frac{dx}{dt} = 3

Therefore:

dydt=2x×3=6x=18t\frac{dy}{dt} = 2x \times 3 = \boxed{6x = 18t}

8.2. Now, the multivariate chain rule

Imagine a function depends on two variables instead of one.

z=x2+y2z = x^2 + y^2

But both xx and yy depend on another variable tt:

x=2t,y=3tx = 2t, \qquad y = 3t

Here’s the relationship:

Algebra-parabola

There are two paths from tt to zz: through xx and through yy.

When tt changes:

  • xx changes, affecting zz.
  • yy changes, also affecting zz.

So, to find the total change in zz, we add the effects from both paths.

The formula is:

dzdt=∂z∂xdxdt+∂z∂ydydt\boxed{\frac{dz}{dt} = \frac{\partial z}{\partial x}\frac{dx}{dt} + \frac{\partial z}{\partial y}\frac{dy}{dt}}

The symbol ∂\partial means partial derivative: differentiate with respect to one variable while treating the others as constants.

8.3. Solve the example step by step

We have:

z=x2+y2,x=2t,y=3tz = x^2 + y^2, \quad x = 2t, \quad y = 3t

Step 1: Find the partial derivatives.

∂z∂x=2x\frac{\partial z}{\partial x} = 2x ∂z∂y=2y\frac{\partial z}{\partial y} = 2y

Step 2: Find how xx and yy change with tt.

dxdt=2,dydt=3\frac{dx}{dt} = 2, \qquad \frac{dy}{dt} = 3

Step 3: Apply the multivariate chain rule.

dzdt=(2x)(2)+(2y)(3)=4x+6y\frac{dz}{dt} = (2x)(2) + (2y)(3) = 4x + 6y

Substitute x=2tx = 2t and y=3ty = 3t:

dzdt=4(2t)+6(3t)=8t+18t\frac{dz}{dt} = 4(2t) + 6(3t) = 8t + 18t dzdt=26t\boxed{\frac{dz}{dt} = 26t}

That’s it!

9. Jacobian Matrix

A Jacobian matrix is a table of derivatives that tells us how multiple outputs change when multiple inputs change.

Think of it as an extension of the ordinary derivative to functions with several inputs and outputs.

9.1. First, remember the derivative

Suppose:

y=x2y = x^2

Its derivative is:

dydx=2x\frac{dy}{dx} = 2x

At x=3x = 3:

dydx=2(3)=6\frac{dy}{dx} = 2(3) = 6

This means when xx increases by a tiny amount, yy increases approximately 6 times as much.

But what happens when a function has two inputs and two outputs?

That’s where the Jacobian comes in.

9.2. Imagine a machine with two inputs

Algebra-parabola

Suppose the machine has these functions:

f(x,y)=x2+yf(x,y) = x^2 + y g(x,y)=xyg(x,y) = xy

Notice:

  • Inputs: x,yx, y
  • Outputs: f,gf, g

We want to know how each output changes when we change either input.

9.3. Calculate the Jacobian matrix

First, calculate the partial derivatives.

For f(x,y)=x2+yf(x,y) = x^2 + y:

∂f∂x=2x,∂f∂y=1\frac{\partial f}{\partial x} = 2x, \qquad \frac{\partial f}{\partial y} = 1

For g(x,y)=xyg(x,y) = xy:

∂g∂x=y,∂g∂y=x\frac{\partial g}{\partial x} = y, \qquad \frac{\partial g}{\partial y} = x

Now arrange these derivatives into a matrix:

J=[∂f∂x∂f∂y∂g∂x∂g∂y]\boxed{ J = \begin{bmatrix} \dfrac{\partial f}{\partial x} & \dfrac{\partial f}{\partial y} \\ \dfrac{\partial g}{\partial x} & \dfrac{\partial g}{\partial y} \end{bmatrix} }

Substitute the derivatives:

J=[2x1yx]\boxed{ J = \begin{bmatrix} 2x & 1 \\ y & x \end{bmatrix} }

That’s the Jacobian matrix!

Each row represents one output, and each column represents one input.

9.4. Let’s calculate with real numbers

Suppose x=2x = 2 and y=3y = 3.

J(2,3)=[4132]J(2,3) = \begin{bmatrix} 4 & 1 \\ 3 & 2 \end{bmatrix}

Here’s what it means:

ValueMeaning
4How fast ff changes with xx
1How fast ff changes with yy
3How fast gg changes with xx
2How fast gg changes with yy

So if we increase xx by a very small amount, ff changes approximately 4 times that amount, while gg changes approximately 3 times that amount.

9.5. Visual intuition: What does the Jacobian actually do?

Imagine drawing a tiny square on a rubber sheet. A mathematical transformation stretches, rotates, or shears that sheet.

Algebra-parabola

The Jacobian tells us approximately how the transformation stretches, rotates, or shears a tiny region near a point.

This is especially useful in computer graphics, robotics, physics, and machine learning.

9.6. Jacobian vs Gradient vs Hessian

ConceptWhat it tells us
DerivativeHow one output changes with one input
GradientHow one output changes with multiple inputs
JacobianHow multiple outputs change with multiple inputs
HessianHow the gradient itself changes

Important connection to machine learning: The Jacobian is used in backpropagation, alongside the multivariable chain rule, to calculate how changes in model parameters affect outputs.

🎉 Congrats, you have completed Calculus Part-1.