Basic Statistics
## Learning Objectives
- Calculate descriptive statistics
- Perform statistical analysis
- Use built-in statistical functions
- Analyze data distributions
## Descriptive Statistics
### Central Tendency
```matlab
data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
% Mean (average)
mean(data) % 55
% Median (middle value)
median(data) % 55
% Mode (most frequent)
mode(data) % 10 (first occurrence if multimodal)
```
### Variation/Spread
```matlab
data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
% Standard deviation
std(data) % Sample std (N-1)
std(data, 1) % Population std (N)
% Variance
var(data) % Sample variance (N-1)
var(data, 1) % Population variance (N)
% Range
range(data) % 90 (max - min)
```
### Quartiles and Percentiles
```matlab
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
% Quartiles
q1 = quantile(data, 0.25) % 3.25
q2 = quantile(data, 0.50) % 5.5 (same as median)
q3 = quantile(data, 0.75) % 7.75
% Using prctile
p25 = prctile(data, 25) % 3.25
p50 = prctile(data, 50) % 5.5
p75 = prctile(data, 75) % 7.75
% IQR (Interquartile Range)
iqr(data) % 4.5
```
## Statistical Functions
### Basic Functions
```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]
```
### Summary Statistics
```matlab
data = randn(1000, 1) + 5; % Normal distribution
% All basic stats
summary = [mean(data), median(data), mode(data)', std(data), var(data)];
summary = [min(data), max(data), range(data)];
% Using histfit for visualization
histfit(data)
```
## Random Numbers
### Random Number Generation
```matlab
% Uniform random (0 to 1)
r = rand(1, 5); % [0.8147, 0.9058, ...]
% Uniform random in range [a, b]
r = a + (b-a) * rand(1, 5);
% Normal (Gaussian) random
r = randn(1, 5); % Mean 0, std 1
% Normal with specific mean and std
r = mean + std * randn(1, 5);
% Random integers
r = randi([1, 10], 1, 5); % Random integers 1-10
```
### Random Sampling
```matlab
data = 1:100;
% Random permutation
perm = randperm(100); % Random ordering 1-100
sample = data(perm(1:10)); % 10 random samples
% With replacement (bootstrap)
idx = randi(length(data), 1, 10);
sample = data(idx);
% Shuffle array
shuffled = data(randperm(length(data)));
```
## Distributions
### Normal Distribution
```matlab
% PDF (Probability Density Function)
x = -3:0.1:3;
y = normpdf(x, 0, 1); % Mean 0, Std 1
% CDF (Cumulative Distribution Function)
p = normcdf(x, 0, 1);
% Inverse CDF (Quantile function)
x = norminv(0.975, 0, 1); % 97.5th percentile
% Random numbers from normal
r = normrnd(0, 1, 100, 1);
```
### Distribution Functions
| Function | Distribution |
|----------|--------------|
| normpdf, normcdf, norminv, normrnd | Normal |
| exppdf, expcdf, expinv, exprnd | Exponential |
| poisspdf, poisscdf, poissinv, poissrnd | Poisson |
| binopdf, binocdf, binoinv, binornd | Binomial |
| unifpdf, unifcdf, unifinv, unifrnd | Uniform |
### Distribution Fitting
```matlab
% Fit normal distribution to data
data = normrnd(5, 2, 1000, 1);
pd = fitdist(data, 'Normal');
% Get parameters
mu = pd.mu % Estimated mean
sigma = pd.sigma % Estimated std
% Goodness of fit
[h, p] = kstest(data); % Kolmogorov-Smirnov test
```
## Hypothesis Testing
### t-Test
```matlab
% One-sample t-test (mean = mu)
data = [10.2, 9.8, 10.1, 10.3, 9.9];
[h, p, ci] = ttest(data, 10);
% Two-sample t-test
group1 = randn(100, 1) + 5;
group2 = randn(100, 1) + 6;
[h, p, ci] = ttest2(group1, group2);
```
### Other Tests
```matlab
% Chi-square test
observed = [10, 20, 30];
expected = [15, 15, 30];
[h, p] = chisquare(observed, expected);
% Kolmogorov-Smirnov test
data = normrnd(0, 1, 100, 1);
[h, p] = kstest(data); % Test if normal
% ANOVA
group1 = randn(30, 1) + 5;
group2 = randn(30, 1) + 6;
group3 = randn(30, 1) + 7;
[p, tbl] = anova1([group1, group2, group3]);
```
## Correlation and Covariance
```matlab
x = 1:10;
y = 2 * x + randn(1, 10); % Linear relationship
% Correlation coefficient
r = corrcoef(x, y); % 2x2 matrix
r = r(1, 2); % Correlation: ~0.99
% Covariance
C = cov(x, y); % 2x2 covariance matrix
% Pearson correlation (linear)
[R, P] = corr(x', y'); % R=corr, P=p-value
```
## Regression
### Simple Linear Regression
```matlab
x = 1:10;
y = 2 * x + 5 + randn(1, 10);
% Fit linear model
mdl = fitlm(x', y');
% Predictions
y_pred = predict(mdl, x');
% Statistics
coef = mdl.Coefficients.Estimate; % [intercept, slope]
rsq = mdl.Rsquared.Ordinary; % R-squared
```
### Polynomial Regression
```matlab
x = 0:0.1:5;
y = x.^2 - 2*x + 1 + randn(1, 51) * 0.5;
% Fit polynomial (degree 2)
p = polyfit(x, y, 2); % [1, -2, ~1]
% Evaluate
y_fit = polyval(p, x);
% Plot
plot(x, y, 'o', x, y_fit, '-')
```
## Moving Statistics
```matlab
data = randn(100, 1);
% Moving average (window = 5)
window = 5;
ma = conv(data, ones(window, 1)/window, 'same');
% Moving sum
ms = conv(data, ones(window, 1), 'same');
% Cumulative statistics
cumsum(data)
cummax(data)
cummin(data)
```
## Data Binning
```matlab
data = randn(1000, 1) * 10 + 50;
% Histogram counts
[n, edges] = hist(data, 20);
% Bin centers
bin_centers = (edges(1:end-1) + edges(2:end)) / 2;
% Discretize data
bins = discretize(data, 0:10:100); % Bin into 0-10, 10-20, etc.
```
## Summary Statistics by Group
```matlab
% Using groupsummary (R2019b+)
data = table();
data.Value = randn(100, 1);
data.Group = categorical(randi(3, 100, 1));
% Summary by group
summary = groupsummary(data, 'Group', {'mean', 'std', 'median'});
% Using grpstats (older)
stats = grpstats(data.Value, data.Group, 'mean');
```
## Normality Tests
```matlab
data = randn(1000, 1); % Normal data
% Jarque-Bera test
[h, p] = jbtest(data);
% Kolmogorov-Smirnov test
[h, p] = kstest(data);
% Lilliefors test
[h, p] = lillietest(data);
```
## Summary
- `mean()`, `median()`, `mode()` for central tendency
- `std()`, `var()`, `range()` for spread
- `quantile()`, `prctile()` for percentiles
- `rand()`, `randn()`, `randi()` for random numbers
- `fitdist()` to fit distributions to data
- `ttest()`, `ttest2()` for hypothesis testing
- `corrcoef()` for correlation
- `fitlm()` for linear regression
- Use vectorized operations for efficiency
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →