How can we estimate (conditional) probabilities from a dataset? Given three random variables $X$, $Y$ and $Z$, how can we estimate $P(X)$, $P(X\mid Y)$ and determine whether $P(X \mid Y, Z) = P(X \mid Z)$ from a dataset (of e.g. $N$ observations) which contains features $X$, $Y$ and $Z$? 
To estimate the conditional probability, I think we need to filter on the variable we are conditioning on. For example, in the case of $P(X \mid  Y)$, we look at $X$ for all values of $Y$. But how exactly do we estimate $P(X \mid  Y)$?
I think an example would definitely be very useful.
 A: In the case of $P(X)$, if $X$ is discrete, then it takes a finite number of values $\{ x_1, x_2, \dots, x_m \}$. Then we can count how many observations have $X=x_1$, $X=x_2$, etc. In this case, we can estimate $P(X=x)$, for some $x \in \{ x_1, x_2, \dots, x_m \}$, so we can also estimate the distribution of $X$ (i.e., essentially it would be a histogram).
Similarly, we can also estimate $P(X \mid Y)$. For each value $y \in \{y_1, \dots, y_t\}$, we calculate $P(X \mid Y=y)$, that is, for each $y$, we count how how many observations have $X=x_1$, $X = x_2$, etc.
To estimate the conditional independence given a third variable, that is, $P(X \mid Y, Z) = P(X \mid Z)$, we again do a similar thing. First, we estimate $P(X \mid Z)$ as above, then we estimate $P(X \mid Y, Z)$. How do we estimate $P(X \mid Y, Z)$? For each combination of $y \in \{y_1, \dots, y_t\}$ and $z \in \{ z_1, \dots, z_r \}$, we can count the number of $X=x_1$, $X = x_2$, etc.
A: One way to deal with this is using a contingency table with counts of the events. Say the variables are categorical: $X \in  \lbrace 1,...,l \rbrace$, $Y \in  \lbrace 1,...,m \rbrace$, $Z \in  \lbrace 1,...,n \rbrace$ then you could put the numbers of observations $n_{xyz}$ in the categories $x,y,z$ for a given $z=i$ in a $l $ by $m $ contingency table:
$$\begin{array}{c|cccc|c}
& y=1 & y=2 & \cdots &y=m &  \\ \hline 
x=1 & n_{11i} & n_{12i} &&n_{1mi} & n_{1\cdot i}\\ 
x=2 & n_{21i} & n_{22i} &&n_{2mi} & n_{2\cdot i}\\ 
\vdots & &  && &\\ 
x=l & n_{l1i} & n_{l2i} &&n_{lmi} & n_{l\cdot i}\\ \hline 
 & n_{\cdot1i} & n_{\cdot2i} &&n_{\cdot mi} & n_{\cdot\cdot i}
\end{array}$$
where the $n_{ijk}$ are counts of the joint events $x=i$, $y=k$ and $z=k$. A dot like  $n_{\cdot jk}$ relates to marginal sum. 

Then the probabilities could be estimated by ratios of those counts.
$$P(X=i|Y=j,Z=k) = \frac{n_{ijk}}{n_{\cdot jk}}$$
(NB for other type of distributions than the categorical distribution there can be better approaches)

The equality or independence can be determined in several ways (the same is true for estimating the probabilities). There is no general method for this and you would need to specify the problem and needs more specifically in order to decide on the right test. 
Examples are Pearson's chi-squared test or the 
 Cochran-Mantel-Haenszel test.
The second is a more specific test than the first and is for a three-way relationship with two binary variables.
