Using Mathematica to study basic probability

by Nasser Abbasi, september 30, 2007

Basic relations. Let f(x) be a PDF for some continuous random variable.  Then the following is true
P(X≤ a)=(∫_(-∞)^a)f(t)dt
As can be verified as follows (using Normal distribution as an example) by evaluating the above integral and see if it the same as CDF(X=a)

μ=0; σ=1;
a=1;
((∫_(-∞)^a)PDF[NormalDistribution[μ,σ],x]\[DifferentialD]x)//N

Now find F (a), it should be the same as above

CDF[NormalDistribution[μ,σ],a]//N

Another important relation is probability of X being in some range. This is the same as the area under the curve of f(x) between the 2 points:

P(a≤ X≤ b)=(∫_a^b)f(t)dt=CDF(b-a)
The above is found as follows

μ=0; σ=1;
a=1;b=2;
(Integrate[PDF[NormalDistribution[μ,σ],x],{x,a,b}])//N

Now find F (b)-F(a), it should be the same as above

(CDF[NormalDistribution[μ,σ],b]-CDF[NormalDistribution[μ,σ],a])//N

Plot[PDF[NormalDistribution[μ,σ],x],{x,-3 σ,3σ}];

lets try to see how to find probabilty of X be in some range when X is discrete. Assume X is a discrete random variable, say a Binomial. We need to do the same as above. Now we can not use Integrate, but need to us SUM

p=.3;n=10;  (*parameters for binomial*)
Needs["BarCharts`"];
BarChart[Table[PDF[BinomialDistribution[n,p],x],{x,0,4}],PlotLabel->"Example of binomial",BarLabels->Map[ToString,Range[0,4,1]],BarSpacing->0,BarGroupSpacing->0,BarStyle->White]

Let find P (X < 3) in the above. Now instead of integration, we use sum, we want to add the area under the PDF from 0 to 3. But the width is 1 for each interval. So we just sum the y values.

Sum[PDF[BinomialDistribution[n,p],x],{x,0,3}]

Verify by checking the CDF at 3, it should be the same as above

CDF[BinomialDistribution[n,p],3]

To show that probability mass function adds to one. Say we have binomial distribution

Remove["Global`*"]
Simplify[Sum[ Binomial[n,k](p^k)(((1-p))^((n-k))),{k,0,n}]]

Assuming[Element[n,Integers],Simplify[%]]