Hello! Welcome to the first lesson of our course on classic machine learning. We're kicking things off with a deep dive into the mathematical foundations that make these algorithms work, starting with a cornerstone concept: optimization.
Most problems in machine learning, from fitting a regression line to training a complex model, can be framed as finding the minimum of a function—typically a "loss" or "cost" function that measures how poorly the model performs. The properties of this function dictate how easy it is to find that minimum.
In this lesson, you'll learn to identify a critically important class of functions known as convex functions. For these functions, the task of finding a minimum is drastically simplified: any local minimum is guaranteed to be the global minimum. This property is the bedrock of many optimization algorithms we'll study. We will cover the formal definition of convexity and then explore two powerful methods for verifying it: the first-order and second-order conditions.
What is a Convex Function?
At its core, a convex function is one that curves "upwards". We can formalize this intuition in a few ways.
The Geometric Definition
The most intuitive way to think about a convex function is to visualize its graph. If you pick any two points on the function's curve and draw a straight line segment between them (a "chord"), the entire portion of the curve between those two points will lie on or below the line segment.
This idea is captured by Jensen's inequality. A function is convex if its domain is a convex set and for any two points and in its domain, and for any between 0 and 1, the following holds:
Here, represents any point on the line segment between and . The left side of the inequality is the function's value at that point, while the right side is the value on the straight line connecting and .
A function is strictly convex if the inequality is strict () for and . A function is concave if is convex.
To get a more formal handle on this, please read the introductory section of the following notes.
[PDF] Lecture 5. Convex Functions
These notes provide a rigorous introduction to convex functions.
Please read the first page, from the beginning down to the subsection "Extended-value functions". Pay close attention to the definition using Jensen's inequality and the critical theorem stating that for a convex function, any local minimum is also a global minimum. The proof of this theorem is short and insightful.
The property that local minima are global minima is precisely why convexity is so desirable in optimization. It means that an algorithm can't get "stuck" in a suboptimal valley, believing it has found the best solution when a better one exists elsewhere.
First-Order Condition for Convexity
For functions that are differentiable, we can use their derivatives to check for convexity. The first-order condition provides a powerful geometric interpretation.
A differentiable function is convex if and only if it always lies above its tangent lines. Formally, for any two points and in its domain:
Here, the term is the first-order Taylor approximation of around . This expression defines the tangent hyperplane (a line in 1D, a plane in 2D, etc.) to the function at point . The condition states that the function's value, , is always greater than or equal to the value predicted by the tangent line approximation based at .

This condition is not just a theoretical curiosity; it's the foundation for the correctness of gradient-based optimization methods.
For the formal statement and a proof, please continue with the lecture notes.
[PDF] Lecture 5. Convex Functions
This reading covers the derivation and implications of the first-order condition.
In the document, find the section titled "First order condition". Read through this section and its proof. You can stop before the corollary on "monotone gradient". The proof cleverly uses the definition of the derivative and Jensen's inequality to establish the result.
Second-Order Condition for Convexity
For functions that are twice differentiable, there is an even more direct and often easier test for convexity that involves the second derivative. In single-variable calculus, you know that if , the function is convex (or "concave up"). The second-order condition for multivariable functions is the natural generalization of this idea.
To understand this generalization, we first need to understand the multivariable equivalent of the second derivative: the Hessian matrix. This video from OptWhiz provides an excellent intuition for why the gradient is a vector and the Hessian is a matrix.
Gradients, Hessians, and All Those Derivative Tests
This video builds a strong geometric intuition for the gradient and the Hessian.
Please watch the first two minutes to understand the motivation (the introduction). Then, skip ahead and watch the derivation of the Hessian and its relationship to the directional second derivative (Hessian derivation). The key takeaway is that the Hessian matrix H allows us to find the curvature (second derivative) in any direction v via the quadratic form v^T H v.
With the concept of the Hessian in hand, the second-order condition is as follows:
A twice-differentiable function is convex if and only if its Hessian matrix, , is positive semidefinite for all in its domain.
A matrix is positive semidefinite (written as ) if for any non-zero vector , the quadratic form . If for all non-zero , the matrix is positive definite (), which implies strict convexity.
This is arguably the most common method used to verify convexity in machine learning.
The following video provides a clear, step-by-step walkthrough of how to compute a Hessian and test for positive semidefiniteness.
Operations Research 10B: Hessian Matrix, Convex & Concave Functions
This video from Yong Wang offers a practical guide to using the Hessian for convexity analysis.
Watch the video from the beginning until the conclusion of the main example. This will cover: The definition of the Hessian and its connection to convexity. An example of calculating the Hessian for a function of three variables. The definition of a positive semidefinite matrix. A demonstration of how to check the positive semidefinite condition for the example Hessian.
Examples
Let's solidify this with a few examples. The CS229 notes are a great resource for this.
This reading provides the formal second-order condition and several canonical examples.
Please read the sections titled "Second Order Condition for Convexity" and "Examples". Pay special attention to the quadratic function example, f(x) = \frac{1}{2}x^TAx + b^Tx + c. Its convexity is determined entirely by the matrix A.
Let's consider that quadratic function example more closely. The Hessian is simply . Therefore, the function is convex if and only if the matrix is positive semidefinite.
This is a profoundly important result. The objective function for ordinary least squares regression is , which can be expanded into a quadratic form in with the matrix . The matrix is always positive semidefinite, which means the least squares objective is always convex. This guarantees that the solution we find via the normal equations (or gradient descent) is the unique global minimum.
Conclusion
In this lesson, we established the fundamental importance of convexity for optimization in machine learning. We explored three key criteria for verifying if a function is convex:
- Jensen's Inequality: The formal definition, stating that the function's graph lies below any of its chords. .
- First-Order Condition: For differentiable functions, the graph must lie on or above all of its tangent hyperplanes. .
- Second-Order Condition: For twice-differentiable functions, the Hessian matrix must be positive semidefinite everywhere. .
Understanding these conditions is not just a mathematical exercise; it provides the theoretical guarantee that many of the algorithms we will study are able to find optimal solutions efficiently.
In our next lesson, we will put this theory into practice by deriving our first, and perhaps most famous, optimization algorithm: gradient descent. We will see how it uses the first-order information of a function to iteratively move towards a minimum, a process that is guaranteed to succeed for the convex functions we've just learned to identify.