2
simple-stats.nb
RandomChoice[{0, 1}]
Random[Integer, 1]
RandomVariate[BernoulliDistribution[0.5]]
0
0
A Sample of Fair Coin Tosses
If we record the outcomes from repeating this experiment over and over, we say we are sampling from
the distribution. Let us count up the number of heads. Here are three equivalent ways of writing the sum
of the outcomes.
outcomes = Table[fairtoss, {10}]
FullForm[outcomes]
Total[outcomes]
Plus @@ outcomes
Apply[Plus, outcomes]
{0, 0, 1, 0, 0, 0, 1, 0, 0, 0}
List[0, 0, 1, 0, 0, 0, 1, 0, 0, 0]
2
2
2
Next we determine the number of heads in n trials by using the Sum function.
heads[n_] := Sum[fairtoss, {n}]
heads[1000]
503
Sample Mean and Sample Variance
Review mean and variance.
{1, 1, 1, 0, 0, 1, 0, 0, 0, 0}
simple-stats.nb
cointoss[n_, p_: 0.5] := Boole[Thread[RandomReal[1, n] < p]]
nTosses = 1000
outcomes = cointoss[nTosses];
(* mean outcome *)
meanOutcome = Total[outcomes] / nTosses
% // N
Mean[outcomes] // N
(* variance *)
Total(outcomes - meanOutcome)2 (nTosses - 1) // N
Variance[outcomes] // N
1000
243
500
0.486
0.486
0.250054
0.250054
Discrete Distribution: Finite Support
Bernouilli
PDF[BernoulliDistribution[p], x]
1-p x ⩵ 0
p
x⩵1
0
True
CDF[BernoulliDistribution[p], x]
0
x<0
1-p 0 ≤ x < 1
1
True
3
4
simple-stats.nb
Manipulate[DiscretePlot[CDF[BernoulliDistribution[p], x], {x, 0, 1.5},
PlotRange → {{0, 1.5}, {0, 1}},
ExtentSize → Right, ExtentMarkers → {"Filled", "Empty"}], {p, 0, 1}]
p
1.0
0.8
0.6
0.4
0.2
0.0
0.0
0.2
0.4
0.6
0.8
1.0
1.2
1.4
Mean[BernoulliDistribution[p]]
Variance[BernoulliDistribution[p]]
p
(1 - p) p
p0 = 0.4;
sample = RandomVariateBernoulliDistribution[p0], 103 ;
Mean[sample] // N
Variance[sample] // N
0.392
0.238575
Binomial
Clear[n, p]
PDF[BinomialDistribution[n, p], k]
(1 - p)-k+n pk Binomial[n, k] 0 ≤ k ≤ n
0
True
CDF[BinomialDistribution[n, p], x]
BetaRegularized[1 - p, n - Floor[x], 1 + Floor[x]] 0 ≤ x ≤ n
1
x>n
0
True
simple-stats.nb
In[4]:=
Out[4]=
n0 = 20
{
Manipulate[DiscretePlot[PDF[BinomialDistribution[n0, p], x], {x, 0, n0},
PlotRange → {{0, n0}, {0, 1}},
ExtentSize → Right, ExtentMarkers → {"Filled", "Empty"}], {{p, 0.5}, 0, 1}],
Manipulate[DiscretePlot[CDF[BinomialDistribution[n0, p], x], {x, 0, n0},
PlotRange → {{0, n0}, {0, 1}},
ExtentSize → Right, ExtentMarkers → {"Filled", "Empty"}], {{p, 0.5}, 0, 1}]
}
20
p
1.0
0.8
Out[5]=
,
0.6
0.4
0.2
0.0
0
5
10
15
20
p
1.0
0.8
0.6
0.4
0.2
0.0
0
5
10
15
20
5
6
simple-stats.nb
Mean[BinomialDistribution[n, p]]
Variance[BinomialDistribution[n, p]]
np
n (1 - p) p
p0 = 0.5;
sample = RandomVariateBinomialDistribution[20, p0], 103 ;
Mean[sample] // N
Variance[sample] // N
Histogram[sample, 21, "PDF"]
9.921
4.7435
0.15
0.10
0.05
4
6
8
10
12
14
16
18
Discrete Distributions: Infinite Support
Geometric
If at first you don’t succeed, try try again. A different coin flipping experiment: if the probability of heads
is p, what is the probability that we will toss k tails and then toss a head.
Clear[p, k]
PDF[GeometricDistribution[p], k]
(1 - p)k p k ≥ 0
0
True
simple-stats.nb
g1 = GraphicsRow[
Map[DiscretePlot[PDF[GeometricDistribution[#], k], {k, 0, 15},
PlotRange → {0, 0.6}, AxesLabel → {"k", "P(X=k)"}] &, {0.2, 0.4, 0.6}],
ImageSize → Large
]
SetDirectory[NotebookDirectory[]]
Export["images/pdf-geom.pdf", g1]
P(X=k)
0.6
P(X=k)
0.6
P(X=k)
0.6
0.5
0.5
0.5
0.4
0.4
0.4
0.3
0.3
0.3
0.2
0.2
0.2
0.1
0.1
0
2
4
6
8
10 12 14
k
0.1
0
2
4
6
8
C:\Users\aisaac\svn\aisaac\mydocs\math
images/pdf-geom.pdf
CDF[GeometricDistribution[p], k]
1 - (1 - p)1+Floor[k] k ≥ 0
0
True
10 12 14
k
0
2
4
6
8
10 12 14
k
7
8
simple-stats.nb
n0 = 20
{
Manipulate[DiscretePlot[PDF[GeometricDistribution[p], k], {k, 0, 15},
PlotRange → {{0, 15}, {0, 1}}], {{p, 0.5}, 0, 1}],
Manipulate[DiscretePlot[CDF[GeometricDistribution[p], k], {k, 0, 15},
PlotRange → {{0, 15}, {0, 1}},
ExtentSize → Right, ExtentMarkers → {"Filled", "Empty"}], {{p, 0.5}, 0, 1}]
}
20
p
1.0
0.8
,
0.6
0.4
0.2
0.0
0
2
4
6
8
10
12
14
p
1.0
0.8
0.6
0.4
0.2
0.0
0
2
4
6
8
10
12
14
simple-stats.nb
Mean[GeometricDistribution[p]]
Variance[GeometricDistribution[p]]
-1 +
1-p
p2
1
p
9
10
simple-stats.nb
p0 = 0.2;
gdist = GeometricDistribution[p0]
sample = RandomVariategdist, 103 ;
Show[
Histogram[sample, {- 0.5, 40.5, 2}, "PDF"],
DiscretePlot[PDF[gdist, k], {k, 0, 40},
PlotRange → {Automatic, {0, p0}}, PlotStyle → {PointSize[Medium]}]
]
Mean[sample] // N
Variance[sample] // N
Histogram[sample, 21, "PDF"]
GeometricDistribution[0.2]
0.20
0.15
0.10
0.05
0
10
20
30
40
3.81
18.2081
0.20
0.15
0.10
0.05
5
10
15
Poisson
PDF[PoissonDistribution[λ], k]
ⅇ-λ λk
k!
k≥0
0
True
20
simple-stats.nb
g1 = GraphicsRow[
Map[DiscretePlot[PDF[PoissonDistribution[#], k], {k, 0, 25},
PlotRange → {0, 0.2}, AxesLabel → {"k", "P(X=k)"}] &, {5, 10, 15}],
ImageSize → Large
]
SetDirectory[NotebookDirectory[]]
Export["images/pdf-poisson.pdf", g1]
P(X=k)
0.20
P(X=k)
0.20
P(X=k)
0.20
0.15
0.15
0.15
0.10
0.10
0.10
0.05
0.05
0.05
0
5
10
15
20
25
k
0
5
10
15
20
25
k
0
5
10
15
20
25
k
C:\Users\aisaac\svn\aisaac\mydocs\math
images/pdf-poisson.pdf
(* from Mma docs *)
DiscretePlot[Evaluate @ Table[PDF[PoissonDistribution[λ], k], {λ, {5, 10, 20}}],
{k, 0, 30}, PlotRange → All, PlotMarkers → Automatic]
● ●
0.15
●
●
■ ■
■
■
●
0.10
■
■
●
■
●
◆
■
●
●
■
●
■
■ ◆◆◆◆◆◆◆
■ ◆
■ ◆
◆
5
◆
10
◆
●
◆
◆
◆◆◆◆
◆
◆
◆
◆
◆■
■
0.05
●
◆
■
◆
◆
■
■
◆
◆
◆
◆
■ ■
● ● ● ● ● ● ●
■ ●
■ ●
■ ●
■ ●
■ ●
■ ●
■ ●
■ ●
● ●
■ ●
■ ●
■
15
20
25
30
11
12
simple-stats.nb
Manipulate[
DiscretePlot[PDF[PoissonDistribution[λ], k], {k, 0, 30}, PlotMarkers → Automatic],
{λ, {5, 10, 15}}]
λ
5
10
15
● ●
0.10
●
●
●
●
0.08
●
●
0.06
●
●
●
0.04
●
0.02
●
●
●
●
●
● ● ● ● ● ●
5
●
●
10
15
20
● ●
● ● ● ●
25
30
{Mean[PoissonDistribution[λ]], Variance[PoissonDistribution[λ]]}
{λ, λ}
Negative-Binomial Distribution (Pascal)
PDF[PascalDistribution[r, p], x]
(1 - p)-r+x pr Binomial[- 1 + x, - 1 + r] x ≥ r
0
True
CDF[PascalDistribution[n, p], x]
BetaRegularized[p, n, 1 - n + Floor[x]] x ≥ n
0
True
simple-stats.nb
(* from Mma docs *)
DiscretePlot[
Evaluate @ Table[PDF[PascalDistribution[10, p], x], {p, {0.4, 0.5, 0.6}}],
{x, 10, 40}, PlotMarkers → Automatic]
◆◆
0.12
◆
◆
0.10
■
◆
0.08
◆
■ ■
■
0.06
■
■
◆
■
●
◆
◆
●
●
■
0.04
0.02
■
◆■ ●
■ ● ●
●
●
◆
●
■
◆
●
●
■
■
◆
●
■
◆
●
15
●
■
●
●
◆
■
● ● ● ●
●
■
20
■
◆◆
◆◆
25
●
●
●
●
●
● ●
■ ■
● ●
●
■ ■ ■
■ ◆
■ ◆
◆◆◆◆◆◆◆
■ ◆
■ ◆
■ ◆
■
30
35
40
■
(* from Mma docs *)
DiscretePlot[Evaluate @ Table[PDF[PascalDistribution[n, 0.4], x], {n, {5, 8, 10}}],
{x, 5, 40}, PlotMarkers → Automatic]
0.10
●●
●
●
●
0.08
●
■ ■ ■
●
■
0.06
●
■
0.04
◆
■
■
■ ◆◆◆◆
■◆
■◆
■◆
◆
10
■
◆
◆
◆
◆
◆
■
◆
◆
■
◆
■
■
●
■
●
●
◆
15
◆
■
◆●
■
●
◆◆◆◆
●◆
■
0.02
◆
◆
●
■
●
■
■
■
●
20
●
■
■
■
◆
◆
◆
◆
◆
◆◆
■ ■
●●
◆◆
■ ■ ■
●●●
■●
■●
●●●●●●●●●●
■●
■
25
30
35
40
13
© Copyright 2026 Paperzz