Subtract the Mean: For each variable, subtract its mean. This centers the distribution at zero.
Divide by the Standard Deviation: Then, divide by its standard deviation. This scales the dispersion to a standard value—specifically, a variance of 1.
Mathematically, if you have a variable ( X ) with mean ( \mu ) and standard deviation ( \sigma ), the standardized variable ( Z ) is computed as:
[
Z = \frac{X - \mu}{\sigma}
]
After this transformation:
The mean of ( Z ) will be 0.
The variance of ( Z ) will be 1.
If you want to scale them to another specific variance (say, ( \sigma_0^2 )), you can multiply the z-scores by ( \sigma_0 ):
This way, all your variables, regardless of their original dispersion, will have the same variance.
Example in Python
Here's a quick Python example using NumPy:
python
import numpy as np
# Example variables
x = np.array([1, 2, 3, 4, 5])
y = np.array([10, 20, 30, 40, 50])
z = np.array([100, 200, 300, 400, 500])
def standardize(variable):
mean = np.mean(variable)
std = np.std(variable)
return (variable - mean) / std
x_standardized = standardize(x)
y_standardized = standardize(y)
z_standardized = standardize(z)
print("Standardized x:", x_standardized)
print("Standardized y:", y_standardized)
print("Standardized z:", z_standardized)
This approach ensures all three variables have a common variance (1 in this case) and can be compared or used in analyses that assume equal dispersion across variables.
User
What if I want to preserve the means of my variables?
Assistant
If you want to adjust the variances of your variables so that they are all the same while keeping the original means intact, you can do so by scaling the deviations from the mean rather than standardizing to zero. The basic idea is:
Compute the Mean and Standard Deviation: For each variable, calculate its mean (μ) and standard deviation (σ).
Scale the Deviations from the Mean: Decide on a target standard deviation (σ₀). For each observation in a variable, subtract the mean, multiply by the ratio of the target standard deviation to the original standard deviation, and then add the original mean back.
Mathematically, for a variable ( X ) with mean ( \mu ) and standard deviation ( \sigma ), the transformed variable ( X' ) with target standard deviation ( \sigma_0 ) is:
Preserves the mean ( \mu ) because when ( X = \mu ), ( X' ) is also ( \mu ).
Scales the variance so that the new variance is ( \sigma_0^2 ). Specifically, if the original variance was ( \sigma^2 ), the scaling multiplies the variance by ( \left(\frac{\sigma_0}{\sigma}\right)^2 = \frac{\sigma_0^2}{\sigma^2} ) resulting in a variance of ( \sigma_0^2 ).
Example in Python
Below is a sample Python code illustrating how to perform this transformation:
python
import numpy as np
# Sample data for three variables
x = np.array([1, 2, 3, 4, 5])
y = np.array([10, 20, 30, 40, 50])
z = np.array([100, 200, 300, 400, 500])
# Define a function to adjust variance while preserving the mean
def adjust_variance(variable, target_std):
mean = np.mean(variable)
std = np.std(variable)
return mean + (variable - mean) * (target_std / std)
# Choose a target standard deviation. For example, target standard deviation 2.
target_std = 2
x_adjusted = adjust_variance(x, target_std)
y_adjusted = adjust_variance(y, target_std)
z_adjusted = adjust_variance(z, target_std)
print("Original means: ", np.mean(x), np.mean(y), np.mean(z))
print("Adjusted standard deviations: ", np.std(x_adjusted), np.std(y_adjusted), np.std(z_adjusted))
print("Adjusted x:", x_adjusted)
print("Adjusted y:", y_adjusted)
print("Adjusted z:", z_adjusted)
Explanation
Step 1: For each variable, we calculate the mean and standard deviation.
Step 2: We compute the adjusted value for each observation such that:
The original deviation from the mean is scaled by the factor ( \frac{\text{target std}}{\text{original std}} ).
We then add back the original mean, ensuring that the mean remains unchanged.
Result: The adjusted variables will have the same targeted standard deviation (and hence variance), but each will retain its original mean.
This method allows you to equalize the variability across your variables while preserving their central tendency.