lecture 3 and 4: algorithms for linear algebra

Similar documents
lecture 2 and 3: algorithms for linear algebra

Linear Algebra Section 2.6 : LU Decomposition Section 2.7 : Permutations and transposes Wednesday, February 13th Math 301 Week #4

Solution of Linear Equations

This can be accomplished by left matrix multiplication as follows: I

Numerical Methods I Solving Square Linear Systems: GEM and LU factorization

Matrix decompositions

Today s class. Linear Algebraic Equations LU Decomposition. Numerical Methods, Fall 2011 Lecture 8. Prof. Jinbo Bi CSE, UConn

Linear Algebraic Equations

Scientific Computing: Dense Linear Systems

Numerical Methods - Numerical Linear Algebra

Numerical Linear Algebra

Matrix decompositions

Introduction to Mathematical Programming

Cheat Sheet for MATH461

Linear Analysis Lecture 16

LINEAR ALGEBRA: NUMERICAL METHODS. Version: August 12,

Linear Equations and Matrix

3 QR factorization revisited

LU Factorization. LU factorization is the most common way of solving linear systems! Ax = b LUx = b

1 GSW Sets of Systems

Scientific Computing

COURSE Numerical methods for solving linear systems. Practical solving of many problems eventually leads to solving linear systems.

Matrix decompositions

Linear Algebra in Actuarial Science: Slides to the lecture

Conceptual Questions for Review

Linear Algebra Linear Algebra : Matrix decompositions Monday, February 11th Math 365 Week #4

Numerical Linear Algebra

Numerical Linear Algebra

Program Lecture 2. Numerical Linear Algebra. Gaussian elimination (2) Gaussian elimination. Decompositions, numerical aspects

Y = ax + b. Numerical Applications Least-squares. Start with Self-test 10-1/459. Linear equation. Error function: E = D 2 = (Y - (ax+b)) 2

AMS526: Numerical Analysis I (Numerical Linear Algebra)

forms Christopher Engström November 14, 2014 MAA704: Matrix factorization and canonical forms Matrix properties Matrix factorization Canonical forms

Lecture 9. Errors in solving Linear Systems. J. Chaudhry (Zeb) Department of Mathematics and Statistics University of New Mexico

1.5 Gaussian Elimination With Partial Pivoting.

Linear Algebra, part 3 QR and SVD

Linear Algebra Review

Scientific Computing: Solving Linear Systems

1.Chapter Objectives

Computational Methods. Systems of Linear Equations

Important Matrix Factorizations

AMS 147 Computational Methods and Applications Lecture 17 Copyright by Hongyun Wang, UCSC

Direct Methods for Solving Linear Systems. Simon Fraser University Surrey Campus MACM 316 Spring 2005 Instructor: Ha Le

MODEL ANSWERS TO THE THIRD HOMEWORK

MAA507, Power method, QR-method and sparse matrix representation.

Example Linear Algebra Competency Test

A Review of Linear Algebra

Linear Algebra. Solving Linear Systems. Copyright 2005, W.R. Winfrey

Orthogonal Transformations

Applied Linear Algebra in Geoscience Using MATLAB

Applied Linear Algebra in Geoscience Using MATLAB

Matrix Algebra for Engineers Jeffrey R. Chasnov

(Mathematical Operations with Arrays) Applied Linear Algebra in Geoscience Using MATLAB

MIDTERM. b) [2 points] Compute the LU Decomposition A = LU or explain why one does not exist.

Matrix notation. A nm : n m : size of the matrix. m : no of columns, n: no of rows. Row matrix n=1 [b 1, b 2, b 3,. b m ] Column matrix m=1

LU Factorization. Marco Chiarandini. DM559 Linear and Integer Programming. Department of Mathematics & Computer Science University of Southern Denmark

MATH 304 Linear Algebra Lecture 20: The Gram-Schmidt process (continued). Eigenvalues and eigenvectors.

STAT 309: MATHEMATICAL COMPUTATIONS I FALL 2018 LECTURE 13

Chapter 2. Solving Systems of Equations. 2.1 Gaussian elimination

1 Last time: least-squares problems

STAT 309: MATHEMATICAL COMPUTATIONS I FALL 2018 LECTURE 9

Review of matrices. Let m, n IN. A rectangle of numbers written like A =

Computational Linear Algebra

Total 170. Name. Final Examination M340L-CS

