improve comments

This commit is contained in:
Jonathan J Lawlor
2014-11-15 15:14:24 -05:00
parent 17b440e6be
commit 35fb0d0413

View File

@@ -16,26 +16,28 @@ func CovarianceMatrix(x mat64.Matrix) *mat64.Dense {
// the correction found in the Covariance and Variance functions. // the correction found in the Covariance and Variance functions.
r, _ := x.Dims() r, _ := x.Dims()
// determine the mean of each of the columns
b := ones(1, r) b := ones(1, r)
b.Mul(b, x) b.Mul(b, x)
b.Scale(1/float64(r), b) b.Scale(1/float64(r), b)
// todo: avoid unneeded memory expansion here.
mu := b.RowView(0) mu := b.RowView(0)
// this could also be done with a clone & row viewer // subtract the mean from the data
xc := mat64.DenseCopyOf(x) xc := mat64.DenseCopyOf(x)
for i := 0; i < r; i++ { for i := 0; i < r; i++ {
rv := xc.RowView(i) rv := xc.RowView(i)
for j, mean := range(mu) { for j, mean := range mu {
rv[j] -= mean rv[j] -= mean
} }
} }
// todo: avoid matrix copy // todo: avoid matrix copy?
xt := new(mat64.Dense) xt := new(mat64.Dense)
xt.TCopy(xc) xt.TCopy(xc)
// It would be nice if we could indicate that this was a symmetric
// matrix.
ss := new(mat64.Dense) ss := new(mat64.Dense)
ss.Mul(xt, xc) ss.Mul(xt, xc)
ss.Scale(1/float64(r-1), ss) ss.Scale(1/float64(r-1), ss)