Quadratic functions¶
Part 1 · Preliminaries · Chapter 4 · Lecture notes, volume 1 (PDF) · Slides · Exercises: Quadratic functions · App: The matrix Q
4.1 Quadratic functions¶
Definition 4.1: quadratic function
Given a square symmetric matrix
the associated (scalar) function
is called a quadratic function.
-
If the square matrix of a quadratic function is not symmetric, it can be made symmetric by replacing each off-diagonal pair with its average:
\[ q_{ij} \leftarrow \frac{q_{ij}+q_{ji}}{2},~~~ q_{ji} \leftarrow \frac{q_{ij}+q_{ji}}{2}, \qquad \forall\, i,j \in \{1,2,\dots,n\},~ i<j \]This is valid because for every \(i,j \in \{1,2,\dots,n\}\) with \(i<j,\) we have:
\[\begin{align*} q_{ij}\,x_i x_j + q_{ji}\,x_j x_i &= q_{ij}\,x_i x_j + q_{ji}\,x_i x_j \\[2ex] &= (q_{ij}+q_{ji})\,x_i x_j \\[2ex] &= \tfrac12(q_{ij}+q_{ji})\,x_i x_j + \tfrac12(q_{ij}+q_{ji})\,x_i x_j \\[2ex] &= \frac{q_{ij}+q_{ji}}{2}\,x_i x_j + \frac{q_{ij}+q_{ji}}{2}\,x_j x_i \end{align*}\]Therefore, the quadratic functions defined by \(\boldsymbol{Q}\) and \(\tfrac{1}{2}(\boldsymbol{Q}+\boldsymbol{Q}')\) are equivalent, since they yield the same value for every \(\boldsymbol{x} \in \mathbb{R}^n\).
Without loss of generality, we can assume that the matrix \(\boldsymbol{Q}\) of a quadratic function is symmetric.
Example 4.1: equivalent matrices for quadratic functions
Consider the following non-symmetric \(3\times 3\) matrix:
An equivalent quadratic function is obtained by using the following symmetric matrix:
Indeed, both matrices define the same function:
4.2 Gradient, Hessian and level sets¶
Observation 4.2: gradient of a quadratic function
The gradient (vector of partial derivatives) of a quadratic function is:
Proof
For every \(k \in \{1,2,\dots,n\}\), only the terms of the double sum with \(i=k\) or \(j=k\) contain the variable \(x_k\): the terms \(q_{kj}\,x_k x_j\) contribute \(q_{kj}\,x_j\) to the derivative and the terms \(q_{ik}\,x_i x_k\) contribute \(q_{ik}\,x_i\) (the term \(q_{kk}\,x_k^2\), with \(i=j=k\), contributes \(2\,q_{kk}\,x_k\), once in each sum). Therefore:
where the last equality holds since the matrix \({\boldsymbol Q}\) is symmetric (\(q_{jk} = q_{kj}\)). □
Observation 4.3: Hessian of a quadratic function
The Hessian (matrix of second-order partial derivatives) of a quadratic function is:
Proof
By the previous observation, for every \(k \in \{1,2,\dots,n\}\) we have \(\frac{ \partial f({\boldsymbol x})}{ \partial x_k} = 2 \sum_{j=1}^n q_{kj}\, x_j\), whose derivative with respect to \(x_h\) is \(2\,q_{kh}\), for every \(h \in \{1,2,\dots,n\}\). □
Definition 4.4: level sets of a quadratic function
The level sets of a quadratic function (the sets of points in which the function is constant) are:
- Depending on the matrix \(\boldsymbol Q\), the nonempty level sets can be ellipsoids (ellipses in \(\R^2\)), hyperboloids (hyperbolas in \(\R^2\)) or, when some directions do not change the value of the function, cylinders (pairs of parallel lines in \(\R^2\)). For \(k=0\) there are also degenerate cases: the single point \({\boldsymbol 0}\) in place of an ellipsoid, two lines crossing at the origin in \(\R^2\) (a cone in \(\R^n\)) in place of a hyperboloid, and a single line through the origin in \(\R^2\) (a linear subspace in \(\R^n\)) in place of a cylinder. The graph of the quadratic functions of the following examples is a paraboloid.
For a quadratic function with symmetric matrix \({\boldsymbol Q}\), the gradient is \(\nabla f({\boldsymbol x}) = 2\,{\boldsymbol Q}\,{\boldsymbol x}\) and the Hessian is the constant matrix \(\nabla^2 f({\boldsymbol x}) = 2\,{\boldsymbol Q}\). At every point where the gradient is nonzero, the gradient is perpendicular to the level set through that point.
Example 4.2: level sets of quadratic functions
With \({\boldsymbol Q}=\begin{pmatrix} 1 & 0\\ 0 & 1 \end{pmatrix}\), the associated quadratic function is:
The figure shows:
-
Blue circle: level set \(x_1^2+x_2^2=1\) (radius \(1\))
-
Orange circle: level set \(x_1^2+x_2^2=4\) (radius \(2\))
-
Green circle: level set \(x_1^2+x_2^2=8\) (radius \(\sqrt{8}\))
Example 4.3: level sets of quadratic functions
With \({\boldsymbol Q}=\begin{pmatrix} 2 & -1\\ -1 & 2 \end{pmatrix}\), the associated quadratic function is:
The figure shows:
-
Blue ellipse: level set \(2\;x_1^2 - 2\;x_1\;x_2 + 2\;x_2^2=2\)
-
Orange ellipse: level set \(2\;x_1^2 - 2\;x_1\;x_2 + 2\;x_2^2=8\)
Example 4.4: gradients of quadratic functions
With \({\boldsymbol Q}=\begin{pmatrix} 1 & 0\\ 0 & 1 \end{pmatrix}\), the gradient of the quadratic function is:
The figure shows:
-
Blue vectors (gradients with norm \(2\) on level set \(x_1^2+x_2^2=1\)):
\[\begin{align*} \nabla f\left(\begin{pmatrix} -1 \\ 0 \end{pmatrix}\right) &= \begin{pmatrix} -2 \\ 0 \end{pmatrix}, & \nabla f\left(\begin{pmatrix} 0 \\ -1 \end{pmatrix}\right) &= \begin{pmatrix} 0 \\ -2 \end{pmatrix}, \\[1ex] \nabla f\left(\begin{pmatrix} 0 \\ 1 \end{pmatrix}\right) &= \begin{pmatrix} 0 \\ 2 \end{pmatrix}, & \nabla f\left(\begin{pmatrix} 1 \\ 0 \end{pmatrix}\right) &= \begin{pmatrix} 2 \\ 0 \end{pmatrix} \end{align*}\] -
Orange vectors (gradients with norm \(4\) on level set \(x_1^2+x_2^2=4\)):
\[\begin{align*} \nabla f\left(\begin{pmatrix} -2 \\ 0 \end{pmatrix}\right) &= \begin{pmatrix} -4 \\ 0 \end{pmatrix}, & \nabla f\left(\begin{pmatrix} 0 \\ -2 \end{pmatrix}\right) &= \begin{pmatrix} 0 \\ -4 \end{pmatrix}, \\[1ex] \nabla f\left(\begin{pmatrix} 0 \\ 2 \end{pmatrix}\right) &= \begin{pmatrix} 0 \\ 4 \end{pmatrix}, & \nabla f\left(\begin{pmatrix} 2 \\ 0 \end{pmatrix}\right) &= \begin{pmatrix} 4 \\ 0 \end{pmatrix} \end{align*}\] -
Green vectors (gradients with norm \(4\sqrt{2}\) on level set \(x_1^2+x_2^2=8\)):
\[\begin{align*} \nabla f\left(\begin{pmatrix} 2 \\ 2 \end{pmatrix}\right) &= \begin{pmatrix} 4 \\ 4 \end{pmatrix}, & \nabla f\left(\begin{pmatrix} -2 \\ -2 \end{pmatrix}\right) &= \begin{pmatrix} -4 \\ -4 \end{pmatrix}, \\[1ex] \nabla f\left(\begin{pmatrix} -2 \\ 2 \end{pmatrix}\right) &= \begin{pmatrix} -4 \\ 4 \end{pmatrix}, & \nabla f\left(\begin{pmatrix} 2 \\ -2 \end{pmatrix}\right) &= \begin{pmatrix} 4 \\ -4 \end{pmatrix} \end{align*}\]
Each gradient is perpendicular to the level set through its point.
Example 4.5: gradients of quadratic functions
With \({\boldsymbol Q}=\begin{pmatrix} 2 & -1\\ -1 & 2 \end{pmatrix}\), the gradient of the quadratic function is:
The figure shows:
-
Blue ellipse: level set \(2\;x_1^2 - 2\;x_1\;x_2 + 2\;x_2^2=2\)
-
Blue vectors (gradients on the level set with norm \(2\sqrt{2}\) at diagonal points and norm \(2\sqrt{5}\) at axis points):
\[\begin{align*} \nabla f\left(\begin{pmatrix} 1 \\ 1 \end{pmatrix}\right) &= \begin{pmatrix} 2 \\ 2 \end{pmatrix}, & \nabla f\left(\begin{pmatrix} -1 \\ -1 \end{pmatrix}\right) &= \begin{pmatrix} -2 \\ -2 \end{pmatrix}, \\[1ex] \nabla f\left(\begin{pmatrix} 1 \\ 0 \end{pmatrix}\right) &= \begin{pmatrix} 4 \\ -2 \end{pmatrix}, & \nabla f\left(\begin{pmatrix} -1 \\ 0 \end{pmatrix}\right) &= \begin{pmatrix} -4 \\ 2 \end{pmatrix}, \\[1ex] \nabla f\left(\begin{pmatrix} 0 \\ 1 \end{pmatrix}\right) &= \begin{pmatrix} -2 \\ 4 \end{pmatrix}, & \nabla f\left(\begin{pmatrix} 0 \\ -1 \end{pmatrix}\right) &= \begin{pmatrix} 2 \\ -4 \end{pmatrix} \end{align*}\]
Also in this case, each gradient is perpendicular to the level set through its point.
Example 4.6: graphs of quadratic functions
With \({\boldsymbol Q}=\begin{pmatrix} 1 & 0\\[1ex] 0 & 1 \\ \end{pmatrix}\), we have \(f\left(\begin{pmatrix} x_1 \\ x_2 \\ \end{pmatrix}\right)=x_1^2+x_2^2\). Its graph is:
Example 4.7: graphs of quadratic functions
With \({\boldsymbol Q}=\begin{pmatrix} 2 & -1\\[1ex] -1 & 2 \\ \end{pmatrix}\), we have \(f\left(\begin{pmatrix} x_1 \\ x_2 \\ \end{pmatrix}\right) = 2\;x_1^2 - 2 \;x_1\;x_2 + 2\;x_2^2\). Its graph is:
4.3 Convexity of quadratic functions¶
Definition 4.5: positive semidefinite matrix
A square symmetric matrix \({\boldsymbol Q} \in \R^{n \times n}\) is positive semidefinite if:
Proposition 4.6
For every square matrix \({\boldsymbol Q}\), if \({\boldsymbol Q}\) is positive semidefinite then the associated quadratic function is convex in its domain.
Proof
If the matrix \({\boldsymbol Q}\) is positive semidefinite, for every pair of points \({\boldsymbol p}, {\boldsymbol w} \in \R^n\) and every \(\lambda \in [0,1]\), we have:
Therefore,
By nature, the domain \(\mathbb{R}^{n}\) is a convex set. Accordingly, the function is convex in its domain. □
-
We have shown that:
\[\begin{equation*} {\boldsymbol Q} \text{ positive semidefinite} ~~~\Longrightarrow~~~ \text{ convex quadratic function} \end{equation*}\]so “\({\boldsymbol Q}\) positive semidefinite” is a sufficient condition for “convex quadratic function” and “convex quadratic function” is a necessary condition for “\({\boldsymbol Q}\) positive semidefinite”.
-
The implication works also in the other direction, as stated by the following proposition.
Proposition 4.7
For every quadratic function, if the function is convex in its domain then the associated square matrix \({\boldsymbol Q}\) is positive semidefinite.
Proof
If the quadratic function is convex, for every pair of column vectors \({\boldsymbol p},{\boldsymbol w} \in \R^{n}\) and every scalar \(\lambda \in (0,1)\), we have:
where the left-hand side is:
This implies, rearranging and simplifying, that:
which, since \(\lambda\;(1-\lambda) >0\), implies:
Every vector \({\boldsymbol x} \in \R^n\) can be written as \({\boldsymbol x} = {\boldsymbol p} - {\boldsymbol w}\) (for instance, with \({\boldsymbol p} = {\boldsymbol x}\) and \({\boldsymbol w} = {\boldsymbol 0}\)). Therefore, \({\boldsymbol x}' {\boldsymbol Q} \; {\boldsymbol x} \ge 0\) for every \({\boldsymbol x} \in \R^n\), and the square matrix \({\boldsymbol Q}\) is positive semidefinite. □
-
We have shown that:
\[\begin{equation*} \text{ convex quadratic function} ~~~\Longrightarrow~~~ {\boldsymbol Q} \text{ positive semidefinite} \end{equation*}\]so “convex quadratic function” is a sufficient condition for “\({\boldsymbol Q}\) positive semidefinite” and “\({\boldsymbol Q}\) positive semidefinite” is a necessary condition for “convex quadratic function”.
-
So we finally have:
\[\begin{equation*} {\boldsymbol Q} \text{ positive semidefinite} ~~~\Longleftrightarrow~~~ \text{ convex quadratic function} \end{equation*}\]so “\({\boldsymbol Q}\) positive semidefinite” is a necessary and sufficient condition for “convex quadratic function” and vice versa.
Corollary 4.8
For every square matrix \({\boldsymbol Q}\), the associated quadratic function is convex in its domain if and only if \({\boldsymbol Q}\) is positive semidefinite.
-
With \({\boldsymbol Q}\) equal to the identity matrix, the quadratic function is \(f({\boldsymbol x}) = {\boldsymbol x}'{\boldsymbol x} = \|{\boldsymbol x}\|_2^2\), the squared Euclidean norm, whose convexity is proved in the chapter «Convex and concave functions» directly from the definition. The identity matrix is positive semidefinite, since \({\boldsymbol x}'{\boldsymbol x} = x_1^2 + x_2^2 + \dots + x_n^2 \ge 0\), in agreement with the corollary.
-
When \({\boldsymbol x}' {\boldsymbol Q} \; {\boldsymbol x}\) can be written as a sum of squares of linear functions, as in the following example, the matrix \({\boldsymbol Q}\) is positive semidefinite, and the convexity can also be proved directly from the definition, with the inequality \(\big(\lambda a+(1-\lambda)b\big)^2 \le \lambda a^2+(1-\lambda)b^2\) of the chapter «Convex and concave functions».
Example 4.8: a convex quadratic function
With \({\boldsymbol Q}=\begin{pmatrix} 2 & -1\\ -1 & 2 \end{pmatrix}\), for every \({\boldsymbol x} \in \R^2\) we have:
Therefore, the matrix \({\boldsymbol Q}\) is positive semidefinite and, by the corollary, the quadratic function of the previous examples is convex in its domain.
The same conclusion follows from the definition: for every pair of points \({\boldsymbol p}, {\boldsymbol w} \in \R^2\) and every \(\lambda \in [0,1]\), we have:
Therefore,
By nature, the domain \(\R^2\) is a convex set. Accordingly, the function is convex in its domain.
Example 4.9: a quadratic function that is not convex
With \({\boldsymbol Q}=\begin{pmatrix} 1 & 0\\ 0 & -1 \end{pmatrix}\), the quadratic function is
and its nonempty level sets with \(k \neq 0\) are hyperbolas. The matrix \({\boldsymbol Q}\) is not positive semidefinite, since:
By the corollary, the function is not convex in its domain. Indeed, with
the inequality of the definition of convex function does not hold:
4.4 Quadratic functions with a linear part¶
-
In optimization problems we often find functions that are the sum of a quadratic function, a linear function and a constant: given a square symmetric matrix \({\boldsymbol Q} \in \R^{n \times n}\), a vector \({\boldsymbol c} \in \R^n\) and a scalar \(d \in \R\),
\[ g:\, \mathbb{R}^{n} \to \mathbb{R}, \qquad g: {\boldsymbol x} \mapsto {\boldsymbol x}' {\boldsymbol Q} \; {\boldsymbol x} + {\boldsymbol c}' {\boldsymbol x} + d = \sum_{i=1}^n \sum_{j=1}^n q_{ij} x_i x_j + \sum_{j=1}^n c_j x_j + d \] -
Since the derivative of \({\boldsymbol c}' {\boldsymbol x} + d\) with respect to \(x_k\) is \(c_k\), the gradient and the Hessian are:
\[ \nabla g({\boldsymbol x}) = 2\,{\boldsymbol Q}\,{\boldsymbol x} + {\boldsymbol c}, \qquad \nabla^2 g({\boldsymbol x}) = 2\,{\boldsymbol Q} \] -
The function \(h({\boldsymbol x}) = {\boldsymbol c}' {\boldsymbol x} + d\) is convex (and concave) in its domain, since the definition holds with equality: for every pair of points \({\boldsymbol p}, {\boldsymbol w} \in \R^n\) and every \(\lambda \in [0,1]\), we have:
\[\begin{align*} & h\big(\lambda {\boldsymbol p} + (1-\lambda){\boldsymbol w}\big) \\[2ex] &\quad = {\boldsymbol c}'\big(\lambda {\boldsymbol p} + (1-\lambda){\boldsymbol w}\big) + d \\[2ex] &\quad = \lambda\, {\boldsymbol c}'{\boldsymbol p} + (1-\lambda)\, {\boldsymbol c}'{\boldsymbol w} + \lambda d + (1-\lambda) d \\[1ex] &\qquad \text{(since $\lambda + (1-\lambda) = 1$)} \\[2ex] &\quad = \lambda h({\boldsymbol p}) + (1-\lambda) h({\boldsymbol w}) \end{align*}\]Therefore, both inequalities of the definitions of convex and concave function hold with equality. By nature, the domain \(\R^n\) is a convex set. Accordingly, the function \(h\) is convex and concave in its domain. Since the sum of convex functions is convex (chapter «Convex and concave functions»), if the matrix \({\boldsymbol Q}\) is positive semidefinite, the function \(g\) is convex in its domain. Conversely, if \(g\) is convex, the quadratic function \({\boldsymbol x}' {\boldsymbol Q} \; {\boldsymbol x} = g({\boldsymbol x}) + \big(-{\boldsymbol c}' {\boldsymbol x} - d\big)\) is convex, as the sum of two convex functions, and the matrix \({\boldsymbol Q}\) is positive semidefinite by the corollary.
A quadratic function \({\boldsymbol x}' {\boldsymbol Q} \; {\boldsymbol x}\) is convex in its domain if and only if the matrix \({\boldsymbol Q}\) is positive semidefinite. Adding a linear function and a constant does not change its convexity.
Exercises on this chapter: Quadratic functions
Interactive lab¶
The ideas of the chapter, hands on: move the sliders and drag the points. All the graphs of the course are in the interactive lab.