Download presentation
Presentation is loading. Please wait.
1
Five Basic Programming Elements
input: getting data and commands into the computer output: getting your results out of the computer arithmetic: performing mathematical calculations on your data conditional: testing to see if a condition is true or false looping: cycling through a set of instructions until some condition is met
2
MATRIX Language Matrix language is a higher level computer language, comparing to C, C+, Fortran, etc. Matrix language is more natural than the low level language More difficult language, but powerful, is low-level language, like machine-code For a statistician, the higher-level language is better
3
Example 1: Solving a System of Equations
consider the problem of solving three simultaneous equations: These equations can be written in matrix form as Or Ax = c, then x=A-1c A Fortran program:
4
MATRIX Languages SAS IML – Interactive Matrix Language
MATHEMATIKA – Symbolic calculation, base unit is expression, the operation could be applied into matrix MATLAB – base unit: matrix, all operations, functions are based on matrix R – another similar language Python Which one I should learn?
5
SAS IML SAS/IML software gives you access to a powerful and flexible programming language (Interactive Matrix Language) in a dynamic, interactive environment. 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 store statements in a module and execute them later. The programming is dynamic because necessary activities such as memory allocation and dimensioning of matrices are performed automatically. SAS/IML software is powerful. You can access built-in operators and call routines to perform complex tasks such as matrix inversion or eigenvector generation. You can define your own functions and subroutines using SAS/IML modules. You can operate on a single value or take advantage of matrix operators to perform operations on an entire data matrix.
6
SAS IML IML is a programming language
complete set of control statements, such as DO/END, START/FINISH, iterative DO, IF-THEN/ELSE, GOTO IML software operates on matrices. IML uses operators that apply to entire matrices SAS/IML software possesses a powerful vocabulary of operators. SAS/IML software is interactive. SAS/IML software is dynamic.
7
proc IML; reset print; x={1 2 3}; y={2,3,4}; z=x*y; xx={a b c};
X row cols (numeric) Y rows col (numeric) 2 3 4 Z row col (numeric) 20 XX row cols (character, size 1) A B C X rows cols (numeric) X row col (numeric) -5 X rows cols (numeric) X rows cols (numeric) proc IML; reset print; x={1 2 3}; y={2,3,4}; z=x*y; xx={a b c}; x1={1 2, 4 3}; x2=det(x1); x3=inv(x1); x5=x1+4;
8
Creating Matrices from Matrix Literals
You simply input the element values one at a time, usually inside of braces. A=12; B=‘abc’; C={1 2 3}; X={1,-2,3}; E={1 2, 3 4}; y=x#(x>0); A matrix can also be created as a result of a function, a CALL statement, or an assignment statement. a=sqrt(b); y=inv(x); r=rank(x); CALL EIGEN( eigenvalues, eigenvectors, A)
9
IML Operators Addition, Subtraction Operator: + -
Multiplication Operator, Elementwise: # Multiplication Operator, Matrix: * Division Operator: / Power Operator, Elementwise: ## Power Operator, Matrix: ** Concatenation Operator, Horizontal: || Concatenation Operator, Vertical: // Direct Product Operator: (i.e. Kronecker product)
10
IML Operator Comparison Operators: < > = <= >= ^=
Element Maximum Operator: <> Element Minimum Operator: >< Index Creation Operator: : Logical Operators: & | ^ Subscripts: [ ] Transpose Operator: `
11
IML Operators Addition, Subtraction Operator: + -
matrix1 + matrix matrix1 - matrix2 matrix + scalar matrix - scalar Multiplication Operator, Elementwise: # matrix1#matrix2 matrix#scalar matrix#vector Multiplication Operator, Matrix: * matrix1*matrix2 Division Operator: / matrix1/matrix2 matrix/scalar scalar/matrix
12
IML Operators Power Operator, Elementwise: ##
raises each element to a power matrix1##matrix2 matrix##scalar Power Operator, Matrix: ** raises a matrix to a power matrix**scalar A**(-1) is permitted and is equivalent to INV(A). Concatenation Operator, Horizontal: || A||B concatenates matrices horizontally Concatenation Operator, Vertical: // A//B concatenates matrices vertically Direct Product Operator: takes the direct product of two matrices (Kronecker product )
13
IML Operator Comparison Operators: < > = <= >= ^=
compare matrix elements matrix1<matrix2 matrix1<=matrix2 matrix1>matrix2 matrix1>=matrix2 matrix1=matrix2 matrix1^=matrix2 When you are making conditional comparisons, all values of the result must be nonzero for the condition to be evaluated as true. Consider the following statement: if x>=y then goto loop1; The GOTO statement is executed only if every element of x is greater than or equal to the corresponding element in y. See also the descriptions of the ALL and ANY functions.
14
IML Operator Element Maximum Operator: <>
selects the larger of two elements matrix1<>matrix2 Element Minimum Operator: >< selects the smaller of two elements Logical Operators: & | ^ The AND logical operator (&) compares two matrices, element by element, to produce a new matrix. An element of the new matrix is 1 if the corresponding elements of matrix1 and matrix2 are both nonzero; otherwise, it is a zero. An element of the new matrix produced by the OR operator (|) is 1 if either of the corresponding elements of matrix1 and matrix2 is nonzero. If both are zero, the element is zero. The NOT prefix operator (^) examines each element of a matrix and produces a new matrix containing elements that are ones and zeros. If an element of matrix equals 0, the corresponding element in the new matrix is 1. If an element of matrix is nonzero, the corresponding element in the new matrix is 0.
15
Elementwise Binary Operators
Action + addition, concatenation - subtraction # elementwise multiplication ## elementwise power / division <> element maximum >< element minimum | logical OR & logical AND < less than <= less than or equal to > greater than >= greater than or equal to ^= not equal to = equal to MOD(m,n) modulo (remainder)
16
IML Operator Index Creation Operator: : creates an index vector
value1:value2 X=5:8; z=6:3; varlist='var1':'var5'; Use the DO function if you want an increment other than 1 or -1. (or eg. (5:8)*2)
17
IML Operator Subscripts: [ ] proc IML; reset print; x={1 2 3, 4 5 6,
select submatrices matrix[rows,columns] matrix[elements] proc IML; reset print; x={1 2 3, 4 5 6, 7 8 9}; z=x[1,]; z1=x[{1 3},]; z3=x[{1 3},{2 1}]; z4=x[,{2 3}];
18
Subscripts [] You can use subscripts to Subscripted Assignment
refer to a single element of a matrix refer to an entire row or column of a matrix refer to any submatrix contained within a matrix perform a reduction across rows or columns of a matrix Subscripted Assignment t={ , , }; i=loc(t<0); t[i]=0; /*a[a<0]=0; is wrong in SAS, right in Matlab in SAS a#(a>0)*/
19
Subscript Reduction Operators
You can use reduction operators, which return a matrix of reduced dimension, in place of values for subscripts to get reductions across all rows and columns. Operator Action + addition # multiplication <> maximum >< minimum <:> index of maximum >:< index of minimum : mean ## sum of squares
20
Subscript Reduction Operators
column sums of the matrix X X[+,] row sums of the matrix X X[,+] the expression A[+,<>] results in the maximum (<>) of the column sums (+). You can repeat reduction operators. To get the sum of the row maxima, use the expression A[,<>] [+,]. A subscript such as A[{2 3},+] first selects the second and third rows of A and then finds the row sums of that matrix.
21
Subscript Reduction Operators
22
Control Statements
23
Functions
24
Functions
27
Optimazation
28
Example 1: Solving a System of Equations
consider the problem of solving three simultaneous equations: These equations can be written in matrix form as Or Ax = c, then x=A-1c A Fortran program:
29
Example 1: Solving a System of Equations
Or Ax = c, then x=A-1c A rows cols (numeric) C rows col (numeric) 8 2 9 X rows col (numeric) 3 5 proc IML; reset print; A={ , , }; c={8, 2, 9}; x=inv(A)*c;
30
solving the normal equations
Example 2: Regression In matrix algebra notation, a linear model is written as where X is the n ×k design matrix (rows are observations and columns are the regressors), is the k ×1 vector of unknown parameters, and is the n ×1 vector of unknown errors. The first column of X is usually a vector of 1s used in estimating the intercept term. solving the normal equations (X' W X)b = X' W y b = (X'WX)-1X'Wy The covariance matrix of the estimates is The estimate of the covariance matrix COVB = (X' WX)-1 s2
31
Regression Standard errors of the estimates are computed using the equation where (X' WX)-1ii is the ith diagonal element of (X' WX)-1. The ratio t = [(bi)/( STDERR(bi))] is distributed as Student's t under the hypothesis that is zero.
32
Example 2: Regression /* Regression Routine */
/* Given X, and Y, this fits Y = X B + E */ /* by least squares */ Proc IML; start reg; n=nrow(x); /* number of observations */ k=ncol(x); /* number of variables */ xpx=x`*x; /* cross-products */ xpy=x`*y; xpxi=inv(xpx); /* inverse cross products */ b=xpxi*xpy; /* parameter estimates */ yhat=x*b; /* predicted values */ resid=y-yhat; /* residuals */ sse=resid`*resid; /* sum of squared errors */ dfe=n-k; /* degrees of freedom error */ mse=sse/dfe; /* mean squared error */
33
Example: Regression rmse=sqrt(mse); /* root mean squared error */
covb=xpxi#mse; /* covariance of estimates */ stdb=sqrt(vecdiag(covb)); /* standard errors */ t=b/stdb; /* ttest for estimates=0 */ probt=1-probf(t#t,1,dfe);/* significance probability */ print name b stdb t probt; s=diag(1/stdb); corrb=s*covb*s; /* correlation of estimates */ print ,"Covariance of Estimates", covb[r=name c=name] , "Correlation of Estimates",corrb[r=name c=name] ; if nrow(tval)=0 then return;/* is a t-value specified? */ projx=x*xpxi*x`; /* hat matrix */ vresid=(i(n)-projx)*mse; /* covariance of residuals */ vpred=projx#mse; /* covariance of predicted values */ h=vecdiag(projx); /* hat leverage values */
34
Example: Regression lowerm=yhat-tval#sqrt(h*mse);/*low.conf.lim. for mean */ upperm=yhat+tval#sqrt(h*mse);/* upper limit for mean */ lower=yhat-tval#sqrt(h*mse+mse);/*lower limit for indiv*/ upper=yhat+tval#sqrt(h*mse+mse);/*upper limit for indiv*/ print ,,"Predicted Values, Residuals, and Limits" ,, y yhat resid h lowerm upperm lower upper; finish reg;
35
Regression /* Run it on population of U.S. for decades beginning 1790 */ x= { 1 1 1, 1 2 4, 1 3 9, 1 4 16, 1 5 25, 1 6 36, 1 7 49, }; y= {3.929,5.308,7.239,9.638,12.866,17.069,23.191,31.443}; name={"Intercept", "Decade", "Decade**2" }; tval=2.57;/*for 5 df at level to get 95% conf. int. */ reset fw=7; /* set field width, default=9 */ run reg;
36
Example 3: solve a equation
/*solve x^3+x^2+x+1=0*/ /*solve x**3+x**2+x+1=0 in SAS*/ How to solve it? Theorem
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.