stat: append To suffix to extraction methods

This commit is contained in:
kortschak
2017-06-21 14:00:44 +09:30
committed by Dan Kortschak
parent eac07bbebd
commit 03a2dba19b
5 changed files with 24 additions and 24 deletions

View File

@@ -85,11 +85,11 @@ func ExampleCC() {
} }
// Unpack cc. // Unpack cc.
ccors := cc.Corrs(nil) ccors := cc.CorrsTo(nil)
pVecs := cc.Left(nil, true) pVecs := cc.LeftTo(nil, true)
qVecs := cc.Right(nil, true) qVecs := cc.RightTo(nil, true)
phiVs := cc.Left(nil, false) phiVs := cc.LeftTo(nil, false)
psiVs := cc.Right(nil, false) psiVs := cc.RightTo(nil, false)
// Canonical Correlation Matrix, or the correlations between the sphered // Canonical Correlation Matrix, or the correlations between the sphered
// data. // data.

View File

@@ -160,11 +160,11 @@ tests:
continue tests continue tests
} }
corrs = cc.Corrs(corrs) corrs = cc.CorrsTo(corrs)
pVecs = cc.Left(pVecs, true) pVecs = cc.LeftTo(pVecs, true)
qVecs = cc.Right(qVecs, true) qVecs = cc.RightTo(qVecs, true)
phiVs = cc.Left(phiVs, false) phiVs = cc.LeftTo(phiVs, false)
psiVs = cc.Right(psiVs, false) psiVs = cc.RightTo(psiVs, false)
if !floats.EqualApprox(corrs, test.wantCorrs, test.epsilon) { if !floats.EqualApprox(corrs, test.wantCorrs, test.epsilon) {
t.Errorf("%d use %d: unexpected variance result got:%v, want:%v", t.Errorf("%d use %d: unexpected variance result got:%v, want:%v",

View File

@@ -46,12 +46,12 @@ func (c *PC) PrincipalComponents(a mat.Matrix, weights []float64) (ok bool) {
return c.ok return c.ok
} }
// Vectors returns the component direction vectors of a principal components // VectorsTo returns the component direction vectors of a principal components
// analysis. The vectors are returned in the columns of a d×min(n, d) matrix. // analysis. The vectors are returned in the columns of a d×min(n, d) matrix.
// If dst is not nil it must either be zero-sized or be a d×min(n, d) matrix. // If dst is not nil it must either be zero-sized or be a d×min(n, d) matrix.
// dst will be used as the destination for the direction vector data. If dst // dst will be used as the destination for the direction vector data. If dst
// is nil, a new mat.Dense is allocated for the destination. // is nil, a new mat.Dense is allocated for the destination.
func (c *PC) Vectors(dst *mat.Dense) *mat.Dense { func (c *PC) VectorsTo(dst *mat.Dense) *mat.Dense {
if !c.ok { if !c.ok {
panic("stat: use of unsuccessful principal components analysis") panic("stat: use of unsuccessful principal components analysis")
} }
@@ -64,13 +64,13 @@ func (c *PC) Vectors(dst *mat.Dense) *mat.Dense {
return c.svd.VTo(dst) return c.svd.VTo(dst)
} }
// Vars returns the column variances of the principal component scores, // VarsTo returns the column variances of the principal component scores,
// b * vecs, where b is a matrix with centered columns. Variances are returned // b * vecs, where b is a matrix with centered columns. Variances are returned
// in descending order. // in descending order.
// If dst is not nil it is used to store the variances and returned. // If dst is not nil it is used to store the variances and returned.
// Vars will panic if the receiver has not successfully performed a principal // Vars will panic if the receiver has not successfully performed a principal
// components analysis or dst is not nil and the length of dst is not min(n, d). // components analysis or dst is not nil and the length of dst is not min(n, d).
func (c *PC) Vars(dst []float64) []float64 { func (c *PC) VarsTo(dst []float64) []float64 {
if !c.ok { if !c.ok {
panic("stat: use of unsuccessful principal components analysis") panic("stat: use of unsuccessful principal components analysis")
} }
@@ -199,10 +199,10 @@ func (c *CC) CanonicalCorrelations(x, y mat.Matrix, weights []float64) error {
return nil return nil
} }
// Corrs returns the canonical correlations, using dst if it is not nil. // CorrsTo returns the canonical correlations, using dst if it is not nil.
// If dst is not nil and len(dst) does not match the number of columns in // If dst is not nil and len(dst) does not match the number of columns in
// the y input matrix, Corrs will panic. // the y input matrix, Corrs will panic.
func (c *CC) Corrs(dst []float64) []float64 { func (c *CC) CorrsTo(dst []float64) []float64 {
if !c.ok { if !c.ok {
panic("stat: canonical correlations missing or invalid") panic("stat: canonical correlations missing or invalid")
} }
@@ -213,14 +213,14 @@ func (c *CC) Corrs(dst []float64) []float64 {
return c.c.Values(dst) return c.c.Values(dst)
} }
// Left returns the left eigenvectors of the canonical correlation matrix if // LeftTo returns the left eigenvectors of the canonical correlation matrix if
// spheredSpace is true. If spheredSpace is false it returns these eigenvectors // spheredSpace is true. If spheredSpace is false it returns these eigenvectors
// back-transformed to the original data space. // back-transformed to the original data space.
// If dst is not nil it must either be zero-sized or be an xd×yd matrix where xd // If dst is not nil it must either be zero-sized or be an xd×yd matrix where xd
// and yd are the number of variables in the input x and y matrices. dst will // and yd are the number of variables in the input x and y matrices. dst will
// be used as the destination for the vector data. If dst is nil, a new // be used as the destination for the vector data. If dst is nil, a new
// mat.Dense is allocated for the destination. // mat.Dense is allocated for the destination.
func (c *CC) Left(dst *mat.Dense, spheredSpace bool) *mat.Dense { func (c *CC) LeftTo(dst *mat.Dense, spheredSpace bool) *mat.Dense {
if !c.ok || c.n < 2 { if !c.ok || c.n < 2 {
panic("stat: canonical correlations missing or invalid") panic("stat: canonical correlations missing or invalid")
} }
@@ -245,14 +245,14 @@ func (c *CC) Left(dst *mat.Dense, spheredSpace bool) *mat.Dense {
return dst return dst
} }
// Right returns the right eigenvectors of the canonical correlation matrix if // RightTo returns the right eigenvectors of the canonical correlation matrix if
// spheredSpace is true. If spheredSpace is false it returns these eigenvectors // spheredSpace is true. If spheredSpace is false it returns these eigenvectors
// back-transformed to the original data space. // back-transformed to the original data space.
// If dst is not nil it must either be zero-sized or be an yd×yd matrix where yd // If dst is not nil it must either be zero-sized or be an yd×yd matrix where yd
// is the number of variables in the input y matrix. dst will // is the number of variables in the input y matrix. dst will
// be used as the destination for the vector data. If dst is nil, a new // be used as the destination for the vector data. If dst is nil, a new
// mat.Dense is allocated for the destination. // mat.Dense is allocated for the destination.
func (c *CC) Right(dst *mat.Dense, spheredSpace bool) *mat.Dense { func (c *CC) RightTo(dst *mat.Dense, spheredSpace bool) *mat.Dense {
if !c.ok || c.n < 2 { if !c.ok || c.n < 2 {
panic("stat: canonical correlations missing or invalid") panic("stat: canonical correlations missing or invalid")
} }

View File

@@ -35,12 +35,12 @@ func ExamplePC() {
if !ok { if !ok {
return return
} }
fmt.Printf("variances = %.4f\n\n", pc.Vars(nil)) fmt.Printf("variances = %.4f\n\n", pc.VarsTo(nil))
// Project the data onto the first 2 principal components. // Project the data onto the first 2 principal components.
k := 2 k := 2
var proj mat.Dense var proj mat.Dense
proj.Mul(iris, pc.Vectors(nil).Slice(0, d, 0, k)) proj.Mul(iris, pc.VectorsTo(nil).Slice(0, d, 0, k))
fmt.Printf("proj = %.4f", mat.Formatted(&proj, mat.Prefix(" "))) fmt.Printf("proj = %.4f", mat.Formatted(&proj, mat.Prefix(" ")))

View File

@@ -157,8 +157,8 @@ tests:
var vars []float64 var vars []float64
for j := 0; j < 2; j++ { for j := 0; j < 2; j++ {
ok := pc.PrincipalComponents(test.data, test.weights) ok := pc.PrincipalComponents(test.data, test.weights)
vecs = pc.Vectors(vecs) vecs = pc.VectorsTo(vecs)
vars = pc.Vars(vars) vars = pc.VarsTo(vars)
if !ok { if !ok {
t.Errorf("unexpected SVD failure for test %d use %d", i, j) t.Errorf("unexpected SVD failure for test %d use %d", i, j)
continue tests continue tests