Docsity

Probability and Computing Course Notes, Lecture notes of Probability and Statistics

Toggle sidebar
bg1
Fall 2009 version of Course 15-359,
Computer Science Department,
Carnegie Mellon University.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Probability and Computing Course Notes and more Lecture notes Probability and Statistics in PDF only on Docsity!

Fall 2009 version of Course 15-359,

Computer Science Department,

Carnegie Mellon University.

Acknowledgments:

CMU’s course 15-359, Probability and Computing, was originally conceived and

designed by Mor Harchol-Balter and John Lafferty. The choice, order, and presentation of topics

in the latter half of the course is strongly informed by the work of Mor Harchol-Balter. Indeed,

you might like to buy her book! Performance Modeling and Design of Computer Systems:

Queueing Theory in Action , http://www.cs.cmu.edu/~harchol/PerformanceModeling/book.html

The choice, order, and presentation of topics in the earlier half of the course is informed by the

work of John Lafferty: http://galton.uchicago.edu/~lafferty/

Further, a very great deal of material in these lecture notes was strongly informed by the

outstanding book Probability and Computing by Michael Mitzenmacher and Eli Upfal,

http://www.cambridge.org/us/academic/subjects/computer-science/algorithmics-complexity-

computer-algebra-and-computational-g/probability-and-computing-randomized-algorithms-and-

probabilistic-analysis

Many thanks to Mor Harchol-Balter, John Lafferty, Michael Mitzenmacher, Eli Upfal (and many

other web sources from which I borrowed)!

Data structures. When building, e.g., a static dictionary data structure, one can optimize response time if one knows the probability distribution on key lookups. Even moreso, the time for operations in hash tables can be greatly improved by careful probabilistic analysis.

Symmetry-breaking. In distributed algorithms, one often needs a way to let one of several identical processors “go first”. In combinatorial optimization algorithms — e.g., for solving TSP or SAT instances — it is sometimes effective to use randomness to decide which city or variable to process next, especially on highly symmetric instances.

Theory of large networks. Much work on the study of large networks — e.g., social networks like Facebook, or physical networks, like the Internet — models the graphs as arising from special kinds of random processes. Google’s PageRank algorithm is famously derived from modeling the hyperlinks on the internet as a “Markov chain”.

Quantum computing. The laws of physics are quantum mechanical and there has been tremen- dous recent progress on designing “quantum algorithms” that take advantage of this (even if quan- tum computers have yet to be built). Quantum computing is inherently randomized — indeed, it’s a bit like computing with probabilities that can be both positive and negative.

Statistics. Several areas of computing — e.g., Human-Computer Interaction — involve running experimental studies, often with human subjects. Interpreting the results of such studies, and decid- ing whether their findings are “statistically significant”, requires a strong knowledge of probability and statistics.

Games and gambling. Where would internet poker be without randomness?

Making algorithms run faster. Perhaps surprisingly, there are several examples of algorith- mic problems which seem to have nothing to do with randomness, yet which we know how to solve much more efficiently using randomness than without. This is my personal favorite example of probability in computing.

We will see an example of using randomness to make algorithms more efficient today, in the problem of verifying matrix multiplication.

3 About this course

This course will explore several of the above uses of probability in computing. To understand them properly, though, you will need a thorough understanding of probability theory. Probability is traditionally a “math” topic, and indeed, this course will be very much like a math class. The emphasis will be on rigorous definitions, proofs, and theorems. Your homework solutions will be graded according to these “mathematical standards”.

One consequence of this is that the first part of the course may be a little bit dry, because we will spend a fair bit of time going rigorously through the basics of probability theory. But of course it is essential that you go through these basics carefully so that you are prepared for the more advanced applications. I will be interspersing some “computer science” applications throughout

this introductory material, to try to keep a balance between theory and applications.

