forked from joanby/r-basic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
652 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--- | ||
title: "Distribución Hipergeométrica" | ||
author: "Curso de Estadística Descriptiva" | ||
date: "7/2/2019" | ||
output: pdf_document | ||
--- | ||
|
||
```{r setup, include=FALSE} | ||
knitr::opts_chunk$set(echo = TRUE) | ||
``` | ||
|
||
## Distribución Hipergeométrica | ||
|
||
Supongamos que tenemos 20 animales, de los cuales 7 son perros. Queremos medir la probabilidad de encontrar un número determinado de perros si elegimos $k=12$ animales al azar. | ||
|
||
# En `R` | ||
|
||
```{r} | ||
library(Rlab) | ||
M = 7 | ||
N = 13 | ||
k = 12 | ||
dhyper(x = 0:12, m = M, n = N, k = k) | ||
phyper(q = 0:12, m = M, n = N, k = k) | ||
qhyper(p = 0.5, m = M, n = N, k = k) | ||
rhyper(nn = 1000, m = M, n = N, k = k) -> data | ||
hist(data, breaks = 8) | ||
``` | ||
|
||
|
||
## En `Python` | ||
```{python} | ||
from scipy.stats import hypergeom | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
[M, n, N] = [20, 7, 6] | ||
rv = hypergeom(M, n, N) | ||
x = np.arange(0, n+1) | ||
y = rv.pmf(x) | ||
mean, var, skew, kurt = rv.stats(moments = 'mvsk') | ||
print("Media %f"%mean) | ||
print("Varianza %f"%var) | ||
print("Sesgo %f"%skew) | ||
print("Curtosis %f"%kurt) | ||
fig = plt.figure() | ||
ax = fig.add_subplot(111) | ||
ax.plot(x, y, 'bo' ) | ||
ax.vlines(x,0,y, lw = 2, alpha = 0.5) | ||
ax.set_xlabel("Número de perros entre los 12 elegidos al azar") | ||
ax.set_ylabel("Distribución de probabilidad de H(13,7,12)") | ||
plt.show() | ||
``` | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
title: "Distribución de Poisson" | ||
author: "Curso de Estadística Descriptiva" | ||
date: "7/2/2019" | ||
output: html_document | ||
--- | ||
|
||
```{r setup, include=FALSE} | ||
knitr::opts_chunk$set(echo = TRUE) | ||
``` | ||
|
||
## Distribución de Poisson | ||
|
||
Supongamos que $X$ modela el número de errores por página que tiene un valor esperado $\lambda = 5$. | ||
|
||
## En `R` | ||
```{r} | ||
l = 5 | ||
plot(0:20, dpois(x = 0:20, lambda = l)) | ||
ppois(0:20, l) | ||
qpois(0.5, 5) | ||
rpois(1000, lambda = l) -> data | ||
hist(data) | ||
``` | ||
|
||
## En `Python` | ||
|
||
```{python} | ||
import numpy as np | ||
from scipy.stats import poisson | ||
import matplotlib.pyplot as plt | ||
fig, ax = plt.subplots(1,1) | ||
mu = 5 | ||
mean, var, skew, kurt = poisson.stats(mu, moments = 'mvsk') | ||
print("Media %f"%mean) | ||
print("Varianza %f"%var) | ||
print("Sesgo %f"%skew) | ||
print("Curtosis %f"%kurt) | ||
x = np.arange(0, 12) | ||
ax.plot(x, poisson.pmf(x, mu), 'bo', ms = 8, label = 'Poisson(0.8)') | ||
ax.vlines(x,0, poisson.pmf(x,mu), colors = 'b', lw = 4, alpha = 0.5) | ||
ax.legend(loc = "best", frameon = False) | ||
plt.show() | ||
``` | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
--- | ||
title: "Distribución Uniforme" | ||
author: "Curso de Estadística Descriptiva" | ||
date: "7/2/2019" | ||
output: pdf_document | ||
--- | ||
|
||
```{r setup, include=FALSE} | ||
knitr::opts_chunk$set(echo = TRUE) | ||
``` | ||
|
||
## Distribución Uniforme | ||
|
||
Supongamos que $X\sim U([0,1])$ entonces podemos estudiar sus parámetros | ||
|
||
## En `R` | ||
```{r} | ||
a = 0 | ||
b = 1 | ||
x = seq(-0.1, 1.1, 0.1) | ||
plot(x, dunif(x, min = a, max = b)) | ||
plot(x, punif(x, a, b), type = "l") | ||
qunif(0.5, a, b) | ||
runif(1000000, a, b) -> data | ||
hist(data) | ||
``` | ||
|
||
## En `Python` | ||
|
||
```{python} | ||
from scipy.stats import uniform | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
a = 0 | ||
b = 1 | ||
loc = a | ||
scale = b-a | ||
fig, ax = plt.subplots(1,1) | ||
rv = uniform(loc = loc, scale = scale) | ||
mean, var, skew, kurt = rv.stats(moments = 'mvsk') | ||
print("Media %f"%mean) | ||
print("Varianza %f"%var) | ||
print("Sesgo %f"%skew) | ||
print("Curtosis %f"%kurt) | ||
x = np.linspace(-0.1, 1.1, 120) | ||
ax.plot(x, rv.pdf(x), 'k-', lw = 2, label = "U(0,1)") | ||
r = rv.rvs(size = 100000) | ||
ax.hist(r, density = True, histtype = "stepfilled", alpha = 0.25) | ||
ax.legend(loc = 'best', frameon = False) | ||
plt.show() | ||
``` | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
title: "Distribución exponencial" | ||
author: "Curso de Estadística Descriptiva" | ||
date: "7/2/2019" | ||
output: pdf_document | ||
--- | ||
|
||
```{r setup, include=FALSE} | ||
knitr::opts_chunk$set(echo = TRUE) | ||
``` | ||
|
||
## En `Python` | ||
|
||
```{python} | ||
from scipy.stats import expon | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
fig, ax = plt.subplots(1,1) | ||
lam = 3 | ||
rv = expon(scale = 1/lam) | ||
mean, var, skew, kurt = rv.stats(moments = 'mvsk') | ||
print("Media %f"%mean) | ||
print("Varianza %f"%var) | ||
print("Sesgo %f"%skew) | ||
print("Curtosis %f"%kurt) | ||
x = np.linspace(0, 3, 1000) | ||
ax.plot(x, rv.pdf(x), 'r-', lw = 5, alpha = 0.6, label = "Exp(10)") | ||
r = rv.rvs(size = 100000) | ||
ax.hist(r, density = True, histtype = 'stepfilled', alpha = 0.2) | ||
ax.legend(loc = "best", frameon= False) | ||
plt.show() | ||
``` | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.