I have been normalizing vectors for my work and there are generally two methods that I have been following. I assumed both the methods are equivalent until I found out they are not. The two methods are described below.
- Take the square of the norm of the vector and divide this value by its length. To normalize, divide the vector by the square root of the above obtained value. This corresponds to the operation given below for any vector $\mathbf{x}$. $$ E_{\mathbf{x}} = \frac{||\mathbf{x}||^2}{|x|}\hspace{2mm} \text{and}\hspace{2mm} \hat{\mathbf{x}} = \frac{\mathbf{x}}{\sqrt{E_{\mathbf{x}}}} $$ $|\cdot|$ refers to dimension of the argument.
- Use the variance function in the math library and divide the vector by the square root of its variance. Equivalent operations for vector $\textbf{x}$ are outlined below. $$ \text{var}_{\textbf{x}} = \mathbb{E}(\textbf{x} - \mathbb{E}\textbf{x})^2 \hspace{2mm} \text{and}\hspace{2mm} \hat{\mathbf{x}} = \frac{\mathbf{x}}{\sqrt{\text{var}_{\mathbf{x}}}} $$
The reason I believe both are equivalent is because in the norm case, we assume the origin to be at $\textbf{0}$ and we add the square of the distances from origin to each entry in the vector. In the variance case we move the origin to the mean of the random variable and then add the square of the distances taking the mean as origin.
Now I try to implement these two in python and following are the results.
import numpy as np a = np.random.randn(1000) np.linalg.norm(a) ** 2 / 1000 1.006560252222734 np.var(a) 1.003290114164144 In these lines of code I generate 1000 length standard normal samples. Method 1 and method 2 give me equal values in this case. However when my samples have correlation, this is not the case.
import numpy as np cov_matrix = 0.3*np.abs(1-np.eye(1000)) + np.eye(1000) a = np.random.multivariate_normal(mean=np.zeros(1000), cov=cov_matrix) np.linalg.norm(a) ** 2 / 1000 1.036685431728734 np.var(a) 0.6900743017090415 I generate 1000 length multivariate normal random vector having covariance matrix with 1's along diagonals and 0.3 in all other off-diagonal entries. This is where I am confused. Method 1 and method 2 return different values.
Why is this the case? Why do both methods return same values in i.i.d. case and different values when the vector is correlated? Thanks.