statistics, data analysis, and simulation – ss 2017 · 1946 john von neumann schlägt die...

36
Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 1/1 Mainz, 11. Mai 2017 Statistics, Data Analysis, and Simulation – SS 2017 08.128.730 Statistik, Datenanalyse und Simulation Dr. Michael O. Distler <[email protected]>

Upload: dodiep

Post on 20-Aug-2019

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 1 / 1

Mainz, 11. Mai 2017

Statistics, Data Analysis, andSimulation – SS 2017

08.128.730 Statistik, Datenanalyse undSimulation

Dr. Michael O. Distler<[email protected]>

Page 2: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

2. Zufallszahlen

2.1 Warum Zufallszahlen:

SimulationenStichprobenentnahmeNumerische AnalysenProgrammerstellung (Computer)EntscheidungsfindungKryptographieÄsthetikFreizeitaktivitäten (Computerspiele)

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 2 / 1

Page 3: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Zahlendarstellung

unsigned integernibble 4 bit 0 . . . 15

byte, char 8 bit 0 . . . 255short word 16 bit 0 . . . 65 535

word, int 32 bit 0 . . . 4 294 967 295long word 64 bit 0 . . . 1.8446744 · 1019

signed integernibble 4 bit −8 . . . 7

byte, char 8 bit −128 . . . 127short word 16 bit −32 768 . . . 32 767

word, int 32 bit −2 147 483 648 . . . 2 147 483 647floating-point format

float (32-bit)double (64-bit)

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 3 / 1

Page 4: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Die Gleitkommadarstellung

sign exponent (8 bits) fraction (23 bits)

02331

0 0 1 1 1 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 = 0.1562530 22 (bit index)

Einfache Genauigkeit Doppelte Genauigkeitsingle precision double precision

width 32 bits 64 bitsexponent 8-bit 11-bit

fraction 23-bit 52-bitfurthest from zero ±3.403 · 1038 ±1.798 · 10308

closest to zero ±1.175 · 10−38 ±2.225 · 10−308

denormalized ±1.401 · 10−45 ±4.941 · 10−324

gap ±1.192 · 10−7 ±2.220 · 10−16

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 4 / 1

Page 5: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

2.3 Zufallszahlengeneratoren

1927 L.H.C. Tippett veröffentlicht 40.000 zufällige Ziffern„die zufällig Volkszählungsberichten entnommen wurden“

1939 M.G. Kendall und B. Babington-Smith erzeugeneine Tabelle mit 100.000 zufälligen Ziffernmit Hilfe eines mechanischen Zufallszahlengenerators

1946 John von Neumann schlägt die “middle-square” Methode vor.Seine Idee ist, eine vorherige Zufallszahl zu quadrierenund die mittleren Ziffern zu extrahieren.Z.B. wenn wir 10-stellige Zahlen erzeugen wollen:rj+1 = (r2

j div 100,000) mod 10,000,000,000r0 = 5772156649, r2

0 = 33317 7923805949︸ ︷︷ ︸r1=7923805949

09201

Von Neumanns ursprüngliche “middle-square” Methode hat sich alsvergleichsweise schlechte Quelle von Zufallszahlen erwiesen. DieGefahr ist, das die Zahlenfolge sich in einem kurzen Zyklus sichwiederholender Elemente festfährt. Sobald etwa die Null in derSequenz auftaucht, würde diese ständig wiederholt werden.

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 5 / 1

Page 6: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Random Number Generators

Ein anderer offensichtlicher Einwand gegen die“middle-square” Methode ist: Wie kann eine Sequenzüberhaupt zufällig sein, wenn jede Zahl durch ihre Vorgängerindeterminiert ist? Die Antwort darauf ist, dass die Sequenz nichtzufällig ist, aber so erscheint.Sequenzen, die auf eine deterministische Weise erzeugtwerden, werden in der Literatur als Pseudo-Zufallszahlen oderQuasi-Zufallszahlen bezeichnet. Wir wollen hier aber einefeinere Unterscheidung treffen.

