Skip to content

Commit 3a19857

Browse files
committed
Merge branch 'master' of https://github.com/joanby/r-basic
# Conflicts: # teoria/Tema11.Rmd # teoria/Tema11.html
2 parents 4b4de60 + 34bd62a commit 3a19857

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

scripts/tema11/02-binomial.Rmd

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

scripts/tema11/02-binomial.pdf

176 KB
Binary file not shown.

scripts/tema11/03-geom.Rmd

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
title: "Geométrica"
3+
author: "Curso de Estadística Descriptiva"
4+
date: "4/2/2019"
5+
output: pdf_document
6+
---
7+
8+
```{r setup, include=FALSE}
9+
knitr::opts_chunk$set(echo = TRUE)
10+
```
11+
12+
## Función de densidad
13+
14+
Sea $X=Geom(p=0.1)$ la distribución que modela la probabilidad de intentar abrir una puerta hasta conseguirlo.
15+
16+
$$f(k) = (1-p)^{k-1}p$$
17+
18+
19+
## En `R`
20+
21+
```{r}
22+
library(Rlab)
23+
p = 0.1
24+
plot(0:20, dgeom(0:20, p))
25+
plot(0:20, pgeom(0:20, p), ylim = c(0,1))
26+
qgeom(0.5, p)
27+
qgeom(0.75, p)
28+
hist(rgeom(10000, p))
29+
```
30+
31+
## En Python
32+
```{python}
33+
from scipy.stats import geom
34+
import matplotlib.pyplot as plt
35+
import numpy as np
36+
37+
fig, ax = plt.subplots(1,1)
38+
p = 0.3
39+
mean, var, skew, kurt = geom.stats(p, moments = 'mvsk')
40+
print("Media %f"%mean)
41+
print("Varianza %f"%var)
42+
print("Sesgo %f"%skew)
43+
print("Curtosis %f"%kurt)
44+
45+
x = np.arange(geom.ppf(0.01,p), geom.ppf(0.99, p))
46+
ax.plot(x, geom.pmf(x, p), 'bo', ms = 8, label = "Función de probabilidad de Geom(0.3)")
47+
ax.vlines(x,0,geom.pmf(x,p), colors = 'b', lw = 4, alpha = 0.5)
48+
49+
rv = geom(p)
50+
ax.vlines(x,0,rv.pmf(x), colors = 'k', linestyles = '--', lw = 1, label = "Frozen PMF")
51+
ax.legend(loc = 'best')
52+
plt.show()
53+
54+
55+
fig, ax = plt.subplots(1,1)
56+
prob = geom.cdf(x,p)
57+
ax.plot(x, prob, 'bo', ms = 8, label = "Función de distribución acumulada")
58+
plt.show()
59+
60+
fig, ax = plt.subplots(1,1)
61+
r = geom.rvs(p, size = 10000)
62+
plt.hist(r)
63+
plt.show()
64+
```
65+

scripts/tema11/03-geom.pdf

211 KB
Binary file not shown.

0 commit comments

Comments
 (0)