section 1.1 - a motivating example: predicting the weatherpredicting the weather with octave make...

28
Section 1.1 - A Motivating Example: Predicting the Weather Maggie Myers Robert A. van de Geijn The University of Texas at Austin Practical Linear Algebra – Fall 2009 http://z.cs.utexas.edu/wiki/pla.wiki/ 1

Upload: others

Post on 24-Sep-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Section 1.1 - A Motivating Example: Predictingthe Weather

Maggie MyersRobert A. van de Geijn

The University of Texas at Austin

Practical Linear Algebra – Fall 2009

http://z.cs.utexas.edu/wiki/pla.wiki/ 1

Page 2: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Let us assume that on any day, the following table tells us how theweather on that day predicts the weather the next day:

Todaysunny cloudy rainy

Tomorrowsunny 0.4 0.3 0.1cloudy 0.4 0.3 0.6rainy 0.2 0.4 0.3

This table is interpreted as follows: If today it is rainy, then theprobability that it will be cloudy tomorrow is 0.6, etc.

http://z.cs.utexas.edu/wiki/pla.wiki/ 2

Page 3: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Todaysunny cloudy rainy

Tomorrowsunny 0.4 0.3 0.1cloudy 0.4 0.3 0.6rainy 0.2 0.4 0.3

Example

If today is cloudy, what is the probability that tomorrow is

sunny?

cloudy?

rainy?

http://z.cs.utexas.edu/wiki/pla.wiki/ 3

Page 4: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Todaysunny cloudy rainy

Tomorrowsunny 0.4 0.3 0.1cloudy 0.4 0.3 0.6rainy 0.2 0.4 0.3

Example - Answer

If today is cloudy, what is the probability that tomorrow is

sunny? 0.3cloudy? 0.3rainy? 0.4

http://z.cs.utexas.edu/wiki/pla.wiki/ 4

Page 5: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Todaysunny cloudy rainy

Tomorrowsunny 0.4 0.3 0.1cloudy 0.4 0.3 0.6rainy 0.2 0.4 0.3

Example

If today is cloudy, what is the probability that the day aftertomorrow is sunny?

http://z.cs.utexas.edu/wiki/pla.wiki/ 5

Page 6: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Todaysunny cloudy rainy

Tomorrowsunny 0.4 0.3 0.1cloudy 0.4 0.3 0.6rainy 0.2 0.4 0.3

Example - Answer

If today is cloudy, what is the probability that the day aftertomorrow is sunny?We don’t know what the weather will be tomorrow. What we doknow is

The probability that it will be

sunny the day after tomorrow and sunny tomorrow is 0.4× 0.3.sunny the day after tomorrow and cloudy tomorrow is0.3× 0.3.sunny the day after tomorrow and rainy tomorrow is 0.1× 0.4.

The probability that it will be sunny the day after tomorrow if it iscloudy today is 0.4× 0.3 + 0.3× 0.3 + 0.1× 0.4 = 0.25.

http://z.cs.utexas.edu/wiki/pla.wiki/ 6

Page 7: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Todaysunny cloudy rainy

Tomorrowsunny 0.4 0.3 0.1cloudy 0.4 0.3 0.6rainy 0.2 0.4 0.3

Exercise

If today is cloudy, what is the probability that the day aftertomorrow is cloudy?

http://z.cs.utexas.edu/wiki/pla.wiki/ 7

Page 8: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Todaysunny cloudy rainy

Tomorrowsunny 0.4 0.3 0.1cloudy 0.4 0.3 0.6rainy 0.2 0.4 0.3

Exercise

If today is cloudy, what is the probability that the day aftertomorrow is rainy?

http://z.cs.utexas.edu/wiki/pla.wiki/ 8

Page 9: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Todaysunny cloudy rainy

Tomorrowsunny 0.4 0.3 0.1cloudy 0.4 0.3 0.6rainy 0.2 0.4 0.3

Exercise - Answer

If today is cloudy, what is the probability that a week from today itis sunny? cloudy? rainy? We will not answer this question now.

We insert it to make the point that things can get messy.

http://z.cs.utexas.edu/wiki/pla.wiki/ 9

Page 10: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Notation

As is usually the case when we are trying to answer quantitativequestions, it helps to introduce some notation.

Let χ(k)s denote the probability that it will be sunny k days

from now.

Let χ(k)c denote the probability that it will be cloudy k days

from now.

Let χ(k)r denote the probability that it will be rainy k days

from now.

Here, χ is the Greek lower case letter chi, pronounced [kai] inEnglish.