In particular, today we’re going to start with an application, verifying matrix multiplication. Don’t worry if you don’t understand all the details today; it would be a bit unfair of me to insist you do, given that we haven’t even done the basic theory yet! I wanted to give you a flavor of things to come, before we get down to the nitty-gritty of probability theory.

4 Verifying matrix multiplication

Let’s see an example of an algorithmic task which a probabilistic algorithm can perform much more efficiently than any deterministic algorithm we know. The task is that of verifying matrix multiplication.

4.1 Multiplying matrices

There exist extremely sophisticated algorithms for multiplying two matrices together. Suppose we have two n × n matrices, A and B. Their product, C = AB, is also an n × n matrix, with entries given by the formula

Cij =

∑^ n

k=

AikBkj. (1)

How long does it take to compute C? Since C has n^2 entries, even just writing it down in memory will take at least n^2 steps. On the other hand, the “obvious” method for computing C given by (1) takes about n^3 steps; to compute each of the n^2 entries it does roughly n many arithmetic operations.

Actually, there may be some extra time involved if the numbers involved get very large; e.g., if they’re huge, it could take a lot of time just to, say, multiply two of them together. Let us eliminate this complication by just thinking about:

Matrix multiplication mod 2. Here we assume that the entries of A and B are just bits, 0 or 1, and we compute the product mod 2:

Cij =

∑^ n

k=

AikBkj (mod 2). (2)

This way, every number involved is just a bit, so we needn’t worry about the time of doing arithmetic on numbers. The “obvious” matrix multiplication algorithm therefore definitely takes at most O(n^3 ) steps.^1

As you may know, there are very surprising, nontrivial algorithms that multiply matrices in time faster than O(n^3 ). The first and perhaps most famous is Strassen’s algorithm:

Theorem 1. (Strassen, 1969.) It is possible to multiply two matrices in time roughly nlog^2 7 ≈ n^2.^81.

(^1) By the way, if you are not so familiar with big-Oh notation and analysis of running time, don’t worry too much. This is not an algorithms course, and we won’t be studying algorithmic running time in too much detail. Rather, we’re just using it to motivate the importance of probability in algorithms and computing.

Algorithm “Basic-Freivalds” On input A, B, and D:

  1. Choose a random n-bit vector x, by making each bit xi either 0 or 1 “independently”, “with probability 1/2” each.
  2. Compute y = ABx and z = Dx. Output “yes” if y = z.

We will discuss later what is meant precisely by our choosing x “randomly” in this way. But for now I will assume that you’ve seen a little bit of probability before, and you know intuitively what this means.

Before getting to probability, let’s first confirm the running time:

Proposition 5. Basic-Freivalds can be executed in O(n^2 ) steps.

Proof. Step 1 involves writing down the n bits of x: O(n) steps. What about Step 2? Let’s start with computing z = Dx. This is a matrix-vector product; if we use the “obvious” formula,

zi =

∑^ n

j=

Dij xj (mod 2),

the computation will take O(n^2 ) steps — O(n) steps per entry of z. To compute y = ABx, we think of it as y = A(Bx). We can compute w = Bx in O(n^2 ) steps, and then compute y = Aw in another O(n^2 ) steps.

Note that this algorithm is faster than the n^2.^37 time to multiply two matrices, and also that it is very very simple to implement correctly! That said, does Basic-Freivalds actually properly check

AB =? D (mod 2)? There are two cases to check:

First: When AB = D (mod 2), does the algorithm output “yes”?

Second: When AB 6 = D (mod 2), does the algorithm output “no”?

Proposition 6. When AB = D (mod 2), Basic-Freivalds always outputs “yes”.

Proof. This is obvious. If AB = D (mod 2), then ABx = Dx (mod 2) for any vector x, so the algorithm will always have y = z.

Here is the amazing aspect of Basic-Freivalds:

Theorem 7. For any input matrices A, B, and D such that AB 6 = D (mod 2), the Basic-Freivalds algorithm will output “yes” with probability at most 1/ 2.