Pseudo-Zufallszahlen sollten die gleichen statistischenTests bestehen wie echte Zufallszahlen.Quasi-Zufallszahlen oder Sequenzen können wie echteZufallszahlen transformiert werden - eignen sich jedochausschließlich für die Monte-Carlo Integration.

Xj+1 = f (Xj ,Xj−1, . . . ,X1)

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 6 / 1

Page 7: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

2.3.1 Allgemeiner Kongruenzgenerator

Ein Kongruenzgenerator wird durch folgende Parameterdefiniert:

Anzahl n ∈ N+ der ZustandswerteModul (modulus) m ∈ {2,3,4, . . .}Faktoren (multiplier) a1, . . . ,an ∈ {0, . . . ,m − 1} mit an > 0Inkrement (increment) b ∈ {0, . . . ,m − 1}Startwerte y1, . . . , yn ∈ {0, . . . ,m − 1}(nicht alle 0, wenn b = 0

Für i > n setzt man nun

yi =

((n∑

k=1

akyi−k

)+ b

)mod m.

Dabei bezeichnet mod den Divisionsrest.Die so berechneten yi werden als Zufallszahlen verwendet.https://de.wikipedia.org/wiki/Kongruenzgenerator

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 7 / 1

Page 8: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

2.3.2 Linearer Kongruenzgenerator

Linear Congruential Generator (LCG)Die heute mit Abstand am weitesten verbreitetenZufallszahlengeneratoren sind Spezialfälle eines Schemas, das 1949von D.H. Lehmer eingeführt wurde.Wir wählen vier „magische Zahlen“:

m, der Modul; 0 < m.a, der Faktor; 0 ≤ a < m.c, das Inkrement; 0 ≤ c < m.

X0, den Startwert; 0 ≤ X0 < m.Die gewünschte Sequenz von Zufallszahlen ergibt sich aus

Xn+1 = (a Xn + c) mod m, n ≥ 0

Für m = 10 and X0 = a = c = 7 erhält man

7,6,9,0,7,6,9,0, . . .

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 8 / 1

Page 9: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Linearer Kongruenzgenerator

Lineare Kongruenzgeneratoren erreichen nach dem Satz vonKnuth genau dann ihre maximal mögliche Periodenlänge m,wenn die folgenden Voraussetzungen erfüllt sind:

i) Das Inkrement c ist zum Modul m teilerfremd.ii) Jeder Primfaktor von m teilt auch a− 1iii) Wenn m durch 4 teilbar ist, dann auch a− 1

Beweis: siehe Donald E. Knuth, The Art of ComputerProgramming, Vol. 2

Beispiel: m = 16, a = 5, c = 3, X0 = 0:

Xn+1 = (5 Xn + 3) mod 16

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 9 / 1

Page 10: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

LCG Parameter in Verwendung

Source m a c output bitsNumerical Recipes 232 1664525 1013904223Borland C/C++ 232 22695477 1 bits 30..16 in rand(),

bits 30..0 in lrand()glibc (used by GCC) 231 1103515245 12345 bits 30..0ANSI C: Watcom, . . . 231 1103515245 12345 bits 30..16Borland Delphi, Virtual Pascal 232 134775813 1 bits 63..32 of (seed * L)Microsoft Visual/Quick C/C++ 232 214013 2531011 bits 30..16

(343FD16) (269EC316)Microsoft Visual Basic (≤v6) 224 1140671485 12820163

(43FD43FD16) (C39EC316)MMIX by Donald Knuth 264 6364136223846793005

1442695040888963407VAX’s MTH$RANDOM,old versions of glibc 232 69069 1Java’s java.util.Random 248 25214903917 11 bits 47...16LC53 in Forth 232 − 5 232 - 333333333 0

Source: Wikipedia

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 10 / 1

