CS1112 Lecture 27: Models, Data, Apportionment, Sensitivity Analysis, and Page Rank, Study notes of Computer Science

A set of lecture slides from cs1112, a computer science course, covering various topics including errors in calculating earth's surface area, the apportionment problem, sensitivity analysis, and the page rank algorithm. The slides also include examples and code snippets.

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-alb
koofers-user-alb 🇺🇸

8 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS1112 Lecture 27 2008/12/4
Lecture slides 1
Previous Lecture:
Insertion sort vs. merge sort
Timing with tic toc
Time efficiency vs. memory efficiency
Today’s Lecture:
Models and data
Congressional apportionment
Sensitivity analysis
Simulation—Google “page rank”
December 4, 2008 Lecture 27 6
What are the “errors” in calculating the surface area of
the Earth?
myPi= 3.14;
r= 3961.11345;
earthArea= 4*myPi*r*r;
A. Math error in myPi
B. Measurement error in r
C. Rounding error in arithmetic
D. All of the above
December 4, 2008 Lecture 27 11
The apportionment problem
Number of states: n
State populations: p(1),…,p(n)
Total population: P
State delegation size: d(1),…,d(n)
Number of seats: D
Distribute 435 Congressional seats among the 50 states
so that the ratio of population to delegation size is
roughly the same from state to state.
December 4, 2008 Lecture 27 16
An Apportionment Method determines
delegation sizes d(1),…,d(n) that are whole
numbers so that representation is approximately
equal:
)(
)(
...
)1( )1(
nd
np
d
p
Definition
December 4, 2008 Lecture 27 29
% Every state gets a district
d= ones(50,1);
% “Deal out” remaining districts
for k= 51:435
% Let i be the index of the state that
% most deserves an additional district
d(i)= d(i) + 1;
end
December 4, 2008 Lecture 27 34
% Every state gets a district
d= ones(50,1);
% “Deal out” remaining districts
for k= 51:435
[z,i]= max((p./d).*(p./(d+1)))
d(i)= d(i) + 1;
end
Allocate using the method of equal proportions
See posted version for detail
pf3
pf4

Partial preview of the text

Download CS1112 Lecture 27: Models, Data, Apportionment, Sensitivity Analysis, and Page Rank and more Study notes Computer Science in PDF only on Docsity!

„ Previous Lecture:

„ Insertion sort vs. merge sort

„ Timing with tic toc

„ Time efficiency vs. memory efficiency

„ Today’s Lecture:

„ Models and data

„ Congressional apportionment „ Sensitivity analysis

„ Simulation—Google “page rank”

December 4, 2008 Lecture 27 6

What are the “errors” in calculating the surface area of

the Earth?

myPi= 3.14;

r= 3961.11345;

earthArea= 4myPir*r;

A. Math error in myPi

B. Measurement error in r

C. Rounding error in arithmetic

D. All of the above

December 4, 2008 Lecture 27 11

The apportionment problem

Number of states: n

State populations: p(1),…,p(n)

Total population: P

State delegation size: d(1),…,d(n)

Number of seats: D

Distribute 435 Congressional seats among the 50 states

so that the ratio of population to delegation size is

roughly the same from state to state.

December 4, 2008 Lecture 27 16

An Apportionment Method determines

delegation sizes d(1),…,d(n) that are whole

numbers so that representation is approximately

equal:

( )

( ) ... ( 1 )

( 1 )

d n

pn

d

p ≈ ≈

Definition

December 4, 2008 Lecture 27 29

% Every state gets a district

d= ones(50,1);

% “Deal out” remaining districts

for k= 51:

% Let i be the index of the state that

% most deserves an additional district

d(i)= d(i) + 1;

end

December 4, 2008 Lecture 27 34

% Every state gets a district

d= ones(50,1);

% “Deal out” remaining districts

for k= 51:

[z,i]= max((p./d).*(p./(d+1)))

d(i)= d(i) + 1;

end

Allocate using the method of equal proportions

See posted version for detail

December 4, 2008 Lecture 27 36

A Sensitivity Analysis

„ The 435th^ district was awarded to North

Carolina.

„ Was that a “close call”? Was there another state

that “almost” won this last district? Quantify the

close call.

„ Look at the “most deserving” ranks for the last

district handed out. Which state was second? (Utah)

Was this 2 nd^ highest rank “close” to the max?

„ How many people will need to move from NC to UT

in order for the last district to go to UT (instead of

NC)?

December 4, 2008 Lecture 27 44

Background

Index all the pages on the Web from 1 to n. (n is

around ten billion.)

The PageRank algorithm orders these pages from

“most important” to “least important”.

It does this by analyzing links, not content.

December 4, 2008 Lecture 27 46

A 3-node network with specified transition probabilities

1

2

3

A node

Transition

probabilities

December 4, 2008 Lecture 27 47

A special random walk

Suppose there are a 1000 people on each node.

At the sound of a whistle they hop to another

node in accordance with the “outbound”

probabilities.

December 4, 2008 Lecture 27 52

New Distribution

Before After

Node 1 1000 1000

Node 2 1000 1300

Node 3 1000 700

December 4, 2008 Lecture 27 55

After 100 iterations

Before After

Node 1 1142.85 1142.

Node 2 1357.14 1357.

Node 3 500.00 500.

Appears to reach a steady state

Call this the stationary vector

December 4, 2008 Lecture 27 67

Connectivity (G) Æ Transition Probability (P)

[n,n] = size(G);

P = zeros(n,n);

for j=1:n

P(:,j) = G(:,j)/sum(G(:,j));

end

December 4, 2008 Lecture 27 70

statVec sorted idx pR

[sorted, idx] = sort(-statVec); for k= 1:length(statVec) j = idx(k); % index of kth largest pR(j) = k; end

December 4, 2008 Lecture 27 71

A new random walk on the Web that deals with dead ends

Repeat: You are on a webpage. If there are no outlinks Pick a random page and go there. else Flip an unfair coin. if heads Click on a random outlink and go there. else Pick a random page and go there. end end

In practice, an unfair coin with prob .85 heads works well.

This results in a different

transitional probability matrix.

December 4, 2008 Lecture 27 72

Connectivity

Page 6 has no

outlinks, so pick

a random page

and go there

Transition Probability

A. 0

B. 0.

C. 1

D. rand(1)

Other columns need to

be changed as well. See

PageRank.m

December 4, 2008 Lecture 27 76

What we learned…

„ Develop/implement algorithms for problems

„ Develop programming skills

„ Design, implement, document, test, and debug

„ Programming “tool bag”

„ Control flow (if-else; loops)

„ Functions for reducing redundancy

„ Data structures

„ Graphics

„ File handling

December 4, 2008 Lecture 27 77

What we learned… (cont’d)

„ Applications and concepts

„ Image and sound

„ Sorting and searching—you should know the

algorithms covered

„ Divide-and-conquer strategies

„ Approximation and error

„ Simulation

„ Computational effort and efficiency