• No results found

Applied Computational Economics Workshop. Part 2: Linear Equations

N/A
N/A
Protected

Academic year: 2021

Share "Applied Computational Economics Workshop. Part 2: Linear Equations"

Copied!
25
0
0

Loading.... (view fulltext now)

Full text

(1)

Applied Computational

Economics Workshop

(2)

Overview

Introduction

Gaussian elimination

Rounding error

(3)

Introduction

An n-dimensional linear equation takes the form

where

is a known

matrix

is a known

vector

(4)

Introduction

Many models in economics are naturally linear, e.g. linear

multi-commodity market equilibrium models, ordinary

least squares, etc.

However, we are primarily interested in linear equations

here because the methods we will employ to solve the

functional equations underlying dynamic models will

require us to solve very large linear equations repeatedly

Execution speed, storage requirements, and rounding

error associated with solving linear equations are thus

important practical issues

(5)

Gaussian Elimination

A linear equation may be solved using Gaussian

elimination

Gaussian elimination employs elementary row operations

Interchange two rows

Multiply a row by a nonzero constant

Add a nonzero multiple of one row to another

Elementary row operations alter the form of a linear

equation without changing its solution

(6)

Gaussian Elimination

Gaussian elimination is the most efficient known method for

solving a general -dimensional linear equation

=

For large , Gaussian elimination requires about

/3 +

multiplication/division operations

Explicitly computing

requires about

+

operations

Cramer’s rule requires ( + 1)! operations

For = 10, the number of operations are

Gaussian Elimination

430

Explicit Inverse

1,100

(7)

Gaussian Elimination

The MATLAB “backslash” operator

\

uses Gaussian

elimination to solve linear equations

For example, to solve the linear equation

1 1 2

3 4 8

2 1 1

=

18

5

6

one may execute the MATLAB code

A = [1 1 2; 3 4 8; 2 1 1];

b = [5; 18; 6];

(8)

Gaussian Elimination

These operations return

x =

2.0000

1.0000

1.0000

The backslash operator is the most frequently-used operator

in MATLAB, apart from routine arithmetic operators

The backslash operator detects special structures, such as

positive definiteness of A, and employs faster algorithms, such

as Cholesky factorization, whenever possible

(9)

Rounding Error

A computer has finite storage and can represent

only finitely many numbers exactly

Thus, exact arithmetic and computer arithmetic

do not always give the same results

If you attempt to compute a number that can’t

be represented exactly on a computer, the result

will be rounded to the nearest representable

(10)

Rounding Error

 In particular, when adding or subtracting two numbers of extremely different magnitudes, the smaller number is effectively ignored

 For example, in exact arithmetic

( + 1) − 1 = + (1 − 1) =

 However, in MATLAB computer arithmetic

e = 1e–20; x = (e+1)–1; y = e+(1–1); will return x = 0 y = 1.0000e–020

(11)

Rounding Error

 Rounding error can cause problems when solving linear equations

 Consider the linear equation 1

1 1 = 12 where = 10

 One can easily verify that the exact solution is

= which is slightly more than 1 = which is slightly less than 1

(12)

Rounding Error

To solve the linear equation using Gaussian elimination,

add

times the first row to the second row

then solve recursively

/ /

(13)

Rounding Error

If you compute

and

in this manner in MATLAB,

e = 1e–17;

x2 = (2–1/e)/(1–1/e)

x1 = (1–x2)/e

the operations return

x2 = 1

x1 = 0

The computed value for

is grossly inaccurate

(14)

Rounding Error

 In the first step of Gaussian elimination, we computed

= 2 − 1/ 1 − 1/

 However, since 1/ is very large compared to 1 or 2, rounding error was introduced, and the computer actually calculated

= −1/ −1/

which evaluated to exactly 1

 The computer then calculated

= 1 −

(15)

Rounding Error

Now solve the linear equation again by Gaussian elimination, but

first interchange the two rows, which will not affect the solution