Page 11: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

2.3.3 Der multiplikative Kongruenzgenerator

multiplicative congruential generatorSetzt man c = 0, so erhält man den multiplikativeKongruenzgenerator.

Xn+1 = (a Xn) mod m

Vorteil: Schnellerer AlgorithmusNachteil: Keine Null, kürzere Periodenlänge

Der Satz von Carmichael besagt: Bei gegebenem m ist seinePeriodenlänge genau dann maximal, wenn gilt:

y1 ist zu m teilerfremd,a ist ein primitives Element modulo m.

https://de.wikipedia.org/wiki/Primitivwurzel

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 11 / 1

Page 12: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

2.3.4 Fibonacci-Generator

Ein Fibonacci-Generator ist ebenfalls ein Kongruenzgenerator(mit n = 2, b = 0, a1 = a2 = 1) und besteht aus folgendenKomponenten:

Modul m ∈ {2,3,4, . . .}Startwerte y1, y2 ∈ {0, . . . ,m − 1}

Es sollte ggT(m, y1, y2) = 1 sein.Mit folgender Bildungsregel werden die Pseudozufallszahlenerzeugt:

yi = (yi−1 + yi−2) mod m (mit i ≥ 3)

Nachteil: Eine Eigenschaft ist, dass die Fälle yi−1 < yi+1 < yibzw. yi < yi+1 < yi−1 nie auftreten. Fibonacci-Generatoren(allein) sind daher als Pseudozufallszahlengeneratoren weniggeeignet.

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 12 / 1

Page 13: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

2.3.5 Verzögerter Fibonacci-Generator

Das Prinzip des Fibonacci-Generators kann aberverallgemeinert werden, indem man nicht die beiden letzten,sondern weiter zurückliegende Zustandswerte yi zurErzeugung der neuen Zufallszahl verwendet. Dies ergibt einenverzögerten (engl. ’lagged’) Fibonacci-Generator:

yi = (yi−B + yi−A) mod m mit A,B ∈ N+, A > B; i > A

mit den Startwerten y1, . . . , yA ∈ {0, . . . ,m − 1}Dann ist also n = A und aA = aB = 1, die übrigen ak sind Null.Dabei wählt man in der Regel m und A und B so, dass dasPolynom xA + xB + 1 ein primitives Polynom modulo 2 ist. Dannbeträgt die Periodenlänge des Generators mindestens 2A − 1.Die folgende Tabelle gibt einige Wertepaare für A und B an, diediese Bedingung erfüllen:

A 2 31 55 73 98 100 135 258 607 3217 23209B 1 13 24 25 27 37 22 83 273 576 9739

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 13 / 1

Page 14: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Marsaglia KISS Zufallszahlengenerator

Das KISS-Prinzip: (englisch Keep it simple, stupid!)Der von George Marsaglia entwickelte Zufallszahlengeneratorkombiniert drei einfache Zufallszahlengeneratoren, die für sichgenommen praktisch keinen Test auf Zufälligkeit bestehen. DieKombination in Form des KISS-Generators besteht jedoch allestatistischen Tests aus dem BigCrush-Test derTestU01-Bibliothek.

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 14 / 1

Page 15: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Marsaglia KISS Zufallszahlengenerator#include <stdint.h>// interner Zustandstatic uint32_t x = 123456789; // <- beliebige seed != 0static uint32_t y = 362436000;static uint32_t z = 521288629;static uint32_t c = 7654321;

uint32_t KISS() {uint64_t t;x = 69069 * x + 12345; // Linearer Kongruenzgenerator

y ^= y << 13; // Xorshifty ^= y >> 17;y ^= y << 5;

t = 698769069ULL * z + c; // Multiply-with-carryc = t >> 32;z = (uint32_t) t;

return x + y + z;}

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 15 / 1

Page 16: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Deterministisches Chaos

