I am trying to understand the definition $$p (g(X,Y)=z) = \sum_{(x,y)\in g^{-1}(\{z\})} p_{X,Y} (x,y)$$ where $g$ is a two-variable function, and $X,Y$ are discrete random variables.
I understand that $g(X,Y)$ is a random variable hence $p (g(X,Y)=z)=p(\omega \mid (X(\omega),Y(\omega))=z)$.
On the other hand I tried to expand $p_{X,Y} (x,y)$ (in the sum of the RHS), but I can't help misunderstanding it:
$$p_{X,Y}(x,y)=p(X=x \cap Y=y) = p(X^{-1} \{x\} \cap Y^{-1} \{y\}) \tag{1}$$ and, since the sample spaces of $X$ and $Y$ could very well be different, this intersection could very well be empty.
I know something must be wrong in $(1)$ but I can't spot the error.
Edit: I have drawn an example of two random variables (that I can redraw in a clean drawing and show here if you guys are ok) to help me understand the formula.