0% found this document useful (0 votes)
44 views

Computer Algebra in Statistics An Introduction To Maple

maple notes

Uploaded by

SameOldHat
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Computer Algebra in Statistics An Introduction To Maple

maple notes

Uploaded by

SameOldHat
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Computer Algebra in Statistics An Introduction to Maple

Michael J. Wichura Department of Statistics The University of Chicago Email: [email protected] c M. J. Wichura, 1995. October, 1995

Preface
Course notes for Statistics 304. Comments and corrections are welcome. Please address email correspondence to the author at [email protected]. Michael J. Wichura, University of Chicago, October, 1995.

Contents
Preface 1 Introduction 1.1 1.2 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting in, getting help, and getting out . . . . . . . . . . . . . . . . . . . . . . . . . i 1 1 1 3 3 3 3 5 6

2 The Change of Variable Formula 2.1 Three examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.1.1 2.1.2 2.1.3 2.2 2.3 2.4 Index The logistic distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Pareto-Beta connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Chisquare distribution with one degree of freedom . . . . . . . . . . . . .

Automating the procedure: the one-dimensional case . . . . . . . . . . . . . . . . . .

The multi-dimensional case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Example: the Gamma-Beta-Chisquare-F-T connection . . . . . . . . . . . . . . . . . 18 21

ii

Introduction

Introduction

Maple is an interactive computer algebra system that can be used to analyze mathematical problems using a combination of symbolic, numerical, and graphical tools. Among other things its features include: arbitrary precision arithmetic; routines for doing dierential and integral calculus, both symbolically and numerically; innite and nite series, limits, and products; expansion and factoring of algebraic equations; linear algebra; solutions of systems of equations; pattern matching; special functions; and two- and three-dimensional graphing. Maple has a simple and eective programming language. Loops, conditionals, and user-dened functions are available. Results can be saved to and retrieved from les, and can be incorporated A into Fortran and C programs or L TEX documents. Libraries/packages of special routines can be added to the environment to extend Maples capabilities. Maple will greatly enhance your ability to manipulate mathematical expressions. It can save you a lot of time and eort, by quickly and accurately performing a task that would otherwise be tedious, cumbersome, and prone to errors. Maple is an on-going project of the Symbolic Computation Group at the University of Waterloo in Ontario, Canada. The system is available on a large number of machines, including most Unix workstations, the Macintosh, 386-based PCs, and the NeXT. These notes were developed using Maple on a Unix workstation at the University of Chicago.

1.1

References

A good way for a you to learn Maple in depth is to rst work through the on-line tutorial and then read First Leaves: A Tutorial Introduction to Maple V published by Springer-Verlag. These notes are intended only as a brief introduction, to illustrate some of Maples capabilities in the context of some case studies. Additional references are the Maple V Language Reference Guide and the Maple V Library Reference Manual , also published by Springer-Verlag. Much of the information in these manuals is available through the on-line help facility.

1.2

Getting in, getting help, and getting out

To get started, enter maple in response to your operating systems prompt:


tcsh: maple |\^/| ._|\| |/|_. \ MAPLE / <____ ____> | > Maple V Release 2 (University of Chicago) Copyright (c) 1981-1993 by the University of Waterloo. All rights reserved. Maple and Maple V are registered trademarks of Waterloo Maple Software. Type ? for help.

The > is Maples prompt for you to enter your rst command.

21:38 10/19/1998

Introduction

To nd out about a Maple feature, use the extensive on-line help command ?. Here is what part of what ? has to say about itself:
> ? FUNCTION: help - descriptions of syntax, datatypes, and functions CALLING SEQUENCE: ?topic or ?topic,subtopic or ?topic[subtopic] or help(topic) or help(topic,subtopic) or help(topic[subtopic]) SYNOPSIS: ?intro ?library ?index ?index,<category> ?<topic> ?<topic>,<subtopic> ?distribution ?copyright introduction to Maple Maple library functions and procedures list of all help categories list of help files on specific topics explanation of a specific topic explanation of a subtopic under a topic for information on how to obtain Maple for information about copyrights

- Note 1: The recommended way to invoke help is to use the question mark.

In the following lectures I present many Maple commands with only a brief explanation as to what they do. You should make a habit of using ? to get the full story. Beginners are well-advised to work through the on-line tutorial:
> ? tutorial HELP FOR: tutorial - on-line tutorial introduction to Maple CALLING SEQUENCE: tutorial() tutorial(n) PARAMETERS: n - an integer SYNOPSIS: - The function tutorial() takes the user through a beginning level, on-line tutorial for Maple. This tutorial is not meant to be a replacement for the Maple manuals (First Leaves, etc.) or the on-line help files, but is instead meant to offer a quick method for getting started with Maple. - There are 14 chapters in the on-line tutorial. tutorial(n) skips the standard welcoming text for the tutorial and starts the user at chapter n. - Most chapters have question and answer sections and there are two quizzes included. There is a main menu, from which the reader can begin any of the chapters, which can be accessed from any break in the text.

Important: The commands you type to Maple may extend over several physical lines and must be terminated by a semi-colon (to display the result) or colon (to suppress the display), not a carriage return. (Maple wont see the line until you type a carriage return.) An important point to remember is No (semi-)colon, no computation! The help command ? is one of the few exceptions to this rule. To start afresh, without leaving Maple, give the command restart; this will erase all your current variables, functions, etc. To end your Maple session, you may enter either quit, stop, or done.

21:38 10/19/1998

The Change of Variable Formula

The Change of Variable Formula

Let X be a random variable with density fX and let Y = h(X ) be obtained from X via a transformation h. If h has a continuous non-zero derivative, then Y has a density given by fY (y ) = fX (h1 (y )) dh1 (y ) dx = fX (x) , dy dy (1)

