Application Center - Maplesoft

App Preview:

Statistics Supplement

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application


 

examplesMaster.mws

Examples for Statistics Supplement

by Zaven Karian

 1.    Simple Simulations

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

>    A := Die(6,8);

A := [4, 3, 4, 6, 5, 3, 6, 3]

>    Freq(A, 1..6);

[0, 0, 3, 2, 1, 2]

>    A := Die(4, 400):

>    Freq(A, 1..4);

[101, 107, 87, 105]

>    Coins := DiscreteS( [0, 1/2, 1, 1/2], 8);

Coins := [1, 1, 0, 1, 1, 0, 0, 1]

>    pdf := x/10;

pdf := 1/10*x

>    Sample := DiscreteS(pdf, 1..4, 5);

Sample := [3, 4, 1, 1, 1]

>    Sample := DiscreteS(pdf, 1..4, 300):

>    EH := Histogram(Sample, 0.5..4.5, 4):

>    PH := ProbHist(pdf, 1..4):

>    display({EH,PH});

[Maple Plot]

>    S1 := BinomialS(10, 0.25, 200):

>    S2 := GammaS(3, 2, 100):

>    PlotEmpPDF(S2);

[Maple Plot]

>    PlotEmpPDF(S2, 0..25, 10);

[Maple Plot]

>    PlotEmpCDF(S2,0..25);

[Maple Plot]

>    BoxWhisker(S1,S2);

[Maple Plot]

>    QQ(S1,S2);

[Maple Plot]

>   

 2.    Relationships Between Distributions

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

>    for i from 1 to 16 do

>    H := ProbHist(BinomialPDF(2*i, 0.25, x), 0..16, 17):

>    N := plot(NormalPDF(i/2, 3*i/8, x), x=i/2-4*sqrt(3*i/8)..i/2+4*sqrt(3*i/8)):

>    P||i := display({H,N}):

>    od:

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

[Maple Plot]

>    display(P2);

[Maple Plot]

>    display(P3);

[Maple Plot]

>    display(P12);

[Maple Plot]

>    display(P16);

[Maple Plot]

>   

 3.    The Power Function of a Statistical Test

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

>    n := 21; var := 100; alpha := 0.05;

n := 21

var := 100

alpha := .5e-1

>    L := var*ChisquareP(n-1, alpha/2)/sigma^2;

L := 959.0777392/sigma^2

>    U := var*ChisquareP(n-1, 1-alpha/2)/sigma^2;

U := 3416.960690/sigma^2

>    K := 1-ChisquareCDF(n-1,U) + ChisquareCDF(n-1,L);