Numerical methods, midterm test I (2018/19 autumn, group A) Solutions

Next topics: Solving systems of linear equations

Linear Algebra, part 3. Going back to least squares. Mathematical Models, Analysis and Simulation = 0. a T 1 e. a T n e. Anna-Karin Tornberg

Lecture 11. Linear systems: Cholesky method. Eigensystems: Terminology. Jacobi transformations QR transformation

L2-7 Some very stylish matrix decompositions for solving Ax = b 10 Oct 2015

Elementary Linear Algebra

MAT 343 Laboratory 3 The LU factorization

Solving Ax = b w/ different b s: LU-Factorization

COMP 558 lecture 18 Nov. 15, 2010

7. LU factorization. factor-solve method. LU factorization. solving Ax = b with A nonsingular. the inverse of a nonsingular matrix

Math Computation Test 1 September 26 th, 2016 Debate: Computation vs. Theory Whatever wins, it ll be Huuuge!

MATHEMATICS FOR COMPUTER VISION WEEK 2 LINEAR SYSTEMS. Dr Fabio Cuzzolin MSc in Computer Vision Oxford Brookes University Year

Linear algebra for MATH2601 Numerical methods

Main matrix factorizations

MIT Final Exam Solutions, Spring 2017

Systems of Linear Equations

SUMMARY OF MATH 1600

LINEAR SYSTEMS (11) Intensive Computation

Outline. Math Numerical Analysis. Errors. Lecture Notes Linear Algebra: Part B. Joseph M. Mahaffy,

Linear Algebra Primer

30.3. LU Decomposition. Introduction. Prerequisites. Learning Outcomes

There are six more problems on the next two pages

MATLAB Project: LU Factorization

GAUSSIAN ELIMINATION AND LU DECOMPOSITION (SUPPLEMENT FOR MA511)

Algebra C Numerical Linear Algebra Sample Exam Problems

Least squares: the big idea

CS227-Scientific Computing. Lecture 4: A Crash Course in Linear Algebra

Chapter 2. Solving Systems of Equations. 2.1 Gaussian elimination

5.6. PSEUDOINVERSES 101. A H w.

Lecture 4: Applications of Orthogonality: QR Decompositions

Since the determinant of a diagonal matrix is the product of its diagonal elements it is trivial to see that det(a) = α 2. = max. A 1 x.

1 Number Systems and Errors 1

22A-2 SUMMER 2014 LECTURE 5

18.06SC Final Exam Solutions

Preliminary/Qualifying Exam in Numerical Analysis (Math 502a) Spring 2012

Lecture # 11 The Power Method for Eigenvalues Part II. The power method find the largest (in magnitude) eigenvalue of. A R n n.

Linear Systems of n equations for n unknowns

Transcription:

lecture 3 and 4: algorithms for linear algebra STAT 545: Introduction to computational statistics Vinayak Rao Department of Statistics, Purdue University August 30, 2016

Solving a system of linear equations Consider AX = b, where A is N N, and X and b are N k. Solve for X: X = A 1 b Calculate the inverse of A and multiply? No! Directly solving for X is faster, and more stable numerically A 1 need not even exist > solve(a,b) # Directly solve for b > solve(a) %*% b # Return inverse and multiply http://www.johndcook.com/blog/2010/01/19/ dont-invert-that-matrix/ 1/23

Gauss-Jordan elimination A X = b A [ X, A 1 ] = [ b, I ] 1 0 1 x 1 v 11 v 12 v 13 4 1 0 0 2 0 1 x 2 v 21 v 22 v 23 = 10 0 1 0 1 2 1 x 3 v 31 v 32 v 33 3 0 0 1 Manipulate to get: I [ X, A 1 ] = [ ĉ 1, Ĉ 2 ] At step i: Make element a ii = 1 (by scaling or pivoting) Set other elements in column i to 0 by multiplying and subtracting that row 2/23

Gauss-Jordan elimination 1 0 1 x 1 v 11 v 12 v 13 4 1 0 0 2 0 1 x 2 v 21 v 22 v 23 = 10 0 1 0 1 2 1 x 3 v 31 v 32 v 33 3 0 0 1 3/23

Gauss-Jordan elimination 1 0 1 x 1 v 11 v 12 v 13 4 1 0 0 2 0 1 x 2 v 21 v 22 v 23 = 10 0 1 0 1 2 1 x 3 v 31 v 32 v 33 3 0 0 1 Multiply row 1 by 2 and subtract 3/23

