forked from joanby/r-basic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-binomial.Rmd
More file actions
66 lines (48 loc) · 1.22 KB
/
Copy path02-binomial.Rmd
File metadata and controls
66 lines (48 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
---
title: "Binomial"
author: "Curso de Estadística Descriptiva"
date: "4/2/2019"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Función de densidad
Sea $X = B(n = 30, p = 0.6)$,
TODO: escribir la FDens y la FDistr
## En `R`
```{r}
library(Rlab)
n = 30
p = 0.6
plot(0:n, dbinom(0:n, size = n, prob = p))
plot(0:n, pbinom(0:n, size = n, prob = p))
qbinom(0.5, n, p)
qbinom(0.25, n, p)
hist(rbinom(100000, n, p), breaks = 0:30)
```
## En Python
```{python}
from scipy.stats import binom
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(1,1)
n = 7
p = 0.4
mean, var, skew, kurt = binom.stats(n, p, moments = 'mvsk')
print("Media %f"%mean)
print("Varianza %f"%var)
print("Sesgo %f"%skew)
print("Curtosis %f"%kurt)
x = np.arange(0, n+1)
ax.plot(x, binom.pmf(x, n, p), 'bo', ms = 8, label = "Función de densidad de B(7,0.4)")
ax.vlines(x, 0, binom.pmf(x,n,p), colors = 'b', lw = 4, alpha = 0.5)
rv = binom(n,p)
ax.vlines(x,0, rv.pmf(x), colors = 'k', linestyles='--', lw = 1, label = "Distribución teórica")
ax.legend(loc = 'best', frameon = False)
plt.show()
fix, ax = plt.subplots(1,1)
r = binom.rvs(n, p, size = 10000)
ax.hist(r, bins = n)
plt.show()
```