Derivative of a quadratic form: Difference between revisions

From Machinelearning
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
Let <math>A \in \mathcal M_{n,n}(\mathbf R)</math> be an <math>n</math> by <math>n</math> real-valued matrix, and let <math>f\colon \mathbf R^n \to \mathbf R</math> be defined by <math>f(x) = x^{\mathrm T}Ax</math>. On this page, we calculate the derivative of <math>f</math>.
Let <math>A \in \mathcal M_{n,n}(\mathbf R)</math> be an <math>n</math> by <math>n</math> symmetric real-valued matrix, and let <math>f\colon \mathbf R^n \to \mathbf R</math> be defined by <math>f(x) = x^{\mathrm T}Ax</math>. On this page, we calculate the derivative of <math>f</math> using three methods.


==Understanding the problem==
==Understanding the problem==
Since <math>f</math> is a real-valued function of <math>\mathbf R^n</math>, the derivative and the gradient coincide.


==Straightforward method==
==Straightforward method==
Line 16: Line 18:


:<math>\sum_{k=1}^n x_k \sum_{i=1}^n a_{ki}x_i = x_j \sum_{i=1}^n a_{ji}x_i + \sum_{k\ne j} x_k \sum_{i=1}^n a_{ki}x_i = x_j\left(a_{jj}x_j + \sum_{i\ne j} a_{ji} x_i\right) + \sum_{k\ne j} x_k \left(a_{kj}x_j + \sum_{i\ne j} a_{ki} x_i\right)</math>
:<math>\sum_{k=1}^n x_k \sum_{i=1}^n a_{ki}x_i = x_j \sum_{i=1}^n a_{ji}x_i + \sum_{k\ne j} x_k \sum_{i=1}^n a_{ki}x_i = x_j\left(a_{jj}x_j + \sum_{i\ne j} a_{ji} x_i\right) + \sum_{k\ne j} x_k \left(a_{kj}x_j + \sum_{i\ne j} a_{ki} x_i\right)</math>
The first equality comes from splitting the outer summation, and the second comes from splitting the two inner summations.


Now distributing we have
Now distributing we have


:<math>a_{jj}x_j^2 + \left(\sum_{i\ne j} a_{ji} x_i\right)x_j + \sum_{k\ne j} \left(a_{kj}x_k x_j + x_k \sum_{i\ne j} a_{ki} x_i\right) = a_{jj}x_j^2 + \left(\sum_{i\ne j} a_{ji} x_i\right)x_j + \left(\sum_{k\ne j}a_{kj}x_k\right) x_j + \sum_{k\ne j}x_k \sum_{i\ne j} a_{ki} x_i</math>
:<math>\begin{align}a_{jj}x_j^2 + \left(\sum_{i\ne j} a_{ji} x_i\right)x_j + \sum_{k\ne j} \left(a_{kj}x_k x_j + x_k \sum_{i\ne j} a_{ki} x_i\right) \\ = a_{jj}x_j^2 + \left(\sum_{i\ne j} a_{ji} x_i\right)x_j + \left(\sum_{k\ne j}a_{kj}x_k\right) x_j + \sum_{k\ne j}x_k \sum_{i\ne j} a_{ki} x_i\end{align}</math>


It is now easy to do the differentiation. We have
It is now easy to do the differentiation. We obtain


:<math>2a_{jj}x_j + \sum_{i\ne j} a_{ji} x_i + \sum_{k\ne j}a_{kj}x_k</math>
:<math>2a_{jj}x_j + \sum_{i\ne j} a_{ji} x_i + \sum_{k\ne j}a_{kj}x_k</math>
Line 28: Line 32:


:<math>2a_{jj}x_j + 2\sum_{i\ne j} a_{ji} x_i = 2\sum_{i=1}^n a_{ji} x_i</math>
:<math>2a_{jj}x_j + 2\sum_{i\ne j} a_{ji} x_i = 2\sum_{i=1}^n a_{ji} x_i</math>
But this is just the <math>j</math>th component of <math>2Ax</math>. It follows that the full derivative is just <math>2Ax</math> (or its transpose, depending on whether we want to view it as a row or column vector).


==Using the definition of the derivative==
==Using the definition of the derivative==


This is an expanded version of the answer at [https://math.stackexchange.com/a/189436/35525].
This is an expanded version of the answer at [https://math.stackexchange.com/a/189436/35525].
Using the definition, we can compute the derivative from first principles without exposing the components.


The derivative is the linear transformation <math>L</math> such that:
The derivative is the linear transformation <math>L</math> such that:
Line 60: Line 68:


==Using the chain rule==
==Using the chain rule==
In this approach, we think of <math>f</math> as a composition of <math>g(x,y) = x\cdot y</math> and <math>h(x) = (x, Ax)</math> and use the multivariable chain rule.
Define:
* <math>y = Ax = (h(x))_{n+1,\ldots,n+n}</math>
* <math>z = x\cdot y = g(x,y)</math>
What is tricky is that <math>y</math> is not <math>h(x)</math>; to make the composition work, we must stick on <math>x</math> to <math>y</math> to form <math>(x,y)</math> before passing to <math>g</math>.
Now the multivariable chain rule says:
:<math>\frac{\partial z}{\partial x_j} = \underbrace{\frac{\partial z}{\partial x_1}\frac{\partial x_1}{\partial x_j} + \cdots + \frac{\partial z}{\partial x_n}\frac{\partial x_n}{\partial x_j}}_{\text{first half of terms}} + \underbrace{\frac{\partial z}{\partial y_1}\frac{\partial y_1}{\partial x_j} + \cdots + \frac{\partial z}{\partial y_n}\frac{\partial y_n}{\partial x_j}}_{\text{second half of terms}}</math>
The notation is confusing because <math>\frac{\partial z}{\partial x_j}</math> means different things on each side of the equation (since <math>x</math> is both the input variable and an intermediate variable).
Looking only at the first half of the terms, <math>\frac{\partial x_k}{\partial x_j}</math> is <math>1</math> if <math>k=j</math> and <math>0</math> otherwise, so we keep only the <math>j</math>th term, where we see <math>\frac{\partial z}{\partial x_j} = y_j</math>.
Now looking at the second half of the terms, <math>\frac{\partial z}{\partial y_k} = x_k</math> and <math>\frac{\partial y_k}{\partial x_j} = a_{kj}</math>.
Putting all the above together, we obtain
:<math>\frac{\partial z}{\partial x_j} = y_j + x_1 a_{1j} + \cdots + x_n a_{nj} = 2y_j</math>
In the last equality we used the fact that <math>A</math> is symmetric.
We now have the <math>j</math>th component of the derivative, so the full derivative is <math>2y = 2Ax</math>.
See [http://michael.orlitzky.com/articles/the_derivative_of_a_quadratic_form.xhtml] for something similar.

Latest revision as of 03:23, 14 July 2018

Let AMn,n(R) be an n by n symmetric real-valued matrix, and let f:RnR be defined by f(x)=xTAx. On this page, we calculate the derivative of f using three methods.

Understanding the problem

Since f is a real-valued function of Rn, the derivative and the gradient coincide.

Straightforward method

This method is the most straightforward, and involves breaking apart the matrix and vector into components and performing the differentiation. While straightforward, it appears messy due to the indices involved.

Let A=(aki) and x=(x1,,xn).

We expand

xTAx=xT(i=1na1ixii=1nanixi)=k=1nxki=1nakixi

Now we find the partial derivative of the above with respect to xj. To distinguish the constants from the variable, it makes sense to split the sum:

k=1nxki=1nakixi=xji=1najixi+kjxki=1nakixi=xj(ajjxj+ijajixi)+kjxk(akjxj+ijakixi)

The first equality comes from splitting the outer summation, and the second comes from splitting the two inner summations.

Now distributing we have

ajjxj2+(ijajixi)xj+kj(akjxkxj+xkijakixi)=ajjxj2+(ijajixi)xj+(kjakjxk)xj+kjxkijakixi

It is now easy to do the differentiation. We obtain

2ajjxj+ijajixi+kjakjxk

Since the matrix is symmetric, akj=ajk so kjakjxk=kjajkxk=ijajixi. The final equality follows because k is just an indexing variable and we are free to rename it. But now the derivative becomes

2ajjxj+2ijajixi=2i=1najixi

But this is just the jth component of 2Ax. It follows that the full derivative is just 2Ax (or its transpose, depending on whether we want to view it as a row or column vector).

Using the definition of the derivative

This is an expanded version of the answer at [1].

Using the definition, we can compute the derivative from first principles without exposing the components.

The derivative is the linear transformation L such that:

limxx0;xx0|f(x)(f(x0)+L(xx0))||xx0|=0

Using our function, this is:

limxx0;xx0|xTAxx0TAx0L(xx0)||xx0|=0

Defining h=xx0, we have x=x0+h and

|(x0+h)TA(x0+h)x0TAx0L(h)||h|

Focusing on the subexpression (x0+h)TA(x0+h), since A is a matrix, it is a linear transformation, so we obtain (x0+h)T(Ax0+Ah). Since the transpose of a sum is the sum of the transposes, we have (x0T+hT)(Ax0+Ah). Now using linearity we have x0TAx0+hTAx0+x0TAh+hTAh.

Now the fraction is

|x0TAx0+hTAx0+x0TAh+hTAhx0TAx0L(h)||h|=|hTAx0+x0TAh+hTAhL(h)||h|

Focusing on hTAx0, it is a real number so taking the transpose leaves it unchanged: hTAx0=(hTAx0)T=x0TATh.

Now the fraction is

|x0TATh+x0TAh+hTAhL(h)||h|=|x0T(AT+A)h+hTAhL(h)||h|

In the numerator, hTAh is a higher order term that will disappear when taking the limit, so the linear transformation we are looking for must be L(h)=x0T(AT+A)h. Since A is symmetric, we have AT+A=2A and L(h)=2x0TAh.

Using the chain rule

In this approach, we think of f as a composition of g(x,y)=xy and h(x)=(x,Ax) and use the multivariable chain rule.

Define:

  • y=Ax=(h(x))n+1,,n+n
  • z=xy=g(x,y)

What is tricky is that y is not h(x); to make the composition work, we must stick on x to y to form (x,y) before passing to g.

Now the multivariable chain rule says:

zxj=zx1x1xj++zxnxnxjfirst half of terms+zy1y1xj++zynynxjsecond half of terms

The notation is confusing because zxj means different things on each side of the equation (since x is both the input variable and an intermediate variable).

Looking only at the first half of the terms, xkxj is 1 if k=j and 0 otherwise, so we keep only the jth term, where we see zxj=yj.

Now looking at the second half of the terms, zyk=xk and ykxj=akj.

Putting all the above together, we obtain

zxj=yj+x1a1j++xnanj=2yj

In the last equality we used the fact that A is symmetric.

We now have the jth component of the derivative, so the full derivative is 2y=2Ax.

See [2] for something similar.