Calculate sample covariance from aligned returns, explain n−1 versus n, track squared units, and connect the estimate to portfolio variance.
Sample covariance
Sample covariance measures how two observed variables move together around their own sample means. For a risk analyst, it is the cross-asset input that turns separate asset variances into a portfolio variance. Positive covariance means above-mean observations tend to align with above-mean observations, and below with below; negative covariance means opposite-side deviations tend to dominate.
It is a statistic calculated from paired data, distinct from the population covariance it may be used to estimate. A sample supplies observations; a sampling model supplies the justification for inference beyond them.
Centre, multiply, then choose the divisor
For n≥2 aligned pairs (xi,yi), let xˉ and yˉ be their sample means. The usual sample covariance is
sxy=n−11i=1∑n(xi−xˉ)(yi−yˉ).
Each row contributes a signed cross-product. Two deviations with the same sign contribute positively; opposite signs contribute negatively. Larger deviations receive more weight through their product. Covariance is symmetric: sxy=syx. Setting yi=xi gives the sample variance.
The pairing is part of the definition. For returns, row i must refer to matching start and end times, a common horizon and compatible return conventions. Independently sorting each series, shifting one series by a day or mixing closing times changes the question. A lagged relationship can be studied deliberately, but it is different from contemporaneous covariance.
Four paired returns, all positive
Suppose two assets have the following synthetic one-period simple total returns on four matching dates. Both returns use the same currency basis. The entries are percent returns; subtracting their means produces deviations in percentage points (pp).
Period
X return (%)
Y return (%)
X deviation (pp)
Y deviation (pp)
Product (pp²)
1
1
2
−1.5
−0.5
+0.75
2
2
1
−0.5
−1.5
+0.75
3
3
4
+0.5
+1.5
+0.75
4
4
3
+1.5
+0.5
+0.75
Both means are 2.5%. Every pair lies on the same side of its two means, giving
i=1∑4(xi−xˉ)(yi−yˉ)=3 pp2,sxy=4−13=1 pp2.
Four synthetic return pairs (1%, 2%), (2%, 1%), (3%, 4%) and (4%, 3%), with both mean axes at 2.5%. Periods 1 and 2 are below both means, while periods 3 and 4 are above both. Each centred product is +0.75 pp², summing to 3 pp²; sample covariance is 1 pp². The highlighted period-1 rectangle has deviation side lengths 1.5 pp and 0.5 pp.
Original synthetic four-period example. The reference lines are sample means, not zero returns; the shaded rectangle illustrates one product of deviations.
Positive raw returns alone would not establish positive covariance. For example, two positive-return series could alternate between relatively high and low observations in opposite directions. The centring step distinguishes those cases.
Why n−1 — and when n is appropriate
If the task is to describe the empirical distribution that puts equal probability 1/n on each observed pair, its covariance is
cxyemp=n1i=1∑n(xi−xˉ)(yi−yˉ)=nn−1sxy.
That is 0.75 pp2 here. It is a legitimate descriptive quantity with a different divisor. Calling the four observations the entire finite collection of interest also makes this their population-of-observations covariance. Neither calculation needs independent observations simply to exist.
The n−1 convention has an additional estimation property. If the random pairs (Xi,Yi) are independent and identically distributed across rows, with finite second moments, then
E[SXY]=Cov(X,Y).
This unbiasedness does not require X and Y within a pair to be independent, or a normal distribution. It concerns the average estimate over repeated samples, not the accuracy of this one four-row estimate.
Under the stated assumptions, the first term has expectation nσXY and the second σXY, because Cov(Xˉ,Yˉ)=σXY/n. The expected centred sum is therefore (n−1)σXY.
Selected histories, serial cross-dependence and changing distributions need separate analysis; dividing by n−1 does not automatically correct them. Even a well-defined historical estimate need not describe tomorrow's joint return distribution.
Units travel into portfolio risk
Covariance has product units: X units times Y units. Converting both series from percent entries to decimal returns divides each deviation by 100, so
1 pp2=0.0001 decimal-return2.
Doubling one series doubles covariance; adding a constant to either leaves it unchanged. Consequently, covariance magnitude is not a universal strength scale. Sample correlation divides by the two sample standard deviations, giving a dimensionless measure of linear association when both are nonzero. Here both sample variances are 5/3 pp2, so correlation is 1/(5/3)=0.6.
The sample covariance matrix is
S=(5/3115/3) pp2.
For equal period-start weights, held fixed within each period, portfolio simple returns are (1.5,1.5,3.5,3.5)%. Assuming no external cash flows or unaccounted costs, their sample variance agrees with the portfolio variance calculation:
Using a common aligned sample across every column gives S=Z⊤Z/(n−1), where Z contains column-centred observations. This construction is positive semidefinite: every fixed-weight sample portfolio has nonnegative variance. Pairwise deletion of missing data, using different rows for different matrix entries, can lose that guarantee. Common complete rows preserve the algebra, but deleting incomplete records can still change the population being represented.
To connect the calculation to a fitted hedge slope, Basic Statistics and Regression develops the ratio of sample covariance to predictor variance. Full access requires a free signed-in account; the public link provides a preview.
Sources
Mavrakakis, M. C. and Penzer, J. (2021). Probability and Statistical Inference: From Basic Principles to Advanced Models, first edition, §7.2.2, Definitions 7.2.7–7.2.10 and Claim 7.2.9: joint sample moments, covariance, scaling and variance of sums.