TITLE: Conditioning a random variable $X$ by a function of $X$: the continuous case
QUESTION [8 upvotes]: I have a seemingly basic question, but surprisingly my web search didn't give any satisfying answers. 
Consider some random variable $X$ of support $(a,b)$ with continuous density $f$. Let 
$$ \gamma:(a,b)\rightarrow (c,d) $$
be some non-injective differentiable function. This certainly induces some random variable $Y=\gamma(X)$ with density $g$ on $(c,d)$. Assume for simplicity that the pre-image $\gamma^{-1}(y)$ is finite for all $y\in\gamma(X)$.
I am interested in 
$P(X=x\mid Y=\gamma(x))$.
So, if the distributions were discrete, we would just have
$$P(X=x\mid Y=\gamma(x))=\frac{P(X=x\text{ and }Y=\gamma(x))}{P(Y=\gamma(x))}=\frac{P(X=x)}{\sum\limits_{x_i\in\gamma^{-1}(y)}P(X=x_i)}$$
I am convinced that the solution in the continuous case should be
$$P(X=x\mid Y=\gamma(x))=\frac{\gamma'(x)f(x)}{\sum\limits_{x_i\in\gamma^{-1}(y)}\gamma'(x_i)f(x_i)}$$
Is this true? What is the best way to prove it? Are there any hidden measure-theoretic traps? Are there any references for that kind of calculation rules?

REPLY [5 votes]: This question is interesting but to solve it requires to come back at the definitions of conditional distributions, so let us try to be precise. We solve in details a simple case, hoping that this makes apparent the general solution. 
Assume that $X$ is uniformly distributed on $(0,1)$, thus, $X$ has density $f_X(x)=1$ on $(0,1)$, and that $Y=g(X)$ for some noninjective function $g$, say, $g(x)=6x$ if $x<\frac12$ and $g(x)=4-2x$ if $x>\frac12$. Thus, $Y$ has density $f_Y(y)=\frac16$ on $(0,2)$ and $f_Y(y)=\frac16+\frac12=\frac23$ on $(2,3)$. 
Recall that, by definition, the conditional distribution of $X$ conditionally on $Y$ can be any family of probability measures $(q_y)$ such that, for every (bounded measurable) function $u$, $$E(u(X)\mid Y)=v(Y)\ \text{almost surely}$$ with $$ v(y)=\int_\mathbb R u(x)q_y(dx)$$
Recall that, again by definition, the function $v$ is characterized (up to sets of measure zero for the distribution of $Y$) by the condition that, for every (bounded measurable) function $w$, $$E(u(X)w(Y))=E(v(Y)w(Y))$$ In our case, one asks that, for every function $w$, $E(u(X)w(Y))$, which is, by definition of $f_X$ and $Y$, $$\int_0^{1/2}u(x)w(6x)dx+\int_{1/2}^1u(x)w(4-2x)dx=\int_0^{3}u\left(\tfrac16y\right)w(y)\tfrac1{6}dy+\int_{2}^3u\left(2-\tfrac12y\right)w(y)\tfrac12dy$$
equals $E(v(Y)w(Y))$, which is, by definition of $f_Y$,
$$\int_0^{2}v(y)w(y)\tfrac16dy+\int_{2}^3v(y)w(y)\tfrac23dy$$ This holds if and only if $$v(y)=u\left(\tfrac16y\right)\mathbf 1_{0<y<2}+\tfrac32\left(\tfrac16u\left(\tfrac16y\right)+\tfrac12u\left(2-\tfrac12y\right)\right)\mathbf 1_{2<y<3}$$
that is, $$v(y)=u\left(\tfrac16y\right)\mathbf 1_{0<y<2}+\left(\tfrac14u\left(\tfrac16y\right)+\tfrac34u\left(2-\tfrac12y\right)\right)\mathbf 1_{2<y<3}$$
To sum up, the conditional distribution of $X$ conditionally on $Y$ is $(q_y)$ with $$q_y=\delta_{y/6}$$ if $0<y<2$ and $$q_y=\tfrac14\delta_{y/6}+\tfrac34\delta_{2-y/2}$$ if $2<y<3$. Identifying each term of $q_y$ in this specific case, one may arrive at the conclusion that, in the general case, the conditional distribution of $X$ conditionally on $Y=y$ is

$$q_y=\frac1{G(y)}\sum_{x:g(x)=y}\frac{f_X(x)}{|g'(x)|}\,\delta_x$$ with 
  $$ G(y)=\sum_{x:g(x)=y}\frac{f_X(x)}{|g'(x)|}$$ 

Note that such a formula cannot hold if $g$ is constant on some Borel set $B$ such that $P(X\in B)\ne0$, but if the sets $\{x\mid g(x)=y\}$ are, say, finite for every $y$ then we are good.