Die logistische Gleichung wurde ursprünglich 1837 von PierreFrançois Verhulst als demographisches mathematischesModell eingeführt. Die Gleichung ist ein Beispiel dafür, wiekomplexes, chaotisches Verhalten aus einfachen nichtlinearenGleichungen entstehen kann.

xt = f · xt−1(1− xt−1); x0 = 0,5

Das logistische Modell berücksichtigt zwei Einflüsse:Durch Fortpflanzung vermehrt sich die Populationgeometrisch.Durch Verhungern verringert sich die Population. DieIndividuenzahl vermindert sich in Abhängigkeit von derDifferenz zwischen ihrer aktuellen Größe und einertheoretischen Maximalgröße.

Variation von f führt zum Feigenbaum-Diagramm

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 16 / 1

Page 17: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Zufallszahlen für beliebige Verteilungen

Die Aufgabe ist, Zufallszahlen xi gemäß einerWahrscheinlichkeitsdichte f (x) zu erzeugen −→ The inversetransform sampling methodVoraussetzung ist: Die Umkehrfunktion F−1(u) derVerteilungsfunktion existiert als analytische Funktion.

1 Wir generieren ein Zufallszahl u, die wir einerGleichverteilung auf dem Intervall [0,1] entnehmen.

2 Wir berechnen x , so dass F (x) = u gilt.

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 17 / 1

Page 18: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Inverse transform sampling

Der Generator liefert:

0 ≤ Xn < m ↪→ 0 ≤ Xn

m′< 1

Gleichverteilung: U(0,1)

Transformation:

f (x) dx = U(0,1) du

Verteilungsfunktion:∫ x

−∞f (t) dt = F (x) = u

x = F (−1)(u)

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 18 / 1

Page 19: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Acceptance-Rejection Method

Suppose we want to generate samples from a density f definedon some set X.

Let g be a density on X from which we know how to generatesamples and with the property that

f (x) ≤ cg(x)

for some constant c.

1 generate X from distribution g.2 generate U from U(0,1).3 If (U ≤ f (X)

cg(x))

return Xotherwise

go to Step 1.The acceptance-rejection method for sampling from density fuses candidates from density g.

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 19 / 1

Page 20: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

press any key

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 20 / 1

Page 21: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

2.4. Qualität von Generatoren

2.4.1 SpektraltestBilde Paare aus benachbarten Zahlen

(xj , xj+1) j = 0,1, . . . ,n − 1

Darstellung als Punkte in einem 2dim kartesischenKoordinatensystem:

a = 3, m = 7 : 1,3,2,6,4,5,1, . . .

(1,3), (3,2), (2,6), (6,4), (4,5), (5,1)

Punkte eines MLCG bilden regelmäßiges Gitter.

Warum? Im Wertebereich 0 ≤ xj < m gibt es m2 Zahlenpaare.MLCG liefert aber nur m − 1 Zahlenpaare

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 21 / 1

Page 22: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Beispiele:

0

1

2

3

4

5

6

0 1 2 3 4 5 6

x i+

1

xi

Spektraltest

a=3 m=7

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 22 / 1

Page 23: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Beispiele:

0

10

20

30

40

50

60

70

80

90

0 10 20 30 40 50 60 70 80 90

x i+

1

xi

Spektraltest

a=29 m=97

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 23 / 1

Page 24: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Beispiele:

0

10

20

30

40

50

60

70

80

90

0 10 20 30 40 50 60 70 80 90

x i+

1

xi

Spektraltest

a=23 m=97

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 24 / 1

Page 25: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Beispiele:

0

0.2

0.4

0.6

0.8

1

0 0.2 0.4 0.6 0.8 1

x i+

1

xi

Spektraltest

a=29 m=97

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 25 / 1

Page 26: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Beispiele:

0

0.2

0.4

0.6

0.8

1

0 0.2 0.4 0.6 0.8 1

x i+

1

xi

Spektraltest

a=23 m=97

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 26 / 1

Page 27: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Umrechnung auf Gitter 0 ≤ xjm < 1.

Voll besetztes Gitter hat Linienabstand d = 1m

Unser Gitter hat bei gleichmäßiger Verteilung bestenfalls:

d ≈ m−1/2 für 2 Dimensionen

Ungleichmäßige Abstände:

d � m−1/2

Theoretische Überlegungen liefern obere Grenzen für diekleinstmöglichen Gitterabstände in t Dimensionen:

dt ≥ d∗t = ct m−1/t

c2 = 4√

3/4, c3 = 6√

1/2, c4 = 4√

1/2, c5 = 2−0.3

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 27 / 1

Page 28: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

2.4.2 Test auf gleichmäßige Verteilung

Das Intervall [0,1] wird in k gleiche Unterintervalle derLänge 1/k unterteilt.N Zufallszahlen werden erzeugt.Ni fallen in das Unterintervall i .∑

Ni = N, 〈Ni〉 = Nk ,

k∑i=1

(Ni − N/k)2

N/k= χ2

sollte einer χ2-Verteilung mit (k − 1) Freiheitsgradenfolgen.

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 28 / 1

Page 29: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

2.4.3 Sequenz-(up-down-)Test

Vergleiche xi und xi+1

Erzeuge Bitfolge mit{

1 für xi < xi+10 für xi > xi+1

Zähle die Folgen von Nullen und Einser der Länge k : N(k)N∑

k=1

k · N(k) = N für N + 1 Zufallszahlen

Für unkorrelierte Zufallszahlen erwartet man:N(1) = 5N+1

12 N(2) = 11N−1460 N(3) = 19N−47

360

N(k) = (k2+3k+1)N−(k3+3k2−k−4)(k+3)!/2

0 3 2 13 4 7 6 1 8 11 10 5 12 15 14 9 01 0 1 0 1 0 0 1 1 0 0 1 1 0 0 01 1 1 1 1 2 2 2 2 3

N(1) = 5 (6.75) N(2) = 4 (2.75) N(3) = 1 (0.58)

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 29 / 1

Page 30: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

2.4.4 Random Walk-Test

Wähle ein kleine Zahl 0 < α� 1.Bilde eine große Zahl von Zufallszahlen und registriere dieZahl r der Fälle, in denen eine Zufallszahl kleiner αerscheint.Man erwartet eine Binomialverteilung für r mit p = α.Diese Test sollte auch gemacht werden für Zufallszahlen,die größer als (1− α) sind.

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 30 / 1

Page 31: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

2.4.5 Lücken-(gap)Test

Wähle zwei Zahlen 0 ≤ α < β ≤ 1.Erzeuge (r + 1) Zufallszahlen im Intervall [0,1].Die Wahrscheinlichkeit, dass die ersten r Zahlenausserhalb des Intervalls (α, β) liegen und die (r + 1)steinnerhalb, sollte sein:

Pr = p (1− p)r

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 31 / 1

Page 32: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

2.4.6 Collision-Test

Teile das Intervall [0,1) in d gleiche Segmente.Teile entsprechend [0,1)t in k = d t Hyperkuben.Erzeuge n zufällige Punkte in [0,1)t .Wir definieren eine neue Zufallsvariable C, in dem wirzählen, wie oft wir eine Zahl in eine Hyperkubus füllen, derschon besetzt ist.Wir erwarten für C eine Poisson-Verteilung um denMittelwert:

λC =n2

2k(k groß)

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 32 / 1

Page 33: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

2.4.7 Birthday-Spacing-Test

Teile den Wertebereich in k gleich Intervalle (Hyperkuben).Definiere eine Ordnungsfunktion für die Zellen, damit fürdie gefüllten Zellen gilt: I(1) ≤ I(2) ≤ . . . ≤ I(n)Definiere den Abstand Sj = I(j+1) − I(j) j = 1, . . . ,n − 1.Die neue Zufallsvariable Y zählt die Fälle (Kollisionen), fürdie gilt: S(j+1) = S(j).Wir erwarten für Y eine Poisson-Verteilung um denMittelwert:

λY =n3

4k(k groß)

Der Name stammt von dem Geburtstagsparadoxon(n Personen, das Jahr hat k Tage).

http://www.iro.umontreal.ca/~lecuyer/myftp/papers/wsc01rng.pdf

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 33 / 1

Page 34: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Markov chain Monte CarloMetropolis-Hastings Algorithmus

Aufgabe: Bestimmen Sie Mittelwert, Varianz und Schiefe derWahrscheinlichkeitsdichte

p(x) = C exp(− 1

50(x − 3.2)(x − 2)(x − 1.1)(x + 0.9)(x + 2)(x + 3.3)

)C ≡ 0.0824759

mit einer MCMC Methode (Metropolis-Hastings Algorithmus).

-4 -2 2 4

0.1

0.2

0.3

0.4

Dazu erzeugen Sie eine Folge vonZahlen xt , t = 1, . . . ,N (Markovchain), die der Dichte p(x) folgen.Nach den Regeln der MC Integrationgilt dann für den Erwartungswert

〈A〉 ≈ 1N

N∑t=1

A(xt ).

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 34 / 1

Page 35: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Metropolis-Hastings Algorithmus1 Wählen Sie einen Startwert und legen Sie die Kettenlänge fest,

z.B. x1 = 1 und N = 10000.2 Wählen Sie eine Vorschlagsdichte q(x |x̄), z.B. die

Normalverteilung N (µ, σ2). Setzen Sie σ2 = 2. Je ähnlicher dieVorschlagsdichte der Dichte p(x) ist, desto besser funktioniertder Algorithmus.

3 Generieren Sie zufällig einen Vorschlag x∗ für xt+1 aus derVorschlagsdichte N (xt , σ

2).4 Generieren Sie eine gleichverteilte Zufallszahl u aus U(0,1).5 Der Vorschlag x∗ wird akzeptiert, falls:

p(x∗)

p(xt )

q(xt |x∗)

q(x∗|xt )≥ u

Für den Fall einer symmetrischen Vorschlagsdichte (hierNormalverteilung) gilt q(xt |x∗)/q(x∗|xt ) = 1.

6 Wird der Vorschlag nicht akzeptiert, setzen Sie xt+1 = xt .7 Fahren Sie mit Punkt ??. fort, bis die gewünschte Kettenlänge N

erreicht ist.Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 35 / 1

Page 36: Statistics, Data Analysis, and Simulation – SS 2017 · 1946 John von Neumann schlägt die “middle-square” Methode vor. Seine Idee ist, eine vorherige Zufallszahl zu quadrieren

Metropolis-Hastings Algorithmus

Mögliche Optimierungen:Wenn der Startwert ungünstig gewählt wurde, dannbenötigt der Algorithmus einige Zeit um einzuschwingen(sog. burn-in). In der Praxis werden die ersten 100 oder1000 Werte verworfen und so das "‘Gedächtnis"’ (d.h. dieAbhängigkeit vom Startwert) gelöscht.Variieren Sie die Varianz der Vorschlagsdichte, z.B.σ2 = 0.1, 2, 10 und betrachten Sie jeweils den Anteil derakzeptierten Vorschläge und plotten einen Ausschnitt von1000 Werten der Kette. In der Praxis bewährt sich einAnteil von 50%.Wählen Sie eine ganz andere Vorschlagsdichte, z.B.U(−2 + xt ,2 + xt ). Funktioniert der Algorithmus damit?

(siehe auch http://de.wikipedia.org/wiki/Metropolisalgorithmus)

Dr. Michael O. Distler <[email protected]> Statistics, Data Analysis, and Simulation – SS 2017 36 / 1