Gauss-Jordan elimination 1 0 1 x 1 v 11 v 12 v 13 4 1 0 0 0 0 1 x 2 v 21 v 22 v 23 = 2 2 1 0 1 2 1 x 3 v 31 v 32 v 33 3 0 0 1 3/23

Gauss-Jordan elimination 1 0 1 x 1 v 11 v 12 v 13 4 1 0 0 0 0 1 x 2 v 21 v 22 v 23 = 2 2 1 0 0 2 2 x 3 v 31 v 32 v 33 1 1 0 1 Subtract row 1 3/23

Gauss-Jordan elimination 1 0 1 x 1 v 11 v 12 v 13 4 1 0 0 0 2 2 x 2 v 21 v 22 v 23 = 1 1 0 1 0 0 1 x 3 v 31 v 32 v 33 2 2 1 0 Pivot 3/23

Gauss-Jordan elimination 1 0 0 x 1 v 11 v 12 v 13 6 1 1 0 0 1 0 x 2 v 21 v 22 v 23 = 2.5 1.5 1 0.5 0 0 1 x 3 v 31 v 32 v 33 2 2 1 0 Continue till we get an identity matrix 3/23

Gauss-Jordan elimination 1 0 0 x 1 v 11 v 12 v 13 6 1 1 0 0 1 0 x 2 v 21 v 22 v 23 = 2.5 1.5 1 0.5 0 0 1 x 3 v 31 v 32 v 33 2 2 1 0 What is the cost of this algorithm? 3/23

Gauss elimination with back-substitution A [ X, A 1 ] = [ b, I ] O(N 3 ) manipulation to get: U [ X, A 1 ] = [ ĉ 1, Ĉ 2 ] Here, U is an upper-triangular matrix. Cannot just read off solution. Need to backsolve. 4/23

LU decomposition What are we actually doing? A = LU Here L and U are lower and upper triangular matrices. Is this always possible? 1 0 0 u 11 u 12 u 13 L = l 21 1 0, U = 0 u 22 u 23 l 31 l 32 1 0 0 u 33 [ ] 0 1 A = 1 0 PA = LU, P is a permutation matrix Crout s algorithm, O(N 3 ), stable, L, U can be computed in place. 5/23

Backsubstitution AX = b LUX = Pb First solve Y by forward substitution LY = Pb 1 0 0 y 1 ˆb 1 l 21 1 0 y 2 = ˆb 2 l 31 l 32 1 y 3 ˆb 3 Then solve X by back substitution UX = Y 6/23

Comments LU-decomposition can be reused for different b s. Calculating LU decomposition: O(N 3 ). Given LU decomposition, solving for X: O(N 2 ). A = P 1 LU = ( 1) S N i=1 u ii (S: num. of exchanges) LUA 1 = PI, can solve for A 1. (back to Gauss-Jordan) 7/23

Cholesky decomposition If A is symmetric positive-definite: A = LL T Square-root of A More stable. Twice as efficient. Related: A = LDL T. 8/23

Eigenvalue decomposition An N N matrix A: a map from R N R N. An eigenvector v undergoes no rotation: Av = λv λ is the corresponding eigenvalue, and gives the rescaling. Let Λ = diag(λ 1,, λ N ) with λ 1 λ 2,..., λ N, and V = [v 1,, v N ] be the matrix of corresponding eigenvectors Real Symmetric matrices have real eigenvalues AV = VΛ different eigenvalues have orthogonal eigenvectors 9/23

Gauss-Jordan elimination 10 7 8 7 7 5 6 5 8 6 10 9 7 5 9 10 x 1 x 2 x 3 x 4 32 = 23 33 31 What is the solution? How about for b = [32.1, 22.9, 33.1, 30.9] T? Why the difference? the determinant? the inverse? the condition number? An ill-conditioned problem can strongly amplify errors. Even without any rounding error 10/23

Stability analysis The norm of a matrix A is A 2 = A = max v =1 Av For a symmetric, real matrix, A = λ max (A) (why?) For a general, real matrix, A = λ max (A T A) (why?) For A in R N N and any v R N, Av A v (why?) If A = BC, and all matrices are in R N N, A B C (why?) 11/23

