@@ -62,3 +62,95 @@ linewidth = 2.0, color = "red")
6262plt.show()
6363```
6464
65+ ### Medidas de dispersión
66+ - Rango de ` mpg ` , five nums, cuartiles
67+ ``` {python}
68+ from ggplot import mtcars
69+
70+ rang = max(mtcars["mpg"]) - min(mtcars["mpg"])
71+ print(rang)
72+
73+ five_nums = [mtcars["mpg"].quantile(0),
74+ mtcars["mpg"].quantile(0.25),
75+ mtcars["mpg"].quantile(0.5),
76+ mtcars["mpg"].quantile(0.75),
77+ mtcars["mpg"].quantile(1.0)
78+ ]
79+ print(five_nums)
80+
81+ print(mtcars["mpg"].describe())
82+
83+ print(mtcars["mpg"].quantile(0.75) - mtcars["mpg"].quantile(0.25))
84+
85+ import matplotlib.pyplot as plt
86+ plt.clf()
87+ mtcars.boxplot(column = "mpg", return_type = "axes", figsize = (10,10))
88+
89+ plt.text(x=0.8, y = mtcars["mpg"].quantile(0.25), s = "1r cuartil")
90+ plt.text(x=0.8, y = mtcars["mpg"].quantile(0.5), s = "Mediana")
91+ plt.text(x=0.8, y = mtcars["mpg"].quantile(0.75), s = "3r cuartil")
92+
93+ plt.text(x=0.9, y = mtcars["mpg"].quantile(0), s = "Min")
94+ plt.text(x=0.9, y = mtcars["mpg"].quantile(1), s = "Max")
95+
96+ plt.text(x = 0.7, y = mtcars["mpg"].quantile(0.5), s = "RIC", rotation = 90, size = 25)
97+ plt.show()
98+ ```
99+
100+ - Varianza y desviación típica
101+
102+ ``` {python}
103+ from ggplot import mtcars
104+
105+ print(mtcars["mpg"].var())
106+ print(mtcars["mpg"].std())
107+
108+ mad = abs(mtcars["mpg"]-mtcars["mpg"].median())
109+ k = 1.4826
110+ print(mad.median()*k)
111+ ```
112+
113+ - El sesgo y la curtosis
114+
115+ ``` {python}
116+ from ggplot import mtcars
117+
118+ print(mtcars["mpg"].skew())
119+ print(mtcars["mpg"].kurt())
120+ ```
121+
122+
123+ ``` {python}
124+ import numpy as np
125+ import pandas as pd
126+ import matplotlib.pyplot as plt
127+
128+ norm = np.random.normal(size=100000)
129+ skew = np.concatenate((np.random.normal(size=35000)+2,
130+ np.random.exponential(size=65000)),
131+ axis = 0)
132+ unif = np.random.uniform(-2,2,size = 100000)
133+ peak = np.concatenate((np.random.exponential(size=50000),
134+ np.random.exponential(size=50000)*(-1)),
135+ axis = 0)
136+
137+
138+ data = pd.DataFrame({
139+ "normal": norm,
140+ "skew": skew,
141+ "unif": unif,
142+ "peak": peak
143+ })
144+
145+ plt.clf()
146+ data.plot(kind="density", figsize = (10,10), xlim = (-5,5))
147+ plt.show()
148+
149+ print("Normal, Sesgo = %f, Curtosis = %f"%(data["normal"].skew(), data["normal"].kurt()))
150+ print("Normal+Exp, Sesgo = %f, Curtosis = %f"%(data["skew"].skew(), data["skew"].kurt()))
151+ print("Uniforme, Sesgo = %f, Curtosis = %f"%(data["unif"].skew(), data["unif"].kurt()))
152+ print("Suma de Exp, Sesgo = %f, Curtosis = %f"%(data["peak"].skew(), data["peak"].kurt()))
153+ ```
154+
155+
156+
0 commit comments