# Change of Variables
### Recap the derivative
Before diving in here, reread the notes on the [Derivative](Derivative.md).
### Updated Intuition
When we are changing variables, we are fundamentally transforming the underlying original space (that the integral is on). This, done naively, will of course *alter* the value of the integral! Simply think about a function such as $f(x) = x^2$ and then an integral of interest such as $\int_0^2 f(x) dx$. If we transform the underlying space via a map such as $y = g(x) = 5x^3$, we will be mapping *larger* inputs through $f$, and thus the integral will be larger.
The entire point of changing variables is because we are not so much interested in transforming $f$, but rather we are interested in the value of the integral! We simply wish to transform **the way that that value is represented** so that it is easier to compute.
For instance, consider our integral again:
$A = \int_0^2 f(x) dx$
We state that the above integral will equal $A$. In this context, our objective is simply to *determine the value of* $A$. Currently, $A$ is represented by the integral on the right hand side of the equation. The entire idea behind change of variables is:
> Sometimes it is easier to determine the value of $A$ if we change our integral (following a specific set of steps). Specifically, we will *transform the underlying space* (domain) and update our function accordingly to ensure that $A$ is conserved.
This is most readily explained in [Probability Density Functions](Probability%20from%20first%20principles.md#Probability%20Density%20Functions). At the risk of being overly redundant, let me spell out exactly what change of variables entails:
1. Change to underlying domain via a transformation. For instance, transform $x$ via $g(x) = y = \sqrt{x}$.
2. Now, our underlying space has been transformed. This will *change* the area under the curve. We do not want that. We wish it to be conserved. Hence, we must *update the function*, in this case $f$, to be on the new space.
3. We must update $f$ to *counteract* the changing of the underlying space introduced by $g$ to ensure that area is preserved
So, again, the big idea here (and main source of confusion) is:
> We are focused on the value of $A$, the area under the curve. That value can often be more easily represented in other spaces. We just need to make sure that as we transform our underlying space that our function updates in the inverse direction to ensure conservation of $A$.
### Key Intuition for Change of Variables via an example
#### 1. The problem
We start with a function (expression) of $x$:
$2x cos(x^2)$
We want to integrate that expression over a domain of integration, expressed in $x$. Let that domain be $x \in [0, \sqrt{\pi}]$. Visually, that may look like:

We can represent formally as:
$\int_{x=0}^{x = \sqrt{\pi}} 2x cos(x^2) dx$
#### 2. Guess the antiderivative
In this case it is rather easy to just *guess* the antiderivative; it is simply $sin(x^2)$:
$\frac{d}{dx}(sin(x^2)) = 2x cos(x^2)$
We can then evaluate this at the bounds of our region, and arrive at a final result::
$sin(x^2) \Big|_{x=0}^{x=\sqrt{\pi}} = 0 - 0 = 0$
#### 3. Perform U-Substitution
Alternatively, when guessing the antiderivative is too challenging, we can perform *u-substitution*. This involves two steps:
1. Let part of our expression equal $u$.
2. Based on this choice, find $du$.
3. Change our bounds of integration to be in terms of $u$.
So, given our original expression in:
$2x cos(x^2)$
We can let:
$u = x^2$
$\frac{du}{dx} = 2x \longrightarrow du = 2 x dx $
Now we can represent our original integral in terms of $u$:
$\int_{x=0}^{x = \sqrt{\pi}} cos(x^2) 2x dx \longrightarrow
\int_{u=0}^{u = \sqrt{\pi^2} = \pi} cos(u) du
$
We can solve this very simply:
$\int_{u=0}^{u =\pi} cos(u) du = sin(u) \Big| _{u=0}^{u=\pi} = 0- 0= 0$
And we arrive at the same result. But what did we really do? We actually **transformed** our coordinate system to be in terms of $u$, *not* $x$. This made our expression easier to solve. Visually, the transformed curve and areas under it looked like:

Where, we can see the original vs. transformed curve below:

A few questions likely arise right now. First and foremost, why would we want to do this? Sometimes that expression and the domain of integration lead to a very messy integration problem. Often there is an opportunity to *transform* our coordinate system so as to make our expression easier to solve. This an example of where the mathematician (us) actually [encodes information](Integration-Difficulty.md#Answer%201) into the expression to make it easier to solve.
The main idea is that it may be easier to solve for the integral (area/volume) of the new expression in the new coordinate system, compared to the original expression in the original coordinate system. So, we can transform our expression, and we can transform our bounds of integration (via the same transformation, since we must keep things consistent).
If we take a step back, there is a meta point to consider. When we perform this "u-substitution" we are in fact *changing our coordinate system*. It was originally in terms of $x$ and now it is in terms of $u$. This is truly a *transformation*, similar to those we work with in linear algebra, on there is no longer the constraint of linearity in this case. Just to make this incredibly concrete, consider the transformation from a unit disc/circle to an ellipse. Here, our coordinates change from $x$ and $y$ to $u$ and $v$:

Above, our transformation is incredibly straightforward, $u = ax$ and $v = by$, clearly satisfying linearity condition. Yet of course these [transformations can be nonlinear](Does%20Linearity%20provide%20Information.md#Nonlinear%20transformation) as well. And of course we can change our coordinates to be [polar](https://www.youtube.com/watch?v=51v2UBsO6XY&list=PL8erL0pXF3JaJdUcmc_PeGV-vG5z87BkD&index=91), or cylindrical, spherical, etc! We need to keep in mind that performing a u-substitution is simply another form of coordinate change via a transformation.
Now, again, let us review what we must do in order to change coordinates/transform successfully
* We must figure out how to correctly map/transform our expression (i.e., what does $u$ equal?)
* Based on this, we must transform our bounds of integration to be in terms of $u$ as well. This makes sense; if our curve was transformed from being described in $x$ to now being described in $u$ we should be sure to evaluate it at the original bounds *expressed in $u$*.
* We must be sure to account for the effect that our transformation had on *area/volume*. Likely this transformation was not invariant with respect to area (unless it was a simply rotation), so we will need to account for this in order to get the correct result for our integration.
We are now ready to dig into the official **change of variables theorem**.
### Change of Variables Theorem
The change of variables theorem can be stated as follows:
$\int_R h(F(x)) \Big| DET [DF] \Big| dx = \int_{F(R)} h(u) du \tag{1}$
Where in English our terms are:
* $R$: a region of space described in $x$
* $F(R)$: a region of space after being transformed via $F$, described in $u$
* $F$: a function that maps from $x$ to $u$
* $h$: a function that takes $u$ as an input and maps to an output
* $\Big| DET [DF] \Big|$: The absolute value of the determinant of the derivative of $F$ (where the derivative of $F$ is the [Jacobian](Jacobian-Matrix.md#Key%20Intuitions))
Now, to get from the LHS to RHS we must:
1. Change our integrand from being a function of $x$ to being a function $u$.
2. Change integration element from $dx$ to $du$.
3. Change the limits of integration from being in terms of $x$ to being in terms of $u$.
Returning to the example that we have been working with:
$\int_{x=0}^{x = \sqrt{\pi}} cos(x^2) 2x dx \longrightarrow
\int_{u=0}^{u = \sqrt{\pi^2} = \pi} cos(u) du
$
* $R \longrightarrow [x=0, x=\sqrt{\pi}]$
* $F(R) \longrightarrow [u=0, u=\pi]$
* $F = x^2$
* $h = cos$
* $\Big| DET [DF] \Big| = \frac{d}{dx}(x^2) = 2x$ (Note: The determinant of a 1x1 matrix, $[a]$, is simply $a$)
Now, what is equation $(1)$ really saying? We can state is as follows.
**Left Hand side of $(1)$**
Starting in $x$ domain, we want to find the area/volume (over region $R$) of $h(F(x)) |DET[DF]|$. Here, $F$ transforms $x$. $h$ then transforms this transformed version of $x$. $DET[DF]$ describes how the volume is changed via $F$.
**Right Hand side of $(1)$**
Starting in $u$ domain (where $u = F(x)$, i.e. $u$ is the transformed $x$), we wish to find the area/volume in region $F(R)$ (the transformed $R$ domain) of $h(u)$.
The incredibly key idea here is that we can transform our expression and our bounds. If we then make sure to account for how this transformation scales area, we can easily work in the transformed space!
### Final Thoughts
* We often use change of variables to make our domain of integration *simple*
### Probability Density Change of Variables
See more in [Probability Density Functions](Probability%20from%20first%20principles.md#Probability%20Density%20Functions).
---
References:
* [Desmos combined](https://www.desmos.com/calculator/el9gnxo1lj)
* [Desmos original](https://www.desmos.com/calculator/crf4ooirft)
* [Desmos transformed](https://www.desmos.com/calculator/qwfsoqvnnl)
* [Whiteboard derivation](https://photos.google.com/photo/AF1QipNB8KggXAQs7rnar6YZRfb74TdeDkV0BiPXgBKq)
* [Linear changes of coordinates](https://www.youtube.com/watch?v=6ORSPyrOYjc&list=PL8erL0pXF3JaJdUcmc_PeGV-vG5z87BkD&index=108)
* [Changes of variables as subsitution](https://www.youtube.com/watch?v=YHjVnYgEWlo&list=PL8erL0pXF3JaJdUcmc_PeGV-vG5z87BkD&index=113)
* [Change of variables, double integral example](https://www.youtube.com/watch?v=35oGiILfT9Q&list=PL8erL0pXF3JaJdUcmc_PeGV-vG5z87BkD&index=114)
* [Polar Coordinates - Variable Change](https://www.youtube.com/watch?v=51v2UBsO6XY&list=PL8erL0pXF3JaJdUcmc_PeGV-vG5z87BkD&index=91)
*