Stability analysis For a perturbation δb let δx be the change in solution to Ax = b A(x + δx) = b + δb δx x δb is the relative change in the solution from the change b From b = Ax and δx = A 1 δb, we have: δx x A A 1 δb b Condition number of a matrix A is given by κ(a) = A A 1 12/23

Stability analysis κ(a) 1 (why?) For a real symmetric matrix, κ(a) = λ max(a) λ min (A) (why?) For a real matrix, κ(a) = λmax (A T A) λ min (A T A) (why?) Condition number is a property of a problem Stability is a property of an algorithm A bad algorithm can mess up a simple problem 13/23

Gaussian elimination with partial pivoting Consider reducing to upper triangular v 11 v 12 v 13 v 21 v 22 v 23 v 31 v 32 v 33 Gaussian elimination: divide row 1 by v 11 Partial pivoting: Pivot rows to bring max v 1 to top Can dramatically improve performance. E.g. [ ] 1e 4 1 1 1 Why does it work? 14/23

Gaussian elimination with partial pivoting Recall Gaussian elimination decomposes A = LU and solves two intermediate problems. What are the condition numbers of L and U? Try [ ] 1e 4 1 1 1 15/23

QR decomposition In general, for A = BC, κ(a) κ(b)κ(c) (why?) QR decomposition: A = QR Here, R is an upper (right) triangular matrix. Q is an orthonormal matrix: Q T Q = I κ(a) = κ(q)κ(r) Can use to solve Ax = b (How?) Most stable decomposition Does this mean we should use QR decomposition? 16/23

Gram-Schmidt orthonormalization Given N vectors x 1,..., x N construct an orthonormal basis: u 1 = x 1 / x 1 ũ i = x i i 1 j=1 (xt i u j)u i, u i = ũ i / ũ i i = 2..., N Modified Gram-Schmidt u 1 = x 1 / x 1 ũ i = x i (x T i u 1)u 1, ũ i = x i (u T i u 1)u 2, u i = ũ i / ũ i 17/23

QR decomposition A = Q R a 11 a 12 a 13 q 11 q 12 q 13 r 11 r 12 r 13 a 21 a 22 a 23 = q 21 q 22 q 23 0 r 22 r 23 a 31 a 32 a 33 q 31 q 32 q 33 0 0 r 33 QR decomposition: Gram-Schmidt on columns of A (can you see why?) Of course, there are more stable/efficient ways of doing this (Householder rotation/givens rotation) O(N 3 ) algorithms (though about twice as slow as LU) 18/23

Calculating eigenvalues and -vectors Let A be any real symmetric matrix. How does one calculate its largest eigenvalue and vector? Start with a random vector u 0 Define u 1 = Au 0, and normalize length. Repeat: u i = Au i 1, u i = u i / u i u i v 1 (Why?) The power method (Google s PageRank). How would we calculate λ 1? What if we wanted the second eigenvector? 19/23

QR algorithm Algorithm to calculate all eigenvalues/eigenvectors of a (not too-large) matrix Start with A 0 = A At iteration i: A i = Q i R i A i+1 = R i Q i Can be made this more stable/efficient. One of Dongarra & Sullivan (2000) s list of top 10 algoirithms. https://www.siam.org/pdf/news/637.pdf See also number 4, decompositional approach to matrix computations 20/23

Multivariate normal log(p(x µ, Σ)) = 1 2 (X µ)t Σ 1 (X µ) N 2 log 2π 1 log Σ 2 Σ = LL T Y = L 1 (X µ) (Forward solve) log(p(x µ, Σ)) = 1 2 YT Y N 2 log 2π log Σ Can also just forward solve for L 1 : LL 1 = I (Inverted triangular matrix isn t too bad) 21/23

Sampling a multivariate normal Sampling a univariate normal: Inversion method (default for rnorm?). Box-Muller transform: (Z 1, Z 2 ) : independent standard normals. Let Z N (0, I) X = µ + LZ Z = N (µ, L T L) 22/23

x 10 10 5 0 5 10 5 0 5 10 y [ ] 1 0 Σ = 0 1 23/23

x 10 10 5 0 5 10 5 0 5 10 y [ ] 4 0 Σ = 0 1 23/23

x 10 10 5 0 5 10 5 0 5 10 y [ ] 4 2 Σ = 2 4 23/23

x 10 10 5 0 5 10 5 0 5 10 y [ ] 4 3.8 Σ = 3.8 4 23/23