I.e., when AB 6 = D (mod 2), the probability Basic-Freivalds gives the wrong answer is at most 1 /2. Now this doesn’t look like what was promised, namely a failure probability of at most 2−^200. But there is a simple way to deliver on the promise: just repeat the Basic-Freivalds algorithm a bunch of times.

Freivalds’s Algorithm: On input A, B, and D, run the Basic-Freivalds algorithm 200 times. If it ever outputs “no”, then output “no”. If it always outputs “yes”, then output “yes”.

The total running time for Freivalds’s Algorithm is 200 × O(n^2 ) ≤ O(n^2 ) steps. Again, we should check two cases when analyzing correctness:

Theorem 8. When AB = D (mod 2), Freivalds’s Algorithm always outputs “yes”. On any input with AB 6 = D (mod 2), Freivalds’s Algorithm outputs “yes” (i.e., acts incorrectly) with probability at most 2 −^200.

Proof. The first case is clear: when AB = D (mod 2), Basic-Freivalds always outputs “yes”, so the overall Freivalds Algorithm will output “yes”. As for the second case, suppose AB 6 = D (mod 2). By Theorem 7, the probability one run of Basic-Freivalds outputs “yes” is 1/2. Therefore, the probability that all 200 runs output “yes” is at most

1 2

×

× · · · (200 times) · · · ×

= 2−^200.

Actually, justifying this proof requires understanding the basics of probability, and we will de- velop these basics properly only over the next few lectures. I hope, though, that you’ve seen a little probability before and that you intuitively agree with the above proof.

By the way, this repetition of the Basic-Freivalds test illustrates a common idea in probabilistic computing: trading off running time and correctness probability.

5 Analyzing Basic-Freivalds, and other randomized code

Let’s now prove Theorem 7 — i.e., analyze the Basic-Freivalds algorithm when AB 6 = D (mod 2). In fact, this naturally leads to a basic theme for the first part of this course:

PROBABILITY THEORY = ANALYZING RANDOMIZED CODE.

This might look surprising, as the left-hand side seems to be a math topic and the right-hand side seems to be a computer science topic. We’ll return to this.

Let’s write out the Basic-Freivalds code a little differently to highlight the randomized compo- nent:

Input A, B, D x[1] ← Bernoulli(. 5 ) x[2] ← Bernoulli(. 5 ) · · · x[n] ← Bernoulli(. 5 ) Output ‘‘yes’’ if ABx = Dx (mod 2), output ‘‘no’’ if ABx 6 = Dx (mod 2)

You might ask, what the heck is “Bernoulli(.5)”? This will be our notation for a built-in function which returns 0 with probability .5 and 1 with probability .5. Basically, it’s a “fair coin

Now comes the main trick. Observe that the following two blocks of code clearly have the same behavior:

x[1] ← Bernoulli(. 5 ) x[2] ← Bernoulli(. 5 ) · · · x[n] ← Bernoulli(. 5 ) answer ← E 5 , 1 · x[1] + E 5 , 2 · x[2] + E 5 , 3 · x[3] + E 5 , 4 · x[4] + · · · + E 5 ,n · x[n] (mod 2)

versus

x[1] ← Bernoulli(. 5 ) x[2] ← Bernoulli(. 5 ) x[4] ← Bernoulli(. 5 ) x[5] ← Bernoulli(. 5 ) · · · x[n] ← Bernoulli(. 5 ) temp ← E 5 , 1 · x[1] + E 5 , 2 · x[2] + E 5 , 4 · x[4] + · · · + E 5 ,n · x[n] (mod 2) x[3] ← Bernoulli(. 5 ) answer ← temp + x[3] (mod 2)

In other words, it doesn’t actually matter what order we pick the x[i]’s in, so we can imagine x[3] being picked last. The point now is that it doesn’t matter what temp turns out to be; just based on the random choice of x[3] we’ll have that answer is 1 with probability exactly 1/2. This is because both 0 + x[3] (mod 2) and 1 + x[3] (mod 2) are equally likely to be 0 or 1 when we choose x[3] ← Bernoulli(. 5 ).

