# Conditional probability

I am new to Mathematica. I am asking how to define a conditinal probability in Mathematica.

Here is an example of what I want to do:

Assume that $P(x_b\,|\,x_i)=\left\{ \begin{array}{} 1-p & : x_b=0\\ p & : x_b=x_i \end{array} \right.$

where $P(x_b|x_i)$ is the conditional probability which takes 0 with probability of $1-p$ and takes the value of $x_i$ with probability $p$, where $0\le p\le 1$.

Also, we have $x_i\in U[3,5]$, by which I mean $x_i$ is uniformly distributed between 3 and 5.

I know that: $P(x_i,x_b)=P(x_b|x_i)P(x_i)$

I know how to define $P(x_i)$ in Mathematica using

xi=UniformDestribution[{3,5}]


but I want to find how to define conditional probability $P(x_b|x_i)$ in Mathematica. I tried to use the following:

xb = TransformedDistribution[xi xb, xb \[Distributed] BernoulliDistribution[p]]


and then multiply $x_b$ with $x_i$, but when I run

RandomVariate[xb xi]


I get error message

The valid numeric parameters of distribution TransformedDistribution[[FormalX]UniformDistribution[{3,5}], [FormalX][Distributed]BernoulliDistribution[0.1]] are expected. Use DistributionParameterAssumptions to obtain the parameter assumptions.

Is there any way to find $P(x_i,x_b)$ which equals $P(x_b|x_i)P(x_i)$

• Welcome! It is always a good idea to show your previous efforts and code (if available). Have you already searched the online help and this site for relevant threads? – Yves Klett May 11 '15 at 14:39
• I tried to find something online, but I couldn't find anything. Also, I tried to find $p(x_i,x_b)$ using ProdutDistribution, but I think this is wrong. – Ahmed Zeki May 11 '15 at 14:40
• – Yves Klett May 11 '15 at 14:42
• Yeah, I'd checked that. Nothing useful for my problem. – Ahmed Zeki May 11 '15 at 14:44
• For all values of $x_i$, $P(1|x_i)>1$ and is not a proper probability. Are you sure this is what you want? – 2012rcampion May 11 '15 at 15:03

The joint density function of $x_b$ and $x_i$ can be written as

f[xb_, xi_] := Piecewise[{{(1 - p)/2, xb == 0 && 3 <= xi <= 5}, {p/2, 3 <= xb <= 5 && 3 <= xi <= 5}}]

• Added May 12, 2015 *

While I haven't been able to get Mathematica to display the joint density function from the TransformedDistribution function, that function does supply some of the appropriate characteristics of the joint distribution function:

xbxi = TransformedDistribution[{\[Alpha] xi,
xi}, {xi \[Distributed]
UniformDistribution[{3, 5}], \[Alpha] \[Distributed]
BernoulliDistribution[p]}]
Mean[xbxi]
Covariance[xbxi]
RandomVariate[xbxi /. p -> 1/3, 10]

• Thanks for your response. This is exactly what I wanted. – Ahmed Zeki May 13 '15 at 21:33