example9.mws

Example 9: Order Statistics

by Zavan Karian

> restart: with(plots, display): libname:="C:/mylib/statistics",libname: with(stat):

A study of the p.d.f.s of order statistics

First, take example 10.1-3

> f := x -> 1;

f := 1

> F := x -> int(f(t), t=0..x);

F := proc (x) options operator, arrow; int(f(t),t =...

> g := (n, r, y) -> (n!/((r-1)!*(n-r)!)) * F(y)^(r-1)*(1-F(y))^(n-r)*f(y);

g := proc (n, r, y) options operator, arrow; n!*F(y...

> G:= (n,r,y) -> sum(n!/(k!*(n-k)!)*F(y)^k*(1-F(y))^(n-k), k=r..n);

G := proc (n, r, y) options operator, arrow; sum(n!...

> pdf := g(4,3,y);

pdf := 12*y^2*(1-y)

> int(pdf, y=1/3..2/3);

13/27

> G(4,3,2/3)-G(4,3,1/3);

13/27

Note that this "hides the expressions for g and G.

We get around this by the following.

Can this be done if we assume that the underlying distribution

is more complicated? Assume we have an exponential.

> assume(theta > 0);

> f := x-> ExponentialPDF(theta, x);

f := proc (x) options operator, arrow; :-Exponentia...

> F := x-> int(f(t), t=0..x);

F := proc (x) options operator, arrow; int(f(t),t =...

> g := (n, r, y) -> (n!/((r-1)!*(n-r)!)) * F(y)^(r-1)*(1-F(y))^(n-r)*f(y);

g := proc (n, r, y) options operator, arrow; n!*F(y...

> G:= (n,r,y) -> sum(n!/(k!*(n-k)!)*F(y)^k*(1-F(y))^(n-k), k=r..n);

G := proc (n, r, y) options operator, arrow; sum(n!...

We can now compute probabilities. For example, if n=4,

what is the probability that the third order statistic is

between 1/2 and 1.

> P := simplify(G(4,3,1)-G(4,3,1/2));

P := -3*exp(-4*1/theta)+8*exp(-3*1/theta)-3*exp(-2*...

If we assume a specific theta such as theta=1, we get

a numeric answer.

> evalf(subs(theta=1,P));

.359579147

We can now study the effect of theta on an order

statistic for specific values of n and r. Let's

take n=4, r=3.

> pdf := simplify(g(4,3,y));

pdf := 12*(exp(-y/theta)-1)^2*exp(-2*y/theta)/theta...

> N := 5;

N := 5

> for i from 1 to N do

> gg := simplify(subs(theta=i, g(4,3,y)));

> P||i := plot(gg, y=0..10, color=blue):

> od:

> display([seq(P||i, i=1..5)], insequence=true);

[Maple Plot]

For a fixed theta, say theta=1, we can also consider

the shapes of the order statistics for r=1, 2, 3, ...

Taking theta =1 and n=4, we look at r=1,2,3, and 4.

> for i from 1 to 4 do

> gg := simplify(subs(theta=1, g(4,i,y)));

> P||i := plot(gg, y=0..4, color=blue):

> od:

> display({P1,P2,P3,P4});

[Maple Plot]

Unfortunately, the expressions for g and G in both examples

"hide" their actual forms. The following will produce more

explicit representations of g and G, if that is desired.

> assume( theta > 0);

> f := ExponentialPDF(theta, y);

f := exp(-y/theta)/theta

> int(f, y=0..t);

-exp(-t/theta)+1

> F := subs(t=y,%);

>

>

>

F := -exp(-y/theta)+1

> g := (n!/((r-1)!*(n-r)!)) * F^(r-1)*(1-F)^(n-r)*f;

g := n!*(-exp(-y/theta)+1)^(r-1)*exp(-y/theta)^(n-r...

> G := sum(n!/(k!*(n-k)!)*F^k*(1-F)^(n-k), k=r..n);

G := n!*(-exp(-y/theta)+1)^r*exp(-y/theta)^(n-r)*hy...

For contrasrt, we now consider order statistics from a

symmetric, two-parameter distribution: N(mu,var).

> assume(sigma > 0);

> f := NormalPDF(mu, sigma^2, y);

f := 1/2*sqrt(2)*exp(-1/2*(y-mu)^2/(sigma^2))/(sigm...

> int(f, y=-infinity..t);

1/2*erf(1/2*sqrt(2)*(t-mu)/sigma)+1/2

> F := subs(t=y,%);

>

>

F := 1/2*erf(1/2*sqrt(2)*(y-mu)/sigma)+1/2

> g := (n!/((r-1)!*(n-r)!)) * F^(r-1)*(1-F)^(n-r)*f;

g := 1/2*n!*(1/2*erf(1/2*sqrt(2)*(y-mu)/sigma)+1/2)...

> G := simplify(sum(n!/(k!*(n-k)!)*F^k*(1-F)^(n-k), k=r..n));

G := GAMMA(n+1)*(1/2*erf(1/2*sqrt(2)*(y-mu)/sigma)+...

As before, we can calculate probabilities in

specific cases.

> GG := simplify(subs({n=4, r=3, mu=0, sigma=1}, G));

GG := -1/16*(-5+3*erf(1/2*sqrt(2)*y))*(erf(1/2*sqrt...

> evalf(subs(y=1/2, GG)- subs(y=-1/2, GG));

.5463129558

And we can obtain expressions and graphs for

specific p.d.f.s of order statistics or through

animation observe the effect of the symmetry

of the underlying distribution by comparing

g_i with g_(n-i+1).

> gg := simplify(subs({n=4, r=3, mu=0, sigma=1}, g));

gg := -3/4*(erf(1/2*sqrt(2)*y)+1)^2*(-1+erf(1/2*sqr...

> F;

1/2*erf(1/2*sqrt(2)*(y-mu)/sigma)+1/2

> plot(gg, y=-3.5..3.5);

[Maple Plot]

> N := 10;

N := 10

> for i from 1 to N do

> gg := simplify(subs({n=N, r=i, mu=0, sigma=1},g));

> P||i := plot(gg, y=-3.5..3.5, color=blue):

> od:

> display([seq(P||i, i=1..N)], insequence=true);

[Maple Plot]

> N:=20;

N := 20

> for i from 1 to N/2 do

> gg := simplify(subs({n=N, r=i, mu=0, sigma=1},g));

> ggg := simplify(subs({n=N, r=N-i+1, mu=0, sigma=1},g));
P||i := plot({gg,ggg}, y=-3.5..3.5, color=blue):

> od:

> display([seq(P||i, i=1..N/2)], insequence=true);

[Maple Plot]