Neat trick, huh?

15-359: Probability and Computing

Fall 2009

Lecture 2: Discrete probability modeling; probability of events; conditioning

In this lecture we will learn the basics of probability — sample spaces, events, conditioning, etc. Our basic framework will have a “computer science-y” slant. In my opinion, this is the right way to think about probability — even if you’re a mathematician with no interest in computer science.

1 Probability Modeling

1.1 Modeling with computer code

Probability is all about modeling reality with math. (Actually, this is what all of math is about.) But the way math people teach and model probability is often not so great. They do probability modeling like this: Description of reality → Math.

This often leads to vagueness (e.g., what exactly is a “random experiment”) or ambiguities (think of the “Monty Hall Problem”).

The right way to do probability is the computer science way: always be analyzing randomized code! This is how we will do all our probability modeling:

Description of reality → Computer code → Math

And often, the reality will be the computer code. The beauty of this is that any ambiguity in the English-language description can be debated when doing the first step, modeling the real-world situation with randomized code. But once the randomized code is decided on, there will be an unambiguous and clear-cut way to analyze it mathematically.

Let’s explain. You know how all probability exercises start with something like, “A person flips 2 fair coins... ”? We will immediately model this with code:

coin1 ← Bernoulli( 1 / 2 ) coin2 ← Bernoulli( 1 / 2 )

(Well, I suppose Bernoulli(.5) returns 0 or 1, but you can add in statements like

if coin1 = 0 then coin1 ← ‘‘tails’’ else coin1 ← ‘‘heads’’

if you like. But we won’t care much about such niceties.)

ASSUMPTION: We have a built-in random function, Bernoulli(p), which given a real 0 ≤ p ≤ 1, returns 1 with probability p, 0 with probability 1 − p.

Now the above code could simply be written as:

patient.hasDisease ← Bernoulli(.1)

1.3 “Discrete” randomness only for now

So far we’ve assumed access to two kinds of “built-in” random number generators: Bernoulli(p) for any real 0 ≤ p ≤ 1, and RandInt(m) for any m ∈ N. On the homework, you will explore how, using these tools, you can build up other random number-generating functions such as

Rand

1 with probability. 2 5 with probability. 3 − 3 with probability. 5

Really, anything where there are finitely many different outcomes with nonnegative probabilities adding up to 1.

What we are absolutely disallowing for the first half of the course is the use of a random number generator like “Unif([0, 1])”, which returns a uniformly random real number between 0 and 1 — whatever that means!

Why do we disallow this? As we are about to see, to analyze randomized code we will be drawing trees, with a node for each call to a random number generator and a branch for each possible return value it may have. It will be mighty hard to draw such a tree if it is to have infinitely (uncountably!) many branches, as it would be with “Unif([0, 1])”. Seriously, random processes with uncountably infinitely many different outcomes require significantly more complicated analysis, which it’s best to postpone for later.

You might be disappointed, since every standard programming language (claims to) have a random number generator like “Unif([0, 1])”. Of course, you also know that you can’t truly store a real number in a computer, so actually, these random number generators do something like returning

RandInt(2^64 )/ 264.

Indeed, as we will see in the second half of the class, it’s a good idea to think about analyzing “Unif([0, 1])” in precisely this way — except we use “dx” rather than 2−^64...

2 Analyzing randomized code & Basic definitions

So we’ve decided we will model probabilistic stuff with randomized code; for example, something like the following:

flip1 ← Bernoulli( 1 / 2 )

flip2 ← Bernoulli( 1 / 3 )

This little block of code models a standard probability exercise beginning with, “You flip 2 coins, the first of which is fair, the second having bias 1/3 for heads... ” In mathematics, they make the following definition:

Definition: An experiment is a block of randomized code.

