Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5K views7 pages

Maple Stat

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

International Statistical Institute, 56th Session, 2007: Alex Potapchik

MATHEMATICAL STATISTICS WITH MAPLE

ALEX POTAPCHIK

Abstract. Widely adopted by universities, colleges, research institu-


tions, and companies, Maple delivers the comprehensive environment
and support resources to visualize and explore mathematical concepts,
develop application tools, and share mathematical information and doc-
uments through the Web. Combined with hundreds of free add-on pack-
ages and applications available through the Maple application centre,
Maple constitutes a flexible analytical tool for professors, researchers,
and students. The goal of my talk is to demonstrate some of the bene-
fits of using Maple for teaching mathematical statistics. I will focus on
the new Statistics package.

The Statistics package - introduced with Maple 10 - is a collection of func-


tions and interactive tools for mathematical statistics and data analysis. It
supports a wide range of common statistical tasks, such as quantitative and
graphical data analysis, simulation, and curve fitting. In contrast to purely
numerical/graphical systems such as SPSS, JMP, Gauss, the package was
designed to take advantage of Maple’s symblic power. The tools available
in the Statistics package can greatly facilitate various tasks that are of pri-
mary interest in mathematical statistics such as calculations with abstract
random variables and symbolic manipulation of likelihood functions.

1. Probability Calculations
At the most basic level, the Statistics package can be used as a reference
tool to get information about various continuous and discrete probability
density functions.

Example 1. The following Maple command will return the mean of a


random variable which has a Weibull distribution with scale parameter b
and shape parameter c.

> Mean(Weibull(b, c));

This yields  
c+1
b·Γ .
c
Here is the standard deviation:
Maplesoft, Waterloo, Canada.
1
International Statistical Institute, 56th Session, 2007: Alex Potapchik

2 ALEX POTAPCHIK

> StandardDeviation(Weibull(b, c));


v
u     2 !
u
tb Γ c + 2 c + 1
− Γ
c c
(Note that the latex code for the above output was also generated by Maple).
We can also get the corresponding probability density function

> f := PDF(Weibull(b, c), t);


(
0 t<0
t c ,
ct−1+c e−( b ) (bc )−1 otherwise
and evaluate it at various points

> eval(f, t = 1);


c
ce−(b )
−1

bc
> eval(f, [t = 1, c = 1/6, b = 1/3]);
√ √
6
1/6 3e− 3
6

Note that Maple stores only the most important facts about any given dis-
tribution in the data base. Everything else is calculated on the fly.
In the next set of examples we will define an abstract random variable
and calculate some quantities of interest.

Example 2. The following command defines a random variable X ≡


N ormal(a, b), where a and b are parameters.

> X := RandomVariable(Normal(a, b));

X
Note, that X is a perfectly valid Maple object. It can be used as an argument
to Maple functions and can appear as a part of larger expressions. Just as
a simple test, let’s compute the mean and the variance of X

> Mean(X), Variance(X);

a, b2 .
Here is the 5th moment of X
International Statistical Institute, 56th Session, 2007: Alex Potapchik

MATHEMATICAL STATISTICS WITH MAPLE 3

> Moment(X, 5);

15 b4 a + 10 b2 a3 + a5
We can also calculate the mean of X 2 :

> Mean(X^2);

a2 + b 2
For that matter, we can also calculate the whole density function of X 2

> PDF(X^2, t);



0 t≤0
2 2

√ √
( t+a) (− t+a)
 
1/4 1 √1 −1
2 e−1/2 b2 + e−1/2 b2 √
t π
b 0<t

Similarly, we compute the density of ((X − a)/b) 2 and compare the result
with the χ2 -density.

> PDF(((X-a)/b)^2, t) = PDF(ChiSquare(1), t);


( (
0 t≤0 0 t<0
√ −1/2 t
2e = √ −1/2 t
2e
1/2 √ √
π t
0<t 1/2 √ √
π t
otherwise

Example 3. As a little more complicated example, consider five random


variables Xi each having a standard normal distribution. We will assume
our random variables to be independent.

> X := seq(RandomVariable(Normal(0, 1)), i = 1..5);

Here is the density function for their sum:

> PDF(X[1]+X[2]+X[3]+X[4]+X[4]+X[5], t);

2√ √
e−1/10 t 5 2
1/10 √
π
and their sum of squares

> PDF(X[1]^2+X[2]^2+X[3]^2+X[4]^2+X[4]^2+X[5]^2, t);


International Statistical Institute, 56th Session, 2007: Alex Potapchik

4 ALEX POTAPCHIK
(
0 t≤0
√ −1/2 t
t3/2 √
2e .
1/6 π
0<t
Again, it may be instructive to compare the result with a χ 2 density with 5
degrees of freedom:

> PDF(ChiSquare(5), t);


(
0 t≤0
√ −1/2 t
t3/2 √
2e .
1/6 π
0<t

2. Regression and Parameter Estimation


The following set of examples demonstrates the use of Maple’s symbolic
capabilities for parameter estimation.
Let us create a sample data set.

> S := Sample(Weibull(2.3, 3.7), 1000);

Here is how our data looks like.

0.7

0.6

0.5

0.4

0.3

0.2

0.1

0.0
0.0 0.4 0.8 1.2 1.6 2.0 2.4 2.8 3.2 3.6 4.0

We would like to fit a Weibull distribution to this data set.


One of the simplest methods is the method of moments. Since we have
two parameter in our distribution, we need two equations to calculate them.
So we will match the corresponding moments of the data set and the distri-
bution.
Here is the first equation.
International Statistical Institute, 56th Session, 2007: Alex Potapchik

MATHEMATICAL STATISTICS WITH MAPLE 5

> E[1] := Moment(Weibull(a, b), 1) = Moment(S, 1);


 
b+1
aΓ = 2.043681895
b
Here is the second equation.

> E[2] := Moment(Weibull(a, b), 2) = Moment(S, 2);


 
2 b+2
a Γ = 4.532388451
b
Now all we need is to solve these equations.

> fsolve({E[1], E[2]}, {a, b});

We thus obtain our first solution.


{a = 2.260287371, b = 3.828867240}
We can refine the estimate using the maximum likelihood method. The
procedure is very simple. We build the likelihood function symbolically and
find the values of a and b that maximize the value of this function.

> F := LogLikelihood(Weibull(a, b), S);


> NLPSolve(-F, a = 2.1 .. 2.4, b = 3.5 .. 3.8);
This gives our second solution
[901.045550122, [a = 2.261386112, b = 3.788696941]]
Yet another option is to use rank regression. First we will rank our data set
and convert the ranks to probabilities.

> A := map(t -> 0.001 t, Rank(S));


> f := CDF(Weibull(a, b), t);
> g := NonlinearFit(f, S, A, t);

To find a and b we simply solve the equations.

> solve({eval(f = g, t = 2), eval(f = g, t = 1)}, {a, b});

This gives our third solution.


{a = 2.255897938, b = 3.776849178}
International Statistical Institute, 56th Session, 2007: Alex Potapchik

6 ALEX POTAPCHIK

3. Random Variate Generation / Simulation


The package provides optimized algorithms for simulating from all sup-
ported distributions as well as tools for creating custom random number
generators.

Example 1. The following command will generate a random sample drawn


from the standard normal distribution.

> Sample(Normal(0, 1), 10^6);

Alternatively, the Sample command can return a Maple procedure which can
be used to generate random samples drawn from the normal distribution.

> F := Sample(Normal(0, 1));


> F(10^6);

Consider the following two random variables

> X := RandomVariable(Logistic(0, 1));


> Y := RandomVariable(Logistic(10, 2));

Let’s compare the two densities.

0.25

0.2

0.15

0.1

0.05

0.0
−4 −2 0 2 4 6 8 10 12 14 16 18 20

We would like to consider a new random variable, which is X with prob-


ability 1/5 and Y with probability 4/5.
International Statistical Institute, 56th Session, 2007: Alex Potapchik

MATHEMATICAL STATISTICS WITH MAPLE 7

> U := RandomVariable(Bernoulli(1/5)):
> Z := U*X+(1-U)*Y:

We can generate a random sample drawn from this distribution.

> S := Sample(Z, ^(10, 6));

Here is the result of the simulation.

0.25

0.2

0.15

0.1

0.05

0.0
−4 −2 0 2 4 6 8 10 12 14 16 18 20

References
[1] Karian, Z. A., and Tanis, E. A., Probability and Statistics: Explorations with MAPLE
(2nd ed.), 1999, New York: Wiley.
[2] Rose, C., and Smith, M. D., Mathematical Statistics and Mathematica, 2001, New
York: Springer-Verlag.
[3] Tanis, E., A Review of Maple V, Release 5, The American Statistician, 53, 1999,
389-392.
[4] Monagan, M., Geddes, K., Heal, M., Labahn, G., Vorkoetter, S., McCarron, J., and
DeMarco, P., Maple 10 Programming Guide, Maplesoft, 2005.

You might also like