Skip to content
Snippets Groups Projects
Commit 11111792 authored by Marc Sommerhalder's avatar Marc Sommerhalder
Browse files

Update solutions

parent 74223413
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:06419e14 tags:
# 8 - Diskrete Verteilungen
%% Cell type:markdown id:4b055420 tags:
## Übung 2. Empirische Verteilung
%% Cell type:code id:23ede07d tags:
``` R
x <- c(1, 4, 2, 2, 4, 3, 1, 2, 2, 2, 3, 4, 5, 1, 1, 2, 3, 4, 5, 5)
histogram <- hist(x, breaks=0.5:5.5, probability=TRUE)
histogram$counts <- cumsum(histogram$counts)
histogram$counts <- histogram$counts / max(histogram$counts)
plot(histogram)
```
%% Output
%% Cell type:code id:f9cf4327 tags:
``` R
histogram <- hist(x, breaks=0.5:5.5, plot=FALSE)
E <- sum(histogram$mids * histogram$density)
S <- sum(histogram$mids^2 * histogram$density) - E^2
print(c(E, S))
```
%% Output
[1] 2.80 1.86
%% Cell type:markdown id:3e18a91f tags:
## Übung 5. Binomialverteilung
%% Cell type:code id:55ce6ecd tags:
``` R
dbinom(0, 10, 0.05)
```
%% Output
0.598736939238379
0.598736939238379
%% Cell type:code id:15a4c19c tags:
``` R
dbinom(1, 10, 0.05)
```
%% Output
0.315124704862305
0.315124704862305
%% Cell type:code id:4202c5f8 tags:
``` R
dbinom(2, 10, 0.05)
```
%% Output
0.0746347985200195
0.0746347985200195
%% Cell type:code id:0f4191b2 tags:
``` R
1 - pbinom(2, 10, 0.05)
```
%% Output
0.0115035573792969
0.0115035573792969
%% Cell type:markdown id:97c95e85 tags:
## Übung 6. Poissonverteilung
%% Cell type:code id:10a26cd1 tags:
``` R
dpois(3, 5)
```
%% Output
0.140373895814281
0.140373895814281
%% Cell type:code id:93ac0221 tags:
``` R
1 - ppois(9, 5)
```
%% Output
0.0318280573062049
0.0318280573062049
%% Cell type:code id:5018483c tags:
``` R
padd <- function (a, b) {a + b - a * b}
padd(1 - ppois(9, 5), 1 - ppois(7, 4))
```
%% Output
0.081334189445324
0.081334189445324
%% Cell type:code id:fe96ddab tags:
``` R
1 - ppois(16, 9)
```
%% Output
0.0111059093775838
0.0111059093775838
%% Cell type:markdown id:43a530b0 tags:
## Übung 7. Hypergeometrische Verteilung
%% Cell type:code id:4f8102b9 tags:
``` R
k <- 0:6
p <- sapply(k, function (x) dhyper(x, 6, 42 - 6, 6))
dist <- data.frame(k, p)
dist
```
%% Output
A data.frame: 7 × 2
| k &lt;int&gt; | p &lt;dbl&gt; |
|---|---|
| 0 | 3.713060e-01 |
| 1 | 4.311941e-01 |
| 2 | 1.684352e-01 |
| 3 | 2.722185e-02 |
| 4 | 1.801446e-03 |
| 5 | 4.117591e-05 |
| 6 | 1.906292e-07 |
A data.frame: 7 × 2
\begin{tabular}{ll}
k & p\\
<int> & <dbl>\\
\hline
0 & 3.713060e-01\\
1 & 4.311941e-01\\
2 & 1.684352e-01\\
3 & 2.722185e-02\\
4 & 1.801446e-03\\
5 & 4.117591e-05\\
6 & 1.906292e-07\\
\end{tabular}
%% Cell type:code id:90922910 tags:
``` R
1 - phyper(2, 6, 42 - 6, 6)
sum(dist[3 <= dist$k & dist$k <= 6,]$p)
```
%% Output
0.0290646625691555
0.0290646625691555
0.0290646625691555
0.0290646625691555
%% Cell type:code id:522939ab tags:
``` R
phyper(5, 6, 42 - 6, 6)
sum(dist[0 <= dist$k & dist$k <= 5,]$p)
```
%% Output
0.999999809370798
0.999999809370798
0.999999809370797
0.999999809370797
%% Cell type:code id:3c04e5b7 tags:
``` R
phyper(4, 6, 42 - 6, 6) - phyper(0, 6, 42 - 6, 6)
sum(dist[1 <= dist$k & dist$k <= 3,]$p)
```
%% Output
0.628652598485718
0.628652598485718
0.626851152525093
0.626851152525093
%% Cell type:code id:bd0f4343 tags:
%% Cell type:markdown id:c4d34af2 tags:
## Übung 10. Empirische Verteilung
%% Cell type:code id:98c2f256 tags:
``` R
x <- c(
5, 6, 8, 10, 1, 2, 9, 10, 3, 4, 9, 5, 3, 9, 3, 5, 7,
6, 1, 1, 9, 8, 9, 6, 9, 4, 5, 8, 2, 4, 2, 5, 10, 2,
4, 5, 10, 3, 6, 3, 1, 8, 1, 3, 5, 5, 2, 10, 8, 10
)
histogram <- hist(x, breaks=0.5:10.5, plot=FALSE)
E <- sum(histogram$mids * histogram$density)
print(E)
```
%% Output
[1] 5.48
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment