In [6]:
library(ggplot2)

options(repr.plot.width=4, repr.plot.height=4)
Registered S3 methods overwritten by 'ggplot2':
  method         from 
  [.quosures     rlang
  c.quosures     rlang
  print.quosures rlang

Der zentrale Grenzwertsatz

Seine X1,,Xn eine Folge von unabhängigen und identisch verteilten Zufallsvariablen, so dass Mittelwert μ und Standardabweichung σ definiert und endlich sind. Sei

Sn=X1++XnZn=Snnμσn

Dann gilt:

Φ(z)z12πex22dxlimnP(Znz)=Φ(z)

Vulgo: Die Summe von i.i.d. Zufallsvariablen kann durch eine Normalverteilung approximiert werden.

Illustration des zentralen Grenzwertsatzes

Für eine Standard-Gleichverteilung in [0,1] gilt:

  • μ=12
  • σ=36

Wenn x standard-gleichverteilt ist, hat 3 (2x1) Mittelwert 0 und Standardabweichung 1.

In [7]:
x <- replicate(10000, sum(replicate(1000, 
                                    sqrt(3)*(2*runif(1)-1)))/sqrt(1000))

ggplot(data.frame(x), aes(x=x)) + 
    geom_histogram(aes(y = ..density..), color='black', fill='white') + 
    geom_density(col='red')

(mean(x))
(sd(x))
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
-0.000167248423063552
0.998423718772677
In [8]:
qqnorm(x)
abline(0, 1, col='red')
In [9]:
ks.test(x, "pnorm")
	One-sample Kolmogorov-Smirnov test

data:  x
D = 0.0071074, p-value = 0.6933
alternative hypothesis: two-sided
In [10]:
shapiro.test(x[1:5000])
	Shapiro-Wilk normality test

data:  x[1:5000]
W = 0.99962, p-value = 0.4647
In [11]:
ks.test(runif(100), "pnorm")
	One-sample Kolmogorov-Smirnov test

data:  runif(100)
D = 0.50537, p-value < 2.2e-16
alternative hypothesis: two-sided

Simulation der Normalverteilung durch die Verwerfungsmethode

Für die Funktion g wähle ich

g(x)=ex(1+ex)2

In [12]:
xx <- seq(-5, 5, length.out=1000)
g <- function(x) exp(x)/(1+exp(x))^2
plot(xx, g(xx), type='l', xlab='x', ylab='g(x)',
    ylim=c(0, dnorm(0)))
points(xx, dnorm(xx), type='l', col='red')

Die Verteilungsfunktion G(x)=yg(x) ist die logistische Funktion

G(x)=11+ex

Deren Umkehrung G1 ist die Logit-Funktion

G1(p)=logp1p

Daher lässt sich durch die inverse Methode leicht eine g-verteilte Zufallsvariable generieren.

In [13]:
logit <- function(p) log(p/(1-p))
x <- replicate(100000, logit(runif(1)))

ggplot(data.frame(x), aes(x=x)) + 
    geom_histogram(aes(y = ..density..), color='black', fill='white') + 
    geom_density(col='red')

(mean(x))
(sd(x))
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
-0.00155928328575634
1.81268319549831
c=maxf(x)g(x)argmaxx f(x)g(x)=0c=f(0)g(0)=42π
In [14]:
C = 4/sqrt(2*pi)

f <- function(x) {
    return(1/sqrt(2*pi) * exp(-x^2/2))
}

rnormal <- function() {
    x <- logit(runif(1))
    u <- runif(1)
    weiter = TRUE
    while (weiter) {
        if (u > f(x)/(C*g(x))) {
            x <- logit(runif(1))
            u <- runif(1)
        } else {
            weiter = FALSE
        }
    }
    return(x)
}
In [15]:
x <- replicate(10000, rnormal())
ggplot(data.frame(x), aes(x=x)) + 
    geom_histogram(aes(y = ..density..), color='black', fill='white') + 
    geom_density(col='red')

(mean(x))
(sd(x))
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
0.000530496842727181
1.00466910968585
In [16]:
qqnorm(x)
abline(0, 1, col='red')
In [17]:
ks.test(x, "pnorm")
	One-sample Kolmogorov-Smirnov test

data:  x
D = 0.0045542, p-value = 0.9856
alternative hypothesis: two-sided
In [18]:
shapiro.test(x[1:500])
	Shapiro-Wilk normality test

data:  x[1:500]
W = 0.99623, p-value = 0.2845

Simulation der Normalverteilung durch die Polar-Methode

Box-Muller-Verfahren

Seien X und Y zwei unabhängige standard-normalverteilte Zufallsvariablen. R und Θ sind die entsprechenden polaren Koordinaten, also

R²=d=X2+Y2Θ=arctanYX

Sei d=R2=X2+Y2.

Die gemeinsame Dichte von X und Y ist

f(x,y)=12πex2212πex22=12πe(x2+y2)/2

Allgemeine Regel zum Wechsel von Variablen in Dichtefunktionen

Sei H eine differenzierbare bijektive Funktion zwischen Vektorräumen. Dann gilt

g(H(x))=f(x)|det(H1(y)y|y=H(x))|

Angewandt auf f ergibt das

g(d,Θ)=1212πed/2=12π12ed/2

Das ist das Produkt einer Gleichverteilung über Θ im Intervall [0,2π] und einer Exponentialverteilung über d mit Mittelwert 2.

Wenn man also eine exponential verteilte Zufallsvariable d mit Mittelwert 2 und eine im Interval [0,2π] gleichverteilte Zufallsvariable θ, generiert, sind die folgenden Variablen unabhängig und standard-normalverteilt:

x=dcosθy=dsinθ

Dieses Verfahren heißt Box-Muller-Methode.

Die kostspielige Berechnung der trigonometrischen Funktionen lässt sich vermeiden, indem man state Θ gleichverteilte Koordinaten (x,y) innerhalb des Einheitskreises simuliert.

In [14]:
plot(NULL, xlim=c(-1, 1), ylim=c(-1,1), asp=1)
for (i in 1:1000) {
    weiter <- TRUE
    while (weiter) {
        x <- 2*runif(1)-1
        y <- 2*runif(1)-1
        if ((x^2 + y^2) <= 1) {
            weiter <- FALSE
        }
    }
    points(x, y)
    points(x/sqrt(x^2+y^2), y/sqrt(x^2+y^2), col='red', cex=.1)
}

Aus den Regeln für das Wechseln von Variablen folgt, dass bei diesem Verfahren R2=x2+y2 und θ=arctanyx unabhängige, gleichverteilte Zufallsvariablen sind (über die Intervalle [0,1] und [0,2π]. Daher kann man R2=x2+y2 zur Simulierung der exponentiell verteilten Variable recyclen.

In [19]:
polarNormal <- function() {
    weiter <- TRUE
    while (weiter) {
        x <- 2*runif(1)-1
        y <- 2*runif(1)-1
        if ((x^2 + y^2) <= 1) {
            weiter <- FALSE
        }
    }
    R2 <- x^2+y^2
    output <- sqrt(-2*log(R2))*c(x, y)/sqrt(R2)
    return(output)
}
In [20]:
x <- as.vector(replicate(5000, polarNormal()))
ggplot(data.frame(x), aes(x=x)) + 
    geom_histogram(aes(y = ..density..), color='black', fill='white') + 
    geom_density(col='red')

(mean(x))
(sd(x))
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
0.00460925643101696
0.991379223246965
In [21]:
qqnorm(x)
abline(0, 1, col='red')
In [22]:
ks.test(x, "pnorm")
	One-sample Kolmogorov-Smirnov test

data:  x
D = 0.0078845, p-value = 0.563
alternative hypothesis: two-sided
In [23]:
shapiro.test(x[1:500])
	Shapiro-Wilk normality test

data:  x[1:500]
W = 0.99632, p-value = 0.3034

Normalverteilung: Allgemeiner Fall

Eine normal verteilte Zufallsvariable hat die Dichtefunktion

f(x)=12πσ2e(xμ)22σ2

Dabei sind μ der Mittelwert und σ die Standardabweichung.

Wenn X normalverteilt ist mit Parametern μ,σ, dann ist xμσ standard-normalverteilt.

Mit Hilfe dieses Zusammenhangs lassen sich standard-normalverteilte Zufallszahlen in beliebig parametrisierte normalverteilte Zufallszahlen transformieren.

In [28]:
mu <- -10
sigma <- 0.1

z <- as.vector(replicate(5000, polarNormal()))
x <- sigma*z + mu

ggplot(data.frame(x), aes(x=x)) + 
    geom_histogram(aes(y = ..density..), color='black', fill='white') + 
    geom_density(col='red')

(mean(x))
(sd(x))
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
-10.0006454894296
0.0994576663192223

Multivariate Verteilungen

Standard-Normalverteilung

Wahrscheinlichkeitsverteilung über relle Vektoren, mit der Dichte-Funktion:

f(x)=(2π)k2exTx2

(Das ist einfach das Produkt der Dichten von k univariaten Standard-Normalverteilungen.)

In [21]:
d <- data.frame(matrix(replicate(20000, polarNormal()), ncol=2))
colnames(d) <- c('x', 'y')

ggplot(d, aes(x=x, y=y)) + geom_point(alpha=.3) + geom_density_2d() + coord_fixed()

Sei

A=(4823)μ=(1,2)T,

und sei Z=(Z1,Z2) eine standard-normalverteilte bivariate Zufallsvariable. Dann ist

X=(AZT+μ)T
eine bivariate Zufallsvariable mit der Dichte
f(X)=12πdet(Σ)e12(XTμ)TΣ1(XTμ),
wobei
Σ=AAT

Im Beispiel:

Σ=(80323213)det(Σ)=16Σ1=116(13323280)

Σ heißt die Varianz-Kovarianz-Matrix der Verteilung. Es ist das multivariate Analog der univariaten Varianz σ2. Das Verhalten einer multivariaten Normalverteilung ist durch μ und Σ eindeutig bestimmt. Weiterhin ist für gegeben Varianz-Kovarianz-Matrix die multivariate Normalverteilung die Wahrscheinlichkeitsverteilung mit maximaler Entropie.

In [49]:
(A <- matrix(c(4, 8, 2, 3), nrow=2, byrow=T))
mu <- c(1, 2)
(Sigma <- A %*% t(A))
(detSigma <- det(Sigma))
(invSigma <- solve(Sigma))
A matrix: 2 × 2 of type dbl
48
23
A matrix: 2 × 2 of type dbl
8032
3213
15.9999999999999
A matrix: 2 × 2 of type dbl
0.8125-2
-2.0000 5

Multivariat standard-normalverteilte Zufallszahlen können durch Multiplikation mit A und Addition von μ leicht in beliebige Normalverteilungen transformiert werden.

In [50]:
z <- replicate(10000, polarNormal())
x <- data.frame(t((A %*% z) + mu ))
colnames(x) <- c('x1', 'x2')

apply(x, 2, mean)
var(x)
ggplot(x, aes(x=x1, y=x2)) + geom_point(alpha=.3) + geom_density_2d()
x1
0.965381974824931
x2
1.99227754566945
A matrix: 2 × 2 of type dbl
x1x2
x180.5501732.25831
x232.2583113.11769

Dekorrelation einer Varianz-Kovarianz-Matrix durch Cholesky-Zerlegung

Angenommen, wir kennen für eine multivariate Normalverteilung die Parameter μ und Σ. Wir möchten Zufallszahlen für diese Verteilung generieren.

Wenn wir eine Matrix A kennen mit der Eigenschaft Σ=AAT, können wir obige Methode verwenden, um aus standard-multivariat-normalverteilten Zufallszahlen die gewünschten Zufallszahlen zu gewinnen.

Wenn Σ positiv (semi-)definit ist, ist es immer möglich, eine solche Matrix A zu berechnen, dank des Theorems:

Wenn Σ eine symmetrische, positiv (semi-)definite Matrix ist, dann gibt es genau eine untere Dreiecksmatrix A, so dass Σ=AAT.

Diese Zerlegung heißt Cholesky-Zerlegung.

Für unser Beispiel gilt:

B=15(20081)

In [53]:
t(chol(Sigma))
A matrix: 2 × 2 of type dbl
8.9442720.0000000
3.5777090.4472136
In [54]:
(B = matrix(c(20, 0, 8, 1), nrow=2, byrow=T)/sqrt(5))
A matrix: 2 × 2 of type dbl
8.9442720.0000000
3.5777090.4472136
In [55]:
B %*% t(B)
A matrix: 2 × 2 of type dbl
8032
3213

Höherdimensionales Beispiel

In [56]:
Sigma <- matrix(c(4, 12, -16,
                 12, 37, -43,
                 -16, -43, 98), nrow=3)
mu <- c(1, 2, 3)

(A <- t(chol(Sigma)))
(A %*% t(A))
A matrix: 3 × 3 of type dbl
200
610
-853
A matrix: 3 × 3 of type dbl
4 12-16
12 37-43
-16-43 98
In [57]:
z <- matrix(replicate(3*1e5, polarNormal()), ncol=3)
x <- t(A %*% t(z) + mu)
In [58]:
apply(x, 2, mean)
cov(x)
  1. 1.00153633741356
  2. 2.00464446639114
  3. 2.99708596168605
A matrix: 3 × 3 of type dbl
4.016062 12.04149-16.09204
12.041491 37.10821-43.22419
-16.092035-43.22419 98.65080
In [ ]: