### 4 DISKRETNI NAHODNE VELICINY
## 4.1 BINOMICKE ROZDELENI
# 4.1
N <- 'TO-DO'
n <- 0:N
m.obs <- c(3, 24, 104, 286, 670, 1033, 1343, 1112, 829, 478, 181, 45, 7)
M <- 'TO-DO'
p <- 'TO-DO'
(p <- round(p, 4))

# 4.2
m.exp <- round(dbinom(0:12, 'TO-DO', 'TO-DO') * 6115)
tab <- data.frame(rbind(m.obs, m.exp))
names(tab) <- 0:12
tab

plot(0:12, m.obs, type = 'h', col = 'green', 
     xlab = 'pocet starsich sourozencu', ylab = 'absolutni cetnosti', las = 1)
points(0:12, m.obs, pch = 21, col = 'darkred', bg = 'green')
lines (0:12, m.exp, type = 'h', lty = 2, col = 'black')
'TO-DO' # body pro ocekavane cetnosti
legend('topright', pch = c(21, 21), col = c('darkgreen', 'black'), pt.bg = c('green', 'black'),
       legend = c('pozorovane', 'ocekavane'), bty = 'n')

# 4.3
# a
round('TO-DO'(9, 'TO-DO'), 4)

# b
round('TO-DO'(4, 'TO-DO'), 4)

# c
round(1 - pbinom(7, 'TO-DO'), 4)
#round(sum(dbinom(8 : 12, N, p)), 4)

# d
'TO-DO'

# 4.4
x <- 0:N
px <- 'TO-DO' # hustota
Fx <- 'TO-DO' # distribucni fce

plot(x, px, type = 'h', ylim = c(0, 0.25), xlab = 'x', ylab = 'p(x)', las = 1,
     main = 'N = 12, p = 0.5192')
points('TO-DO', 'TO-DO')

plot(x, Fx, xlab = 'x', type = 'n', xlim = c(-1, 13), ylim = c(0, 1), las = 1,
     ylab = 'F(x)', main = 'N = 12, p = 0.5192')
segments(x, Fx, x + 1, Fx)                 # vodorovne cary
arrows(0, 0, -1, 0, length = 0.1)          # dolni sipka
arrows(N, 1, 13, 1, length = 0.1)          # horni sipka
points(x, c(0, Fx[1:N]), pch = 21, col = 'black', bg = 'white') # prazdne body
points('TO-DO', 'TO-DO', pch = 19, col = 'red')       # plne body


## 4.2 POISSONOVO ROZDELENI
# 4.5
'TO-DO' # nacteni dat a odstraneni NA

prch <- 'TO-DO' # pocet sourozencu
N <- 'TO-DO' # pocet objektu
(lambda <- 'TO-DO') 

# 4.6
m.obs <- data.frame(table(prch))$Freq
m.exp <- round(c(dpois(0:8, 'TO-DO'), 1 - ppois(8, 'TO-DO')) * N)
'TO-DO' # sestaveni tabulky ocekavane vs. pozorovane

'TO-DO' # graf ocek. vs. pozorovane pocetnosti

# 4.7
'TO-DO'


# 4.8
x <- 'TO-DO'
px <- c(dpois(0:8, 'TO-DO'), 1 - sum(dpois(0:8, 'TO-DO')) ) # prvnich 8 + zbytek
Fx <- c(ppois(0:8, 'TO-DO'), 1) 

'TO-DO'('TO-DO', 'TO-DO', type = 'h', ylim = c(0, 0.45), xlab = 'x', ylab = 'p(x)', axes = F, 
     main = paste0('lambda = ', round(lambda, 4)))
'TO-DO' # vykresleni bodu
box(bty = 'o')
axis(1, 0:9, c(0:8, '9+'))
axis(2, las = 1)

'TO-DO'('TO-DO', 'TO-DO', type = 'n', xlim = c(-1, N + 1), ylim = c(0, 1), xlab = 'x', 
     ylab = 'F(x)', axes = F, main = paste0('lambda = ', round(lambda, 4)))
box(bty = 'o')
axis(1, -1:9, c(-1:8, '9+'))
axis(2, las = 1)
'TO-DO'(x, Fx, x + 1, Fx)                    # vodorovne cary
arrows(0, 0, -1, 0, length = 0.1)             # dolni sipka
arrows(9, 1, 10, 1, length = 0.1)             # horni sipka
'TO-DO' # vykresleni plnych a prazdnych bodu
