How to compute this derivative of a square root of a sum? I have the function
$$f(\mathbf{x}) = \sqrt{\frac{1}{n}\sum_{i=1}^n\left(\log_e(x_i+1)-c_i\right)^2}$$
where $c_i$ is a constant, and I want to find $f'(\mathbf{x})$, more explicitly,
$$\frac{\partial f}{\partial x_i}$$
the derivative of $f$ with respect to $x_i$, the $i$'th variable.
I know I'll have to apply the chain rule here, but with the sum and the square root, I am unsure of the correct order of the steps.
 A: $$f(\mathbf{x}) = \sqrt{\sum_i \phi_i(x_i)}$$
Hence using the chain rule
$$\frac{\partial f}{\partial x_i}=\frac{1}{2} \frac{\frac{d \phi_i}{d x_i}}{f(\mathbf{x})} = \frac{\log_e(x_i+1)-c_i}{n\left(x_i+1\right)} \frac{1}{f(\mathbf{x})} $$
where $$\phi_i(t) = \frac{\left(\log_e(t+1)-c_i\right)^2}{n}$$
A: Here's a very informal way that I teach the chain rule to Calculus I students.
I like to think of the chain rule as working with layers: 


*

*Derivative of the "outside" with the "original inside" times:

*Derivative of the "inside"


For instance, 
$$f(\mathbf{x}) = \left(\dfrac{1}{n}\sum_{i=1}^{n}[\ln(x_i + 1)-c_i]^2\right)^{1/2}$$
Initially, we have two layers involved:


*

*The "outside" function is $(\cdot)^{1/2}$

*The "inside function $\dfrac{1}{n}\sum_{i=1}^{n}[\ln(x_i + 1)-c_i]^2$


So, using what I've stated above:


*

*The derivative of the "outside" is just applying the power rule from univariate calculus: $(1/2)(\cdot)^{1/2-1} = (1/2)(\cdot)^{-1/2}$. But we need to put the "original inside" it - so we have 
$$\dfrac{1}{2}\left(\dfrac{1}{n}\sum_{i=1}^{n}[\ln(x_i + 1)-c_i]^2 \right)^{-1/2}\text{.}$$

*Now we need to find the derivative of 
$$\dfrac{1}{n}\sum_{i=1}^{n}[\ln(x_i + 1)-c_i]^2\text{.}$$
Well, first of all, we have a constant. Let's just throw that off to the side for now. 
$$\dfrac{\partial}{\partial x_i}\left[\dfrac{1}{n}\sum_{i=1}^{n}[\ln(x_i + 1)-c_i]^2\right]=\dfrac{1}{n}\dfrac{\partial}{\partial x_i}\left[\sum_{i=1}^{n}[\ln(x_i + 1)-c_i]^2\right]$$
And now we're taking the derivative of a sum. The derivative of the sum is the sum of the derivatives, so we can interchange the sum and the derivative:
$$\dfrac{1}{n}\sum_{i=1}^{n}\dfrac{\partial}{\partial x_i}\left[[\ln(x_i + 1)-c_i]^2\right]$$
and the thing that we're taking the derivative of has two layers: $(\cdot)^2$ and $\ln(x_i + 1)-c_i$. So we need to apply the chain rule again: derivative of the outside is $2(\cdot)^{2-1} = 2(\cdot)$, and with the inside in it, we have $2[\ln(x_i + 1)-c_i]$. Lastly, we need to take the derivative of the inside, which is 
$$\dfrac{\partial}{\partial x_i}[\ln(x_i + 1)-c_i] = \dfrac{\partial}{\partial x_i}\ln(x_i+1)-\dfrac{\partial}{\partial x_i}c_i=\dfrac{\partial}{\partial x_i}\ln(x_i+1) - 0 = \dfrac{\partial}{\partial x_i}\ln(x_i+1)\text{.}$$
We have another two layers here: $\ln(\cdot)$ on the outside, and $x_i + 1$ on the inside, so we take the derivative of the outside - which is $\dfrac{1}{\cdot}$, put the inside in it: $\dfrac{1}{x_i + 1}$. Lastly, we multiply by the derivative of the inside, which is going to give us $1$.


So, if you combine everything that I've stated so far by multiplying everything together, we end up with
$$\dfrac{1}{2}\left(\dfrac{1}{n}\sum_{i=1}^{n}[\ln(x_i + 1)-c_i]^2 \right)^{-1/2} \cdot \dfrac{1}{n} \sum_{i=1}^{n}2[\ln(x_i + 1)-c_i]\cdot \dfrac{1}{x_i + 1}\cdot 1\text{.}$$
I will leave any simplification up to you.
