From 0a66e043c5f08177a2ac1975d807e154c72ee18b Mon Sep 17 00:00:00 2001 From: mikoff Date: Sat, 27 Jan 2024 20:42:04 +0000 Subject: [PATCH] deploy: mikoff/blog@58d4c2db0ce3d6ab57b48ca3b1d5525071bf0b27 --- posts/probability-density-transform/index.html | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/posts/probability-density-transform/index.html b/posts/probability-density-transform/index.html index 6bb79b5..7cd71bf 100644 --- a/posts/probability-density-transform/index.html +++ b/posts/probability-density-transform/index.html @@ -32,7 +32,9 @@ $$ g(y) = f(r^{-1}(y))\left|\frac{d}{dy}r^{-1}(y)\right|. $$

What this formula states is that any density $g(y)$ can be obtained from a fixed density $f(x)$.

Example 1

Suppose that $Y = a + bX$, $a \in \mathcal{R}$ and $b \in \mathcal{R} \backslash {0}$, the distribution of $X$ is known: $f(x)$. What is the pdf of $Y$?

Since $r(x) = y = a + bx$, we can find inverse function to be $r^{-1}(y) = x = \frac{y - a}{b}$ and the derivative $\frac{dx}{dy} = \frac{1}{b}$.

Then $g(y) = f(\frac{y - a}{b})\frac{1}{|b|}$.

Example 2

Suppose that $f(x) = 2 x, x \in [0, 1)$ and $y(x) = x^2$.

First, derive the inverse of the function $x(y) = \sqrt{y}$.

Now, having:

$$ -f(x(y)) = 2 \sqrt{y}\text{and} \frac{dx}{dy} = \frac{1}{2\sqrt{y}} +\begin{align} +f(x(y)) = 2 \sqrt{y}~\text{and}~ \frac{dx}{dy} = \frac{1}{2\sqrt{y}} +\end{align} $$ we can find $g(y)$ to be:

$$ g(y) = f(x(y)) \left|\frac{dx}{dy}\right| = 2 \sqrt{y} \frac{1}{2\sqrt{y}} = 1. @@ -54,7 +56,9 @@ # print(sp.latex(x_y), sp.latex(dx_dy))

After solving for $y$ and finding the derivative we got: $$ -x(y) = \log{\left(- \frac{y}{y - 1.0} \right)} + 5.0 \text{and} \frac{dx}{dy} = -\frac{1.0}{y \left(y - 1\right)} +\begin{align} +x(y) = \log{\left(- \frac{y}{y - 1.0} \right)} + 5.0 ~\text{and}~ \frac{dx}{dy} = -\frac{1.0}{y \left(y - 1\right)} +\end{align} $$

Spoiler
import sympy.stats
 
@@ -113,18 +117,18 @@
 ax2.grid(axis='both')
 

png

Example 4

For the next example, let’s consider coordinate transform.

The polar coordinate transforms two numbers $(r, \theta)$, where radial distance $r \in [0, \inf)$ and polar angle $\theta \in [0, 2\pi)$ to a point on a plane. $$ -\begin{eqnarray} +\begin{align} x = r \cos{\left(\theta \right)} \\ y = r \sin{\left(\theta \right)} -\end{eqnarray} +\end{align} $$

Evaluating the derivatives we get: $$ -\begin{eqnarray} +\begin{align} \frac{\partial x}{\partial r} &= \cos(\phi) \\ \frac{\partial x}{\partial \phi} &= - r \sin(\phi) \\ \frac{\partial y}{\partial r} &= \sin(\phi) \\ \frac{\partial x}{\partial \phi} &= r \cos(\phi) -\end{eqnarray} +\end{align} $$

We can view this transformation as a multivariate change of variables. In such a case instead of $\frac{dx}{dy}$ we use the determinant of Jacobian ($\mathbf{J}$) that describes how the volume changes under the transformation.

Now, if we sample a polar coordinate $(r, \theta)$ with probability $f(r, \theta)$, then the distribution $g(x, y)$ is given by: $$ g(x, y) = \frac{f(r, \theta)}{\det\mathbf{J}} = \frac{f(r, \theta)}{r}.