where x = h1 (y ). I am going to illustrate how Maple can be used to evaluate fY .

2.1
2.1.1

Three examples
The logistic distribution

Lets nd the density of Y = log(X/(1 X )) when X has a uniform distribution on (0, 1). First we need to solve the equation y = log(x/(1 x)) = h(x) for x = h1 (y ):
> solve (y = log(x/(1-x)), x); exp(y) ---------exp(y) + 1

Terminating the solve command with a semicolon caused the result to be displayed. Next nd the derivative of this with respect to y :
> diff (", y); 2 exp(y) exp(y) ---------- - ------------exp(y) + 1 2 (exp(y) + 1)

The double quote operator " returns the most recently computed expression, whatever that may be; here its the solution exp(y )/(exp(y ) + 1) to the equation. (Similarly "" returns the second most recently computed expression, and """ the third.) Now simplify the derivative:
> simplify("); exp(y) ------------2 (exp(y) + 1)

This is dx/dy . Since it is clearly continuous and positive, so is h = dy/dx and formula (1) applies. y y 2 Since fX (x) = 1, Y has density fY (y ) = fX (x)| dx dy | = e /(e + 1) , for < y < . Y is said to be distributed according to the standard logistic distribution. Were all done with this calculation, so reinitialize Maple:
> restart;

2.1.2

The Pareto-Beta connection

The Pareto distribution with location parameter k > 0 and shape parameter > 0 has density f (x) = k x+1 for x k. (2)
21:38 10/19/1998

The Change of Variable Formula

This distribution is sometimes used to model certain long-tailed socio-economic variates, such as city size, personal income, etc. Suppose X is a random variable with density (2). What is the density of Y = k/X ? To nd out write y = k/x, solve for x in terms of y , and nd
> solve (y = k/x, x); > diff (", y); k/y k - ---2 y
dx dy :

Next introduce the Pareto density fX (x):


> f := alf * k^alf / x^(alf + 1); alf alf k f := ---------(alf + 1) x

The := operator assigned the expression k /x+1 to the variable f. Note that assignments are specied by := in Maple, not by = as in Fortran or C. Now use the substitution command subs to replace the x in fX (x) by the solution k/y , which was computed three steps earlier:
> subs (x = """, f); alf alf k -------------(alf + 1) (k/y)

The command subs(eqn, expr) returns the result of applying the substitution specied by the equation eqn to the expression expr. Multiply fX (x) by | dx dy | to get fY (y ):
> simplify (" * abs(""")); (alf - 1) alf y abs(k) --------------------k

Tell Maple that k is positive:


> assume (k > 0); > "; alf y (alf - 1)

Note that here " refers back to the result of the simplify command, since assume didnt compute anything. It follows that Y has density fY (y ) = y 1 , for 0 < y 1; that is, Y has a Beta distribution with parameters and 1. Maple has a history command that will automatically assign labels to each output expression. This enables you to easily refer back to any previous result, but there are some disadvantages see the help page. An alternative approach to back referencing is to selectively assign your own labels; this approach is illustrated in the next section.
21:38 10/19/1998

2.1

Three examples

2.1.3

The Chisquare distribution with one degree of freedom

Now lets nd the density of Y = h(X ) := X 2 when X is standard normal. Since the transformation h maps each of the intervals (, 0) and (0, ) monotonically onto (0, ), the density for Y is given by dx2 dx1 fY (y ) = fX (x1 ) + fX (x2 ) dy dy with x1 = y and x2 = + y , for y > 0. To evaluate this in Maple, begin by solving the equation y = x2 for x:
> solutions := solve (y = x^2, x); solutions := - y 1/2 1/2 , y

Note that Maple found two solutions, and returned them as a sequence of comma-separated expressions a datatype Maple calls an expression sequence. Convert the solutions to a list, so that we can easily access its nth component using the subscript operator [n]:
> solutions := [solutions]; > solutions[2]; y 1/2 1/2 solutions := [- y , y ] 1/2

Maple uses the notation [. . . ] for a list of elements; e.g., [1,2,3] is a list whose elements are the numbers 1, 2, and 3. Introduce the density 1 fX (x) = exp(x2/2) 2 of the old random variable X :
> old_f := exp(-x^2/2) / sqrt (2*Pi); 2 1/2 exp(- 1/2 x ) 2 old_f := 1/2 -----------------1/2 Pi

Pi is Maples symbol for the number . Get the contribution to the density of Y from the branch x = y of the inverse transformation:
> sol := solutions[1]; > dx_dy := diff (sol, y); 1/2 sol := - y 1 dx_dy := - -----1/2 2 y

> assume (y > 0); > contrib1 := subs (x = sol, old_f) * abs(dx_dy);

1/2 exp(- 1/2 y~) 2 contrib1 := 1/4 -----------------1/2 1/2 Pi y~


21:38 10/19/1998

The Change of Variable Formula

Note that Maple attaches a ~ to a variable (here y) when something has been assumed about it. Now repeat the computations, using the branch x = + y of the inverse transformation:
> sol := solutions[2]: > dx_dy := diff (sol, y): > contrib2 := subs (x = sol, old_f) * abs(dx_dy);

1/2 exp(- 1/2 y~) 2 contrib2 := 1/4 -----------------1/2 1/2 Pi y~

Here I used terminating colons instead of semicolons to suppress printing the results of the rst two commands; those invisible results nonetheless could have been accessed by the " and "" operators. Finally add the two contributions to get the density of the new variable Y :
> new_f := contrib1 + contrib2; 1/2 exp(- 1/2 y~) 2 new_f := 1/2 -----------------1/2 1/2 Pi y~

We have found the density of Y to be 1 fY (y ) = exp(y/2), (3) 2y for y > 0. This is the density of the so-called Chisquare distribution with one degree of freedom.

2.2

Automating the procedure: the one-dimensional case

Now I am going to automate the procedure illustrated by the preceding examples. Suppose Y = h(X ). Put g = h1 and rewrite equation (1) as fY (y ) = fX (g (y )) |g (y )|. (4) For the time being suppose the inverse transformation g is given, i.e., it is known how to write X in terms of Y . I am going to exhibit a function chng vars1 such that chng vars1 (f (x), y , x = g (y )) returns the density of Y when X = g (Y ) has density f (x). (The 1 in chng vars1 stands for one-dimensional.) Before we can compute g (y ) we will need to peel o g (y ) from the right hand side of the equation x = g (y ); that is easily done using the function rhs:
> rhs (x = g(y)); > %g(y)% := diff (", y); g(y) d %g(y)% := ---- g(y) dy

Maple treats a string of characters enclosed in back quotes () as the name of a variable; this notation must be used if the name includes any non alphanumeric characters (i.e., characters other than letters, digits, and underscore). Here I used this feature to create a self-identifying label for g (y ). Maple doesnt require the enclosing %s; theyre there to remind me that when I see %g(y)% in Maples output Im looking at a name, not an expression.
21:38 10/19/1998

2.2

Automating the procedure: the one-dimensional case

Here is the denition of chng vars1 :


> chng_vars1_ := proc (%f(x)%, y, %x=g(y)%) # chng_vars1_ (some expression f(x) in x, y, x = g(y)) returns # the density of y when x = g(y) has density f(x) local %g(y)%; %g(y)% := diff (rhs(%x=g(y)%), y); simplify (subs (%x=g(y)%, %f(x)%) * abs (%g(y)%)) end:

Like Splus, Maple treats any text after # as a comment. The procedure denition proc (parm 1 , . . . , parm i ) local var 1 , . . . , var j ; statement 1 ; . . . ; statement k end is analogous to dening a function in C or a subroutine in Fortran. The parm s are the formal parameter names. The var s are variables that are local to (i.e., known only in) the procedure. The statement s are Maple commands involving the parm s and the var s and perhaps some global variables as well, separated by semicolons. When the procedure is called, the actual arguments are evaluated and substituted for the parm s throughout the statement s. The statement s are then performed one at a time, with the last value computed being the value returned. Lets test out chng vars1 , rst in general:
> chng_vars1_ (f(x), y, x = g(y)); d f(g(y)) abs(---- g(y)) dy

and then on a specic case:


> f := alf * k^alf / x^(alf + 1): > assume (k > 0); > chng_vars1_ (f, y , x = k/y); (alf - 1) alf y

To see how Maple arrived at this result I asked it to redo the calculation while tracing the execution of chng vars1 . Tracing a procedure displays the entry to and exit from it as well as the results of each statement :
> trace (chng_vars1_): > chng_vars1_ (f, y, x = k/y); {--> enter chng_vars1_, args = alf*k~^alf/(x^(alf+1)), y, x = k~/y k~ %g(y)% := - ---2 y (alf - 1) alf y <-- exit chng_vars1_ (now at top level) = alf*y^(alf-1)} (alf - 1) alf y > %g(y)%; %g(y)% > untrace (chng_vars1_):

21:38 10/19/1998

The Change of Variable Formula

Note that the --> line shows the actual arguments passed to the procedure, and that the local variable %g(y)% doesnt have a value once the procedure is nished. untrace turns o tracing, so that subsequent calls wont generate any diagnostic output. The preceding discussion assumed that the transformation g from Y back to X was given. However, transformation-of-variables problems are usually stated in terms of the transformation h from X to Y . To deal with this situation Im going to exhibit a function chng vars1 such that chng vars1(f (x), x, y = h(x)) returns the density of Y = h(X ) when X has density f (x). The basic idea is to rst use solve to obtain g = h1 from h and to then call chng vars1 on g . If g has several branches, chng vars1 calls chng vars1 on each branch and accumulates the results.
> chng_vars1 := proc (%f(x)%, x, %y=h(x)%) # preliminary version # chng_vars1 (some expression f(x) in x, x, y = h(x)) returns # the density of y = h(x) when x has density f(x) local solutions, y, %f(y)%, %g(y)%, %x=g(y)%, contrib; solutions := solve (%y=h(x)%, x); y := lhs (%y=h(x)%); %f(y)% := 0; for %g(y)% in [solutions] do %x=g(y)% := x = %g(y)%; contrib := chng_vars1_ (%f(x)%, y, %x=g(y)%); %f(y)% := %f(y)% + contrib od; simplify (%f(y)%) end:

The construction for variable in expression do statements od performs a loop: the semicolon-separated statements are repeatedly executed, with variable running over the operands of the expression . (Maples for statement can take a number of other forms, e.g., for var from start by change to nish do . . . od.) To illustrate:
> solutions := [solve (y = x^2, x)]; 1/2 1/2 solutions := [- y , y ] > for solution in solutions do print (solution) od; 1/2 - y 1/2 y > for summand in a + b + c*d do print (summand) od; a b c d

The function print displays its arguments. Lets test out chng vars1, rst on the Pareto example:

21:38 10/19/1998

2.2

Automating the procedure: the one-dimensional case

>

chng_vars1 (alf * k^alf / x^(alf + 1), x, y = k/x); (alf - 1) alf y

and then on the Chisquare example; in this case I asked Maple to trace the execution of chng vars1:
> assume (y > 0): > trace (chng_vars1): > chng_vars1 (exp(-x^2/2) / sqrt (2*Pi), x, y = x^2); {--> enter chng_vars1, args = 1/2*exp(-1/2*x^2)*2^(1/2)/Pi^(1/2), x, y~ = x^2 1/2 1/2 solutions := - y~ , y~ y := y~ %f(y)% := 0 1/2 %x=g(y)% := x = - y~ 1/2 exp(- 1/2 y~) 2 contrib := 1/4 -----------------1/2 1/2 Pi y~ 1/2 exp(- 1/2 y~) 2 %f(y)% := 1/4 -----------------1/2 1/2 Pi y~ 1/2 %x=g(y)% := x = y~ 1/2 exp(- 1/2 y~) 2 contrib := 1/4 -----------------1/2 1/2 Pi y~ 1/2 exp(- 1/2 y~) 2 %f(y)% := 1/2 -----------------1/2 1/2 Pi y~ 1/2 exp(- 1/2 y~) 2 1/2 -----------------1/2 1/2 Pi y~ <-- exit chng_vars1 (now at top level) = 1/2*exp(-1/2*y~)*2^(1/2)/Pi^(1/2)/y~^( 1/2)} 1/2 exp(- 1/2 y~) 2 1/2 -----------------1/2 1/2 Pi y~ > untrace (chng_vars1);

ve. It can happen that g has The way chng vars1 handles multiple branches of g is somewhat na a single branch over part of its domain, two branches over another part, three branches over yet another part, and so on. Since chng vars1 (and perhaps solve itself) is not designed to handle such a complex situation, it should print out a warning message whenever multiple branches are

21:38 10/19/1998

10

The Change of Variable Formula

detected, to remind the user to think about whether the algorithm employed is appropriate for the case at hand. The following code segment shows how this may be accomplished:
> solutions := [solve (y = x^2, x)]; 1/2 1/2 solutions := [- y~ , y~ ] > if nops(solutions) > 1 then print (Warning: transformation is many to one); print (map (proc (expr, var) var = expr end, solutions, x)) fi; Warning: transformation is many to one 1/2 1/2 [x = - y~ , x = y~ ]

Maples nops function returns the number of operands, or components, of an expression:


> nops (solutions); > nops (a + b + c*d); 3 2

The if statement is used for conditional execution: if condition 1 then statements 1 elif condition 2 then statements 2 . . . else statements k returns the result of executing the semicolon-separated statements 1 if condition 1 is true, otherwise the semicolon-separated statements 2 if condition 2 is true, . . . , and otherwise the result of the semicolon-separated statements k . The elif and else clauses may be omitted if they are not needed. The function map is used to apply a procedure to each operand of an expression. The construction map ( fun, expr, arg2 , . . . , argn ) produces a new expression of the same type as the old expression expr, but in which each operand op of expr is replaced by fun (op , arg 2 , . . . , arg n ). For example
> map (f, [1, 2, 3], a, b); [f(1, a, b), f(2, a, b), f(3, a, b)] > map (proc (x) x^2 end, a + b + c*d); 2 2 2 2 a + b + c d

Note the use of the back quotes () as the string delimiters for the message Warning ...; this is a common idiom in Maple. There is another issue that needs to be confronted what chng vars1 should do when solve is unsuccessful in computing the inverse function g , as in the following example:
> solve (y = sin(x) + x, x); >

When a Maple builtin function realizes that it cant complete its assigned task, it typically simply returns the command that invoked it. (solve is an exception to this rule.) So thats what chng vars1 ought to do in cases like the above. The following code segment shows how that may be accomplished:
21:38 10/19/1998

2.2

Automating the procedure: the one-dimensional case

11

> checker := proc (var, equation) local solutions; solutions := solve (equation, var); if solutions = NULL then RETURN (procname (args)); else RETURN (solutions); fi end: > checker (x, y = k/x); k~ ---y~ > checker (x, y = sin(x) + x); checker(x, y~ = sin(x) + x)

The symbol NULL stands for the empty expression sequence, which is what solve returns when it is unsuccessful. RETURN(expr) causes an immediate return from a procedure, with expr as the return value. When a procedure is executed the special names procname and args get replaced respectively by the name by which the procedure was invoked and the sequence of arguments with which it was invoked. Enclosing an expression within single quotes () tells Maple that the expression stands for itself, rather than for its value. For example
> x := 5; x := 5 > x; > x; x 5

The single quotes around procname (args) in checker are needed to keep checker from calling itself recursively, in an innite loop. solve can exhibit some other anomalous behaviors, but I propose to ignore them for now. Here then are the nal versions of chng vars1 and chng vars1. I have these functions stored in a disk le, so that they can be easily modied with a standard text editor:
tcsh: cat chvars1 chng_vars1_ := proc (%f(x)%, y, %x=g(y)%) # chng_vars1_ (some expression f(x) in x, y, x = g(y)) returns # the density of y when x = g(y) has density f(x) local %g(y)%; %g(y)% := diff (rhs(%x=g(y)%), y); simplify (subs (%x=g(y)%, %f(x)%) * abs (%g(y)%)) end: chng_vars1 := proc (%f(x)%, x, %y=h(x)%) # chng_vars1 (some expression f(x) in x, x, y = h(x)) returns # the density of y = h(x) when x has density f(x) local solutions, y, %f(y)%, %g(y)%, %x=g(y)%, contrib; solutions := solve (%y=h(x)%, x); if solutions = NULL then RETURN (procname (args)) fi;

21:38 10/19/1998

12

The Change of Variable Formula

solutions := [solutions]; if nops (solutions) > 1 then print (Warning: transformation is many to one); print (map (proc(expr, var) var = expr end, solutions, x)) fi; y := lhs (%y=h(x)%); %f(y)% := 0; for %g(y)% in solutions do %x=g(y)% := x = %g(y)%; contrib := chng_vars1_ (%f(x)%, y, %x=g(y)%); %f(y)% := %f(y)% + contrib od; simplify (%f(y)%) end:

A le of Maple commands can be loaded using the read command; the le name must be backquoted if contains any non alphanumeric characters, such as period or slash.
> > > > restart; read (./chvars1); assume (y > 0); chng_vars1 (exp(-x^2/2) / sqrt (2*Pi), x, y = x^2); Warning: transformation is many to one 1/2 1/2 [x = - y~ , x = y~ ] 1/2 exp(- 1/2 y~) 2 1/2 -----------------1/2 1/2 Pi y~ > chng_vars1 (6*x*(1-x), x, y = sin(x) + x); chng_vars1(6 x (1 - x), x, y~ = sin(x) + x)

2.3

The multi-dimensional case

Suppose Y = (Y1 , . . . , Yk ) and X = (X1 , . . . , Xk ) are each k -dimensional random vectors, related by the equation Y = h(X ). Under appropriate regularity conditions on the transformation h, the analogue of formula (4) is fY (y ) = fX (g (y )) |Jg (y )| (5) where now y = (y1 , . . . , yk ) and Jg denotes the Jacobian of g = h1 , i.e., the determinant of the matrix x1 x1 1 . . . x y1 y2 yk

x2 y1 xk y1

. . .

x2 y2 xk y2

. . .

... .. . ...

x2 yk

xk yk

. . . .

The standard regularity conditions on h are that it be a one-to-one map of an open set X onto an open set Y , that the partial derivatives xi /yj be continuous in y for y Y , and that Jg (y ) be non-zero for each y Y .

21:38 10/19/1998

2.3

The multi-dimensional case

13

The new feature here is Jg . To see whats involved in calculating it in Maple, suppose that k = 2, X := {(x1 , x2 ) : x1 > 0, x2 > 0 } and h is the transformation y1 = x1 + x2 y2 = x1 /(x1 + x2 ). We rst need to solve these equations for the inverse transformation g :
> solutions := solve ({y1 = x1 + x2, y2 = x1/(x1 + x2)}, {x1, x2}); {x1 = y2 y1, x2 = y1 - y2 y1}

This says that g is the transformation x1 = y1 y2 x2 = y1 (1 y2 ) and it follows that Y = {(y1 , y2 ) : y1 > 0, 0 < y2 < 1 }. In the case of simultaneous equations the arguments to solve have to be expressed as a set of equations and a set of variables (this syntax can also be used for a single equation); moreover solve returns each of the various solutions it nds as a set of equations. In Maple sets are expressed by curly braces: {a, b, c} is the set whose elements are a, b, and c. Sets are dierent from lists in that the order of the elements is irrelevant and there are no duplicate elements.
xi The jacobian Jg = det ( y ) may now be computed as follows. Take the right hand j 1i2, 1j 2 sides y2 y1 and y1 y2 y1 of the equations for x1 and x2 and arrange them in a list:

> map (rhs, solutions); {y2 y1, y1 - y2 y1} > xs := convert (", list); xs := [y2 y1, y1 - y2 y1]

Maples convert procedure is used to convert from one data type to another here from a set to a list. Use the with command to load the jacobian and determinant routines from Maples linear algebra package:
> with (linalg, jacobian, det); [det, jacobian]

Compute the matrix of partial derivatives of x1 and x2 with respect to y1 and y2 :


> ys := [y1, y2]; ys := [y1, y2] > jacobian (xs, ys); [ y2 [ [ 1 - y2 ] ] - y1 ] y1

Finally, get Jg by computing the determinant y2 (y1 ) y1 (1 y2 ) = y1 of this matrix:


> det ("); - y1

The arguments to Maples jacobian procedure have to be lists (or vectors), not sets. Contrary to what its name would suggest, the procedure returns the matrix of partial derivatives rather than
21:38 10/19/1998

14

The Change of Variable Formula

its determinant. Note that in our example the partial derivatives xi /yj are in fact continuous functions of y = (y1 , y2 ) and Jg (y ) = 0 for all y Y . We are nearly ready to dene multi-dimensional versions of chng vars1 and chng vars1. It would be wise for these functions to run some sanity checks on their arguments. To illustrate how this may be done, here is a procedure which expects its one and only one argument to be either an equation or a set of equations.
> arg_checker := proc (a) if nargs <> 1 then ERROR (expecting 1 argument but got, nargs) fi; if not ( type (a, equation) or type (a, set(equation)) ) then ERROR (argument, a, not an equation or a set of equations) fi; OK end: > arg_checker (y = k/x); OK > arg_checker ({y1=x1+x2, y2=x1/(x1+x2)}); OK > arg_checker (x1, y1); Error, (in arg_checker) expecting 1 argument but got, 2 > arg_checker (x1 + y1); Error, (in arg_checker) argument, x1+y1, not an equation or a set of equations

Within a procedure the special name nargs has as its value the number of arguments with which the procedure was called. <> is Maples notation for not equal. The ERROR function causes an immediate return to top-level Maple. The boolean operators not and or have their usual meanings in logic. The command type (expr, type name) returns true if the expression expr is of type type name, and false otherwise. type name may be either a single type name, or a set of type names:
> type (x = g(y), equation); true > type (x = g(y), list); false > type ({y1=x1+x2, y2=x1/(x1+x2)}, {equation, list}); false > type ({y1=x1+x2, y2=x1/(x1+x2)}, {equation, set(equation)}); true

There is actually a simpler way to do type checking on the arguments to a procedure. If you dene a procedure with proc ( . . . , argi : type namei , . . . ) . . . end then each time the procedure is invoked Maple will abort the procedure call with an appropriate error message if arg i isnt of type type name i . Here then is a better way to write arg checker:

21:38 10/19/1998

2.3

The multi-dimensional case

15

> arg_checker := proc (a : {equation, set(equation)} ) if nargs <> 1 then ERROR (expecting 1 argument but got, nargs) fi; OK end: > arg_checker (y = k/x); OK > arg_checker (x1 + y1); Error, arg_checker expects its 1st argument, a, to be of type {equation, set(equation)}, but received x1+y1

Now we can dene multi-dimensional versions of chng vars1 and chng vars1: chng vars (old density, {y1 , . . . , yk }, {x1 = g1 (y1 , . . . , yk ), . . . , xk = gk (y1 , . . . , yk )}) chng vars (old density, {x1 , . . . , xk }, {y1 = h1 (x1 , . . . , xk ), . . . , yk = hk (x1 , . . . , xk )}) Both functions should return the density of y = (y1 , . . . , yk ) when x = (x1 , . . . , xk ) has density given by old density , which is to be expressed in terms of x. chng vars is to be used when x is written as g (y ), while chng vars is to be used when y is written as h(x). When k = 1 it is convenient to allow the simpler calling sequences chng vars (old density, y , x = g (y )) and chng vars (old density, x, y = h(x)).
enset := proc (s) # return s if s is a set, otherwise return the set consisting of s if type (s, set) then s else {s} fi end: chng_vars_ := proc(%f(x)%, y:{name, set(name)}, %x=g(y)%:{equation, set(equation)}) local %{y}%, %{x=g(y)}%, exprs, vars, J; # check arguments, and express them as sets if need be if nargs <> 3 then ERROR (expected 3 arguments but got, nargs); fi; %{y}% := enset (y); %{x=g(y)}% := enset (%x=g(y)%); if nops(%{y}%) <> nops(%{x=g(y)}%) then ERROR (arguments, y, and, %x=g(y)%, are of unequal length); fi; # compute J, the determinant of the matrix of partial derivatives exprs := convert (map (rhs, %{x=g(y)}%), list); vars := convert (%{y}%, list); J := simplify (linalg[det] ( linalg[jacobian] (exprs, vars) ) ); if J = 0 then ERROR (singular transformation) fi; # express the old density in terms of the new variables and multiply by # the absolute value of J simplify ( subs (%{x=g(y)}%, %f(x)%) * abs(J)) end:

Note that Maples relational operator for equality is =, not == as in C or Splus. chng vars refers to the det and jacobian procedures from the linalg package by their long names linalg[det] and linalg[jacobian] so that these procedures dont have to be preloaded by with.

21:38 10/19/1998

16

The Change of Variable Formula

chng_vars := proc (%f(x)%, x:{name, set(name)}, %y=h(x)%:{equation, set(equation)}) local %{x}%, %{y=h(x)}%, %{y}%, solutions, %{x=g(y)}%, %f(y)%, contrib; # check arguments if nargs <> 3 then ERROR (expected 3 arguments but got, nargs); fi; %{x}% := enset (x); %{y=h(x)}% := enset (%y=h(x)%); %{y}% := map (lhs, %{y=h(x)}%); if nops(%{x}%) <> nops(%{y=h(x)}%) then ERROR (arguments, x, and, %y=h(x)%, are of unequal length); fi; # solve the equations for the old variables in terms of the new ones solutions := solve (%{y=h(x)}%, %{x}%); if solutions = NULL then RETURN (procname (args)) fi; if nops([solutions]) > 1 then print (Warning: transformation is many to one); print (solutions); fi; # accumulate the contributions to the new density from the various # branches of the solution (each branch is a set of equations) %f(y)%:= 0; for %{x=g(y)}% in [solutions] do contrib := chng_vars_ (%f(x)%, %{y}%, %{x=g(y)}%); %f(y)%:= %f(y)% + contrib od; simplify (%f(y)%); end:

The functions chng vars1 and chng vars1 of the preceding section are no longer needed, since chng vars and chng vars do everything they did. For example:
> chng_vars (exp (-x^2/2)/sqrt(2*Pi), x, y=x^2); Warning: transformation is many to one 1/2 1/2 {x = - y~ }, {x = y~ } 1/2 exp(- 1/2 y~) 2 1/2 -----------------1/2 1/2 Pi y~

The same disk le that holds the source for these procedures also has the corresponding help information, specied using the construction help/text/function := TEXT( text str1 , text str2 , . . . , text strn ): When the user types ? function, Maple prints each of the text strings text str s in the TEXT data structure help/text/function on a separate line, stripped of the leading and trailing back quotes. For example, here is part of the help text for chng vars:

21:38 10/19/1998

2.3

The multi-dimensional case

17

help/text/chng_vars := TEXT ( FUNCTION: chng_vars - find the density resulting from a change of variables, , CALLING SEQUENCE:, chng_vars (%f(x)%, x, %y=h(x)%), , PARAMETERS:, %f(x)% - an expression, x - a name or set of names, %y=h(x)% - an equation or set of equations, ... SEE ALSO: ): chng_vars_

Notice that to get a back quote into a text string you have enter it as two consecutive back quotes. The help page for a Maple function typically has the following categories: HELP FOR (or FUNCTION) the procedure under discussion; CALLING SEQUENCE how to invoke the procedure; PARAMETERS what each parameter must or can be; SYNOPSIS a brief description what the function does; EXAMPLES examples of the use of the procedure; SEE ALSO related items for which help is available. Here is how the help page for chng vars looks when Maple prints it out:
> ? chng_vars FUNCTION: chng_vars - find the density resulting from a change of variables CALLING SEQUENCE: chng_vars (%f(x)%, x, %y=h(x)%) PARAMETERS: %f(x)% x %y=h(x)% - an expression - a name or set of names - an equation or set of equations

SYNOPSIS: - chng_vars computes the density of a random variable/vector y which is the transformation of some random variable/vector x having a known density. The parameter %y=h(x)% specifies the transformation - the way y depends on x as an equation of the form y=h(x), or as a set of such equations, one for each of the components y1,...yk of y, in terms of the components x1,...,xk of x. The density %f(x)% of x must be expressed in terms of the components of x. EXAMPLE: > assume (k > 0): > chng_vars (a * k^a / x^(a+1), x, y = k/x); a y (a - 1)

21:38 10/19/1998

18

The Change of Variable Formula

> > > >

assume (y1 > 0): dgamma := (x, r, la) -> la^r * x^(r-1) * exp(-la*x) / GAMMA(r): chng_vars (dgamma(x1,r1,la)*dgamma(x2,r2,la), {x1,x2}, {y1=x1+x2, y2=x1/(x1+x2)}): simplify (factor (")); (r1 + r2) (r1 - 1) (r1 + r2 - 1) (r2 - 1) la y2 y1~ (1 - y2) exp(- la y1~) ---------------------------------------------------------------------GAMMA(r1) GAMMA(r2) SEE ALSO: chng_vars_

The last example is discussed in the next subsection.

2.4

Example: the Gamma-Beta-Chisquare-F-T connection

I am going to use the tools we have developed to study some important distributions in statistics. The gamma distribution G(r, ) with shape parameter r > 0 and scale parameter > 0 has density gr, (x) = r xr1 ex /(r) (6)

for x > 0. Suppose X1 and X2 are independent random variables with X1 G(r1 , ) and X2 G(r2 , ). Put Y1 = X1 + X2 , Y2 = X1 /(X1 + X2 ). Lets use chng vars to nd the joint density of Y1 and Y2 . To begin, do:
> restart; > read (chvars); > dgamma := (x, r, la) -> la^r * x^(r-1) * exp(-la*x) / GAMMA(r); r (r - 1) la x exp(- la x) dgamma := (x,r,la) -> -----------------------GAMMA(r)

GAMMA is Maples notation for the Gamma function. The construction vars -> result is short for proc(vars) result end. Here vars is a single variable name or a sequence of variable names enclosed in parentheses, and result is a single statement specifying the result of the procedure acting on vars. dgamma is the name of my user-dened function for the gamma density. Continue with
> assume (y1 > 0); > chng_vars (dgamma(x1,r1,la)*dgamma(x2,r2,la), {x1,x2}, {y1=x1+x2,y2=x1/(x1+x2)}); (r1 + r2) (r1 - 1) r1 (r2 - 1) la y2 y1~ (y1~ - y2 y1~) exp(- la y1~) ----------------------------------------------------------------GAMMA(r1) GAMMA(r2) > simplify(factor(")); (r1 + r2) (r1 - 1) (r1 + r2 - 1) (r2 - 1) la y2 y1~ (1 - y2) exp(- la y1~) ---------------------------------------------------------------------GAMMA(r1) GAMMA(r2)
21:38 10/19/1998

2.4

Example: the Gamma-Beta-Chisquare-F-T connection

19

This is the joint density of Y1 and Y2 , the formula being valid for all y1 > 0 and 0 < y2 < 1. Dividing by gr1 +r2 , (y1 ) produces
> "/dgamma(y1, r1+r2, la); (r1 - 1) (r2 - 1) y2 (1 - y2) GAMMA(r1 + r2) -----------------------------------------GAMMA(r1) GAMMA(r2)

It follows that Y1 and Y2 are independent, Y1 G(r1 + r2 , ), and Y2 has the Beta distribution B (r1 , r2 ), with density br1 ,r2 (y ) = y r1 1 (1 y )r2 1 /B(r1 , r2 ) for 0 < y < 1; here B(r1 , r2 ) = (r1 )(r2 )/(r1 + r2 ) is the usual Beta function. The Chisquare distribution 2 (n) with n degrees of freedom is the distribution of
2 2 2 SS = Z1 + Z2 + + Zn ,

(7a) (7b) (7c) (8)

the Zi s being independent standard normal random variables. It follows formula (3) that Zi2 G(1/2, 1/2) and from (7b) that SS G(n/2, 1/2), with density 2 n (x) = gn/2,1/2 (x) = 1 xn/21 ex/2 . (n/2)2n/2 (9)

We could use -> to dene a Maple function to compute this, but it is easier to say
> dchisq := unapply (simplify (dgamma(x, n/2, 1/2)), x, n); (- 1/2 n) (1/2 n - 1) 2 x exp(- 1/2 x) dchisq := (x,n) -> -----------------------------------GAMMA(1/2 n)

In general the result of unapply (expr, vars) is a function having the variables vars as its parameters and the expression expr as its return value. The unnormalized F -distribution UF (n1 , n2 ) with n1 and n2 degrees of freedom is the distribution of F = SS 1 /SS 2 where SS 1 and SS 2 are independent Chisquare variables with n1 and n2 degrees of freedom respectively. Since F SS 1 B := = , 1+F SS 1 + SS 2 (7c) implies that B B (n1 /2, n2 /2). We can get the density of F from the density of B with chng vars :
21:38 10/19/1998

20

The Change of Variable Formula

> dbeta := (y, r1, r2) -> y^(r1-1) * (1-y)^(r2-1) / Beta(r1, r2); (r1 - 1) (r2 - 1) y (1 - y) dbeta := (y,r1,r2) -> ------------------------Beta(r1, r2) > duf := unapply (chng_vars_ (dbeta (b, n1/2, n2/2), f, b = f/(1+f)), f, n1, n2); (1/2 n1 - 1) (- 1/2 n1 - 1/2 n2) f (1 + f) duf := (f,n1,n2) -> ---------------------------------------Beta(1/2 n1, 1/2 n2)

Beta is Maples notation for the Beta function. My function dbeta computes the beta density (8), and duf computes the density n1 ,n2 (f ) = of F ; this formula is valid for f > 0. The unnormalized t distribution UT (n) with n degrees of freedom is the distribution of T = Z/ SS where Z and SS are independent, with Z N (0, 1) and SS 2 (n). Evidently F := T 2 = Z 2/SS UF (1, n). chng vars gives the density f|T | (t) of |T | = over (0, ):
> assume (abs_t > 0): > chng_vars (duf(f, 1, n), f, abs_t = sqrt(f)); 2 (- 1/2 - 1/2 n) (1 + abs_t~ ) 2 ---------------------------Beta(1/2, 1/2 n)

f n1 /21 1 B(n1 /2, n2 /2) (1 + f )(n1 +n2 )/2

(10)

By symmetry, the density of T itself is fT (t) = f|T | (|t|)/2, for < t < :
> subs (abs_t^2 = t^2, "/2); 2 (- 1/2 - 1/2 n) (1 + t ) ----------------------Beta(1/2, 1/2 n)

This says that T has density n (t) = 1 1 , 2 B(1/2, n/2) (1 + t )(n+1)/2 (11)

for < t < . Make this into a Maple function with


> dut := unapply (", t, n):

21:38 10/19/1998

2.4

Example: the Gamma-Beta-Chisquare-F-T connection

21

For n = 1 we get the density 1 (t) = of the standard Cauchy distribution:


> dcauchy := unapply (dut(x, 1), x); 1 dcauchy := x -> ----------2 (1 + x ) Pi

1 1 1 1 = B(1/2, 1/2) (1 + t)2 (1 + t)2

(12)

To nish o this Maple session, do


> save dgamma, dchisq, dbeta, duf, dut, dcauchy, densities; > save dgamma, dchisq, dbeta, duf, dut, dcauchy, densities.m;

to save the denitions of dgamma, . . . , dcauchy into the text le densities and the Maple internal format le densities.m. By convention, les in internal format have names ending in .m; they can be loaded into Maple at high speed by read and with, but they are not human-readable.

21:38 10/19/1998

Index
" (last expression), 3 "" (second last expression), 3 """ (third last expression), 3, 4 (prevent evaluation), 11 * (multiplication), 4 -> (function denition), 18 .m (le name sux), 21 : (type checking), 14 : (command terminator), 2, 6 ; (command terminator), 2, 3 <> (not equal), 14 = (equality), 15 ? (help command), 2 [. . . ] as list notation, 5 as subscript operator, 5 (exponentiation operator), 4 (back quote), 6, 10, 17 {. . . } (set notation), 13 # (comment), 7 abs, 4 alphanumeric characters, 6 args, 11 arguments, 8 assignment operator, see := assume, 4 back quote, see back referencing, see ", "", """, history Beta, 20 Beta function, 19 by, 8 change of variables formula multi-dimensional, 1221 one-dimensional, 312 chng vars, 15 chng vars , 15 chng vars1, 8, 11, 16 chng vars1 , 6, 11, 16 comment, see # conditional execution, see if 22 convert, 13 dbeta, see densities, dbeta dcauchy, see densities, dcauchy densities dbeta, 20 dcauchy, 21 dgamma, 18 duf (unnormalized F ), 20 dut (unnormalized t), 20 det, 13 dgamma, see densities, dgamma di, 3 dierentiation, see di distributions Beta, 4, 19 Cauchy, 21 Chisquare with n degrees of freedom, 19 with one degree of freedom, 6 gamma, 18 logistic, 3 Pareto, 3 unnormalized F , 19 unnormalized t, 20 do, 8 done, 2 duf, see densities, duf dut, see densities, dut elif, 10 else, 10 end, 7 equation, 4 ERROR, 14 exp, 3 exponentiation, see expression sequence, 5 empty, see NULL factor, 18 false, 14 (match to if), 10

INDEX

23

les Maple internal format, 21 loading, see read, with user format, 21 for, 8 from, 8 function checking the type of arguments, 14 debugging of, 7 dening a, see ->, proc, unapply failure to complete task, 10 saving a, 21 GAMMA (gamma function), 18 help, see ? help page writing a, 16, 17 history, 4 if, 10 in, 8 Jacobian, 12 jacobian, 13
A L TEX, 1 linalg, 13 list extracting an element, 5 mapping a function over a, 10 notation, 5 local, 7 log, 3 logical relations, see =, <> long names, 15 loops, 8

see<>, 14 NULL (empty expression sequence), 11 od (match to do), 8 operand, 8 or, 14 package loading, see with parameter, 7 Pi (the number ), 5 preceding expressions, see ", "", """ print, 8 proc (procedure denition), 7 with argument checking, 14 procname, 11 prompt, 1 quit, 2 quotes, see , , read, 12 reference manuals, 1 restart, 2 RETURN, 11 return values, 7 save, 21 set, 13 simplify, 3 sin, 10 single quote, see solve, 3 anomalous behaviors, 11 case of simultaneous equations, 13 multiple solutions, 5, 10 no solutions, 10, 11 sqrt, 5 starting Maple, 1 afresh, 2 statement, 7 stop, 2 string, 6 string delimiter, see subs, 4 with sets of equations, 15 subscript operator, 5
21:38 10/19/1998

manuals, 1 map, 10 Maple overview, 1 nargs (number of arguments), 14 nops (number of operands), 10 not, 14 not equal

24

INDEX

terminating Maple, 2 TEXT, 16 then, 10 to, 8 trace, 7 true, 14 tutorial, 2 type, 14 unapply (function denition), 19 untrace, 8 with, 13 avoiding use of, 15

21:38 10/19/1998

You might also like