Skip to content

Commit f5d16bc

Browse files
committed
check for repeated non-zero signular values
pick f57ef58 check for repeated non-zero signular values
1 parent 359de04 commit f5d16bc

File tree

2 files changed

+75
-70
lines changed

2 files changed

+75
-70
lines changed

src/MultivariateStats.jl

Lines changed: 69 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
module MultivariateStats
22

3-
using LinearAlgebra
4-
using SparseArrays
5-
using Statistics: middle
6-
using Distributions: cdf, FDist
7-
using StatsAPI: RegressionModel, HypothesisTest
8-
using StatsBase: SimpleCovariance, CovarianceEstimator, AbstractDataTransform,
9-
ConvergenceException, pairwise, pairwise!, CoefTable
10-
11-
import Statistics: mean, var, cov, covm, cor
12-
import Base: length, size, show
13-
import StatsAPI: fit, predict, coef, weights, dof, r2, pvalue
14-
import LinearAlgebra: eigvals, eigvecs
15-
16-
export
3+
using LinearAlgebra
4+
using SparseArrays
5+
using Statistics: middle
6+
using StatsAPI: RegressionModel
7+
using StatsBase:
8+
SimpleCovariance,
9+
CovarianceEstimator,
10+
AbstractDataTransform,
11+
ConvergenceException,
12+
pairwise,
13+
pairwise!,
14+
CoefTable
15+
16+
import Statistics: mean, var, cov, covm, cor
17+
import Base: length, size, show
18+
import StatsAPI: fit, predict, coef, weights, dof, r2
19+
import LinearAlgebra: eigvals, eigvecs
20+
21+
export
1722

1823
## common
1924
evaluate, # evaluate discriminant function values
@@ -38,27 +43,23 @@ module MultivariateStats
3843

3944
# whiten
4045
Whitening, # Type: Whitening transformation
41-
4246
invsqrtm, # Compute inverse of matrix square root, i.e. inv(sqrtm(A))
4347
cov_whitening, # Compute a whitening transform based on covariance
4448
cov_whitening!, # Compute a whitening transform based on covariance (input will be overwritten)
4549
invsqrtm, # Compute C^{-1/2}, i.e. inv(sqrtm(C))
4650

4751
## pca
4852
PCA, # Type: Principal Component Analysis model
49-
5053
pcacov, # PCA based on covariance
5154
pcasvd, # PCA based on singular value decomposition of input data
5255
principalratio, # the ratio of variances preserved in the principal subspace
5356
principalvar, # the variance along a specific principal direction
5457
principalvars, # the variances along all principal directions
55-
5658
tprincipalvar, # total principal variance, i.e. sum(principalvars(M))
5759
tresidualvar, # total residual variance
5860

5961
## ppca
6062
PPCA, # Type: the Probabilistic PCA model
61-
6263
ppcaml, # Maximum likelihood probabilistic PCA
6364
ppcaem, # EM algorithm for probabilistic PCA
6465
bayespca, # Bayesian PCA
@@ -67,10 +68,7 @@ module MultivariateStats
6768
KernelPCA, # Type: the Kernel PCA model
6869

6970
## cca
70-
CCA, # Type: Correlation Component Analysis model
71-
WilksLambdaTest, # Wilks lambda statistics and tests
72-
PillaiTraceTest, # Pillai trace statistics and tests
73-
LawleyHotellingTest, # Lawley-Hotelling statistics and tests
71+
CCA, # Type: Correlation Component Analysis model
7472

7573
ccacov, # CCA based on covariances
7674
ccasvd, # CCA based on singular value decomposition of input data
@@ -81,18 +79,17 @@ module MultivariateStats
8179
MetricMDS,
8280
classical_mds, # perform classical MDS over a given distance matrix
8381
stress, # stress evaluation
84-
85-
gram2dmat, gram2dmat!, # Gram matrix => Distance matrix
86-
dmat2gram, dmat2gram!, # Distance matrix => Gram matrix
82+
gram2dmat,
83+
gram2dmat!, # Gram matrix => Distance matrix
84+
dmat2gram,
85+
dmat2gram!, # Distance matrix => Gram matrix
8786

8887
## lda
8988
LinearDiscriminant, # Type: Linear Discriminant functional
9089
MulticlassLDAStats, # Type: Statistics required for training multi-class LDA
9190
MulticlassLDA, # Type: Multi-class LDA model
9291
SubspaceLDA, # Type: LDA model for high-dimensional spaces
93-
9492
ldacov, # Linear discriminant analysis based on covariances
95-
9693
classweights, # class-specific weights
9794
classmeans, # class-specific means
9895
withclass_scatter, # with-class scatter matrix
@@ -103,56 +100,58 @@ module MultivariateStats
103100

104101
## ica
105102
ICA, # Type: the Fast ICA model
106-
107103
fastica!, # core algorithm function for the Fast ICA
108104

