30
Lab #2-4 Follow-Up: Further into Python

Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Embed Size (px)

Citation preview

Page 1: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Lab #2-4 Follow-Up:

Further into Python

Page 2: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Part 3: RandomNumbers

Page 3: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Part 3: “Random”Numbers

Page 4: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Part 3: Pseudo-Random

Numbers

Page 5: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Why Random Numbers?Traditional use: cryptography

http://www.diablotin.com/librairie/networking/puis/figs/puis_0604.gif

Page 6: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Why Random Numbers?Recall Monte Carlo Estimation: Simulate the behavior of a complicated system using random numbers chosen from a reasonable distribution.

http://knowledgediscovery.jp/assets_c/2011/12/histogram-thumb-800x450-11.png

Page 7: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Two Kinds of Randomness1) Chaos: A deterministic system that is so difficult

to predict that we can treat it as random

http://www.seas.harvard.edu/softmat/downloads/2005-06.pdf

2) True randomness: no underlying deterministic process

Page 8: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Is true randomness possible?

οὐδὲν χρῆμα μάτην γίνεται, ἀλλὰ πάντα ἐκ λόγου τε καὶ ὑπ’ ἀνάγκης

Leucippus(~ 480-420 BC)– Leucippus

Democritus( 486-370 BC)

Page 9: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Is true randomness possible?

Nothing occurs at random, but everything for a reason and by necessity.

Leucippus(~ 480-420 BC)– Leucippus

Democritus( 486-370 BC)

Page 10: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Why Does It Matter?

Page 11: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Is true randomness possible?

We may regard the present state of the universe as the effect of its past and the cause of its future. An intellect which at a certain moment would know all forces that set nature in motion, and all positions of all items of which nature is composed, if this intellect were also vast enough to submit these data to analysis, it would embrace in a single formula the movements of the greatest bodies of the universe and those of the tiniest atom; for such an intellect nothing would be uncertain and the future just like the past would be present before its eyes.

P.-S. Laplace (1749-1827)

Page 12: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Is true randomness possible?

The theory says a lot, but does not really bring us any closer to the secret of the “old one”. I, at any rate, am convinced that He does not throw dice.

A. Einstein (1879-1955)

Page 13: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Is true randomness possible?

E. Schrödinger (1887-1956)Copenhagen Interpretation: Yes

Page 14: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Quantum Weirdness and Consciousness

(“Free Will?”)

R. Penrose (1931-)http://www.quantumconsciousness.org/penrose-hameroff/orchor.html

Page 15: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Free Will Without Quantum Weirdness

H. Bergson (1859-1941)

Freedom is therefore a fact, and among the facts which we observe there is none clearer.

The truth is we change without ceasing...there is no essential difference between passing from one state to another and persisting in the same state. … Just because we close our eyes to the unceasing variation of every physical state, we are obliged when the change has become so formidable as to force itself on our attention, to speak as if a new state were placed alongside the previous one.

Page 16: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

H. Bergson (1859-1941)

Freedom as Flux

The truth is we change without ceasing...there is no essential difference between passing from one state to another and persisting in the same state. …

Heraclitus(~535-475 BC)

Πάντα ῥεῖ( All things flow )‒ Heraclitus

Page 17: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Two Models for Generating Random Numbers

Page 18: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Two Methods for Generating Random Numbers

Numerical “recipes”(Algorithms)

Special-purpose hardware (rare)

Page 19: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Generating Pseudo-Random Numbers

Linear Congruential Method:

• Start with arbitrary “seed” value x• Multiply x by another number a and add a third

number c• Divide by another number m and take the

remainder• Remainder is new x; repeat

xnew = (a xold + c) rem m

Page 20: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Generating Pseudo-Random Numbers

Linear Congruential Method:

• Start with arbitrary “seed” value x• Multiply x by another number a and add a third

number c• Divide by another number m and take the

remainder• Remainder is new x; repeat

xnew = (a xold + c) mod m

Page 21: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

# Generates N pseuedo-random numbers using the Linear # Congruential Methoddef lincongr(n):

# Arbitrary constantsA = 5C = 11 # should have no common divisor with AM = 7SEED = 0

x = SEED

for k in range(n):print(x)x = (A * x + C) % M

remainder (mod)

Page 22: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Output for N = 5

043512

def lincongr(n):

# Arbitrary constantsA = 5C = 11M = 7SEED = 0

x = SEED

for k in range(n):print(x)x = (A * x + C) % M

Page 23: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Output for N = 2004351204351204351204

def lincongr(n):

# Arbitrary constantsA = 5C = 11M = 7 # So this is the maximum!SEED = 0

x = SEED

for k in range(n):print(x)x = (A * x + C) % M

Page 24: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

http://en.wikipedia.org/wiki/Linear_congruential_generator

Page 25: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Yields a Uniform Distribution

Page 26: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

• Shape is given by i.e., something that reaches a peak at x = 0 and tapers off rapidly as x grows positive or negative:

Normal (Gaussian) Distributions2/2xe

• How can we build such a distribution from our uniformly-distributed random numbers?

Page 27: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

• Start with two uniform, independent random distributions:• a from 0 to 2p• r from 0 to 1

• Then compute • Obtain two normal distributions

• b sin(a) + m• b cos(a) + m

Box-Muller-Gauss Method forNormal Distributions with Mean m

And Standard Deviation s

)ln(rb

Page 28: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Exponential Distributions

• Common pattern is exponentially decaying PDF, also called 1/f noise (a.k.a. pink noise)• noise = random• f = frequency; i.e., larger events are less common • pink because uniform distribution is “white” (white

light = all frequencies)• “Universality” is a current

topic of controversy (Shalizi 2006)

Page 29: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Exponential Method for PDF |k|ekt

where t > 0, k < 0

• Start with uniform random r between 0 and 1

• Compute ln(r) / k• E.g., ln(r) / (-2) gives 1/f noise

Page 30: Lab #2-4 Follow-Up: Further into Python. Part 3: Random Numbers

Concluding Topics

• Where do e and ln come from ?

• The Black Swan!