Expectation, Variance, and Covariance — Numerical Characteristics of Data
Expectation, Variance, and Covariance -- Numerical Characteristics of Data
Expectation = where the center is. Variance = how spread out it is. Covariance = how two variables are linked.
Concept Analysis
** Expectation E[X]
Weighted Average = "Long-term average result"
( \mathbb{E}[X] = \sum x_i P(x_i) )
** Variance Var(X)
Data dispersion
( \mathbb{E}[(X - \mu)^2] )
Covariance Cov(X,Y)
The correlation relationship between two variables
( \mathbb{E}[(X-\mu_X)(Y-\mu_Y)] )
Covariance matrix
The covariance matrix of a d-dimensional random vector is a d×d symmetric matrix: ( \Sigma_{ij} = \text{Cov}(X_i, X_j) ).
- Diagonal element = Variance of each feature
- Off-diagonal element = Covariance between features
Real-life example
**Average score of two classes
The average math score for Class A and Class B is 75. However, everyone in Class A scored between 70 and 80, while Class B had some students who scored 30 and some who scored 100.
The expected value is the same, but the variance is completely different—variance reveals the difference in "stability."
Python hands-on practice
Example
import
numpy
as
np
data
=
np.
random
.
normal
(
5
,
2
,
10000
)
(
f
"Mean={np.mean(data):.3f}, Variance={np.var(data):.3f}, Standard Deviation={np.std(data):.3f}"
)
Covariance
x
=
np.
random
.
randn
(
1000
)
y1
=
0.8
*x + np.
random
.
randn
(
1000
)
0.3
Positive correlation
y2
=
0.8
*x + np.
random
.
randn
(
1000
)
0.3
Negative correlation
(
f
"Cov(X, Y+): {np.cov(x,y1)[0,1]:.3f} (Positive)"
)
(
f
"Cov(X, Y-): {np.cov(x,y2)[0,1]:.3f} (Negative)"
)
Covariance matrix
data_3d
=
np.
random
.
randn
(
100
,
3
)
(
Covariance matrix:
,
np.
round
(
np.
cov
(
data_3d.
T
)
,
3
)
)
(
"Symmetric:"
,
np.
allclose
(
np.
cov
(
data_3d.
T
)
,
np.
cov
(
data_3d.
T
)
.
T
)
)
Output:
Original prompt (Chinese, left untranslated)
均值=4.990, 方差=4.008, 标准差=2.002
Cov(X, Y+): 0.846 (正)
Cov(X, Y-): -0.860 (负)
Covariance matrix:
[[ 1.03 0.035 0.033]
[ 0.035 0.89 -0.066]
[ 0.033 -0.066 0.933]]
对称: True
Application scenarios in AI
Mathematics of Batch Normalization
Calculate the mean $\mu_B$ and variance $\sigma^2_B$ for each mini-batch, and then normalize ($\hat{x} = (x - \mu_B) / \sqrt{\sigma^2_B + \epsilon}$). This uses two statistics: expectation (mean) and variance. ($\epsilon$) is a small constant to prevent division by zero (usually 1e-5).
PCA dimensionality reduction = Eigenvalue decomposition of the covariance matrix
The first step of PCA: Calculate the covariance matrix ($\Sigma$) (a $d\times d$ symmetric matrix) of the data. The second step: Perform eigenvalue decomposition on ($\Sigma$), and take the eigenvectors corresponding to the top $k$ largest eigenvalues as the principal component directions. The variance along these directions is maximized $\rightarrow$ retaining the most information.
Variance control for weight initialization
Kaiming initialization: ($\text{Var}(W) = 2/n_{in}$) (for ReLU networks) or Xavier: ($\text{Var}(W) = 2/(n_{in}+n_{out})$) (for tanh networks). Precisely controls the variance of the output of each layer, ensuring that the variance of activation values remains unchanged during the forward pass and the variance of gradients remains unchanged during the backward pass. Improper initialization can lead to vanishing/exploding gradients early in training.
Second moment estimation of the Adam optimizer
Adam maintains an exponentially moving average of the squared gradients ( v_t = \beta_2 v_{t-1} + (1-\beta_2)g_t^2 ). This is essentially an online estimation of the "variance of the gradient in each parameter direction." Directions with large variance (severe oscillation) are given a smaller learning rate, while directions with small variance (stable descent) are given a larger learning rate.