Download Using PROC IML to solve a set of simultaneous equations.

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Cartesian tensor wikipedia , lookup

Quadratic form wikipedia , lookup

Equation wikipedia , lookup

History of algebra wikipedia , lookup

Linear algebra wikipedia , lookup

Eigenvalues and eigenvectors wikipedia , lookup

Jordan normal form wikipedia , lookup

Matrix (mathematics) wikipedia , lookup

Determinant wikipedia , lookup

Perron–Frobenius theorem wikipedia , lookup

System of linear equations wikipedia , lookup

Singular-value decomposition wikipedia , lookup

Non-negative matrix factorization wikipedia , lookup

Matrix calculus wikipedia , lookup

Cayley–Hamilton theorem wikipedia , lookup

Matrix multiplication wikipedia , lookup

Transcript
SUAVe, Tuesday, 25 November 2014.
Using SAS PROC IML to solve a set of
simultaneous equations.
Vincent J. Connor
Senior Programmer/Analyst
University Systems
University of Victoria
Thanks to Mike Atkinson and Peter Ott for for the initial suggestion of a
presentation and to Peter Ott for a suitable problem.
SAS Support documentation:
See the SAS/IML® 13.2 User’s Guide
http://support.sas.com/documentation/cdl/en
/imlug/67502/HTML/default/viewer.htm#imlu
g_imlstart_sect012.htm
e.g., the Tutorial ‘A Module for Linear
Regression’
Example: Solving a system of Linear
equations’.
Overview of SAS/IML Software
SAS/IML software gives you access to a
powerful and flexible programming language
in a dynamic, interactive environment. The
acronym IML stands for "interactive matrix
language."
The fundamental object of the language is a
data matrix. You can use SAS/IML software
interactively (at the statement level) to see
results immediately, or you can submit blocks
of statements or an entire program. You can
also encapsulate a series of statements by
defining a module; you can call the module
later to execute all of the statements in the
module.
Statement of the problem.
• Guy drinks 3 different brands of beer at the SAS Global Forum
social event:
•
• Beer A is $4 a glass
• Beer B is $5 a glass
• Beer C is $7 a glass (the good stuff)
•
• His memory is fuzzy the next day, but he knows this for sure:
• he spent $49 in total
• he drank twice as much beer B as beer A
• he drank 4 glasses of beers A + C combined.
•
• How many of each brand of beer did Guy drink?
Sober-up by translating this into a set
of simultaneous equations.
• Let a, b and c be the unknowns, i.e., the respective number
of glasses of each type of beer, A, B & C that Guy drank at a
respective cost of $4, $5 and $7 per glass.
• 1. $49 was spent in total, so,
• 4a + 5b + 7c = 49
•
•
•
•
2. He drank twice as much beer B as beer A.
2a = b
3. He drank 4 glasses of beers A + C combined.
a+c=4
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
Reach back to our high-school algebra
to arrange the equations in standard
format.
4a + 5b + 7c = 49
2a – 1b + 0c = 0
1a + 0b + 1c = 4
Now solve in the usual way:
b = 2a from equation 2.
Substitute back into equation 1:
14a + 7c= 49
c = 4 – a from equation 3.
Substitute into the new equation 1:
7a = 49 – 28 = 21; a = 3.
b = 2a = 6.
c = 4 – a = 1.
(a)
(3)
(b)
= (6)
(c )
(1)
So, Guy drank 3 glasses of beer A, 6 of beer B and 1 of beer C. And he was done.
And we are done.
But what might Dr. Goodnight say about all of this?
‘Nice work! ‘ (I hope!) ‘But what if it were a much bigger problem with 50, say, simultaneous equations? ‘
‘Let me introduce you to one of my SAS PROC IML experts at UVic way up north in Canada where they have lots of
really cool beers!’
First, formulate these 3 simultaneous
equations:
•
•
•
•
•
•
•
4a + 5b + 7c = 49
2a – 1b + 0c = 0
1a + 0b + 1c = 4
as matrices (sort of):
(4 5 7) (a)
(49)
(2 -1 0) * (b) = (0)
(1 0 1) (c)
(4)
Now let us express them more
generally in terms of matrix algebra:
• M * X = Y where X is the 3 * 1 (a,b,c) matrix, Y is the 3 * 1
(49, 0, 4) matrix of results and M is the 3 * 3 matrix of
coefficients.
• We are now trying to solve this matrix equation for X and
very soon we will enlist SAS PROC IML to help us…
• First, let M-1 be the inverse matrix of M.
• Then M-1 * M = I , the Identity matrix, by definition and in
practice by matrix multiplication.
• E.G., for a matrix of rank 3 this is:
• 100
• 010
• 001
Multiplying both sides by M-1 gives:
•
•
•
•
•
•
•
•
•
•
•
•
•
M-1 * M * X = M-1 * Y
i.e., I * X = M-1 * Y
i.e., X = M-1 * Y
So, if we could invert the matrix M and then multiply Y by it we would have a
matrix that is identical to the matrix X of the unknowns, a, b & c.
The definition of the inverse of a matrix M is:
Adjoint(M)
i.e., the transpose of the matrix formed by the signed co-factors
-------------------- divided by the determinant of M.
Determinant(M)
If you have ever tried to do this manually you will now how labour-intensive it is.
Sufficient to say, the result for this particular matrix M is:
M-1 = ( 1/7 5/7 -1)
( 2/7 3/7 -2)
(-1/7 -5/7 2)
Note from the definition of the M inverse:
Adjoint(M)
--------------Determinant(M)
that this will be valid only if the determinant of
M is not zero, i.e., when the matrix , M, is said
to be ‘non singular’.
Now it is time to move to a SAS script and use
PROC IML (interactive matrix language) to
express these matrices in SAS terms and to
invert M the easy way using the SAS linear
algebra function INV.
From the SAS/IML® 13.2 User’s Guide:
the SAS INV function uses an LU (lower upper)
decomposition followed by back substitution
to solve for the inverse, as described in
Forsythe, Malcom, and Moler (1967).
From Wikipedia:
--------------------In numerical analysis, LU decomposition
(where 'LU' stands for 'Lower Upper', and also
called LU factorization) factors a matrix as the
product of a lower triangular matrix and an
upper triangular matrix. The product
sometimes includes a permutation matrix as
well. The LU decomposition can be viewed as
the matrix form of Gaussian elimination.
Computers usually solve square systems of
linear equations using the LU decomposition,
and it is also a key step when inverting a
matrix, or computing the determinant of a
matrix. The LU decomposition was introduced
by mathematician Alan Turing in 1948.[1][2]