Finding probability density function for $Z$ which is a function of $X$ and $Y$ Let $(X,Y)$ be uniform  on the unit square. Let $Z=\frac{X}{Y}$. Find the density of $Z$.
I found typically when they said $(X,Y)$ has a uniform distribution on the unit square it is defined as
$f(x,y) =  \begin{cases}  
1, & \text{ if } 0\leq x \leq 1\\
0, & \text{otherwise}
  \end{cases}
$
I tried to solve the main problem like this:
First find cdf and from there I calculate the pfd function.
$F_Z(z) = P(Z\leq z)= p(g(x,y) \leq z) = \int_0^1\int_0^1\frac{X}{Y}dxdy$
Probably I should change this to be a function of z which I don't know how. Since I don't know I just continue integrating with respect to x and y I will have
$= \int_0^1\frac{1}{2y}dy = \frac{1}{2}\ln (1) -\ln(0)$ which is not defined
Apparently something is not right here, Can anyone help please?
 A: Seeing the number of similar questions on the site, let us try to delineate some clear and automatic methods.

First step: Write correctly the joint PDF. Here, the PDF $f$ of $(X,Y)$ is defined on the whole plane $\mathbb R^2$ (as every other joint PDF) by the formula $$f(x,y)=\mathbf 1_{0<x<1}\,\mathbf 1_{0<y<1}$$
Again:

First motto: The joint PDF of any $d$-dimensional random vector should be defined on the whole space $\mathbb R^d$, not only on a subset of $\mathbb R^d$.

Second step: Deduce the joint PDF of a transformed couple. Here, one can consider $(Z,T)=(X/Y,Y)$ (but other choices for the second coordinate $T$ are possible). 
Then $(X,Y)=(ZT,T)$ hence $dxdy=tdzdt$ and the change of variable formula based on the Jacobian of the transformation $(x,y)\to(z,t)=(x/y,y)$ yields the joint PDF $g$ of $(Z,T)$ as $$g(z,t)=|t|\,f(x,y)=|t|\,f(zt,t)=t\,\mathbf 1_{0<zt<1}\,\mathbf 1_{0<t<1}$$

Second motto: Get yourself familiar with the change of variables formula.

Third step: Marginalize the joint PDF to deduce the desired PDF. Here, the PDF $f_Z$ of $Z$ is simply $$f_Z(z)=\int_\mathbb Rg(z,t)dzdt=\int_0^1t\,\mathbf 1_{0<t<1/z}dt=\mathbf 1_{z>0}\int_0^{\min\{1,1/z\}}tdt=\tfrac12\min\{1,1/z\}^2\mathbf 1_{z>0}$$ An equivalent formulation is $$f_Z(z)=\tfrac12\mathbf 1_{0<z<1}+\tfrac1{2z^2}\mathbf 1_{z>1}$$

Third motto: Well, no real motto for this last step, simply, one can hope you get the automaticity of the approach...

A: I would suggest finding first the distribution function of $Z$:
$$F_Z(a)=P(Z\le a)=P(X\le aY)$$
for $a>0$. This is the area of the part of the square above the line $y=x/a$. Once we have $F_Z$ we can differentiate to get the density
function.