109105
## fa
110106
FactorAnalysis, # Type: the Factor Analysis model
111-
112107
faem, # EM algorithm for factor analysis
113108
facm, # CM algorithm for factor analysis
114109

115110
## CA, MCA
116-
CA,
117-
MCA,
118-
objectscores,
119-
variablescores,
120-
inertia
121-
122-
## source files
123-
include("types.jl")
124-
include("common.jl")
125-
include("lreg.jl")
126-
include("whiten.jl")
127-
include("pca.jl")
128-
include("ppca.jl")
129-
include("kpca.jl")
130-
include("cca.jl")
131-
include("cmds.jl")
132-
include("mmds.jl")
133-
include("lda.jl")
134-
include("ica.jl")
135-
include("fa.jl")
136-
include("mca.jl")
137-
138-
## deprecations
139-
@deprecate indim(f) size(f,1)
140-
@deprecate outdim(f) size(f,2)
141-
@deprecate transform(f, x) predict(f, x)
142-
@deprecate indim(f::Whitening) length(f::Whitening)
143-
@deprecate outdim(f::Whitening) length(f::Whitening)
144-
@deprecate tvar(f::PCA) var(f::PCA)
145-
@deprecate classical_mds(D::AbstractMatrix, p::Int) predict(fit(MDS, D, maxoutdim=p, distances=true))
146-
@deprecate transform(f::MDS) predict(f::MDS)
147-
@deprecate xindim(M::CCA) size(M,1)
148-
@deprecate yindim(M::CCA) size(M,2)
149-
@deprecate outdim(M::CCA) size(M,3)
150-
@deprecate correlations(M::CCA) cor(M)
151-
@deprecate xmean(M::CCA) mean(M, :x)
152-
@deprecate ymean(M::CCA) mean(M, :y)
153-
@deprecate xprojection(M::CCA) projection(M, :x)
154-
@deprecate yprojection(M::CCA) projection(M, :y)
155-
@deprecate xtransform(M::CCA, x) predict(M, x, :x)
156-
@deprecate ytransform(M::CCA, y) predict(M, y, :y)
111+
CA, # Type: correspondence analysis
112+
MCA, # Type: multiple correspondence analysis
113+
ca, # fit and return a correspondence analysis
114+
mca, # fit and return a multiple correspondence analysis
115+
objectscores, # return the object scores or coordinates from CA or MCA
116+
variablescores, # return the variable/category scores or coordinates from CA or MCA
117+
inertia # return the inertia (derived from eigenvalues) for CA
118+
119+
## source files
120+
include("types.jl")
121+
include("common.jl")
122+
include("lreg.jl")
123+
include("whiten.jl")
124+
include("pca.jl")
125+
include("ppca.jl")
126+
include("kpca.jl")
127+
include("cca.jl")
128+
include("cmds.jl")
129+
include("mmds.jl")
130+
include("lda.jl")
131+
include("ica.jl")
132+
include("fa.jl")
133+
include("mca.jl")
134+
135+
## deprecations
136+
@deprecate indim(f) size(f, 1)
137+
@deprecate outdim(f) size(f, 2)
138+
@deprecate transform(f, x) predict(f, x)
139+
@deprecate indim(f::Whitening) length(f::Whitening)
140+
@deprecate outdim(f::Whitening) length(f::Whitening)
141+
@deprecate tvar(f::PCA) var(f::PCA)
142+
@deprecate classical_mds(D::AbstractMatrix, p::Int) predict(
143+
fit(MDS, D, maxoutdim = p, distances = true),
144+
)
145+
@deprecate transform(f::MDS) predict(f::MDS)
146+
@deprecate xindim(M::CCA) size(M, 1)
147+
@deprecate yindim(M::CCA) size(M, 2)
148+
@deprecate outdim(M::CCA) size(M, 3)
149+
@deprecate correlations(M::CCA) cor(M)
150+
@deprecate xmean(M::CCA) mean(M, :x)
151+
@deprecate ymean(M::CCA) mean(M, :y)
152+
@deprecate xprojection(M::CCA) projection(M, :x)
153+
@deprecate yprojection(M::CCA) projection(M, :y)
154+
@deprecate xtransform(M::CCA, x) predict(M, x, :x)
155+
@deprecate ytransform(M::CCA, y) predict(M, y, :y)
157156

158157
end # module

src/mca.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ function fit!(ca::CA, d::Int)
7070
P, D, Q = svd(ca.SR)
7171
Dq = Diagonal(D)[:, 1:d]
7272

73+
# Check that there are no repeated non-zero eigenvalues.
74+
d = diff(D[D .> 1e-10])
75+
if maximum(d) >= -1e-10
76+
@warn("The indicator matrix has repeated non-zero eigenvalues")
77+
end
78+
7379
Wr = Diagonal(sqrt.(ca.rm))
7480
Wc = Diagonal(sqrt.(ca.cm))
7581
ca.F = Wr \ P * Dq

0 commit comments

Comments
 (0)