Here is how you analyze experiments/code: You build a “probability tree”, with branching for each call to a random number generator. For the above example:

Or, a probability exercise might begin, “An ophthalmology patient has a 5% chance of being color blind if male, and a .5% chance otherwise... ”’ We first write the code:

isMale ← Bernoulli( 1 / 2 ) if isMale, isColorBlind ← Bernoulli(.05) else isColorBlind ← Bernoulli(.005)

We then draw the probability tree (in which it is okay to use “meaningful” names rather than 0 and 1):

Definition: An outcome is a leaf in the probability tree of an experiment. Equivalently, it is a possible sequence of all values returned by the random number generator calls.

For example, here is the above tree with its outcomes labeled:

3.1 Example

Let’s do an example. Here is the beginning of a typical probability exercise:

Flip a fair coin. If it comes up heads, roll a 3 -sided die; if it comes up tails, roll a 4 -sided die.

(Yes, we will talk about d-sided dice. These exist in real life for certain values of d, most notably d = 6; in probability-land, they exist for all d. Actually, this just goes to show it’s simpler to talk about RandInt(d).)

The very first thing you should do is write, “We model this with the following code:

flip ← Bernoulli(1/2) if flip = 1, die ← RandInt(3) else die ← RandInt(4).”

Great. Now you have an unambiguous random experiment to analyze. The next thing you should do is write, “This gives the following probability tree:” And you should label the outcomes and their probabilities.

Note that the sample space here, Ω, has cardinality 7. We computed the probabilities of the outcomes by multiplying the probabilities along each path; e.g.:

Pr[(H, 1)] = (1/2) · (1/3) = 1/ 6 ,

Pr[(T, 3)] = (1/2) · (1/4) = 1/ 8

and so forth.

Now we go back and continue reading the exercise. Suppose it continues:

What is the probability that the die roll is at least 3?

Here is how you answer this problem. First, you write, “Let A be the event of rolling at least

  1. We have A = {(H, 3), (T, 3), (T, 4)}.”

It’s helpful to also circle this event on your probability tree. You then continue, “Therefore,

Pr[A] = Pr[(H, 3)] + Pr[(T, 3)] + Pr[(T, 4)] = 1/6 + 1/8 + 1/8 = 5/ 12. ”

Please memorize this fundamental process! It will be on your next quiz...

4 Basic facts

Here are some simple but important basic facts about the probabilities of events:

Facts:

  • A ⊆ B ⇒ Pr[A] ≤ Pr[B]
  • Pr[Ac] = 1 − Pr[A].
  • Pr[A ∪ B] = Pr[A] + Pr[B] − Pr[A ∩ B].

Here we have used some set theory notation: Ac^ denotes the complement of A, meaning Ω \ A, or “not A”; A ∪ B is the union of A and B, meaning “A or B”; A ∩ B is the intersection of A and B, meaning “A and B”. All of these facts are easy to prove, just by the definition of the probability of an event as the sum of the probabilities of the outcomes in it. The following diagram might be helpful; it shows a sample space Ω, with the outcomes as dots. Remember, each of these has a probability, and all the probabilities add up to 1. An event A is just a subset of outcomes, and its probability is the sum of their probabilities.

The third fact above is important to remember, in light of the common

FALLACY: Pr[A ∪ B] = Pr[A] + Pr[B].

Since Pr[A ∩ B] ≥ 0, though, we do have the following:

Fact: Pr[A ∪ B] ≤ Pr[A] + Pr[B].

More generally, we have the very trivial but extremely useful fact called the “Union Bound”:

Union Bound: Pr[A 1 ∪ A 2 ∪ · · · ∪ An] ≤

∑^ n

i=

Pr[Ai].

It may seem indulgent to give a bold-faced name to such a simple fact, but please remember its name; it really comes up over and over and over again.

What’s happened is that “conditioning on A” has effectively given us a new “probability distribu- tion” on the outcomes:

Ω = { (H, 1), (H, 2), (H, 3), (T, 1), (T, 2), (T, 3), (T, 4) } Pr[·] = { 0 , 0 , 25 , 0 , 0 , 103 , 103 } Now that we have new conditional probabilities for outcomes, we can define new conditional probabilities for events. For example, let B be the event of flipping a head:

B = {(H, 1), (H, 2), (H, 3)} ⊆ Ω.

Now just as before, we calculate the “probability of B given A” by adding up the (conditional) probabilities of the outcomes making up B:

Pr[B | A] = Pr[(H, 1) | A] + Pr[(H, 2) | A] + Pr[(H, 3) | A] = 0 + 0 +

You may find it slightly weird to think about things like “B | A”, namely, “the probability you flipped heads given that the roll was at least 3”, but you’ll get used to it :) If we’re interested in the probability that you flipped tails given that the roll was at least 3, there’s no need to add up conditional outcome probabilities: we can just use the “complement fact”:

Pr[tails | A] = Pr[Bc^ | A] = 1 − Pr[B | A] = 1 −

5.1 The official rules of conditioning

Having gone through an example, let’s now give the official rules and definitions for conditioning:

Definition: Given an event A ⊆ Ω with Pr[A] 6 = 0, define the conditional probability of outcome (AKA the probability of given A) to be

Pr[` | A] =

0 if 6 ∈ A, Pr[] Pr[A] if^ `^ ∈^ A. What about the conditional probability of an event? Well, Pr[B | A] is the sum of conditional probabilities of all outcomes in B. Hence we claim:

Corollary 1.

Pr[B | A] = Pr[B ∩ A] Pr[A]

Pr[A ∩ B] Pr[A]

Proof. The second equality is of course just because B ∩ A = A ∩ B. As for the first,

Pr[B | A] =

`∈B

Pr[` | A]

`∈B∩A

Pr[` | A] +

`∈B\A

Pr[` | A]

`∈B∩A

Pr[`] Pr[A]

`∈B\A

Pr[A]

`∈B∩A

Pr[`]

Pr[B ∩ A] Pr[A]

Of course, just by interchanging A and B, we have that (assuming Pr[B] 6 = 0)

Pr[A | B] =

Pr[A ∩ B] Pr[B]

If you’re going to memorize this formula, the important thing is that the event you’re conditioning on goes into the denominator on the right.

Let’s see these definitions in action one more time. Remember that we showed Pr[heads | roll ≥ 3] = 2/5. What about the reverse, Pr[roll ≥ 3 | heads]? Well, if you think about it, “obviously” the answer should be 1/3. Let’s check:

Pr[roll ≥ 3 | heads] = Pr[heads ∩ roll ≥ 3] Pr[heads]

= Pr[(H, 3)] Pr[(H, 1)] + Pr[(H, 2)] + Pr[(H, 3)]

=

One other corollary of the conditioning formula is the following:

Formula: Pr[A ∩ B] = Pr[A] · Pr[B | A] (assuming Pr[A] 6 = 0).

We often use this formula to calculate the probability of an intersection — i.e., the “and” of two events. We use the following phraseology: “For A and B to happen, first A has to happen (which has probability Pr[A]), and then also B has to happen given that A happened (which has probability Pr[B | A]).]]

The above formula generalizes to what is occasionally called the “Chain Rule” or the “Multi- plication Rule”:

Formulas:

Pr[A ∩ B ∩ C] = Pr[A] · Pr[B | A] · Pr[C | A ∩ B], and more generally,

Pr[A 1 ∩ A 2 ∩ · · · ∩ An] = Pr[A 1 ] · Pr[A 2 | A 1 ] · Pr[A 3 | A 1 ∩ A 2 ] · · · Pr[An | A 1 ∩ A 2 ∩ · · · ∩ An− 1 ]

(assuming all the conditioned-on events have nonzero probability). We leave the proof of this as an exercise for you.