K := 1+1.*(.3417644210e24*exp(-1708.480345/sigma^2)+.1800360068e22*exp(-1708.480345/sigma^2)*sigma^2+.8430228993e19*exp(-1708.480345/sigma^2)*sigma^4+.3454040495e17*exp(-1708.480345/sigma^2)*sigma^6+.1...
K := 1+1.*(.3417644210e24*exp(-1708.480345/sigma^2)+.1800360068e22*exp(-1708.480345/sigma^2)*sigma^2+.8430228993e19*exp(-1708.480345/sigma^2)*sigma^4+.3454040495e17*exp(-1708.480345/sigma^2)*sigma^6+.1...
K := 1+1.*(.3417644210e24*exp(-1708.480345/sigma^2)+.1800360068e22*exp(-1708.480345/sigma^2)*sigma^2+.8430228993e19*exp(-1708.480345/sigma^2)*sigma^4+.3454040495e17*exp(-1708.480345/sigma^2)*sigma^6+.1...
K := 1+1.*(.3417644210e24*exp(-1708.480345/sigma^2)+.1800360068e22*exp(-1708.480345/sigma^2)*sigma^2+.8430228993e19*exp(-1708.480345/sigma^2)*sigma^4+.3454040495e17*exp(-1708.480345/sigma^2)*sigma^6+.1...
K := 1+1.*(.3417644210e24*exp(-1708.480345/sigma^2)+.1800360068e22*exp(-1708.480345/sigma^2)*sigma^2+.8430228993e19*exp(-1708.480345/sigma^2)*sigma^4+.3454040495e17*exp(-1708.480345/sigma^2)*sigma^6+.1...
K := 1+1.*(.3417644210e24*exp(-1708.480345/sigma^2)+.1800360068e22*exp(-1708.480345/sigma^2)*sigma^2+.8430228993e19*exp(-1708.480345/sigma^2)*sigma^4+.3454040495e17*exp(-1708.480345/sigma^2)*sigma^6+.1...
K := 1+1.*(.3417644210e24*exp(-1708.480345/sigma^2)+.1800360068e22*exp(-1708.480345/sigma^2)*sigma^2+.8430228993e19*exp(-1708.480345/sigma^2)*sigma^4+.3454040495e17*exp(-1708.480345/sigma^2)*sigma^6+.1...

>    P4 := plot(K, sigma=0..20): P4;

[Maple Plot]

>   

>    display(P4);

[Maple Plot]

>    Alpha := [0.005, 0.01, 0.025, 0.05, 0.1, 0.2];

Alpha := [.5e-2, .1e-1, .25e-1, .5e-1, .1, .2]

>    for i from 1 to 6 do

>    alpha := Alpha[i];

>    L := var*ChisquareP(n-1, alpha/2)/sigma^2;

>    U := var*ChisquareP(n-1, 1-alpha/2)/sigma^2;

>    K := 1-ChisquareCDF(n-1,U) + ChisquareCDF(n-1,L):

>    P||i := plot(K, sigma=0..20):

>    od:

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

[Maple Plot]

>    display([P1,P2,P3,P4,P5,P6], insequence = true);

[Maple Plot]

>   

>    alpha := 0.05;

alpha := .5e-1

>    for i from 1 to 6 do

>    n := 1+4*i:

>    L := var*ChisquareP(n-1,alpha/2)/sigma^2:

>    U := var*ChisquareP(n-1,1-alpha/2)/sigma^2:

>    K := 1-ChisquareCDF(n-1,U) + ChisquareCDF(n-1,L):

>    Q||i := plot(K, sigma=0..20):

>    od:

>    display({Q1,Q2,Q3,Q4,Q5,Q6});

[Maple Plot]

>    display([Q1,Q2,Q3,Q4,Q5,Q6], insequence = true);

[Maple Plot]

>   

 4.    Minimum-Length Confidence Intervals for Variances

Minimal length CI for variance.

Using the ordianry CI (with equal probability in each tail),

we get the following for the length of the CI for the variance.

 

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

>    n := 7: alpha := 0.05:

>    UsualL := S^2*(n-1)*( 1/ChisquareP(n-1,alpha/2) - 1/ChisquareP(n-1,1-alpha/2) );

UsualL := 4.433852300*S^2

>    f:=ChisquarePDF(n-1,x);

f := 1/16*x^2*exp(-1/2*x)

>    eq1 := int(f, x=a..b)=1-alpha;

eq1 := -1/8*b^2*exp(-1/2*b)-1/2*b*exp(-1/2*b)-exp(-1/2*b)+1/8*a^2*exp(-1/2*a)+1/2*a*exp(-1/2*a)+exp(-1/2*a) = .95

>    eq2:=a^2*subs(x=a,f)=b^2*subs(x=b,f);

eq2 := 1/16*a^4*exp(-1/2*a) = 1/16*b^4*exp(-1/2*b)

>    solution := fsolve({eq1,eq2},{a,b},{a=0..n-1, b=n-1..infinity});

solution := {a = 1.623295402, b = 22.74097663}

>    assign(%);

>    MinimumL := S^2*(n-1)*(1/a-1/b);

MinimumL := 3.432344018*S^2

>    100*(UsualL-MinimumL)/UsualL;

22.58776825

>   

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

>    n := 7:

>    UsualL:=(n-1)*(1/ChisquareP(n-1,alpha/2)-1/ChisquareP(n-1,1-alpha/2));

UsualL := 6/stat:-ChisquareP(6,1/2*alpha)-6/stat:-ChisquareP(6,1-1/2*alpha)

>    A := plot(UsualL, alpha=0.01..0.2):

>    f:=ChisquarePDF(n-1,x):

>    eq1 := int(f, x=a..b)=1-alpha:

>    eq2:=a^2*subs(x=a,f)=b^2*subs(x=b,f):

>    MinLenArray := array(1..39):

>    for i from 2 to 40 do

>    eqns := subs(alpha=0.005*i, {eq1, eq2});
Sol := fsolve(eqns, {a,b}, {a=0..n-1, b=n-1..infinity});

>    assign(Sol);

>    MinLenArray[i-1] := [0.005*i, (n-1)*(1/a-1/b)];

>    a:='a'; b:='b';

>    od:

>    MinLenList := convert(MinLenArray, list);

MinLenList := [[.10e-1, 6.688789122], [.15e-1, 5.705101596], [.20e-1, 5.081050957], [.25e-1, 4.634923603], [.30e-1, 4.293057188], [.35e-1, 4.018863241], [.40e-1, 3.791727490], [.45e-1, 3.598984828], [....
MinLenList := [[.10e-1, 6.688789122], [.15e-1, 5.705101596], [.20e-1, 5.081050957], [.25e-1, 4.634923603], [.30e-1, 4.293057188], [.35e-1, 4.018863241], [.40e-1, 3.791727490], [.45e-1, 3.598984828], [....
MinLenList := [[.10e-1, 6.688789122], [.15e-1, 5.705101596], [.20e-1, 5.081050957], [.25e-1, 4.634923603], [.30e-1, 4.293057188], [.35e-1, 4.018863241], [.40e-1, 3.791727490], [.45e-1, 3.598984828], [....
MinLenList := [[.10e-1, 6.688789122], [.15e-1, 5.705101596], [.20e-1, 5.081050957], [.25e-1, 4.634923603], [.30e-1, 4.293057188], [.35e-1, 4.018863241], [.40e-1, 3.791727490], [.45e-1, 3.598984828], [....
MinLenList := [[.10e-1, 6.688789122], [.15e-1, 5.705101596], [.20e-1, 5.081050957], [.25e-1, 4.634923603], [.30e-1, 4.293057188], [.35e-1, 4.018863241], [.40e-1, 3.791727490], [.45e-1, 3.598984828], [....
MinLenList := [[.10e-1, 6.688789122], [.15e-1, 5.705101596], [.20e-1, 5.081050957], [.25e-1, 4.634923603], [.30e-1, 4.293057188], [.35e-1, 4.018863241], [.40e-1, 3.791727490], [.45e-1, 3.598984828], [....
MinLenList := [[.10e-1, 6.688789122], [.15e-1, 5.705101596], [.20e-1, 5.081050957], [.25e-1, 4.634923603], [.30e-1, 4.293057188], [.35e-1, 4.018863241], [.40e-1, 3.791727490], [.45e-1, 3.598984828], [....

>    A := plot(UsualL, alpha=0.01..0.2):

>    B:=plot(MinLenList, color=blue):

>    display({A,B});

[Maple Plot]

>    Improv := [seq([0.005+0.005*i,(value(subs(alpha=0.005+0.005*i,UsualL)-MinLenList[i][2]))], i=1..39)];

Improv := [[.10e-1, 1.867047064], [.15e-1, 1.607394266], [.20e-1, 1.442080100], [.25e-1, 1.323516085], [.30e-1, 1.232387244], [.35e-1, 1.159089353], [.40e-1, 1.098206009], [.45e-1, 1.046406439], [.50e-...
Improv := [[.10e-1, 1.867047064], [.15e-1, 1.607394266], [.20e-1, 1.442080100], [.25e-1, 1.323516085], [.30e-1, 1.232387244], [.35e-1, 1.159089353], [.40e-1, 1.098206009], [.45e-1, 1.046406439], [.50e-...
Improv := [[.10e-1, 1.867047064], [.15e-1, 1.607394266], [.20e-1, 1.442080100], [.25e-1, 1.323516085], [.30e-1, 1.232387244], [.35e-1, 1.159089353], [.40e-1, 1.098206009], [.45e-1, 1.046406439], [.50e-...
Improv := [[.10e-1, 1.867047064], [.15e-1, 1.607394266], [.20e-1, 1.442080100], [.25e-1, 1.323516085], [.30e-1, 1.232387244], [.35e-1, 1.159089353], [.40e-1, 1.098206009], [.45e-1, 1.046406439], [.50e-...
Improv := [[.10e-1, 1.867047064], [.15e-1, 1.607394266], [.20e-1, 1.442080100], [.25e-1, 1.323516085], [.30e-1, 1.232387244], [.35e-1, 1.159089353], [.40e-1, 1.098206009], [.45e-1, 1.046406439], [.50e-...
Improv := [[.10e-1, 1.867047064], [.15e-1, 1.607394266], [.20e-1, 1.442080100], [.25e-1, 1.323516085], [.30e-1, 1.232387244], [.35e-1, 1.159089353], [.40e-1, 1.098206009], [.45e-1, 1.046406439], [.50e-...
Improv := [[.10e-1, 1.867047064], [.15e-1, 1.607394266], [.20e-1, 1.442080100], [.25e-1, 1.323516085], [.30e-1, 1.232387244], [.35e-1, 1.159089353], [.40e-1, 1.098206009], [.45e-1, 1.046406439], [.50e-...

>    PercImprov := [seq([0.005+0.005*i, 100*(value(subs(alpha=0.005+0.005*i,UsualL))-MinLenList[i][2])/value(subs(alpha=0.005+0.005*i,UsualL))], i=1..39)];

PercImprov := [[.10e-1, 21.82191224], [.15e-1, 21.98147249], [.20e-1, 22.10717656], [.25e-1, 22.21246088], [.30e-1, 22.30385735], [.35e-1, 22.38509009], [.40e-1, 22.45850601], [.45e-1, 22.52569006], [....
PercImprov := [[.10e-1, 21.82191224], [.15e-1, 21.98147249], [.20e-1, 22.10717656], [.25e-1, 22.21246088], [.30e-1, 22.30385735], [.35e-1, 22.38509009], [.40e-1, 22.45850601], [.45e-1, 22.52569006], [....
PercImprov := [[.10e-1, 21.82191224], [.15e-1, 21.98147249], [.20e-1, 22.10717656], [.25e-1, 22.21246088], [.30e-1, 22.30385735], [.35e-1, 22.38509009], [.40e-1, 22.45850601], [.45e-1, 22.52569006], [....
PercImprov := [[.10e-1, 21.82191224], [.15e-1, 21.98147249], [.20e-1, 22.10717656], [.25e-1, 22.21246088], [.30e-1, 22.30385735], [.35e-1, 22.38509009], [.40e-1, 22.45850601], [.45e-1, 22.52569006], [....
PercImprov := [[.10e-1, 21.82191224], [.15e-1, 21.98147249], [.20e-1, 22.10717656], [.25e-1, 22.21246088], [.30e-1, 22.30385735], [.35e-1, 22.38509009], [.40e-1, 22.45850601], [.45e-1, 22.52569006], [....
PercImprov := [[.10e-1, 21.82191224], [.15e-1, 21.98147249], [.20e-1, 22.10717656], [.25e-1, 22.21246088], [.30e-1, 22.30385735], [.35e-1, 22.38509009], [.40e-1, 22.45850601], [.45e-1, 22.52569006], [....
PercImprov := [[.10e-1, 21.82191224], [.15e-1, 21.98147249], [.20e-1, 22.10717656], [.25e-1, 22.21246088], [.30e-1, 22.30385735], [.35e-1, 22.38509009], [.40e-1, 22.45850601], [.45e-1, 22.52569006], [....

>    StandLen := S^2*(n-1)*( 1/ChisquareP(n-1,alpha/2) - 1/ChisquareP(n-1,1-alpha/2) );

StandLen := 6*S^2*(1/stat:-ChisquareP(6,1/2*alpha)-1/stat:-ChisquareP(6,1-1/2*alpha))

>    LFact := subs({alpha=0.05, S=1}, StandLen);

LFact := 6/stat:-ChisquareP(6,.2500000000e-1)-6/stat:-ChisquareP(6,.9750000000)

>    PlotList1 := [seq( [i, value(subs(n=i,LFact))], i=3..15)];

PlotList1 := [[3, 4.433852300], [4, 4.433852300], [5, 4.433852300], [6, 4.433852300], [7, 4.433852300], [8, 4.433852300], [9, 4.433852300], [10, 4.433852300], [11, 4.433852300], [12, 4.433852300], [13,...
PlotList1 := [[3, 4.433852300], [4, 4.433852300], [5, 4.433852300], [6, 4.433852300], [7, 4.433852300], [8, 4.433852300], [9, 4.433852300], [10, 4.433852300], [11, 4.433852300], [12, 4.433852300], [13,...

>    A := plot(PlotList1, color=red): A;

[Maple Plot]

>    f := ChisquarePDF(n-1, x);

f := 1/16*x^2*exp(-1/2*x)

>    eq1 := int(f, x=a..b)=1-alpha;

eq1 := -1/8*b^2*exp(-1/2*b)-1/2*b*exp(-1/2*b)-exp(-1/2*b)+1/8*a^2*exp(-1/2*a)+1/2*a*exp(-1/2*a)+exp(-1/2*a) = 1-alpha

>    eq2 := a^2*subs(x=a,f) = b^2*subs(x=b,f);

eq2 := 1/16*a^4*exp(-1/2*a) = 1/16*b^4*exp(-1/2*b)

>    alpha := 0.05;

alpha := .5e-1

>    PlotArray := array(1..13);

PlotArray := array(1 .. 13,[])

>    for i from 3 to 15 do

>    eqns := subs(n=i, {eq1, eq2});
Sol := fsolve(eqns, {a,b}, {a=0..i-1, b=i-1..infinity});

>    assign(Sol);

>    PlotArray[i-2] := [i, (i-1)*(1/a-1/b)];

>    a:='a'; b:='b';

>    od:

>    PlotList2 := convert(PlotArray, list);

PlotList2 := [[3, 1.144114673], [4, 1.716172009], [5, 2.288229345], [6, 2.860286682], [7, 3.432344018], [8, 4.004401354], [9, 4.576458690], [10, 5.148516027], [11, 5.720573363], [12, 6.292630699], [13,...
PlotList2 := [[3, 1.144114673], [4, 1.716172009], [5, 2.288229345], [6, 2.860286682], [7, 3.432344018], [8, 4.004401354], [9, 4.576458690], [10, 5.148516027], [11, 5.720573363], [12, 6.292630699], [13,...

>    B:=plot(PlotList2, color=blue):

>    display({A,B});

[Maple Plot]

>    [seq( [PlotList1[i][1], 100*(PlotList1[i][2] - PlotList2[i][2])/PlotList1[i][2]], i=1..13)];

[[3, 74.19592274], [4, 61.29388412], [5, 48.39184551], [6, 35.48980687], [7, 22.58776825], [8, 9.685729631], [9, -3.216308987], [10, -16.11834763], [11, -29.02038625], [12, -41.92242486], [13, -54.8244...
[[3, 74.19592274], [4, 61.29388412], [5, 48.39184551], [6, 35.48980687], [7, 22.58776825], [8, 9.685729631], [9, -3.216308987], [10, -16.11834763], [11, -29.02038625], [12, -41.92242486], [13, -54.8244...

>   

 5.    Investigating the Properties of a Distribution

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

>    f := ChisquarePDF(nu, x);

f := 1/GAMMA(1/2*nu)/(2^(1/2*nu))*x^(1/2*nu-1)*exp(-1/2*x)

>    assume(nu>0); interface(showassumed=0);

>    simplify(int(f, x=0..infinity));

1

>    mu := simplify(int(x*f, x=0..infinity));

mu := nu

>    int(simplify(x^2*f), x=0..infinity) - mu^2;

1/GAMMA(1/2*nu)*2^(-1/2*nu)*(1/((1/2)^(1/2*nu))*GAMMA(1/2*nu)*nu^2+2/((1/2)^(1/2*nu))*GAMMA(1/2*nu)*nu)-nu^2

>    var := simplify(%);

var := 2*nu

>    animate(f, x=0..25, nu=1..15);

[Maple Plot]

>    plot3d(f, x=0..10, nu=0..5, orientation=[135,45], color=blue, axes=boxed);

[Maple Plot]

>    df := simplify(diff(f, x));

df := -2^(-1-1/2*nu)*x^(1/2*nu-2)*exp(-1/2*x)*(-nu+2+x)/GAMMA(1/2*nu)

>    solve(df=0, x);

nu-2, 0

>   

 6.    Understanding an Important Theorem

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

>    f := x -> (3/2)*x^2;

f := proc (x) options operator, arrow; 3/2*x^2 end proc

>    int(f(x), x=-1..1);

1

>    mu := int(x*f(x), x=-1..1);

mu := 0

>    var := int(x^2*f(x), x=-1..1)- mu^2;

var := 3/5

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

F := 1/2*x^3+1/2

>    A := ContinuousS(f(x), -1..1, 5);

A := [-.5242038871, -.7095721400, -.6776578358, -.3719396403, .4886458716]

>    A := ContinuousS(f(x), -1..1, 400):

>    H := Histogram(A, -1..1, 12):

>    P := plot(f(x), x=-1..1):

>    display({P,H});

[Maple Plot]

>    S4 := [seq(ContinuousS(f(x), -1..1, 4), i=1..300)]:

>    S4[1];

[-.6818391032, .8768027182, .9309436746, .8620100903]

>    M4 := [seq(Mean(S4[i]), i=1..300)]:

>    H4 := Histogram(M4, -1..1, 7):

>    n4 := NormalPDF(mu, var/4, x):

>    N4 := plot(n4, x=-1..1, color=blue):

>    display({P, H4, N4});

[Maple Plot]

>    S8 := [seq(ContinuousS(f(x), -1..1, 8), i=1..300)]:

>    A[1];

.7893526922

>    M8 := [seq(Mean(S8[i]), i=1..300)]:

>    H8 := Histogram(M8, -1..1, 7):

>    n8 := NormalPDF(mu, var/8, x):

>    N8 := plot(n8, x=-1..1, color=blue):

>    display({P, H8, N8});

[Maple Plot]

>   

>    A := [seq(ContinuousS(f(x), -1..1, 16), i=1..300)]:

>    M16 := [seq(Mean(A[i]), i=1..300)]:

>    H16 := Histogram(M16, -1..1, 13):

>    n16 := NormalPDF(mu, var/16, x):

>    N16 := plot(n16, x=-1..1, color=blue):

>    display({P, H16, N16});

[Maple Plot]

>   

 7.    Relationships Between Random Variables

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

>    Z1 := NormalS(0,1,500):

>    Z1H := Histogram(Z1, -4..4, 9):

>    N01 := plot(NormalPDF(0,1,x), x=-4..4):

>    display({Z1H, N01});

[Maple Plot]

>    Y := [seq(Z1[i]^2, i=1..500)]:

>    YH := Histogram(Y, 0..12, 16):

>    CH1 := plot(ChisquarePDF(1,x), x=0..12):

>    display({YH, CH1});

[Maple Plot]

>   

>    Z2 := NormalS(0,1,500):

>    Z3 := NormalS(0,1,500):

>    Y := [seq(Z1[i]^2+Z2[i]^2+Z3[i]^2, i=1..500)]:

>    YH := Histogram(Y, 0..18, 24):

>    CH3 := plot(ChisquarePDF(3,x), x=0..12):

>    #interface(plotdevice=postscript, plotoutput=Fig7b);

>    display({YH, CH3});

[Maple Plot]

>   

>    Z := [seq(NormalS(37, 16, 3), i=1..200)]:

>    S := [seq(sum( (Z[i][j]-37)^2/16, j=1..3), i=1..200)]:

>    T := [seq(sum( (Z[i][j]-Mean(Z[i]))^2/16, j=1..3), i=1..200)]:

>    SH := Histogram(S, 0..20, 16):

>    TH := Histogram(T, 0..20, 16):

>    CH2 := plot(ChisquarePDF(2,x), x=0..20):

>    CH3 := plot(ChisquarePDF(3,x), x=0..20):

>    display({CH3, SH});

[Maple Plot]

>    display({CH2, TH});

[Maple Plot]

>   

 8.    Confidence Intervals

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

>    ListOfSamples := [seq(NormalS(40,12,5), i=1..50)]:

>    CIVarUnknown := ConfIntMean(ListOfSamples, 80):

>    ConfIntPlot(CIVarUnknown, 40);

[Maple Plot]

>    CIVarKnown := ConfIntMean(ListOfSamples, 80, 12):

>    ConfIntPlot(CIVarKnown, 40);

[Maple Plot]

>   

 9.    Order Statistics

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

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

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 = 0 .. x) end proc

>    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!/(r-1)!/(n-r)!*F(y)^(r-1)*(1-F(y))^(n-r)*f(y) end proc

>    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!/k!/(n-k)!*F(y)^k*(1-F(y))^(n-k),k = r .. n) end proc

>    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); interface(showassumed=0);

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

f := proc (x) options operator, arrow; stat:-ExponentialPDF(theta,x) end proc

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

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

>    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!/(r-1)!/(n-r)!*F(y)^(r-1)*(1-F(y))^(n-r)*f(y) end proc

>    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!/k!/(n-k)!*F(y)^k*(1-F(y))^(n-k),k = r .. n) end proc

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-8*exp(1/theta)+3*exp(2/theta)-6*exp(3/theta)+8*exp(5/2/theta))*exp(-4/theta)

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

a numeric answer.

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

.3595791453

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 stat 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); interface(showassumed=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!/(r-1)!/(n-r)!*(-exp(-y/theta)+1)^(r-1)*exp(-y/theta)^(n-r)*exp(-y/theta)/theta

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

G := n!*(-1/exp(y/theta)+1)^r*hypergeom([1, -n+r],[1+r],1-exp(y/theta))/r!/(n-r)!/((1/exp(y/theta))^(-n+r))

For contrasrt, we now consider order stat from a

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

>    assume(sigma > 0); interface(showassumed=0);

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

f := 1/2*2^(1/2)/sigma/Pi^(1/2)*exp(-1/2*(y-mu)^2/sigma^2)

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

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

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

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

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

g := 1/2*n!/(r-1)!/(n-r)!*(-1/2*erf(1/2*2^(1/2)*(-y+mu)/sigma)+1/2)^(r-1)*(1/2+1/2*erf(1/2*2^(1/2)*(-y+mu)/sigma))^(n-r)*2^(1/2)/sigma/Pi^(1/2)*exp(-1/2*(y-mu)^2/sigma^2)

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

G := GAMMA(n+1)/GAMMA(1+r)/GAMMA(n+1-r)*(-1/2*erf(1/2*2^(1/2)*(-y+mu)/sigma)+1/2)^r*(1/2+1/2*erf(1/2*2^(1/2)*(-y+mu)/sigma))^(n-r)*hypergeom([1, -n+r],[1+r],1/(1+erf(1/2*2^(1/2)*(-y+mu)/sigma))*(erf(1/...

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*2^(1/2)*y))*(erf(1/2*2^(1/2)*y)+1)^3

>    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 stat 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*2^(1/2)*y)+1)^2*(-1+erf(1/2*2^(1/2)*y))*2^(1/2)*exp(-1/2*y^2)/Pi^(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]

>   

 10.  Regression

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

>    X := [1,2,3,4,5];

X := [1, 2, 3, 4, 5]

>    Y := [2,3,1,5,4];

Y := [2, 3, 1, 5, 4]

>    r:= Correlation(X,Y);

r := .6000000000

>    A := BivariateNormalS(100, 225, 400, 625, 0.9, 200):

>    A[1..5];

[[117.6375935, 420.3172719], [103.5309099, 389.5765374], [83.81205649, 375.4781857], [61.22082454, 337.0008000], [84.95077778, 377.1224633]]
[[117.6375935, 420.3172719], [103.5309099, 389.5765374], [83.81205649, 375.4781857], [61.22082454, 337.0008000], [84.95077778, 377.1224633]]

>    r := Correlation(A);

r := .9021624098

>    y := LinReg(A, x);

y := 249.3232275+1.510706938*x

>    Y := PolyReg(A, 3, x);

Y := 262.2843503+.8090075121*x+.1007968390e-1*x^2-.4290695904e-4*x^3

>    ScatPlot(A);

[Maple Plot]

>    display( ScatPlot(A), plot(y, x=50..150));

[Maple Plot]

>