---
# System prepended metadata

title: mathstat_ch2_random variables_2.11-2.12
tags: [mathematic statistics]

---

# Introduction  
這篇主要是介紹怎麼轉換  
以數學式來說就是 :  
已知 $X \Rightarrow \ Y = g(x)$  



# Transformation of Random Variables  
我們先假設 $X$ 有一個 $pmf : F_X(x)$  
接著定義 $Y = g(X)$  
$\Rightarrow$ 我們就可以得到 $x = g^{-1}(y)$  
然後也可以知道 : $f_Y(y) = f_X(g^{-1}(y))$  

另外，我們假設 $X$ 是 continuous variable 並且 pdf 是 $f_X(x)$  
$Y = g(X)$ 然後 $x = g^{-1}(y)$   
我們就可以知道 $Y$ 的 pdf 是 :  
$f_Y(y) = f_X(g^{-1}(y)) \cdot |\cfrac{dg^{-1}(y)}{dy}|$  



# Comparison  
* discrete pmf ($\color{red}{\text{probability}}$) :  
$f_Y(y) = f_X(g^{-1}(y))$  

* continuous pdf ($\color{red}{\text{density}}$) :  
$f_Y(y) = f_X(g^{-1}(y)) \cdot |\cfrac{dg^{-1}(y)}{dy}|$  



# Examples  
## Question 1 ($g$ is one to one)(discrete)  
**question** : find $pmf$ of $Y = g(X) = e^X,\ \text{with } P(X = -1) = P(X=1) = \cfrac{1}{4},\ P(X = 0) = \cfrac{1}{2}$  

**solution** :  
從題目我們可以知道我們想求出 $f_Y(y) = ?$  
首先我們最簡單的方式就是畫關係圖出來 :  
![image](https://hackmd.io/_uploads/r1Fng6Rrp.png)  
我們可以先知道 :  
$y \in \left\{ e^{-1},\ e^{0},\ e^{1} \right\}$  

然後算出  
$f_Y(e^{-1}) = P(Y = e^{-1}) = \underbrace{P (X = -1)}_{\color{green}{P(X = g^{-1}(e^{-1})) = P(X = ln \cdot e^{-1})}} = 1/4$  
$f_Y(e^{1})$ 同理  
$f_Y(e^{0}) = P(Y = e^{0}) = P(X = 0) = 1/2$  
$\therefore \text{ In general, }$  
$f_{\color{red}{Y}}(y) = P(Y = y) = \underbrace{P(e^{X} = y) = P(X = ln(y))}_{\color{green}{P(g(X) = y) \ = \ P(X = g^{-1}(y))}} = f_{\color{red}{X}}(ln(y))$  



## Question 2 ($g$ is not one to one)(discrete)  
**question** : find $pmf$ of $Y = g(X) = X^2,\ \text{with } P(X = -1) = P(X=1) = \cfrac{1}{4},\ P(X = 0) = \cfrac{1}{2}$  

**solution** :  
我們可以先知道 :  
$y \in \left\{ 0,\ 1 \right\}$  

然後算出  
$f_Y(0) = P(Y = 0) = P(X = 0) = 1/2$  
$f_Y(1) = P(Y = 1) \underbrace{=}_{\because \ y = x^2 = 1 \ \Rightarrow \ x = \pm 1 } P(X = 1) + P(X = -1) = 1/4 + 1/4 = 1/2$  
$f_Y(1) = P(Y = 1) = P(g(x) = 1) = \begin{cases} P(x = \underbrace{g^{-1}_1(1)}_{\color{orange}{y = x^2 \longrightarrow x = \ -\sqrt{y} = g_1(y)}}) = P(x = -1) = 1/4 \\ P(x = \underbrace{g^{-1}_1(1)}_{\color{orange}{y = x^2 \longrightarrow x = \ \sqrt{y} = g_2(y)}}) = P(x = 1) = 1/4  \end{cases}$  



## Question 3 (continuous case)  
**question** : assume $X \sim F$, find pdf of $Y = g(X)$  

**solution** :  
$F_Y(y) = P(Y \le y) = P(g(x) \le y)$  
我們可以先分段討論  
* $g$ is increasing  
$F_{\color{red}{Y}}(y) = ... = P(\underbrace{g(x)}_{ = Y} \le y) = P(x \le g^{-1}(y)) = F_{\color{red}{X}}(g^{-1}(y))$  
接著轉換成 pdf  
$f_Y(y) = \cfrac{d}{dy}F_X(g^{-1}(y)) = f_X(g^{-1}(y)) \times \cfrac{dg^{-1}(y)}{dy} = f_X(g^{-1}(y)) \times |\cfrac{dg^{-1}(y)}{dy}|$


* $g$ is decreasing  
$F_{\color{red}{Y}}(y) = ... = P(\underbrace{g(x)}_{ = Y} \le y) = P(x \ge g^{-1}(y)) = 1-F_{\color{red}{X}}(g^{-1}(y))$  
接著轉換成 pdf  
$f_Y(y) = \cfrac{d}{dy} \left[ 1-F_X(g^{-1}(y)) \right] = -f_X(g^{-1}(y)) \times \underbrace{\cfrac{dg^{-1}(y)}{dy}}_{<0} = f_X(g^{-1}(y)) \times |\cfrac{dg^{-1}(y)}{dy}|$  



## Question 4 ($g$ is one to one)(continuous)  
**question** : find $pdf$ of $Y = g(X) = log(X),\ \text{with } X \sim f_X(x) = e^{-x},\ x > 0$  

**solution** :  
我們可以先確定 $Y \in R$  
然後有 2 種解題方式 :  
* From scratch :  
$F_Y(y)  = P(Y \le y) = \underbrace{P(log(X) \le y)}_{\color{green}{e^{log(X)} \le e^y}} = P(X \le e^y) = \int^{e^y}_{0}f_X(x)dx$  
接著我們是想求出 pdf，所以把 $F_Y(y)$ 微分  
$f_Y(y) = \cfrac{d}{dy} \int^{e^y}_{0}f_X(x)dx \underbrace{=}_{\because \ F_X^{\prime} = f_X(x)} \cfrac{d}{dy} \left[ F_X(e^y) - F(0)  \right] = f_X(e^y) \cdot \cfrac{d \ e^y}{d \ y} = e^{-e^{y}}e^y$  


* From formula :  
$y = g(x) = log(x),\ x = g^{-1}(y) = e^{y}$  
$f_Y(y) = f_X(g^{-1}(y)) \cdot |\cfrac{dg^{-1}(y)}{dy}| = e^{-e^{y}}e^y$  
![image](https://hackmd.io/_uploads/BkV2J3KLT.png)  
















