The sample mean is defined as X¯=1n∑ni=1Xi, which is quite intuitive. But the sample variance is S2=1n−1∑ni=1(Xi−X¯)2. Where did the n−1 come from ?
To answer this question, we must go back to the definition of an unbiased estimator. An unbiased estimator is one whose expectation tends to the true expectation. The sample mean is an unbiased estimator. To see why:
E[X¯]=1n∑i=1nE[Xi]=nnμ=μ
Let us look at the expectation of the sample variance,
S2=1n−1∑i=1n(X2i)−nX¯2
E[S2]=1n−1(nE[(X2i)]−nE[X¯2]).
Notice that X¯ is a random variable and not a constant, so the expectation E[X¯2] plays a role. This is the reason behind the n−1.
E[S2]=1n−1(n(μ2+σ2)−n(μ2+Var(X¯))).
Var(X¯)=Var(1n∑i=1nXi)=∑i=1n1n2Var(Xi)=σ2n
E[S2]=1n−1(n(μ2+σ2)−n(μ2+σ2/n)).=(n−1)σ2n−1=σ2
As you can see, if we had the denominator as n instead of n−1, we would get a biased estimate for the variance! But with n−1 the estimator S2 is an unbiased estimator.