1 1

1

= 2

1

Now add − times the first row to the second row

1

1

0 1 −

=

1 − 2

2

then solve recursively

=

(16)

Rounding Error

 If you compute and in this manner in MATLAB,

e = 1e–17;

x2 = (1–2*e)/(1–e) x1 = 2–x2

the operations return

x2 = 1 x1 = 1

 The computed values for and are a little off, but are much more accurate than the first values we computed

 Why did interchanging the two rows improve the accuracy of the computed solution?

(17)

Rounding Error

 The inaccuracy of the first solution was due to rounding error caused by the very small magnitude of the diagonal element

 By interchanging the two rows first, we brought a number of much larger magnitude into the diagonal, which reduced rounding error in subsequent computations

 Interchanging rows to make the magnitude of the diagonal element as large as possible is called pivoting

 Pivoting substantially enhances the computational accuracy of Gaussian elimination

 All good linear solvers, including the MATLAB backslash operator, employ pivoting

(18)

Ill-Conditioning

Consider the n-dimensional linear equation =

 A perturbation in will induce a change in solution

 The sensitivity of the solution to perturbations in is measured by the condition number of matrix

 The condition number of is the ratio its largest and smallest singular values — too technical

 Rule of Thumb

 Computed value of loses one significant digit per power of 10 of the condition number of

(19)

Ill-Conditioning

If the condition number of is large, small perturbations in

can lead to disproportionately large changes in

When this is the case, we say that is ill-conditioned or nearly

singular

When is ill-conditioned, unavoidable rounding errors in the

representation of make it impossible to compute an

accurate solution to

=

Ill-conditioning is endemic to the matrix and cannot be

corrected with simple tricks such as pivoting

(20)

Ill-Conditioning

Consider the notorious Vandermonde matrices

The

Vandermonde matrix has a typical element

(21)

Ill-Conditioning

Let us solve the linear equation

where is the

Vandermonde matrix and is the

row-sum of , that is, the

vector with a typical

element

By construction, the exact solution to this linear equation

(22)

Ill-Conditioning

In MATLAB, we can solve the linear equation for

as

follows

n = 4;

A = vander(1:n)

b = A*ones(n,1)

x = A\b

error = max(abs(x–1))

Here, we compute the matrix using the special MATLAB

function

vander

and we compute the maximum error

(23)

Ill-Conditioning

With

, executing this code returns, as expected,

x = 1

1 1 1 error = 0

With

, however, executing the code returns

Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 3.4e-032. error = 5.4e+011

Warning indicates A is ill-conditioned and gives reciprocal

condition number

(24)
(25)

Norm of

for Vandermonde Matrix of

Order

References

Related documents

In conclusion, the demands imposed on U12 players during different formats of SSGs differ from the soccer-7 match play demands, presenting a low stimulation of the actions performed

It is understood that the commandment of fasting the month of Ramadan was revealed in the second year of the migration of Prophet Muhammad to Madina, which is approximately 300

Theory (Thompson, 1978) and Cultural Theory (Thompson, Ellis, & Wildavsky, 1990), two variants of the Grid-Group paradigm, rely heavily on models of threshold effects

Quickstart 4 Review and analysis of current marketing, collateral, website positioning, messaging and content, plus SEO and email strategy Planner 10 As above plus creation of

There  being  no  discussion  Mr.  Ryan  asked  for  a  motion  to  approve  a  resolution  authorizing  a  Public  Hearing.    Ms.  Richardson  made  the  motion. 

Cooper Electric can make that possible, with our skilled professionals available to provide you with energy saving products and conduct energy walk throughs.. Let us make your

Wonderfully special for people too late birthday to you mean to your birthday to believe that always go straight to wish someone a little flirtatious fun!. Remembering your birthday

The abstract presented the results of the ANRS IPERGAY trial demonstrating the high effectiveness (86% relative reduction of HIV incidence with TDF/ FTC vs. placebo) and