Estadistica Basica
## Objetivos de Aprendizaje
- Calcular estadisticas descriptivas
- Realizar analisis estadistico
- Usar funciones estadisticas integradas
- Analizar distribuciones de datos
## Estadisticas Descriptivas
### Tendencia Central
```matlab
data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
% Media (promedio)
mean(data) % 55
% Mediana (valor medio)
median(data) % 55
% Moda (mas frecuente)
mode(data) % 10 (primera ocurrencia si es multimodal)
```
### Variacion/Dispersion
```matlab
data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
% Desviacion estandar
std(data) % Desv. est. muestra (N-1)
std(data, 1) % Desv. est. poblacion (N)
% Varianza
var(data) % Varianza muestra (N-1)
var(data, 1) % Varianza poblacion (N)
% Rango
range(data) % 90 (max - min)
```
### Cuartiles y Percentiles
```matlab
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
% Cuartiles
q1 = quantile(data, 0.25) % 3.25
q2 = quantile(data, 0.50) % 5.5 (igual que mediana)
q3 = quantile(data, 0.75) % 7.75
% Usando prctile
p25 = prctile(data, 25) % 3.25
p50 = prctile(data, 50) % 5.5
p75 = prctile(data, 75) % 7.75
% RIC (Rango Intercuartil)
iqr(data) % 4.5
```
## Funciones Estadisticas
### Funciones Basicas
```matlab
data = [5, 10, 15, 20, 25];
min(data) % 5
max(data) % 25
sum(data) % 75
prod(data) % 312500 (5*10*15*20*25)
cumsum(data) % [5, 15, 30, 50, 75]
cumprod(data) % [5, 50, 750, 15000, 375000]
```
### Estadisticas Resumen
```matlab
data = randn(1000, 1) + 5; % Distribucion normal
% Todas las estadisticas basicas
summary = [mean(data), median(data), mode(data)', std(data), var(data)];
summary = [min(data), max(data), range(data)];
% Usando histfit para visualizacion
histfit(data)
```
## Numeros Aleatorios
### Generacion de Numeros Aleatorios
```matlab
% Aleatorio uniforme (0 a 1)
r = rand(1, 5); % [0.8147, 0.9058, ...]
% Aleatorio uniforme en rango [a, b]
r = a + (b-a) * rand(1, 5);
% Normal (Gaussiano)
r = randn(1, 5); % Media 0, Desv. Est. 1
% Normal con media y desv. est. especificas
r = mean + std * randn(1, 5);
% Enteros aleatorios
r = randi([1, 10], 1, 5); % Enteros aleatorios 1-10
```
### Muestreo Aleatorio
```matlab
data = 1:100;
% Permutacion aleatoria
perm = randperm(100); % Orden aleatorio 1-100
sample = data(perm(1:10)); % 10 muestras aleatorias
% Con reemplazo (bootstrap)
idx = randi(length(data), 1, 10);
sample = data(idx);
% Mezclar arreglo
shuffled = data(randperm(length(data)));
```
## Distribuciones
### Distribucion Normal
```matlab
% FDP (Funcion de Densidad de Probabilidad)
x = -3:0.1:3;
y = normpdf(x, 0, 1); % Media 0, Desv. Est. 1
% FDC (Funcion de Distribucion Acumulada)
p = normcdf(x, 0, 1);
% FDC Inversa (Funcion Cuantil)
x = norminv(0.975, 0, 1); % Percentil 97.5
% Numeros aleatorios de la normal
r = normrnd(0, 1, 100, 1);
```
### Funciones de Distribucion
| Funcion | Distribucion |
|---------|--------------|
| normpdf, normcdf, norminv, normrnd | Normal |
| exppdf, expcdf, expinv, exprnd | Exponencial |
| poisspdf, poisscdf, poissinv, poissrnd | Poisson |
| binopdf, binocdf, binoinv, binornd | Binomial |
| unifpdf, unifcdf, unifinv, unifrnd | Uniforme |
### Ajuste de Distribuciones
```matlab
% Ajustar distribucion normal a datos
data = normrnd(5, 2, 1000, 1);
pd = fitdist(data, 'Normal');
% Obtener parametros
mu = pd.mu % Media estimada
sigma = pd.sigma % Desv. est. estimada
% Bondad de ajuste
[h, p] = kstest(data); % Prueba de Kolmogorov-Smirnov
```
## Pruebas de Hipotesis
### Prueba t
```matlab
% Prueba t de una muestra (media = mu)
data = [10.2, 9.8, 10.1, 10.3, 9.9];
[h, p, ci] = ttest(data, 10);
% Prueba t de dos muestras
group1 = randn(100, 1) + 5;
group2 = randn(100, 1) + 6;
[h, p, ci] = ttest2(group1, group2);
```
### Otras Pruebas
```matlab
% Prueba chi-cuadrado
observed = [10, 20, 30];
expected = [15, 15, 30];
[h, p] = chisquare(observed, expected);
% Prueba de Kolmogorov-Smirnov
data = normrnd(0, 1, 100, 1);
[h, p] = kstest(data); % Probar si es normal
% ANOVA
group1 = randn(30, 1) + 5;
group2 = randn(30, 1) + 6;
group3 = randn(30, 1) + 7;
[p, tbl] = anova1([group1, group2, group3]);
```
## Correlacion y Covarianza
```matlab
x = 1:10;
y = 2 * x + randn(1, 10); % Relacion lineal
% Coeficiente de correlacion
r = corrcoef(x, y); % Matriz 2x2
r = r(1, 2); % Correlacion: ~0.99
% Covarianza
C = cov(x, y); % Matriz de covarianza 2x2
% Correlacion de Pearson (lineal)
[R, P] = corr(x', y'); % R=corr, P=valor-p
```
## Regresion
### Regresion Lineal Simple
```matlab
x = 1:10;
y = 2 * x + 5 + randn(1, 10);
% Ajustar modelo lineal
mdl = fitlm(x', y');
% Predicciones
y_pred = predict(mdl, x');
% Estadisticas
coef = mdl.Coefficients.Estimate; % [intercepto, pendiente]
rsq = mdl.Rsquared.Ordinary; % R-cuadrado
```
### Regresion Polinomial
```matlab
x = 0:0.1:5;
y = x.^2 - 2*x + 1 + randn(1, 51) * 0.5;
% Ajustar polinomio (grado 2)
p = polyfit(x, y, 2); % [1, -2, ~1]
% Evaluar
y_fit = polyval(p, x);
% Graficar
plot(x, y, 'o', x, y_fit, '-')
```
## Estadisticas Moviles
```matlab
data = randn(100, 1);
% Promedio movil (ventana = 5)
window = 5;
ma = conv(data, ones(window, 1)/window, 'same');
% Suma movil
ms = conv(data, ones(window, 1), 'same');
% Estadisticas acumuladas
cumsum(data)
cummax(data)
cummin(data)
```
## Segmentacion de Datos
```matlab
data = randn(1000, 1) * 10 + 50;
% Conteo del histograma
[n, edges] = hist(data, 20);
% Centros de bins
bin_centers = (edges(1:end-1) + edges(2:end)) / 2;
% Discretizar datos
bins = discretize(data, 0:10:100); % Segmentar en 0-10, 10-20, etc.
```
## Estadisticas Resumen por Grupo
```matlab
% Usando groupsummary (R2019b+)
data = table();
data.Value = randn(100, 1);
data.Group = categorical(randi(3, 100, 1));
% Resumen por grupo
summary = groupsummary(data, 'Group', {'mean', 'std', 'median'});
% Usando grpstats (mas antiguo)
stats = grpstats(data.Value, data.Group, 'mean');
```
## Pruebas de Normalidad
```matlab
data = randn(1000, 1); % Datos normales
% Prueba de Jarque-Bera
[h, p] = jbtest(data);
% Prueba de Kolmogorov-Smirnov
[h, p] = kstest(data);
% Prueba de Lilliefors
[h, p] = lillietest(data);
```
## Resumen
- `mean()`, `median()`, `mode()` para tendencia central
- `std()`, `var()`, `range()` para dispersion
- `quantile()`, `prctile()` para percentiles
- `rand()`, `randn()`, `randi()` para numeros aleatorios
- `fitdist()` para ajustar distribuciones a datos
- `ttest()`, `ttest2()` para pruebas de hipotesis
- `corrcoef()` para correlacion
- `fitlm()` para regresion lineal
- Usar operaciones vectorizadas para eficiencia
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →