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
6 changed files
with
197 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--- | ||
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 = 7) | ||
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,65 @@ | ||
--- | ||
title: "Geométrica" | ||
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=Geom(p=0.1)$ la distribución que modela la probabilidad de intentar abrir una puerta hasta conseguirlo. | ||
|
||
$$f(k) = (1-p)^{k-1}p$$ | ||
|
||
|
||
## En `R` | ||
|
||
```{r} | ||
library(Rlab) | ||
p = 0.1 | ||
plot(0:20, dgeom(0:20, p)) | ||
plot(0:20, pgeom(0:20, p), ylim = c(0,1)) | ||
qgeom(0.5, p) | ||
qgeom(0.75, p) | ||
hist(rgeom(10000, p)) | ||
``` | ||
|
||
## En Python | ||
```{python} | ||
from scipy.stats import geom | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
fig, ax = plt.subplots(1,1) | ||
p = 0.3 | ||
mean, var, skew, kurt = geom.stats(p, moments = 'mvsk') | ||
print("Media %f"%mean) | ||
print("Varianza %f"%var) | ||
print("Sesgo %f"%skew) | ||
print("Curtosis %f"%kurt) | ||
x = np.arange(geom.ppf(0.01,p), geom.ppf(0.99, p)) | ||
ax.plot(x, geom.pmf(x, p), 'bo', ms = 8, label = "Función de probabilidad de Geom(0.3)") | ||
ax.vlines(x,0,geom.pmf(x,p), colors = 'b', lw = 4, alpha = 0.5) | ||
rv = geom(p) | ||
ax.vlines(x,0,rv.pmf(x), colors = 'k', linestyles = '--', lw = 1, label = "Frozen PMF") | ||
ax.legend(loc = 'best') | ||
plt.show() | ||
fig, ax = plt.subplots(1,1) | ||
prob = geom.cdf(x,p) | ||
ax.plot(x, prob, 'bo', ms = 8, label = "Función de distribución acumulada") | ||
plt.show() | ||
fig, ax = plt.subplots(1,1) | ||
r = geom.rvs(p, size = 10000) | ||
plt.hist(r) | ||
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.