http://z.cs.utexas.edu/wiki/pla.wiki/ 10

Page 11: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Todaysunny cloudy rainy

Tomorrowsunny 0.4 0.3 0.1cloudy 0.4 0.3 0.6rainy 0.2 0.4 0.3

The table can be translated into the following equations:

χ(k+1)s = 0.4× χ(k)

s + 0.3× χ(k)c + 0.1× χ(k)

r

χ(k+1)c = 0.4× χ(k)

s + 0.3× χ(k)c + 0.6× χ(k)

r

χ(k+1)r = 0.2× χ(k)

s + 0.4× χ(k)c + 0.3× χ(k)

r

http://z.cs.utexas.edu/wiki/pla.wiki/ 11

Page 12: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Todaysunny cloudy rainy

Tomorrowsunny 0.4 0.3 0.1cloudy 0.4 0.3 0.6rainy 0.2 0.4 0.3

The probabilities that denote what the weather may be on day kand the table that summarizes the probabilities is often representedas a (state) vector, x, and (transition) matrix, P , respectively:

x(k) =

χ(k)s

χ(k)c

χ(k)r

and P =

0.4 0.3 0.10.4 0.3 0.60.2 0.4 0.3

.

http://z.cs.utexas.edu/wiki/pla.wiki/ 12

Page 13: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

The transition from day k to day k + 1 is then written as thematrix-vector product (multiplication) χ

(k+1)s

χ(k+1)c

χ(k+1)r

=

0.4 0.3 0.10.4 0.3 0.60.2 0.4 0.3

χ

(k)s

χ(k)c

χ(k)r

,

or x(k+1) = Px(k)

This is simply a more compact representation (way of writing) theequations

χ(k+1)s = 0.4× χ(k)

s + 0.3× χ(k)c + 0.1× χ(k)

r

χ(k+1)c = 0.4× χ(k)

s + 0.3× χ(k)c + 0.6× χ(k)

r

χ(k+1)r = 0.2× χ(k)

s + 0.4× χ(k)c + 0.3× χ(k)

r

http://z.cs.utexas.edu/wiki/pla.wiki/ 13

Page 14: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Example

Assume again that today is cloudy so that the probability that it issunny, cloudy, or rainy today is 0, 1, and 0, respectively:

x(0) =

χ(0)s

χ(0)c

χ(0)r

=

010

.

Then the vector of probabilities for tomorrow’s weather (dayk = 1), x(1), is given by χ

(1)s

χ(1)c

χ(1)r

=

0.4 0.3 0.10.4 0.3 0.60.2 0.4 0.3

χ

(0)s

χ(0)c

χ(0)r

http://z.cs.utexas.edu/wiki/pla.wiki/ 14

Page 15: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

But...

χ(1)s

χ(1)c

χ(1)r

=

0.4 0.3 0.10.4 0.3 0.60.2 0.4 0.3

χ

(0)s

χ(0)c

χ(0)r

=

0.4 0.3 0.10.4 0.3 0.60.2 0.4 0.3

010

=

0.4× 0 + 0.3× 1 + 0.1× 00.4× 0 + 0.3× 1 + 0.6× 00.2× 0 + 0.4× 1 + 0.3× 0

=

0.30.30.4

.

http://z.cs.utexas.edu/wiki/pla.wiki/ 15

Page 16: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

The vector of probabilities for the day after tomorrow (day k = 2),x(2), is given by χ

(2)s

χ(2)c

χ(2)r

=

0.4 0.3 0.10.4 0.3 0.60.2 0.4 0.3

χ

(1)s

χ(1)c

χ(1)r

=

0.4 0.3 0.10.4 0.3 0.60.2 0.4 0.3

0.30.30.4

=

0.4× 0.3 + 0.3× 0.3 + 0.1× 0.40.4× 0.3 + 0.3× 0.3 + 0.6× 0.40.2× 0.3 + 0.4× 0.3 + 0.3× 0.4

=

0.250.450.30

.

http://z.cs.utexas.edu/wiki/pla.wiki/ 16

Page 17: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Exercise

Given that today is cloudy, predict the weather for the next week.

Note

Don’t get busy doing this by hand. Let’s let the computer do theheavy lifting.

http://z.cs.utexas.edu/wiki/pla.wiki/ 17

Page 18: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Predicting the Weather with Octave

Make sure you install octave if not yet installed.

Start the octave application.

On linux, type octave on the command line of a commandwindow.On Mac OSX, start the X11 application and type octave onthe command line of a command window or start the Octaveapplication from the Applications folder.On Windows, start the Octave application.

http://z.cs.utexas.edu/wiki/pla.wiki/ 18

Page 19: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Enter the transition matrix, P

octave:1> P = [> 0.4 0.3 0.1> 0.4 0.3 0.6> 0.2 0.4 0.3> ]P =

0.40000 0.30000 0.100000.40000 0.30000 0.600000.20000 0.40000 0.30000

http://z.cs.utexas.edu/wiki/pla.wiki/ 19

Page 20: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Enter the vector that described the weather today (cloudy)

octave:2> x0 = [> 0> 1> 0> ];

The “;” means that M-script does not automatically print out theresult. To see the contents of variable (vector) x0 type

Display x(0) (in variable x0)

octave:2> x0x0 =

010

http://z.cs.utexas.edu/wiki/pla.wiki/ 20

Page 21: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Compute x(1) = Px(0) (probability vector for tomorrow)

octave:4> x1 = P * x0x1 =

0.300000.300000.40000

Compute x(2) = Px(1) (day after tomorrow)

octave:5> x2 = P * x1x2 =

0.250000.450000.30000

http://z.cs.utexas.edu/wiki/pla.wiki/ 21

Page 22: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Exercise

Given that today is cloudy, predict the weather for the next week.

Answer

Continue on to compute x(3), . . . , x(7):

k0 1 2 3 4 5 6 7

x(k) =

0@ 010

1A 0@ 0.30.30.4

1A 0@ 0.250.450.30

1A 0@ 0.2650.4150.320

1A 0@ 0.26250.42250.3150

1A 0@ 0.263250.420750.31600

1A 0@ 0.263120.421120.31575

1A 0@ 0.263160.421040.31580

1A

http://z.cs.utexas.edu/wiki/pla.wiki/ 22

Page 23: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Exercise

Given that today is cloudy, predict the weather for the next month.

Note

Even though we used Octave to compute x(0), . . . , x(7), theprevious approach, computing x0, x1, etc., would get quite tiring.Let’s compute x(0), . . . , x(7) with a loop.

http://z.cs.utexas.edu/wiki/pla.wiki/ 23

Page 24: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Initialize variable x to x(0).

octave:7> x = [> 0> 1> 0> ]x =

010

http://z.cs.utexas.edu/wiki/pla.wiki/ 24

Page 25: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Use a for loop

octave:8> for k=1:7> x = P * x> endx =

0.300000.300000.40000

x =

0.250000.450000.30000

x =

etc.http://z.cs.utexas.edu/wiki/pla.wiki/ 25

Page 26: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Or we could compute the table...

Create a 3× 8 array in which to store the table and set the firstcolumn to x(0).

octave:9> x = zeros( 3, 8 )

x =

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

octave:10> x( :, 1 ) = [

> 0

> 1

> 0

> ]

x =

0 0 0 0 0 0 0 0

1 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

http://z.cs.utexas.edu/wiki/pla.wiki/ 26

Page 27: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Or we could compute the table... (continued)octave:11> for k=1:7

> x( :, k+1 ) = P * x( :, k );

> end

octave:12> x

x =

0.00000 0.30000 0.25000 0.26500 0.26250 0.26325 0.26312 0.26316

1.00000 0.30000 0.45000 0.41500 0.42250 0.42075 0.42112 0.42104

0.00000 0.40000 0.30000 0.32000 0.31500 0.31600 0.31575 0.31580

Here x( :, k ) and x( :, k+1 ) equal the kth and (k + 1)stcolumns of array x, respectively. The “;” ensures that thesubresults aren’t printed out. (Indeed: type the loop in again, butleave off the “;” and see what happens.)These numbers can then be used to fill the below table:

k0 1 2 3 4 5 6 7

x(k) =

0@ 010

1A 0@ 0.30.30.4

1A 0@ 0.250.450.30

1A 0@ 0.2650.4150.320

1A 0@ 0.26250.42250.3150

1A 0@ 0.263250.420750.31600

1A 0@ 0.263120.421120.31575

1A 0@ 0.263160.421040.31580

1A

http://z.cs.utexas.edu/wiki/pla.wiki/ 27

Page 28: Section 1.1 - A Motivating Example: Predicting the WeatherPredicting the Weather with Octave Make sure you install octave if not yet installed. Start the octave application. On linux,

Review of Octave commands

Try the following Octave commands:octave 13> helpoctave 14> help *octave 15> help :octave 16> help zerosoctave 17> help for

http://z.cs.utexas.edu/wiki/pla.wiki/ 28