├── .github └── FUNDING.yml ├── .gitignore ├── .travis.yml ├── JSAT ├── .gitignore ├── nb-configuration.xml ├── nbactions.xml ├── pom.xml ├── src │ └── jsat │ │ ├── ColumnMajorStore.java │ │ ├── DataSet.java │ │ ├── DataStore.java │ │ ├── RowMajorStore.java │ │ ├── SimpleDataSet.java │ │ ├── SimpleWeightVectorModel.java │ │ ├── SingleWeightVectorModel.java │ │ ├── classifiers │ │ ├── BaseUpdateableClassifier.java │ │ ├── CategoricalData.java │ │ ├── CategoricalResults.java │ │ ├── ClassificationDataSet.java │ │ ├── ClassificationModelEvaluation.java │ │ ├── Classifier.java │ │ ├── DDAG.java │ │ ├── DataPoint.java │ │ ├── DataPointPair.java │ │ ├── MajorityVote.java │ │ ├── OneVSAll.java │ │ ├── OneVSOne.java │ │ ├── PriorClassifier.java │ │ ├── RegressorToClassifier.java │ │ ├── Rocchio.java │ │ ├── UpdateableClassifier.java │ │ ├── WarmClassifier.java │ │ ├── bayesian │ │ │ ├── AODE.java │ │ │ ├── BestClassDistribution.java │ │ │ ├── ConditionalProbabilityTable.java │ │ │ ├── MultinomialNaiveBayes.java │ │ │ ├── MultivariateNormals.java │ │ │ ├── NaiveBayes.java │ │ │ ├── NaiveBayesUpdateable.java │ │ │ ├── ODE.java │ │ │ └── graphicalmodel │ │ │ │ ├── DirectedGraph.java │ │ │ │ ├── DiscreteBayesNetwork.java │ │ │ │ └── K2NetworkLearner.java │ │ ├── boosting │ │ │ ├── AdaBoostM1.java │ │ │ ├── ArcX4.java │ │ │ ├── Bagging.java │ │ │ ├── EmphasisBoost.java │ │ │ ├── LogitBoost.java │ │ │ ├── LogitBoostPL.java │ │ │ ├── ModestAdaBoost.java │ │ │ ├── SAMME.java │ │ │ ├── Stacking.java │ │ │ ├── UpdatableStacking.java │ │ │ ├── Wagging.java │ │ │ └── WaggingNormal.java │ │ ├── calibration │ │ │ ├── BinaryCalibration.java │ │ │ ├── BinaryScoreClassifier.java │ │ │ ├── IsotonicCalibration.java │ │ │ └── PlattCalibration.java │ │ ├── evaluation │ │ │ ├── AUC.java │ │ │ ├── Accuracy.java │ │ │ ├── BalancedAccuracy.java │ │ │ ├── ClassificationScore.java │ │ │ ├── F1Score.java │ │ │ ├── FbetaScore.java │ │ │ ├── Kappa.java │ │ │ ├── LogLoss.java │ │ │ ├── MatthewsCorrelationCoefficient.java │ │ │ ├── Precision.java │ │ │ ├── Recall.java │ │ │ └── SimpleBinaryClassMetric.java │ │ ├── imbalance │ │ │ ├── BorderlineSMOTE.java │ │ │ └── SMOTE.java │ │ ├── knn │ │ │ ├── DANN.java │ │ │ ├── LWL.java │ │ │ └── NearestNeighbour.java │ │ ├── linear │ │ │ ├── ALMA2.java │ │ │ ├── AROW.java │ │ │ ├── BBR.java │ │ │ ├── LinearBatch.java │ │ │ ├── LinearL1SCD.java │ │ │ ├── LinearSGD.java │ │ │ ├── LinearTools.java │ │ │ ├── LogisticRegressionDCD.java │ │ │ ├── NHERD.java │ │ │ ├── NewGLMNET.java │ │ │ ├── OWA.java │ │ │ ├── PassiveAggressive.java │ │ │ ├── ROMMA.java │ │ │ ├── SCD.java │ │ │ ├── SCW.java │ │ │ ├── SDCA.java │ │ │ ├── SMIDAS.java │ │ │ ├── SPA.java │ │ │ ├── STGD.java │ │ │ ├── StochasticMultinomialLogisticRegression.java │ │ │ ├── StochasticSTLinearL1.java │ │ │ └── kernelized │ │ │ │ ├── ALMA2K.java │ │ │ │ ├── BOGD.java │ │ │ │ ├── CSKLR.java │ │ │ │ ├── CSKLRBatch.java │ │ │ │ ├── DUOL.java │ │ │ │ ├── Forgetron.java │ │ │ │ ├── KernelSGD.java │ │ │ │ ├── OSKL.java │ │ │ │ └── Projectron.java │ │ ├── neuralnetwork │ │ │ ├── BackPropagationNet.java │ │ │ ├── DReDNetSimple.java │ │ │ ├── LVQ.java │ │ │ ├── LVQLLC.java │ │ │ ├── Perceptron.java │ │ │ ├── RBFNet.java │ │ │ ├── SGDNetworkTrainer.java │ │ │ ├── SOM.java │ │ │ ├── activations │ │ │ │ ├── ActivationLayer.java │ │ │ │ ├── LinearLayer.java │ │ │ │ ├── ReLU.java │ │ │ │ ├── SigmoidLayer.java │ │ │ │ ├── SoftSignLayer.java │ │ │ │ ├── SoftmaxLayer.java │ │ │ │ └── TanhLayer.java │ │ │ ├── initializers │ │ │ │ ├── BiastInitializer.java │ │ │ │ ├── ConstantInit.java │ │ │ │ ├── GaussianNormalInit.java │ │ │ │ ├── TanhInitializer.java │ │ │ │ └── WeightInitializer.java │ │ │ └── regularizers │ │ │ │ ├── Max2NormRegularizer.java │ │ │ │ └── WeightRegularizer.java │ │ ├── svm │ │ │ ├── DCD.java │ │ │ ├── DCDs.java │ │ │ ├── DCSVM.java │ │ │ ├── LSSVM.java │ │ │ ├── Pegasos.java │ │ │ ├── PegasosK.java │ │ │ ├── PlattSMO.java │ │ │ ├── SBP.java │ │ │ ├── SVMnoBias.java │ │ │ ├── SupportVectorLearner.java │ │ │ └── extended │ │ │ │ ├── AMM.java │ │ │ │ ├── CPM.java │ │ │ │ └── OnlineAMM.java │ │ └── trees │ │ │ ├── DecisionStump.java │ │ │ ├── DecisionTree.java │ │ │ ├── ERTrees.java │ │ │ ├── ExtraTree.java │ │ │ ├── ID3.java │ │ │ ├── ImportanceByUses.java │ │ │ ├── ImpurityScore.java │ │ │ ├── MDA.java │ │ │ ├── MDI.java │ │ │ ├── RandomDecisionTree.java │ │ │ ├── RandomForest.java │ │ │ ├── TreeFeatureImportanceInference.java │ │ │ ├── TreeLearner.java │ │ │ ├── TreeNodeVisitor.java │ │ │ └── TreePruner.java │ │ ├── clustering │ │ ├── BayesianHAC.java │ │ ├── CLARA.java │ │ ├── ClusterFailureException.java │ │ ├── Clusterer.java │ │ ├── ClustererBase.java │ │ ├── DBSCAN.java │ │ ├── EMGaussianMixture.java │ │ ├── FLAME.java │ │ ├── GapStatistic.java │ │ ├── HDBSCAN.java │ │ ├── KClusterer.java │ │ ├── KClustererBase.java │ │ ├── LSDBC.java │ │ ├── MEDDIT.java │ │ ├── MeanShift.java │ │ ├── OPTICS.java │ │ ├── PAM.java │ │ ├── SeedSelectionMethods.java │ │ ├── TRIKMEDS.java │ │ ├── VBGMM.java │ │ ├── biclustering │ │ │ ├── Bicluster.java │ │ │ ├── ConsensusScore.java │ │ │ └── SpectralCoClustering.java │ │ ├── dissimilarity │ │ │ ├── AbstractClusterDissimilarity.java │ │ │ ├── AverageLinkDissimilarity.java │ │ │ ├── CentroidDissimilarity.java │ │ │ ├── ClusterDissimilarity.java │ │ │ ├── CompleteLinkDissimilarity.java │ │ │ ├── DistanceMetricDissimilarity.java │ │ │ ├── LanceWilliamsDissimilarity.java │ │ │ ├── MedianDissimilarity.java │ │ │ ├── SingleLinkDissimilarity.java │ │ │ ├── UpdatableClusterDissimilarity.java │ │ │ └── WardsDissimilarity.java │ │ ├── evaluation │ │ │ ├── AdjustedRandIndex.java │ │ │ ├── ClusterEvaluation.java │ │ │ ├── ClusterEvaluationBase.java │ │ │ ├── Completeness.java │ │ │ ├── DaviesBouldinIndex.java │ │ │ ├── DunnIndex.java │ │ │ ├── Homogeneity.java │ │ │ ├── IntraClusterSumEvaluation.java │ │ │ ├── NormalizedMutualInformation.java │ │ │ ├── VMeasure.java │ │ │ └── intra │ │ │ │ ├── IntraClusterEvaluation.java │ │ │ │ ├── MaxDistance.java │ │ │ │ ├── MeanCentroidDistance.java │ │ │ │ ├── MeanDistance.java │ │ │ │ ├── SoSCentroidDistance.java │ │ │ │ └── SumOfSqrdPairwiseDistances.java │ │ ├── hierarchical │ │ │ ├── DivisiveGlobalClusterer.java │ │ │ ├── DivisiveLocalClusterer.java │ │ │ ├── NNChainHAC.java │ │ │ ├── PriorityHAC.java │ │ │ └── SimpleHAC.java │ │ └── kmeans │ │ │ ├── ElkanKMeans.java │ │ │ ├── ElkanKernelKMeans.java │ │ │ ├── GMeans.java │ │ │ ├── HamerlyKMeans.java │ │ │ ├── KMeans.java │ │ │ ├── KMeansPDN.java │ │ │ ├── KernelKMeans.java │ │ │ ├── LloydKernelKMeans.java │ │ │ ├── MiniBatchKMeans.java │ │ │ ├── NaiveKMeans.java │ │ │ └── XMeans.java │ │ ├── datatransform │ │ ├── AutoDeskewTransform.java │ │ ├── DataModelPipeline.java │ │ ├── DataTransform.java │ │ ├── DataTransformBase.java │ │ ├── DataTransformProcess.java │ │ ├── DenseSparceTransform.java │ │ ├── FastICA.java │ │ ├── FixedDataTransform.java │ │ ├── Imputer.java │ │ ├── InPlaceInvertibleTransform.java │ │ ├── InPlaceTransform.java │ │ ├── InsertMissingValuesTransform.java │ │ ├── InverseOfTransform.java │ │ ├── InvertibleTransform.java │ │ ├── JLTransform.java │ │ ├── LinearTransform.java │ │ ├── NominalToNumeric.java │ │ ├── NumericalToHistogram.java │ │ ├── PCA.java │ │ ├── PNormNormalization.java │ │ ├── PolynomialTransform.java │ │ ├── ProjectionTransform.java │ │ ├── RemoveAttributeTransform.java │ │ ├── StandardizeTransform.java │ │ ├── UnitVarianceTransform.java │ │ ├── WhitenedPCA.java │ │ ├── WhitenedZCA.java │ │ ├── ZeroMeanTransform.java │ │ ├── featureselection │ │ │ ├── BDS.java │ │ │ ├── LRS.java │ │ │ ├── MutualInfoFS.java │ │ │ ├── ReliefF.java │ │ │ ├── SBS.java │ │ │ └── SFS.java │ │ ├── kernel │ │ │ ├── KernelPCA.java │ │ │ ├── Nystrom.java │ │ │ └── RFF_RBF.java │ │ └── visualization │ │ │ ├── Isomap.java │ │ │ ├── LargeViz.java │ │ │ ├── MDS.java │ │ │ ├── TSNE.java │ │ │ └── VisualizationTransform.java │ │ ├── distributions │ │ ├── Beta.java │ │ ├── Cauchy.java │ │ ├── ChiSquared.java │ │ ├── ContinuousDistribution.java │ │ ├── Distribution.java │ │ ├── DistributionSearch.java │ │ ├── Exponential.java │ │ ├── FisherSendor.java │ │ ├── Gamma.java │ │ ├── Kolmogorov.java │ │ ├── Kumaraswamy.java │ │ ├── Laplace.java │ │ ├── Levy.java │ │ ├── LogNormal.java │ │ ├── LogUniform.java │ │ ├── Logistic.java │ │ ├── MaxwellBoltzmann.java │ │ ├── Normal.java │ │ ├── Pareto.java │ │ ├── Rayleigh.java │ │ ├── StudentT.java │ │ ├── TruncatedDistribution.java │ │ ├── Uniform.java │ │ ├── Weibull.java │ │ ├── discrete │ │ │ ├── Binomial.java │ │ │ ├── DiscreteDistribution.java │ │ │ ├── Poisson.java │ │ │ ├── UniformDiscrete.java │ │ │ └── Zipf.java │ │ ├── empirical │ │ │ ├── KernelDensityEstimator.java │ │ │ └── kernelfunc │ │ │ │ ├── BiweightKF.java │ │ │ │ ├── EpanechnikovKF.java │ │ │ │ ├── GaussKF.java │ │ │ │ ├── KernelFunction.java │ │ │ │ ├── TriweightKF.java │ │ │ │ └── UniformKF.java │ │ ├── kernels │ │ │ ├── BaseKernelTrick.java │ │ │ ├── BaseL2Kernel.java │ │ │ ├── DistanceMetricBasedKernel.java │ │ │ ├── GeneralRBFKernel.java │ │ │ ├── KernelPoint.java │ │ │ ├── KernelPoints.java │ │ │ ├── KernelTrick.java │ │ │ ├── LinearKernel.java │ │ │ ├── NormalizedKernel.java │ │ │ ├── PolynomialKernel.java │ │ │ ├── PukKernel.java │ │ │ ├── RBFKernel.java │ │ │ ├── RationalQuadraticKernel.java │ │ │ └── SigmoidKernel.java │ │ └── multivariate │ │ │ ├── Dirichlet.java │ │ │ ├── IndependentDistribution.java │ │ │ ├── MetricKDE.java │ │ │ ├── MultivariateDistribution.java │ │ │ ├── MultivariateDistributionSkeleton.java │ │ │ ├── MultivariateKDE.java │ │ │ ├── NormalM.java │ │ │ ├── NormalMR.java │ │ │ ├── ProductKDE.java │ │ │ └── SymmetricDirichlet.java │ │ ├── driftdetectors │ │ ├── ADWIN.java │ │ ├── BaseDriftDetector.java │ │ ├── DDM.java │ │ └── UnhandledDriftException.java │ │ ├── exceptions │ │ ├── FailedToFitException.java │ │ ├── ModelMismatchException.java │ │ └── UntrainedModelException.java │ │ ├── io │ │ ├── ARFFLoader.java │ │ ├── CSV.java │ │ ├── DataWriter.java │ │ ├── JSATData.java │ │ └── LIBSVMLoader.java │ │ ├── linear │ │ ├── CholeskyDecomposition.java │ │ ├── ConcatenatedVec.java │ │ ├── ConstantVector.java │ │ ├── DenseMatrix.java │ │ ├── DenseVector.java │ │ ├── EigenValueDecomposition.java │ │ ├── GenericMatrix.java │ │ ├── HessenbergForm.java │ │ ├── IndexValue.java │ │ ├── LUPDecomposition.java │ │ ├── Lanczos.java │ │ ├── Matrix.java │ │ ├── MatrixOfVecs.java │ │ ├── MatrixStatistics.java │ │ ├── Poly2Vec.java │ │ ├── QRDecomposition.java │ │ ├── RandomMatrix.java │ │ ├── RandomVector.java │ │ ├── RowColumnOps.java │ │ ├── ScaledVector.java │ │ ├── ShiftedVec.java │ │ ├── SingularValueDecomposition.java │ │ ├── SparseMatrix.java │ │ ├── SparseVector.java │ │ ├── SubMatrix.java │ │ ├── SubVector.java │ │ ├── TransposeView.java │ │ ├── TruncatedSVD.java │ │ ├── Vec.java │ │ ├── VecOps.java │ │ ├── VecPaired.java │ │ ├── VecPairedComparable.java │ │ ├── VecWithNorm.java │ │ ├── distancemetrics │ │ │ ├── ChebyshevDistance.java │ │ │ ├── CosineDistance.java │ │ │ ├── CosineDistanceNormalized.java │ │ │ ├── DenseSparseMetric.java │ │ │ ├── DistanceCounter.java │ │ │ ├── DistanceMetric.java │ │ │ ├── EuclideanDistance.java │ │ │ ├── JaccardDistance.java │ │ │ ├── KernelDistance.java │ │ │ ├── MahalanobisDistance.java │ │ │ ├── ManhattanDistance.java │ │ │ ├── MinkowskiDistance.java │ │ │ ├── NormalizedEuclideanDistance.java │ │ │ ├── PearsonDistance.java │ │ │ ├── SquaredEuclideanDistance.java │ │ │ ├── TrainableDistanceMetric.java │ │ │ └── WeightedEuclideanDistance.java │ │ ├── solvers │ │ │ └── ConjugateGradient.java │ │ └── vectorcollection │ │ │ ├── BallTree.java │ │ │ ├── BaseCaseDT.java │ │ │ ├── CoverTree.java │ │ │ ├── DCI.java │ │ │ ├── DefaultVectorCollection.java │ │ │ ├── DualTree.java │ │ │ ├── IncrementalCollection.java │ │ │ ├── IndexDistPair.java │ │ │ ├── IndexNode.java │ │ │ ├── IndexTuple.java │ │ │ ├── KDTree.java │ │ │ ├── RTree.java │ │ │ ├── RandomBallCover.java │ │ │ ├── RandomBallCoverOneShot.java │ │ │ ├── SVPTree.java │ │ │ ├── ScoreDT.java │ │ │ ├── ScoreDTLazy.java │ │ │ ├── VPTree.java │ │ │ ├── VPTreeMV.java │ │ │ ├── VectorArray.java │ │ │ ├── VectorCollection.java │ │ │ ├── VectorCollectionUtils.java │ │ │ └── lsh │ │ │ ├── E2LSH.java │ │ │ └── RandomProjectionLSH.java │ │ ├── lossfunctions │ │ ├── AbsoluteLoss.java │ │ ├── EpsilonInsensitiveLoss.java │ │ ├── HingeLoss.java │ │ ├── HuberLoss.java │ │ ├── LogisticLoss.java │ │ ├── LossC.java │ │ ├── LossFunc.java │ │ ├── LossMC.java │ │ ├── LossR.java │ │ ├── SoftmaxLoss.java │ │ └── SquaredLoss.java │ │ ├── math │ │ ├── Complex.java │ │ ├── ContinuedFraction.java │ │ ├── DescriptiveStatistics.java │ │ ├── ExponentialMovingStatistics.java │ │ ├── FastMath.java │ │ ├── Function.java │ │ ├── Function1D.java │ │ ├── FunctionMat.java │ │ ├── FunctionVec.java │ │ ├── IndexFunction.java │ │ ├── MathTricks.java │ │ ├── OnLineStatistics.java │ │ ├── SimpleLinearRegression.java │ │ ├── SpecialMath.java │ │ ├── TrigMath.java │ │ ├── decayrates │ │ │ ├── DecayRate.java │ │ │ ├── ExponetialDecay.java │ │ │ ├── InverseDecay.java │ │ │ ├── LinearDecay.java │ │ │ ├── NoDecay.java │ │ │ └── PowerDecay.java │ │ ├── integration │ │ │ ├── AdaptiveSimpson.java │ │ │ ├── Romberg.java │ │ │ └── Trapezoidal.java │ │ ├── optimization │ │ │ ├── BFGS.java │ │ │ ├── BacktrackingArmijoLineSearch.java │ │ │ ├── GoldenSearch.java │ │ │ ├── LBFGS.java │ │ │ ├── LineSearch.java │ │ │ ├── ModifiedOWLQN.java │ │ │ ├── NelderMead.java │ │ │ ├── Optimizer.java │ │ │ ├── RosenbrockFunction.java │ │ │ ├── WolfeNWLineSearch.java │ │ │ ├── oned │ │ │ │ └── GoldenSearch.java │ │ │ └── stochastic │ │ │ │ ├── AdaDelta.java │ │ │ │ ├── AdaGrad.java │ │ │ │ ├── Adam.java │ │ │ │ ├── GradientUpdater.java │ │ │ │ ├── NAdaGrad.java │ │ │ │ ├── RMSProp.java │ │ │ │ ├── Rprop.java │ │ │ │ ├── SGDMomentum.java │ │ │ │ └── SimpleSGD.java │ │ └── rootfinding │ │ │ ├── Bisection.java │ │ │ ├── RiddersMethod.java │ │ │ ├── RootFinder.java │ │ │ ├── Secant.java │ │ │ └── Zeroin.java │ │ ├── outlier │ │ ├── DensityOutlier.java │ │ ├── IsolationForest.java │ │ ├── LOF.java │ │ ├── LinearOCSVM.java │ │ ├── LoOP.java │ │ └── Outlier.java │ │ ├── parameters │ │ ├── BooleanParameter.java │ │ ├── DecayRateParameter.java │ │ ├── DoubleParameter.java │ │ ├── GridSearch.java │ │ ├── IntParameter.java │ │ ├── KernelFunctionParameter.java │ │ ├── MetricParameter.java │ │ ├── ModelSearch.java │ │ ├── ObjectParameter.java │ │ ├── Parameter.java │ │ ├── Parameterized.java │ │ └── RandomSearch.java │ │ ├── regression │ │ ├── AveragedRegressor.java │ │ ├── BaseUpdateableRegressor.java │ │ ├── KernelRLS.java │ │ ├── KernelRidgeRegression.java │ │ ├── MultipleLinearRegression.java │ │ ├── NadarayaWatson.java │ │ ├── OrdinaryKriging.java │ │ ├── RANSAC.java │ │ ├── RegressionDataSet.java │ │ ├── RegressionModelEvaluation.java │ │ ├── Regressor.java │ │ ├── RidgeRegression.java │ │ ├── StochasticGradientBoosting.java │ │ ├── StochasticRidgeRegression.java │ │ ├── UpdateableRegressor.java │ │ ├── WarmRegressor.java │ │ └── evaluation │ │ │ ├── CoefficientOfDetermination.java │ │ │ ├── MeanAbsoluteError.java │ │ │ ├── MeanSquaredError.java │ │ │ ├── RegressionScore.java │ │ │ ├── RelativeAbsoluteError.java │ │ │ ├── RelativeSquaredError.java │ │ │ └── TotalHistoryRegressionScore.java │ │ ├── testing │ │ ├── StatisticTest.java │ │ ├── goodnessoffit │ │ │ └── KSTest.java │ │ └── onesample │ │ │ ├── OneSampleTest.java │ │ │ ├── TTest.java │ │ │ └── ZTest.java │ │ ├── text │ │ ├── BasicTextVectorCreator.java │ │ ├── ClassificationHashedTextDataLoader.java │ │ ├── ClassificationTextDataLoader.java │ │ ├── GreekLetters.java │ │ ├── HashedTextDataLoader.java │ │ ├── HashedTextVectorCreator.java │ │ ├── TextDataLoader.java │ │ ├── TextVectorCreator.java │ │ ├── stemming │ │ │ ├── LovinsStemmer.java │ │ │ ├── PaiceHuskStemmer.java │ │ │ ├── PorterStemmer.java │ │ │ ├── Stemmer.java │ │ │ └── VoidStemmer.java │ │ ├── tokenizer │ │ │ ├── NGramTokenizer.java │ │ │ ├── NaiveTokenizer.java │ │ │ ├── StemmingTokenizer.java │ │ │ ├── StopWordTokenizer.java │ │ │ └── Tokenizer.java │ │ ├── topicmodel │ │ │ └── OnlineLDAsvi.java │ │ └── wordweighting │ │ │ ├── BinaryWordPresent.java │ │ │ ├── OkapiBM25.java │ │ │ ├── TfIdf.java │ │ │ ├── WordCount.java │ │ │ └── WordWeighting.java │ │ └── utils │ │ ├── ArrayUtils.java │ │ ├── BooleanList.java │ │ ├── BoundedSortedList.java │ │ ├── BoundedSortedSet.java │ │ ├── ClosedHashingUtil.java │ │ ├── DoubleList.java │ │ ├── FakeExecutor.java │ │ ├── FibHeap.java │ │ ├── GridDataGenerator.java │ │ ├── IndexTable.java │ │ ├── IntDoubleMap.java │ │ ├── IntDoubleMapArray.java │ │ ├── IntList.java │ │ ├── IntPriorityQueue.java │ │ ├── IntSet.java │ │ ├── IntSetFixedSize.java │ │ ├── IntSortedSet.java │ │ ├── IterableIterator.java │ │ ├── ListUtils.java │ │ ├── LongDoubleMap.java │ │ ├── LongList.java │ │ ├── ModifiableCountDownLatch.java │ │ ├── Pair.java │ │ ├── PairedReturn.java │ │ ├── PoisonRunnable.java │ │ ├── ProbailityMatch.java │ │ ├── QuickSort.java │ │ ├── RunnableConsumer.java │ │ ├── SimpleList.java │ │ ├── SortedArrayList.java │ │ ├── StringUtils.java │ │ ├── SystemInfo.java │ │ ├── Tuple3.java │ │ ├── UnionFind.java │ │ ├── concurrent │ │ ├── AtomicDouble.java │ │ ├── AtomicDoubleArray.java │ │ ├── ConcurrentCacheLRU.java │ │ ├── IndexReducer.java │ │ ├── IndexRunnable.java │ │ ├── LoopChunkReducer.java │ │ ├── LoopChunkRunner.java │ │ ├── ParallelUtils.java │ │ └── TreeBarrier.java │ │ └── random │ │ ├── CMWC4096.java │ │ ├── RandomUtil.java │ │ ├── XOR128.java │ │ ├── XOR96.java │ │ └── XORWOW.java └── test │ └── jsat │ ├── FixedProblems.java │ ├── NormalClampedSample.java │ ├── TestTools.java │ ├── classifiers │ ├── DDAGTest.java │ ├── OneVSAllTest.java │ ├── OneVSOneTest.java │ ├── RocchioTest.java │ ├── bayesian │ │ ├── AODETest.java │ │ ├── MultinomialNaiveBayesTest.java │ │ ├── MultivariateNormalsTest.java │ │ ├── NaiveBayesTest.java │ │ └── NaiveBayesUpdateableTest.java │ ├── boosting │ │ ├── AdaBoostM1Test.java │ │ ├── ArcX4Test.java │ │ ├── BaggingTest.java │ │ ├── EmphasisBoostTest.java │ │ ├── LogitBoostPLTest.java │ │ ├── LogitBoostTest.java │ │ ├── ModestAdaBoostTest.java │ │ ├── SAMMETest.java │ │ ├── StackingTest.java │ │ ├── UpdatableStackingTest.java │ │ └── WaggingNormalTest.java │ ├── calibration │ │ ├── IsotonicCalibrationTest.java │ │ └── PlattCalibrationTest.java │ ├── evaluation │ │ ├── AUCTest.java │ │ ├── AccuracyTest.java │ │ ├── F1ScoreTest.java │ │ ├── FbetaScoreTest.java │ │ ├── KappaTest.java │ │ ├── LogLossTest.java │ │ ├── MatthewsCorrelationCoefficientTest.java │ │ ├── PrecisionTest.java │ │ └── RecallTest.java │ ├── imbalance │ │ ├── BorderlineSMOTETest.java │ │ └── SMOTETest.java │ ├── knn │ │ ├── DANNTest.java │ │ ├── LWLTest.java │ │ └── NearestNeighbourTest.java │ ├── linear │ │ ├── ALMA2Test.java │ │ ├── AROWTest.java │ │ ├── BBRTest.java │ │ ├── LinearBatchTest.java │ │ ├── LinearL1SCDTest.java │ │ ├── LinearSGDTest.java │ │ ├── LogisticRegressionDCDTest.java │ │ ├── NHERDTest.java │ │ ├── NewGLMNETTest.java │ │ ├── PassiveAggressiveTest.java │ │ ├── ROMMATest.java │ │ ├── SCDTest.java │ │ ├── SCWTest.java │ │ ├── SDCATest.java │ │ ├── SMIDASTest.java │ │ ├── STGDTest.java │ │ ├── StochasticMultinomialLogisticRegressionTest.java │ │ └── kernelized │ │ │ ├── ALMA2KTest.java │ │ │ ├── BOGDTest.java │ │ │ ├── CSKLRBatchTest.java │ │ │ ├── CSKLRTest.java │ │ │ ├── DUOLTest.java │ │ │ ├── ForgetronTest.java │ │ │ ├── KernelPointTest.java │ │ │ ├── KernelSGDTest.java │ │ │ ├── OSKLTest.java │ │ │ └── ProjectronTest.java │ ├── neuralnetwork │ │ ├── BackPropagationNetTest.java │ │ ├── DReDNetSimpleTest.java │ │ ├── LVQLLCTest.java │ │ ├── LVQTest.java │ │ ├── PerceptronTest.java │ │ ├── RBFNetTest.java │ │ └── SOMTest.java │ ├── svm │ │ ├── DCDTest.java │ │ ├── DCDsTest.java │ │ ├── DCSVMTest.java │ │ ├── LSSVMTest.java │ │ ├── PegasosKTest.java │ │ ├── PegasosTest.java │ │ ├── PlattSMOTest.java │ │ ├── SBPTest.java │ │ ├── SVMnoBiasTest.java │ │ └── extended │ │ │ ├── AMMTest.java │ │ │ └── CPMTest.java │ └── trees │ │ ├── DecisionStumpTest.java │ │ ├── DecisionTreeTest.java │ │ ├── ERTreesTest.java │ │ ├── ImportanceByUsesTest.java │ │ ├── MDATest.java │ │ ├── MDITest.java │ │ └── RandomForestTest.java │ ├── clustering │ ├── BayesianHACTest.java │ ├── CLARATest.java │ ├── DBSCANTest.java │ ├── EMGaussianMixtureTest.java │ ├── FLAMETest.java │ ├── GapStatisticTest.java │ ├── HDBSCANTest.java │ ├── LSDBCTest.java │ ├── MEDDITTest.java │ ├── MeanShiftTest.java │ ├── OPTICSTest.java │ ├── PAMTest.java │ ├── TRIKMEDSTest.java │ ├── VBGMMTest.java │ ├── biclustering │ │ └── SpectralCoClusteringTest.java │ ├── evaluation │ │ ├── AdjustedRandIndexTest.java │ │ ├── CompletenessTest.java │ │ ├── HomogeneityTest.java │ │ ├── NormalizedMutualInformationTest.java │ │ ├── VMeasureTest.java │ │ └── intra │ │ │ └── SumOfSqrdPairwiseDistancesTest.java │ ├── hierarchical │ │ ├── DivisiveGlobalClustererTest.java │ │ ├── DivisiveLocalClustererTest.java │ │ ├── NNChainHACTest.java │ │ ├── PriorityHACTest.java │ │ └── SimpleHACTest.java │ └── kmeans │ │ ├── ElkanKMeansTest.java │ │ ├── ElkanKernelKMeansTest.java │ │ ├── GMeansTest.java │ │ ├── HamerlyKMeansTest.java │ │ ├── KMeansPDNTest.java │ │ ├── LloydKernelKMeansTest.java │ │ ├── MiniBatchKMeansTest.java │ │ ├── NaiveKMeansTest.java │ │ └── XMeansTest.java │ ├── datatransform │ ├── FastICATest.java │ ├── ImputerTest.java │ ├── JLTransformTest.java │ ├── PCATest.java │ ├── RemoveAttributeTransformTest.java │ ├── WhitenedPCATest.java │ ├── WhitenedZCATest.java │ ├── featureselection │ │ ├── BDSTest.java │ │ ├── LRSTest.java │ │ ├── MutualInfoFSTest.java │ │ ├── ReliefFTest.java │ │ ├── SBSTest.java │ │ └── SFSTest.java │ ├── kernel │ │ ├── KernelPCATest.java │ │ ├── NystromTest.java │ │ └── RFF_RBFTest.java │ └── visualization │ │ ├── IsomapTest.java │ │ ├── LargeVizTest.java │ │ ├── MDSTest.java │ │ └── TSNETest.java │ ├── distributions │ ├── BetaTest.java │ ├── CauchyTest.java │ ├── ChiSquaredTest.java │ ├── ContinuousDistributionTest.java │ ├── ExponentialTest.java │ ├── FisherSendorTest.java │ ├── GammaTest.java │ ├── KolmogorovTest.java │ ├── KumaraswamyTest.java │ ├── LaplaceTest.java │ ├── LevyTest.java │ ├── LogNormalTest.java │ ├── LogUniformTest.java │ ├── LogisticTest.java │ ├── MaxwellBoltzmannTest.java │ ├── NormalTest.java │ ├── ParetoTest.java │ ├── RayleighTest.java │ ├── StudentTTest.java │ ├── TruncatedDistributionTest.java │ ├── UniformTest.java │ ├── WeibullTest.java │ ├── discrete │ │ ├── BinomialTest.java │ │ ├── PoissonTest.java │ │ ├── UniformDiscreteTest.java │ │ └── ZipfTest.java │ ├── empirical │ │ └── KernelDensityEstimatorTest.java │ └── multivariate │ │ ├── DirichletTest.java │ │ ├── NormalMTest.java │ │ └── SymmetricDirichletTest.java │ ├── driftdetectors │ ├── ADWINTest.java │ └── DDMTest.java │ ├── io │ ├── CSVTest.java │ ├── JSATDataTest.java │ └── LIBSVMLoaderTest.java │ ├── linear │ ├── CholeskyDecompositionTest.java │ ├── ConcatenatedVecTest.java │ ├── DenseMatrixTest.java │ ├── EigenvalueDecompositionTest.java │ ├── GenericMatrixTest.java │ ├── HessenbergFormTest.java │ ├── LUPDecompositionTest.java │ ├── LanczosTest.java │ ├── MatrixOfVecsTest.java │ ├── MatrixStatisticsTest.java │ ├── MatrixTest.java │ ├── Poly2VecTest.java │ ├── QRDecompositionTest.java │ ├── ShiftedVecTest.java │ ├── SingularValueDecompositionTest.java │ ├── SparseMatrixTest.java │ ├── SparseVectorTest.java │ ├── SubVectorTest.java │ ├── TruncatedSVDTest.java │ ├── VecWithNormTest.java │ ├── distancemetrics │ │ ├── ChebyshevDistanceTest.java │ │ ├── CosineDistanceTest.java │ │ ├── CosineDistanceTestNormalized.java │ │ ├── EuclideanDistanceTest.java │ │ ├── JaccardDistanceTest.java │ │ ├── MahalanobisDistanceTest.java │ │ ├── ManhattanDistanceTest.java │ │ ├── MinkowskiDistanceTest.java │ │ ├── NormalizedEuclideanDistanceTest.java │ │ ├── PearsonDistanceTest.java │ │ ├── SquaredEuclideanDistanceTest.java │ │ └── WeightedEuclideanDistanceTest.java │ ├── solvers │ │ └── ConjugateGradientTest.java │ └── vectorcollection │ │ ├── BallTreeTest.java │ │ ├── CoverTreeTest.java │ │ ├── DCITest.java │ │ ├── DualTreeTest.java │ │ ├── KDTreeTest.java │ │ ├── RTreeTest.java │ │ ├── RandomBallCoverTest.java │ │ ├── VPTreeMVTest.java │ │ ├── VPTreeTest.java │ │ ├── VectorArrayTest.java │ │ └── lsh │ │ ├── E2LSHTest.java │ │ └── RandomProjectionLSHTest.java │ ├── math │ ├── ComplexTest.java │ ├── ExponentialMovingStatisticsTest.java │ ├── FastMathTest.java │ ├── FunctionTest.java │ ├── MathTricksTest.java │ ├── OnLineStatisticsTest.java │ ├── SpecialMathTest.java │ ├── integration │ │ └── AdaptiveSimpsonTest.java │ ├── optimization │ │ ├── BFGSTest.java │ │ ├── LBFGSTest.java │ │ ├── ModifiedOWLQNTest.java │ │ ├── NelderMeadTest.java │ │ ├── oned │ │ │ └── GoldenSearchTest.java │ │ └── stochastic │ │ │ ├── AdaDeltaTest.java │ │ │ ├── AdaGradTest.java │ │ │ ├── AdamTest.java │ │ │ ├── NAdaGradTest.java │ │ │ ├── RMSPropTest.java │ │ │ ├── RpropTest.java │ │ │ ├── SGDMomentumTest.java │ │ │ └── SimpleSGDTest.java │ └── rootfinding │ │ ├── BisectionTest.java │ │ ├── RiddersMethodTest.java │ │ ├── SecantTest.java │ │ └── ZeroinTest.java │ ├── outlier │ ├── DensityOutlierTest.java │ ├── IsolationForestTest.java │ ├── LOFTest.java │ ├── LinearOCSVMTest.java │ └── LoOPTest.java │ ├── parameters │ ├── GridSearchTest.java │ └── RandomSearchTest.java │ ├── regression │ ├── AveragedRegressorTest.java │ ├── KernelRLSTest.java │ ├── KernelRidgeRegressionTest.java │ ├── NadarayaWatsonTest.java │ ├── OrdinaryKrigingTest.java │ ├── RANSACTest.java │ ├── RidgeRegressionTest.java │ ├── StochasticGradientBoostingTest.java │ ├── StochasticRidgeRegressionTest.java │ └── evaluation │ │ ├── CoefficientOfDeterminationTest.java │ │ ├── MeanAbsoluteErrorTest.java │ │ ├── MeanSquaredErrorTest.java │ │ ├── RelativeAbsoluteErrorTest.java │ │ └── RelativeSquaredErrorTest.java │ ├── text │ ├── stemming │ │ ├── LovinsStemmerTest.java │ │ ├── PaiceHuskStemmerTest.java │ │ └── PorterStemmerTest.java │ ├── tokenizer │ │ └── NGramTokenizerTest.java │ └── topicmodel │ │ └── OnlineLDAsviTest.java │ └── utils │ ├── FibHeapTest.java │ ├── IndexTableTest.java │ ├── IntDoubleMapArrayTest.java │ ├── IntDoubleMapTest.java │ ├── IntListTest.java │ ├── IntPriorityQueueTest.java │ ├── IntSetFixedSizeTest.java │ ├── IntSetTest.java │ ├── IntSortedSetTest.java │ ├── ListUtilsTest.java │ ├── LongDoubleMapTest.java │ ├── LongListTest.java │ ├── QuickSortTest.java │ ├── SimpleListTest.java │ └── StringUtilsTest.java ├── LICENSE.txt └── README.md /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [EdwardRaff] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | # patreon: # Replace with a single Patreon username 5 | # open_collective: # Replace with a single Open Collective username 6 | # ko_fi: # Replace with a single Ko-fi username 7 | # tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | # community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | # liberapay: # Replace with a single Liberapay username 10 | # issuehunt: # Replace with a single IssueHunt username 11 | # otechie: # Replace with a single Otechie username 12 | # custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /JSAT/nbproject/private/ 2 | /JSAT/target/ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | language: java 3 | 4 | jdk: 5 | - oraclejdk8 6 | 7 | env: 8 | - JSAT_SOURCE_DIR=JSAT 9 | 10 | before_script: 11 | - echo "MAVEN_OPTS='-Xmx2g -XX:MaxPermSize=512m'" > ~/.mavenrc 12 | 13 | script: 14 | - cd $JSAT_SOURCE_DIR 15 | - mvn clean install 16 | -------------------------------------------------------------------------------- /JSAT/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /JSAT/nb-configuration.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | 16 | gpl30 17 | false 18 | 19 | 20 | -------------------------------------------------------------------------------- /JSAT/src/jsat/SingleWeightVectorModel.java: -------------------------------------------------------------------------------- 1 | package jsat; 2 | 3 | import jsat.linear.Vec; 4 | 5 | /** 6 | * This interface is for binary classification and regression problems where the 7 | * solution can be represented as a single weight vector. 8 | * 9 | * @author Edward Raff 10 | */ 11 | public interface SingleWeightVectorModel extends SimpleWeightVectorModel 12 | { 13 | /** 14 | * Returns the only weight vector used for the model 15 | * @return the only weight vector used for the model 16 | */ 17 | public Vec getRawWeight(); 18 | 19 | /** 20 | * Returns the bias term used for the model, or 0 of the model does not 21 | * support or was not trained with a bias term. 22 | * 23 | * @return the bias term for the model 24 | */ 25 | public double getBias(); 26 | 27 | @Override 28 | default public Vec getRawWeight(int index) 29 | { 30 | if(index == 0) 31 | return getRawWeight(); 32 | else 33 | throw new IndexOutOfBoundsException("SingleWeightVectorModel has only a single weight vector at index 0, index " + index + " is not valid"); 34 | } 35 | 36 | @Override 37 | default public double getBias(int index) 38 | { 39 | if(index == 0) 40 | return getBias(); 41 | else 42 | throw new IndexOutOfBoundsException("SingleWeightVectorModel has only a single weight vector at index 0, index " + index + " is not valid"); 43 | } 44 | 45 | @Override 46 | default public int numWeightsVecs() 47 | { 48 | return 1; 49 | } 50 | 51 | public SingleWeightVectorModel clone(); 52 | } 53 | -------------------------------------------------------------------------------- /JSAT/src/jsat/classifiers/DataPointPair.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.classifiers; 3 | 4 | import java.io.Serializable; 5 | import jsat.linear.Vec; 6 | 7 | /** 8 | * 9 | * This class exists so that any data point can be arbitrarily paired with some value 10 | * @author Edward Raff 11 | */ 12 | public class DataPointPair

implements Serializable 13 | { 14 | 15 | private static final long serialVersionUID = 5091308998873225566L; 16 | DataPoint dataPoint; 17 | P pair; 18 | 19 | public DataPointPair(DataPoint dataPoint, P pair) 20 | { 21 | this.dataPoint = dataPoint; 22 | this.pair = pair; 23 | } 24 | 25 | public void setDataPoint(DataPoint dataPoint) 26 | { 27 | this.dataPoint = dataPoint; 28 | } 29 | 30 | public void setPair(P pair) 31 | { 32 | this.pair = pair; 33 | } 34 | 35 | public DataPoint getDataPoint() 36 | { 37 | return dataPoint; 38 | } 39 | 40 | public P getPair() 41 | { 42 | return pair; 43 | } 44 | 45 | /** 46 | * The same as calling {@link DataPoint#getNumericalValues() } on {@link #getDataPoint() }. 47 | * @return the Vec related to the data point in this pair. 48 | */ 49 | public Vec getVector() 50 | { 51 | return dataPoint.getNumericalValues(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /JSAT/src/jsat/classifiers/PriorClassifier.java: -------------------------------------------------------------------------------- 1 | package jsat.classifiers; 2 | 3 | import java.util.concurrent.ExecutorService; 4 | import jsat.exceptions.UntrainedModelException; 5 | 6 | /** 7 | * A Naive classifier that simply returns the prior probabilities as the 8 | * classification decision. 9 | * 10 | * @author Edward Raff 11 | */ 12 | public class PriorClassifier implements Classifier 13 | { 14 | 15 | private static final long serialVersionUID = 7763388716880766538L; 16 | private CategoricalResults cr; 17 | 18 | /** 19 | * Creates a new PriorClassifeir 20 | */ 21 | public PriorClassifier() 22 | { 23 | } 24 | 25 | /** 26 | * Creates a new Prior Classifier that is given the results it should be 27 | * returning 28 | * 29 | * @param cr the prior probabilities for classification 30 | */ 31 | public PriorClassifier(CategoricalResults cr) 32 | { 33 | this.cr = cr; 34 | } 35 | 36 | @Override 37 | public CategoricalResults classify(DataPoint data) 38 | { 39 | if(cr == null) 40 | throw new UntrainedModelException("PriorClassifier has not been trained"); 41 | return cr; 42 | } 43 | 44 | @Override 45 | public void train(ClassificationDataSet dataSet, boolean parallel) 46 | { 47 | train(dataSet); 48 | } 49 | 50 | @Override 51 | public void train(ClassificationDataSet dataSet) 52 | { 53 | cr = new CategoricalResults(dataSet.getPredicting().getNumOfCategories()); 54 | for(int i = 0; i < dataSet.size(); i++) 55 | cr.incProb(dataSet.getDataPointCategory(i), dataSet.getWeight(i)); 56 | cr.normalize(); 57 | } 58 | 59 | @Override 60 | public boolean supportsWeightedData() 61 | { 62 | return true; 63 | } 64 | 65 | @Override 66 | public Classifier clone() 67 | { 68 | PriorClassifier clone = new PriorClassifier(); 69 | if(this.cr != null) 70 | clone.cr = this.cr.clone(); 71 | return clone; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /JSAT/src/jsat/classifiers/bayesian/MultivariateNormals.java: -------------------------------------------------------------------------------- 1 | package jsat.classifiers.bayesian; 2 | 3 | import jsat.distributions.multivariate.NormalM; 4 | 5 | /** 6 | * This classifier can be seen as an extension of {@link NaiveBayes}. Instead of treating the variables as independent, 7 | * each class uses all of its variables to fit a {@link NormalM Multivariate Normal} distribution. As such, it can only 8 | * handle numerical attributes. However, if the classes are normally distributed, it will produce optimal classification 9 | * results. The less normal the true distributions are, the less accurate the classifier will be. 10 | * 11 | * @author Edward Raff 12 | */ 13 | public class MultivariateNormals extends BestClassDistribution 14 | { 15 | 16 | private static final long serialVersionUID = 5977979334930517655L; 17 | 18 | public MultivariateNormals(boolean usePriors) 19 | { 20 | super(new NormalM(), usePriors); 21 | } 22 | 23 | /** 24 | * Creates a new class for classification by feating each class to a {@link NormalM Multivariate Normal Distribution}. 25 | */ 26 | public MultivariateNormals() 27 | { 28 | super(new NormalM()); 29 | } 30 | 31 | /** 32 | * Copy constructor 33 | * @param toCopy the object to copy 34 | */ 35 | public MultivariateNormals(MultivariateNormals toCopy) 36 | { 37 | super(toCopy); 38 | } 39 | 40 | @Override 41 | public boolean supportsWeightedData() 42 | { 43 | return true; 44 | } 45 | 46 | @Override 47 | public MultivariateNormals clone() 48 | { 49 | return new MultivariateNormals(this); 50 | } 51 | 52 | 53 | } 54 | -------------------------------------------------------------------------------- /JSAT/src/jsat/classifiers/calibration/BinaryScoreClassifier.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.classifiers.calibration; 3 | 4 | import jsat.classifiers.Classifier; 5 | import jsat.classifiers.DataPoint; 6 | 7 | /** 8 | * Many algorithms linear a binary separation between two classes A and 9 | * B by representing the target labels with a {@code -1} ad {@code 1}. At 10 | * prediction, the output is a real valued number - where the sign indicates the 11 | * class label. This interface indicates that an algorithm conforms such 12 | * behavior, and that the "0" class corresponds to the {@code -1} label, and the 13 | * "1" class corresponds to the {@code 1} label.
14 | * 15 | * @author Edward Raff 16 | */ 17 | public interface BinaryScoreClassifier extends Classifier 18 | { 19 | /** 20 | * Returns the numeric score for predicting a class of a given data point, 21 | * where the sign of the value indicates which class the data point is 22 | * predicted to belong to. 23 | * 24 | * @param dp the data point to predict the class label of 25 | * @return the score for the given data point 26 | */ 27 | public double getScore(DataPoint dp); 28 | 29 | @Override 30 | public BinaryScoreClassifier clone(); 31 | } 32 | -------------------------------------------------------------------------------- /JSAT/src/jsat/classifiers/evaluation/F1Score.java: -------------------------------------------------------------------------------- 1 | package jsat.classifiers.evaluation; 2 | 3 | /** 4 | * The F1 score is the harmonic mean of {@link Precision} and 5 | * {@link Recall}. This score is only valid for binary 6 | * classification problems. 7 | * 8 | * @author Edward Raff 9 | */ 10 | public class F1Score extends SimpleBinaryClassMetric 11 | { 12 | 13 | 14 | private static final long serialVersionUID = -6192302685766444921L; 15 | 16 | public F1Score() 17 | { 18 | super(); 19 | } 20 | 21 | public F1Score(F1Score toClone) 22 | { 23 | super(toClone); 24 | } 25 | 26 | @Override 27 | public double getScore() 28 | { 29 | return 2*tp/(2*tp+fp+fn); 30 | } 31 | 32 | @Override 33 | public boolean equals(Object obj) 34 | { 35 | if(this.getClass().isAssignableFrom(obj.getClass()) && obj.getClass().isAssignableFrom(this.getClass())) 36 | { 37 | return true; 38 | } 39 | return false; 40 | } 41 | 42 | @Override 43 | public int hashCode() 44 | { 45 | return getName().hashCode(); 46 | } 47 | 48 | @Override 49 | public F1Score clone() 50 | { 51 | return new F1Score(this); 52 | } 53 | 54 | @Override 55 | public String getName() 56 | { 57 | return "F1 Score"; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /JSAT/src/jsat/classifiers/evaluation/MatthewsCorrelationCoefficient.java: -------------------------------------------------------------------------------- 1 | package jsat.classifiers.evaluation; 2 | 3 | /** 4 | * Evaluates a classifier based on Mathews Correlation Coefficient 5 | * 6 | * @author Edward Raff 7 | */ 8 | public class MatthewsCorrelationCoefficient extends SimpleBinaryClassMetric 9 | { 10 | 11 | 12 | private static final long serialVersionUID = 7102318546460007008L; 13 | 14 | public MatthewsCorrelationCoefficient() 15 | { 16 | super(); 17 | } 18 | 19 | public MatthewsCorrelationCoefficient(MatthewsCorrelationCoefficient toClone) 20 | { 21 | super(toClone); 22 | } 23 | 24 | @Override 25 | public double getScore() 26 | { 27 | double denom = (tp+fp)*(tp+fn)*(tn+fp)*(tn+fn); 28 | if(denom <= 1e-16) 29 | return 0; 30 | return (tp*tn-fp*fn)/Math.sqrt(denom); 31 | } 32 | 33 | @Override 34 | public boolean equals(Object obj) 35 | { 36 | if(this.getClass().isAssignableFrom(obj.getClass()) && obj.getClass().isAssignableFrom(this.getClass())) 37 | { 38 | return true; 39 | } 40 | return false; 41 | } 42 | 43 | @Override 44 | public int hashCode() 45 | { 46 | return getName().hashCode(); 47 | } 48 | 49 | @Override 50 | public MatthewsCorrelationCoefficient clone() 51 | { 52 | return new MatthewsCorrelationCoefficient(this); 53 | } 54 | 55 | @Override 56 | public String getName() 57 | { 58 | return "Matthews Correlation Coefficient"; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /JSAT/src/jsat/classifiers/evaluation/Precision.java: -------------------------------------------------------------------------------- 1 | package jsat.classifiers.evaluation; 2 | 3 | /** 4 | * Evaluates a classifier based on the Precision, where the class of index 0 5 | * is considered the positive class. This score is only valid for binary 6 | * classification problems. 7 | * 8 | * @author Edward Raff 9 | */ 10 | public class Precision extends SimpleBinaryClassMetric 11 | { 12 | 13 | 14 | private static final long serialVersionUID = 7046590252900909918L; 15 | 16 | public Precision() 17 | { 18 | super(); 19 | } 20 | 21 | public Precision(Precision toClone) 22 | { 23 | super(toClone); 24 | } 25 | 26 | @Override 27 | public double getScore() 28 | { 29 | return tp/(tp+fp); 30 | } 31 | 32 | @Override 33 | public boolean equals(Object obj) 34 | { 35 | if(this.getClass().isAssignableFrom(obj.getClass()) && obj.getClass().isAssignableFrom(this.getClass())) 36 | { 37 | return true; 38 | } 39 | return false; 40 | } 41 | 42 | @Override 43 | public int hashCode() 44 | { 45 | return getName().hashCode(); 46 | } 47 | 48 | @Override 49 | public Precision clone() 50 | { 51 | return new Precision(this); 52 | } 53 | 54 | @Override 55 | public String getName() 56 | { 57 | return "Precision"; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /JSAT/src/jsat/classifiers/evaluation/Recall.java: -------------------------------------------------------------------------------- 1 | package jsat.classifiers.evaluation; 2 | 3 | /** 4 | * Evaluates a classifier based on the Recall rate, where the class of index 0 5 | * is considered the positive class. This score is only valid for binary 6 | * classification problems. 7 | * 8 | * @author Edward Raff 9 | */ 10 | public class Recall extends SimpleBinaryClassMetric 11 | { 12 | 13 | 14 | private static final long serialVersionUID = 4832185425203972017L; 15 | 16 | /** 17 | * Creates a new Recall evaluator 18 | */ 19 | public Recall() 20 | { 21 | super(); 22 | } 23 | 24 | /** 25 | * Copy constructor 26 | * @param toClone the object to copy 27 | */ 28 | public Recall(Recall toClone) 29 | { 30 | super(toClone); 31 | } 32 | 33 | @Override 34 | public double getScore() 35 | { 36 | return tp/(tp+fn); 37 | } 38 | 39 | @Override 40 | public boolean equals(Object obj) 41 | { 42 | if(this.getClass().isAssignableFrom(obj.getClass()) && obj.getClass().isAssignableFrom(this.getClass())) 43 | { 44 | return true; 45 | } 46 | return false; 47 | } 48 | 49 | @Override 50 | public int hashCode() 51 | { 52 | return getName().hashCode(); 53 | } 54 | 55 | @Override 56 | public Recall clone() 57 | { 58 | return new Recall(this); 59 | } 60 | 61 | @Override 62 | public String getName() 63 | { 64 | return "Recall"; 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /JSAT/src/jsat/classifiers/neuralnetwork/activations/LinearLayer.java: -------------------------------------------------------------------------------- 1 | package jsat.classifiers.neuralnetwork.activations; 2 | 3 | import jsat.linear.Matrix; 4 | import jsat.linear.Vec; 5 | 6 | /** 7 | * 8 | * @author Edward Raff 9 | */ 10 | public class LinearLayer implements ActivationLayer 11 | { 12 | 13 | 14 | private static final long serialVersionUID = -4040058095010471379L; 15 | 16 | @Override 17 | public void activate(Vec input, Vec output) 18 | { 19 | input.copyTo(output); 20 | } 21 | 22 | @Override 23 | public void activate(Matrix input, Matrix output, boolean rowMajor) 24 | { 25 | input.copyTo(output); 26 | } 27 | 28 | @Override 29 | public void backprop(Vec input, Vec output, Vec delta_partial, Vec errout) 30 | { 31 | delta_partial.copyTo(errout); 32 | } 33 | 34 | @Override 35 | public void backprop(Matrix input, Matrix output, Matrix delta_partial, Matrix errout, boolean rowMajor) 36 | { 37 | delta_partial.copyTo(errout); 38 | } 39 | 40 | @Override 41 | public LinearLayer clone() 42 | { 43 | return new LinearLayer(); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /JSAT/src/jsat/classifiers/neuralnetwork/activations/SigmoidLayer.java: -------------------------------------------------------------------------------- 1 | package jsat.classifiers.neuralnetwork.activations; 2 | 3 | import jsat.linear.Matrix; 4 | import jsat.linear.Vec; 5 | 6 | /** 7 | * This layer provides the standard Sigmoid activation f(x) = 8 | * 1/(1+exp(-x)) 9 | * 10 | * @author Edward Raff 11 | */ 12 | public class SigmoidLayer implements ActivationLayer 13 | { 14 | 15 | 16 | private static final long serialVersionUID = 160273287445169627L; 17 | 18 | @Override 19 | public void activate(Vec input, Vec output) 20 | { 21 | for(int i = 0; i < input.length(); i++) 22 | output.set(i, 1/(1+Math.exp(-input.get(i)))); 23 | } 24 | 25 | @Override 26 | public void activate(Matrix input, Matrix output, boolean rowMajor) 27 | { 28 | for(int i = 0; i < input.rows(); i++) 29 | for(int j = 0; j < input.cols(); j++) 30 | output.set(i, j, 1.0/(1+Math.exp(-input.get(i, j)))); 31 | } 32 | 33 | @Override 34 | public void backprop(Vec input, Vec output, Vec delta_partial, Vec errout) 35 | { 36 | for(int i = 0; i < input.length(); i++) 37 | { 38 | double out_i = output.get(i); 39 | double errin_i = delta_partial.get(i); 40 | errout.set(i, out_i*(1-out_i)*errin_i); 41 | } 42 | } 43 | 44 | 45 | 46 | @Override 47 | public void backprop(Matrix input, Matrix output, Matrix delta_partial, Matrix errout, boolean rowMajor) 48 | { 49 | for(int i = 0; i < input.rows(); i++) 50 | for(int j = 0; j < input.cols(); j++) 51 | { 52 | double out_ij = output.get(i, j); 53 | double errin_ij = delta_partial.get(i, j); 54 | errout.set(i, j, out_ij*(1-out_ij)*errin_ij); 55 | } 56 | } 57 | 58 | @Override 59 | public SigmoidLayer clone() 60 | { 61 | return new SigmoidLayer(); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /JSAT/src/jsat/classifiers/neuralnetwork/activations/SoftmaxLayer.java: -------------------------------------------------------------------------------- 1 | package jsat.classifiers.neuralnetwork.activations; 2 | 3 | import jsat.linear.Matrix; 4 | import jsat.linear.Vec; 5 | import jsat.math.MathTricks; 6 | 7 | /** 8 | * This activation layer is meant to be used as the top-most layer for 9 | * classification problems, and uses the softmax function (also known as cross 10 | * entropy) to convert the inputs into probabilities. 11 | * 12 | * @author Edward Raff 13 | */ 14 | public class SoftmaxLayer implements ActivationLayer 15 | { 16 | 17 | 18 | private static final long serialVersionUID = -6595701781466123463L; 19 | 20 | @Override 21 | public void activate(Vec input, Vec output) 22 | { 23 | input.copyTo(output); 24 | MathTricks.softmax(output, false); 25 | } 26 | 27 | @Override 28 | public void backprop(Vec input, Vec output, Vec delta_partial, Vec errout) 29 | { 30 | if(delta_partial != errout)//if the same object, nothing to do 31 | delta_partial.copyTo(errout); 32 | } 33 | 34 | @Override 35 | public void activate(Matrix input, Matrix output, boolean rowMajor) 36 | { 37 | if(rowMajor)//easy 38 | for(int i = 0; i < input.rows(); i++) 39 | activate(input.getRowView(i), output.getRowView(i)); 40 | else//TODO, do this more efficently 41 | for(int j = 0; j < input.cols(); j++) 42 | activate(input.getColumnView(j), output.getColumnView(j)); 43 | } 44 | 45 | @Override 46 | public void backprop(Matrix input, Matrix output, Matrix delta_partial, Matrix errout, boolean rowMajor) 47 | { 48 | if(delta_partial != errout)//if the same object, nothing to do 49 | delta_partial.copyTo(errout); 50 | } 51 | 52 | @Override 53 | public SoftmaxLayer clone() 54 | { 55 | return new SoftmaxLayer(); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /JSAT/src/jsat/classifiers/neuralnetwork/activations/TanhLayer.java: -------------------------------------------------------------------------------- 1 | package jsat.classifiers.neuralnetwork.activations; 2 | 3 | import jsat.linear.Matrix; 4 | import jsat.linear.Vec; 5 | 6 | /** 7 | * This layer provides the standard tanh activation f(x) = 8 | * tanh(x) 9 | * 10 | * @author Edward Raff 11 | */ 12 | public class TanhLayer implements ActivationLayer 13 | { 14 | 15 | 16 | private static final long serialVersionUID = -8369008344962638121L; 17 | 18 | @Override 19 | public void activate(Vec input, Vec output) 20 | { 21 | for(int i = 0; i < input.length(); i++) 22 | output.set(i, Math.tanh(input.get(i))); 23 | } 24 | 25 | @Override 26 | public void activate(Matrix input, Matrix output, boolean rowMajor) 27 | { 28 | for(int i = 0; i < input.rows(); i++) 29 | for (int j = 0; j < input.cols(); j++) 30 | output.set(i, j, Math.tanh(input.get(i, j))); 31 | } 32 | 33 | @Override 34 | public void backprop(Vec input, Vec output, Vec delta_partial, Vec errout) 35 | { 36 | for(int i = 0; i < input.length(); i++) 37 | { 38 | double out_i = output.get(i); 39 | double errin_i = delta_partial.get(i); 40 | errout.set(i, (1-out_i*out_i)*errin_i); 41 | } 42 | } 43 | 44 | @Override 45 | public void backprop(Matrix input, Matrix output, Matrix delta_partial, Matrix errout, boolean rowMajor) 46 | { 47 | for(int i = 0; i < input.rows(); i++) 48 | for (int j = 0; j < input.cols(); j++) 49 | { 50 | double out_ij = output.get(i, j); 51 | double errin_ij = delta_partial.get(i, j); 52 | errout.set(i, j, (1-out_ij*out_ij)*errin_ij); 53 | } 54 | } 55 | 56 | @Override 57 | public TanhLayer clone() 58 | { 59 | return new TanhLayer(); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /JSAT/src/jsat/classifiers/neuralnetwork/initializers/BiastInitializer.java: -------------------------------------------------------------------------------- 1 | package jsat.classifiers.neuralnetwork.initializers; 2 | 3 | import java.io.Serializable; 4 | import java.util.Random; 5 | import jsat.linear.Vec; 6 | 7 | /** 8 | * This interface specifies the method of initializing the bias connections in a 9 | * neural network. 10 | * @author Edward Raff 11 | */ 12 | public interface BiastInitializer extends Serializable 13 | { 14 | /** 15 | * Performs the initialization of the given vector of bias values 16 | * @param b the vector to store the biases in 17 | * @param fanIn the number of connections coming into the layer that these 18 | * biases are for. 19 | * @param rand the source of randomness for initialization 20 | */ 21 | public void init(Vec b, int fanIn, Random rand); 22 | 23 | public BiastInitializer clone(); 24 | } 25 | -------------------------------------------------------------------------------- /JSAT/src/jsat/classifiers/neuralnetwork/initializers/ConstantInit.java: -------------------------------------------------------------------------------- 1 | package jsat.classifiers.neuralnetwork.initializers; 2 | 3 | import java.util.Random; 4 | import jsat.linear.ConstantVector; 5 | import jsat.linear.Vec; 6 | 7 | /** 8 | * This initializes all bias values to a single constant value 9 | * @author Edward Raff 10 | */ 11 | public class ConstantInit implements BiastInitializer 12 | { 13 | 14 | private static final long serialVersionUID = 2638413936718283757L; 15 | private double c; 16 | 17 | /** 18 | * 19 | * @param c the constant to set all biases to 20 | */ 21 | public ConstantInit(double c) 22 | { 23 | this.c = c; 24 | } 25 | 26 | /** 27 | * 28 | * @param c the constant value to use 29 | */ 30 | public void setConstant(double c) 31 | { 32 | if(Double.isNaN(c) || Double.isInfinite(c)) 33 | throw new IllegalArgumentException("Constant must be a real value, not " + c); 34 | this.c = c; 35 | } 36 | 37 | /** 38 | * 39 | * @return the constant value that will be used for initialization 40 | */ 41 | public double getConstant() 42 | { 43 | return c; 44 | } 45 | 46 | @Override 47 | public void init(Vec b, int fanIn, Random rand) 48 | { 49 | new ConstantVector(c, b.length()).copyTo(b); 50 | } 51 | 52 | @Override 53 | public ConstantInit clone() 54 | { 55 | return new ConstantInit(c); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /JSAT/src/jsat/classifiers/neuralnetwork/initializers/GaussianNormalInit.java: -------------------------------------------------------------------------------- 1 | package jsat.classifiers.neuralnetwork.initializers; 2 | 3 | import java.util.Random; 4 | import jsat.linear.Matrix; 5 | import jsat.linear.Vec; 6 | 7 | /** 8 | * This object initializes the values of weights by sampling from the zero mean 9 | * Gaussian 10 | * @author Edward Raff 11 | */ 12 | public class GaussianNormalInit implements WeightInitializer, BiastInitializer 13 | { 14 | 15 | private static final long serialVersionUID = -882418891606717433L; 16 | private double stndDev; 17 | 18 | /** 19 | * Creates a new GuassianNormalInit object for initializing weights 20 | * @param stndDev the standard deviation of the distribution to sample from 21 | */ 22 | public GaussianNormalInit(double stndDev) 23 | { 24 | this.stndDev = stndDev; 25 | } 26 | 27 | /** 28 | * Sets the standard deviation of the distribution that will be sampled from 29 | * @param stndDev the standard deviation to use 30 | */ 31 | public void setStndDev(double stndDev) 32 | { 33 | this.stndDev = stndDev; 34 | } 35 | 36 | /** 37 | * 38 | * @return the standard deviation of the Gaussian that is sampled from 39 | */ 40 | public double getStndDev() 41 | { 42 | return stndDev; 43 | } 44 | 45 | @Override 46 | public void init(Matrix w, Random rand) 47 | { 48 | for(int i = 0; i < w.rows(); i++) 49 | for(int j = 0; j < w.cols(); j++) 50 | w.set(i, j, rand.nextGaussian()*stndDev); 51 | 52 | } 53 | 54 | @Override 55 | public void init(Vec b, int fanIn, Random rand) 56 | { 57 | for(int i = 0; i < b.length(); i++) 58 | b.set(i, rand.nextGaussian()*stndDev); 59 | } 60 | 61 | @Override 62 | public GaussianNormalInit clone() 63 | { 64 | return new GaussianNormalInit(stndDev); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /JSAT/src/jsat/classifiers/neuralnetwork/initializers/TanhInitializer.java: -------------------------------------------------------------------------------- 1 | package jsat.classifiers.neuralnetwork.initializers; 2 | 3 | import java.util.Random; 4 | import jsat.linear.Matrix; 5 | import jsat.linear.Vec; 6 | 7 | /** 8 | * This initializer samples the weights from an adjusted uniform distribution 9 | * in order to provided better behavior of neuron activation and gradients
10 | *
11 | * See: Glorot, X., & Bengio, Y. (2010). Understanding the difficulty of 12 | * training deep feedforward neural networks. Journal of Machine Learning 13 | * Research - Proceedings Track, 9, 249–256. Retrieved from 14 | * 15 | * here 16 | * @author Edward Raff 17 | */ 18 | public class TanhInitializer implements WeightInitializer, BiastInitializer 19 | { 20 | 21 | 22 | private static final long serialVersionUID = -4770682311082616208L; 23 | 24 | @Override 25 | public void init(Matrix w, Random rand) 26 | { 27 | double cnt = Math.sqrt(6)/Math.sqrt(w.rows()+w.cols()); 28 | for(int i = 0; i < w.rows(); i++) 29 | for(int j = 0; j < w.cols(); j++) 30 | w.set(i, j, rand.nextDouble()*cnt*2-cnt); 31 | 32 | } 33 | 34 | @Override 35 | public void init(Vec b, int fanIn, Random rand) 36 | { 37 | double cnt = Math.sqrt(6)/Math.sqrt(b.length()+fanIn); 38 | for(int i = 0; i < b.length(); i++) 39 | b.set(i, rand.nextDouble()*cnt*2-cnt); 40 | } 41 | 42 | @Override 43 | public TanhInitializer clone() 44 | { 45 | return new TanhInitializer(); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /JSAT/src/jsat/classifiers/neuralnetwork/initializers/WeightInitializer.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.classifiers.neuralnetwork.initializers; 3 | 4 | import java.io.Serializable; 5 | import java.util.Random; 6 | import jsat.linear.Matrix; 7 | 8 | /** 9 | * This interface specifies the method of initializing the weight connections in 10 | * a neural network. 11 | * 12 | * @author Edward Raff 13 | */ 14 | public interface WeightInitializer extends Serializable 15 | { 16 | /** 17 | * Initializes the values of the given weight matrix 18 | * @param w the matrix to initialize 19 | * @param rand the source of randomness for the initialization 20 | */ 21 | public void init(Matrix w, Random rand); 22 | 23 | public WeightInitializer clone(); 24 | } 25 | -------------------------------------------------------------------------------- /JSAT/src/jsat/classifiers/trees/TreeFeatureImportanceInference.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Edward Raff 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | package jsat.classifiers.trees; 18 | 19 | import java.io.Serializable; 20 | import jsat.DataSet; 21 | 22 | /** 23 | * This interface exists for implementing the importance of features from tree 24 | * based models. 25 | * 26 | * @author Edward Raff 27 | */ 28 | public interface TreeFeatureImportanceInference extends Serializable 29 | { 30 | 31 | /** 32 | * 33 | * @param 34 | * @param model the tree model to infer feature importance from 35 | * @param data the dataset to use for importance inference. Should be either 36 | * a Classification or Regression dataset, depending on the type of the 37 | * model. 38 | * @return a double array with one entry for each feature. Numeric features 39 | * start first, followed by categorical features. Larger values indicate 40 | * higher importance, and all values must be non-negative. Otherwise, no 41 | * constraints are placed on the output of this function. 42 | */ 43 | public double[] getImportanceStats(TreeLearner model, DataSet data); 44 | } 45 | -------------------------------------------------------------------------------- /JSAT/src/jsat/classifiers/trees/TreeLearner.java: -------------------------------------------------------------------------------- 1 | package jsat.classifiers.trees; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * This interface provides a contract that allows for the mutation and pruning 7 | * of a tree using the {@link TreeNodeVisitor} and related classes. 8 | * 9 | * @author Edward Raff 10 | * @see TreeNodeVisitor 11 | * @see TreePruner 12 | */ 13 | public interface TreeLearner extends Serializable 14 | { 15 | /** 16 | * Obtains a node visitor for the tree learner that can be used to traverse 17 | * and predict from the learned tree 18 | * @return the root node visitor for the learned tree 19 | */ 20 | public TreeNodeVisitor getTreeNodeVisitor(); 21 | } 22 | -------------------------------------------------------------------------------- /JSAT/src/jsat/clustering/ClusterFailureException.java: -------------------------------------------------------------------------------- 1 | package jsat.clustering; 2 | 3 | /** 4 | * A ClusterFailureException is thrown when a clustering method is unable to 5 | * perform its clustering for some reason. 6 | * 7 | * @author Edward Raff 8 | */ 9 | public class ClusterFailureException extends RuntimeException 10 | { 11 | 12 | 13 | private static final long serialVersionUID = -8084320940762402095L; 14 | 15 | public ClusterFailureException() 16 | { 17 | } 18 | 19 | public ClusterFailureException(String string) 20 | { 21 | super(string); 22 | } 23 | 24 | public ClusterFailureException(Throwable thrwbl) 25 | { 26 | super(thrwbl); 27 | } 28 | 29 | public ClusterFailureException(String string, Throwable thrwbl) 30 | { 31 | super(string, thrwbl); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /JSAT/src/jsat/clustering/KClustererBase.java: -------------------------------------------------------------------------------- 1 | package jsat.clustering; 2 | 3 | import java.util.List; 4 | import java.util.concurrent.ExecutorService; 5 | import jsat.DataSet; 6 | import jsat.classifiers.DataPoint; 7 | 8 | /** 9 | * A base foundation that provides an implementation of the methods that return a list of lists for the clusterings using 10 | * their int array counterparts. 11 | * @author Edward Raff 12 | */ 13 | public abstract class KClustererBase extends ClustererBase implements KClusterer 14 | { 15 | 16 | private static final long serialVersionUID = 2542432122353325407L; 17 | 18 | 19 | @Override 20 | abstract public KClusterer clone(); 21 | } 22 | -------------------------------------------------------------------------------- /JSAT/src/jsat/clustering/dissimilarity/DistanceMetricDissimilarity.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.clustering.dissimilarity; 3 | 4 | import jsat.classifiers.DataPoint; 5 | import jsat.linear.distancemetrics.DistanceMetric; 6 | 7 | /** 8 | * A base class for Dissimilarity measures that are build ontop the use of some {@link DistanceMetric distance metric}. 9 | * 10 | * @author Edward Raff 11 | */ 12 | public abstract class DistanceMetricDissimilarity extends AbstractClusterDissimilarity 13 | { 14 | /** 15 | * The distance metric that will back this dissimilarity measure. 16 | */ 17 | protected final DistanceMetric dm; 18 | 19 | public DistanceMetricDissimilarity(DistanceMetric dm) 20 | { 21 | this.dm = dm; 22 | } 23 | 24 | @Override 25 | public double distance(DataPoint a, DataPoint b) 26 | { 27 | return dm.dist(a.getNumericalValues(), b.getNumericalValues()); 28 | } 29 | 30 | @Override 31 | abstract public DistanceMetricDissimilarity clone(); 32 | } 33 | -------------------------------------------------------------------------------- /JSAT/src/jsat/clustering/dissimilarity/MedianDissimilarity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Edward Raff 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | package jsat.clustering.dissimilarity; 18 | 19 | import jsat.linear.distancemetrics.DistanceMetric; 20 | 21 | /** 22 | * Median link dissimilarity, also called WPGMC. When two points are merged 23 | * under the Median dissimilarity, the weighting to all points in every 24 | * clustering is distributed evenly. 25 | * 26 | * @author Edward Raff 27 | */ 28 | public class MedianDissimilarity extends LanceWilliamsDissimilarity 29 | { 30 | public MedianDissimilarity(DistanceMetric dm) 31 | { 32 | super(dm); 33 | } 34 | 35 | public MedianDissimilarity(MedianDissimilarity toCopy) 36 | { 37 | super(toCopy); 38 | } 39 | 40 | @Override 41 | protected double aConst(boolean iFlag, int ni, int nj, int nk) 42 | { 43 | return 0.5; 44 | } 45 | 46 | @Override 47 | protected double bConst(int ni, int nj, int nk) 48 | { 49 | return -0.25; 50 | } 51 | 52 | @Override 53 | protected double cConst(int ni, int nj, int nk) 54 | { 55 | return 0; 56 | } 57 | 58 | @Override 59 | public MedianDissimilarity clone() 60 | { 61 | return new MedianDissimilarity(this); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /JSAT/src/jsat/clustering/dissimilarity/WardsDissimilarity.java: -------------------------------------------------------------------------------- 1 | package jsat.clustering.dissimilarity; 2 | 3 | import jsat.linear.distancemetrics.SquaredEuclideanDistance; 4 | 5 | /** 6 | * An implementation of Ward's method for hierarchical clustering. This method 7 | * merges clusters based on the minimum total variance of the resulting 8 | * clusters. 9 | * 10 | * @author Edward Raff 11 | */ 12 | public class WardsDissimilarity extends LanceWilliamsDissimilarity 13 | { 14 | 15 | public WardsDissimilarity() 16 | { 17 | super(new SquaredEuclideanDistance()); 18 | } 19 | 20 | @Override 21 | public WardsDissimilarity clone() 22 | { 23 | return new WardsDissimilarity(); 24 | } 25 | 26 | @Override 27 | protected double aConst(boolean iFlag, int ni, int nj, int nk) 28 | { 29 | double totalPoints = ni+nj+nk; 30 | if(iFlag) 31 | return (ni+nk)/totalPoints; 32 | else 33 | return (nj+nk)/totalPoints; 34 | } 35 | 36 | @Override 37 | protected double bConst(int ni, int nj, int nk) 38 | { 39 | double totalPoints = ni+nj+nk; 40 | return -nk/totalPoints; 41 | } 42 | 43 | @Override 44 | protected double cConst(int ni, int nj, int nk) 45 | { 46 | return 0; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /JSAT/src/jsat/clustering/evaluation/ClusterEvaluationBase.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.clustering.evaluation; 3 | 4 | import jsat.DataSet; 5 | import jsat.clustering.ClustererBase; 6 | 7 | /** 8 | * Base implementation for one of the methods in {@link ClusterEvaluation} to 9 | * make life easier. 10 | * 11 | * @author Edward Raff 12 | */ 13 | abstract public class ClusterEvaluationBase implements ClusterEvaluation 14 | { 15 | 16 | @Override 17 | public double evaluate(int[] designations, DataSet dataSet) 18 | { 19 | return evaluate(ClustererBase.createClusterListFromAssignmentArray(designations, dataSet)); 20 | } 21 | 22 | @Override 23 | public abstract ClusterEvaluation clone(); 24 | } 25 | -------------------------------------------------------------------------------- /JSAT/src/jsat/clustering/evaluation/IntraClusterSumEvaluation.java: -------------------------------------------------------------------------------- 1 | package jsat.clustering.evaluation; 2 | 3 | import java.util.List; 4 | import jsat.classifiers.DataPoint; 5 | import jsat.clustering.evaluation.intra.IntraClusterEvaluation; 6 | 7 | /** 8 | * Evaluates a cluster based on the sum of scores for some 9 | * {@link IntraClusterEvaluation} applied to each cluster. 10 | * 11 | * @author Edward Raff 12 | */ 13 | public class IntraClusterSumEvaluation extends ClusterEvaluationBase 14 | { 15 | private IntraClusterEvaluation ice; 16 | 17 | /** 18 | * Creates a new cluster evaluation that returns the sum of the intra 19 | * cluster evaluations 20 | * @param ice the intra cluster evaluation to use 21 | */ 22 | public IntraClusterSumEvaluation(IntraClusterEvaluation ice) 23 | { 24 | this.ice = ice; 25 | } 26 | 27 | /** 28 | * Copy constructor 29 | * @param toCopy the object to copy 30 | */ 31 | public IntraClusterSumEvaluation(IntraClusterSumEvaluation toCopy) 32 | { 33 | this(toCopy.ice.clone()); 34 | } 35 | 36 | @Override 37 | public double evaluate(List> dataSets) 38 | { 39 | double score = 0; 40 | for(List list : dataSets) 41 | score += ice.evaluate(list); 42 | return score; 43 | } 44 | 45 | @Override 46 | public double naturalScore(double evaluate_score) 47 | { 48 | return evaluate_score; 49 | } 50 | 51 | @Override 52 | public IntraClusterSumEvaluation clone() 53 | { 54 | return new IntraClusterSumEvaluation(this); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /JSAT/src/jsat/clustering/evaluation/intra/IntraClusterEvaluation.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.clustering.evaluation.intra; 3 | 4 | import java.util.List; 5 | import jsat.DataSet; 6 | import jsat.classifiers.DataPoint; 7 | 8 | /** 9 | * This interface defines the contract for a method to evaluate the 10 | * intra-cluster distance. This means an evaluation of a single cluster, 11 | * where a higher value indicates a poorly formed cluster. This evaluation does 12 | * not take into account any other neighboring clusters 13 | * 14 | * @author Edward Raff 15 | */ 16 | public interface IntraClusterEvaluation 17 | { 18 | /** 19 | * Evaluates the cluster represented by the given list of data points. 20 | * @param designations the array of cluster designations for the data set 21 | * @param dataSet the full data set of all clusters 22 | * @param clusterID the cluster id in the designations array to 23 | * return the evaluation of 24 | * @return the value in the range [0, Inf) that indicates how well formed 25 | * the cluster is. 26 | */ 27 | public double evaluate(int[] designations, DataSet dataSet, int clusterID); 28 | /** 29 | * Evaluates the cluster represented by the given list of data points. 30 | * @param dataPoints the data points that make up this cluster 31 | * @return the value in the range [0, Inf) that indicates how well formed 32 | * the cluster is. 33 | */ 34 | public double evaluate(List dataPoints); 35 | 36 | public IntraClusterEvaluation clone(); 37 | } 38 | -------------------------------------------------------------------------------- /JSAT/src/jsat/datatransform/DataTransform.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.datatransform; 3 | 4 | import java.io.Serializable; 5 | import jsat.DataSet; 6 | import jsat.classifiers.DataPoint; 7 | import jsat.exceptions.FailedToFitException; 8 | 9 | /** 10 | * A pre-processing step may be desirable before training. If a pre-processing 11 | * step is used, it is necessary to also apply the same transform on the input 12 | * being sent to the learning algorithm. This interface provides the needed 13 | * mechanism.
14 | * A transform may or may not require training, it could be fully specified at 15 | * construction, or learned from the data set. Learning is done via the 16 | * {@link #fit(jsat.DataSet) fit method}. Many DataTransforms will include a 17 | * constructor that takes a dataset as a parameter. These transforms will fit 18 | * the data when constructed, and exist for convenience. 19 | * 20 | * @author Edward Raff 21 | */ 22 | public interface DataTransform extends Cloneable, Serializable 23 | { 24 | /** 25 | * Returns a new data point that is a transformation of the original data 26 | * point. This new data point is a different object, but may contain the 27 | * same references as the original data point. It is not guaranteed that you 28 | * can mutate the transformed point without having a side effect on the 29 | * original point. 30 | * 31 | * @param dp the data point to apply a transformation to 32 | * @return a transformed data point 33 | */ 34 | public DataPoint transform(DataPoint dp); 35 | 36 | /** 37 | * Fits this transform to the given dataset. Some transforms can only be 38 | * learned from classification or regression datasets. If an incompatible 39 | * dataset type is given, a {@link FailedToFitException} exception may be 40 | * thrown. 41 | * 42 | * @param data the dataset to fir this transform to 43 | * @throws FailedToFitException if the dataset type is not compatible with 44 | * the transform 45 | */ 46 | public void fit(DataSet data); 47 | 48 | public DataTransform clone(); 49 | } 50 | -------------------------------------------------------------------------------- /JSAT/src/jsat/datatransform/DataTransformBase.java: -------------------------------------------------------------------------------- 1 | package jsat.datatransform; 2 | 3 | import java.util.List; 4 | import jsat.parameters.Parameter; 5 | import jsat.parameters.Parameterized; 6 | 7 | /** 8 | * This abstract class implements the Parameterized interface to ease the 9 | * development of simple Data Transforms. If a more complicated set of 10 | * parameters is needed then what is obtained from 11 | * {@link Parameter#getParamsFromMethods(java.lang.Object) } than there is no 12 | * reason to use this class. 13 | * 14 | * @author Edward Raff 15 | */ 16 | abstract public class DataTransformBase implements DataTransform, Parameterized 17 | { 18 | 19 | @Override 20 | abstract public DataTransform clone(); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /JSAT/src/jsat/datatransform/FixedDataTransform.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This code contributed in the public domain. 3 | */ 4 | package jsat.datatransform; 5 | 6 | import jsat.classifiers.DataPoint; 7 | 8 | /** 9 | * This interface is meant to be used for convinence when you wish to apply a 10 | * transformation to a data set using the Java 8 lambda features. It is for 11 | * transformations that do not need to be trained on any data, or where all 12 | * training has been done in advance. 13 | * 14 | * @author Edward Raff 15 | */ 16 | public interface FixedDataTransform 17 | { 18 | 19 | /** 20 | * Returns a new data point that is a transformation of the original data 21 | * point. This new data point is a different object, but may contain the 22 | * same references as the original data point. It is not guaranteed that you 23 | * can mutate the transformed point without having a side effect on the 24 | * original point. 25 | * 26 | * @param dp the data point to apply a transformation to 27 | * @return a transformed data point 28 | */ 29 | public DataPoint transform(DataPoint dp); 30 | } 31 | -------------------------------------------------------------------------------- /JSAT/src/jsat/datatransform/InPlaceInvertibleTransform.java: -------------------------------------------------------------------------------- 1 | package jsat.datatransform; 2 | 3 | import jsat.classifiers.DataPoint; 4 | 5 | /** 6 | * This interface behaves exactly as {@link InPlaceTransform} specifies, with 7 | * the addition of an in-place "reverse" method that can be used to alter any 8 | * given transformed data point back into an approximation of the 9 | * original vector, without having to new vector object, but altering the one 10 | * given. 11 | * 12 | * @author Edward Raff 13 | */ 14 | public interface InPlaceInvertibleTransform extends InPlaceTransform, InvertibleTransform 15 | { 16 | 17 | /** 18 | * Mutates the given data point. This causes side effects, altering the data 19 | * point to have the same value as the output of 20 | * {@link #inverse(jsat.classifiers.DataPoint) } 21 | * 22 | * @param dp the data point to alter with an inverse transformation 23 | */ 24 | public void mutableInverse(DataPoint dp); 25 | 26 | @Override 27 | public InPlaceInvertibleTransform clone(); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /JSAT/src/jsat/datatransform/InPlaceTransform.java: -------------------------------------------------------------------------------- 1 | package jsat.datatransform; 2 | 3 | import jsat.classifiers.DataPoint; 4 | 5 | /** 6 | * An In Place Transform is one that has the same number of categorical and 7 | * numeric features as the input. This means it can mutableTransform the input data point 8 | * instead of allocating a new one, which can reduce overhead on memory 9 | * allocations. This can be useful when performing many data transforms in cross 10 | * validation or when processing new examples in an environment that is applying 11 | * an already learned model. 12 | *

This interface is assumed that it will be applied to numeric 13 | * features. Incase this is not true, a {@link #mutatesNominal() } method is 14 | * provided for the implementation to indicate otherwise. 15 | * 16 | * @author Edward Raff 17 | */ 18 | public interface InPlaceTransform extends DataTransform 19 | { 20 | 21 | /** 22 | * Mutates the given data point. This causes side effects, altering the data 23 | * point to have the same value as the output of 24 | * {@link #transform(jsat.classifiers.DataPoint) }. 25 | * 26 | * @param dp the data point to alter 27 | */ 28 | public void mutableTransform(DataPoint dp); 29 | 30 | /** 31 | * By default returns {@code false}. Only returns true if this transform 32 | * will mutableTransform the nominal feature values of a data point. 33 | * 34 | * @return {@code true} if nominal feature values are mutated, {@code false} 35 | * otherwise. 36 | */ 37 | public boolean mutatesNominal(); 38 | } 39 | -------------------------------------------------------------------------------- /JSAT/src/jsat/datatransform/InverseOfTransform.java: -------------------------------------------------------------------------------- 1 | package jsat.datatransform; 2 | 3 | import jsat.DataSet; 4 | import jsat.classifiers.DataPoint; 5 | 6 | /** 7 | * Creates a new Transform object that simply uses the inverse of an 8 | * {@link InvertibleTransform} as a regular transform. This allows one to apply 9 | * inverses after the fact in a simple matter like: 10 | *


11 |  *  DataSet x = //some data set;
12 |  *  InvertibleTransform transform = //some transform;
13 |  *  x.applyTransform(transform);//apply the original transform
14 |  *  //reverse the transform, getting back to where we started
15 |  *  x.applyTransform(new InverseOfTransform(transform));
16 |  * 
17 | * @author Edward Raff 18 | */ 19 | public class InverseOfTransform implements DataTransform 20 | { 21 | 22 | private static final long serialVersionUID = 2565737661260748018L; 23 | private InvertibleTransform transform; 24 | 25 | /** 26 | * Creates a new transform that uses the 27 | * {@link InvertibleTransform#transform(jsat.classifiers.DataPoint) 28 | * transform} of the given transform 29 | * @param transform the transform to use the inverse function of 30 | */ 31 | public InverseOfTransform(InvertibleTransform transform) 32 | { 33 | this.transform = transform; 34 | } 35 | 36 | @Override 37 | public void fit(DataSet data) 38 | { 39 | //no-op, nothing to do 40 | } 41 | 42 | /** 43 | * Copy constructor 44 | * @param toClone the object to copy 45 | */ 46 | public InverseOfTransform(InverseOfTransform toClone) 47 | { 48 | this(toClone.transform.clone()); 49 | } 50 | 51 | @Override 52 | public DataPoint transform(DataPoint dp) 53 | { 54 | return transform.inverse(dp); 55 | } 56 | 57 | @Override 58 | public InverseOfTransform clone() 59 | { 60 | return new InverseOfTransform(this); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /JSAT/src/jsat/datatransform/InvertibleTransform.java: -------------------------------------------------------------------------------- 1 | package jsat.datatransform; 2 | 3 | import jsat.classifiers.DataPoint; 4 | 5 | /** 6 | * A InvertibleTransform is one in which any given transformed vector can be 7 | inverse to recover an approximation of the original vector when using 8 | * a transform that implements this interface. It may not be possible to 9 | * perfectly reproduce the original data point: ie, this process may not be 10 | * loss-less. 11 | * 12 | * @author Edward Raff 13 | */ 14 | public interface InvertibleTransform extends DataTransform 15 | { 16 | 17 | /** 18 | * Applies the inverse or "reverse" transform to approximately undo the 19 | * effect of {@link #transform(jsat.classifiers.DataPoint) } to recover an 20 | * approximation of the original data point. 21 | * 22 | * @param dp the transformed data point 23 | * @return the original data point, or a reasonable approximation 24 | */ 25 | public DataPoint inverse(DataPoint dp); 26 | 27 | @Override 28 | public InvertibleTransform clone(); 29 | } 30 | -------------------------------------------------------------------------------- /JSAT/src/jsat/datatransform/PNormNormalization.java: -------------------------------------------------------------------------------- 1 | package jsat.datatransform; 2 | 3 | import jsat.DataSet; 4 | import jsat.classifiers.DataPoint; 5 | import jsat.linear.Vec; 6 | 7 | /** 8 | * PNormNormalization transformation performs normalizations of a vector x by 9 | * one its p-norms where p is in (0, Infinity) 10 | * 11 | * @author Edward Raff 12 | */ 13 | public class PNormNormalization implements InPlaceTransform 14 | { 15 | 16 | private static final long serialVersionUID = 2934569881395909607L; 17 | private double p; 18 | 19 | /** 20 | * Creates a new object that normalizes based on the 2-norm 21 | */ 22 | public PNormNormalization() 23 | { 24 | this(2.0); 25 | } 26 | 27 | /** 28 | * Creates a new p norm 29 | * @param p the norm to use 30 | */ 31 | public PNormNormalization(double p) 32 | { 33 | if(p <= 0 || Double.isNaN(p)) 34 | throw new IllegalArgumentException("p must be greater than zero, not " + p); 35 | this.p = p; 36 | } 37 | 38 | @Override 39 | public void fit(DataSet data) 40 | { 41 | //no-op, nothing needs to be done 42 | } 43 | 44 | @Override 45 | public DataPoint transform(DataPoint dp) 46 | { 47 | DataPoint dpNew = dp.clone(); 48 | 49 | mutableTransform(dpNew); 50 | return dpNew; 51 | } 52 | 53 | @Override 54 | public void mutableTransform(DataPoint dp) 55 | { 56 | Vec vec = dp.getNumericalValues(); 57 | double norm = vec.pNorm(p); 58 | if(norm != 0) 59 | vec.mutableDivide(norm); 60 | } 61 | 62 | @Override 63 | public boolean mutatesNominal() 64 | { 65 | return false; 66 | } 67 | 68 | @Override 69 | public PNormNormalization clone() 70 | { 71 | return new PNormNormalization(p); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /JSAT/src/jsat/datatransform/UnitVarianceTransform.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.datatransform; 3 | 4 | import jsat.DataSet; 5 | import jsat.classifiers.DataPoint; 6 | import jsat.linear.Vec; 7 | 8 | /** 9 | * Creates a transform to alter data points so that each attribute has a 10 | * standard deviation of 1, which means a variance of 1. 11 | * 12 | * @author Edward Raff 13 | */ 14 | public class UnitVarianceTransform implements InPlaceTransform 15 | { 16 | 17 | private static final long serialVersionUID = 3645532503475641917L; 18 | private Vec stndDevs; 19 | 20 | /** 21 | * Creates a new object for transforming datasets 22 | */ 23 | public UnitVarianceTransform() 24 | { 25 | } 26 | 27 | /** 28 | * Creates a new object for making datasets unit variance fit to the given 29 | * dataset 30 | * 31 | * @param d the dataset to learn this transform from 32 | */ 33 | public UnitVarianceTransform(DataSet d) 34 | { 35 | fit(d); 36 | } 37 | 38 | @Override 39 | public void fit(DataSet d) 40 | { 41 | stndDevs = d.getColumnMeanVariance()[1]; 42 | } 43 | 44 | /** 45 | * Copy constructor 46 | * @param other the transform to make a copy of 47 | */ 48 | private UnitVarianceTransform(UnitVarianceTransform other) 49 | { 50 | this.stndDevs = other.stndDevs.clone(); 51 | } 52 | 53 | @Override 54 | public DataPoint transform(DataPoint dp) 55 | { 56 | DataPoint newDp = dp.clone(); 57 | mutableTransform(newDp); 58 | return newDp; 59 | } 60 | 61 | @Override 62 | public void mutableTransform(DataPoint dp) 63 | { 64 | dp.getNumericalValues().mutablePairwiseDivide(stndDevs); 65 | } 66 | 67 | @Override 68 | public boolean mutatesNominal() 69 | { 70 | return false; 71 | } 72 | 73 | @Override 74 | public DataTransform clone() 75 | { 76 | return new UnitVarianceTransform(this); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /JSAT/src/jsat/distributions/empirical/kernelfunc/BiweightKF.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.distributions.empirical.kernelfunc; 3 | 4 | /** 5 | * 6 | * @author Edward Raff 7 | */ 8 | public class BiweightKF implements KernelFunction 9 | { 10 | 11 | 12 | private static final long serialVersionUID = -7199542934997154186L; 13 | 14 | private BiweightKF() 15 | { 16 | } 17 | 18 | private static class SingletonHolder 19 | { 20 | 21 | public static final BiweightKF INSTANCE = new BiweightKF(); 22 | } 23 | 24 | /** 25 | * Returns the singleton instance of this class 26 | * @return the instance of this class 27 | */ 28 | public static BiweightKF getInstance() 29 | { 30 | return SingletonHolder.INSTANCE; 31 | } 32 | 33 | @Override 34 | public double k(double u) 35 | { 36 | if(Math.abs(u) > 1) 37 | return 0; 38 | return Math.pow(1-u*u, 2)*(15.0/16.0); 39 | } 40 | 41 | @Override 42 | public double intK(double u) 43 | { 44 | if(u < -1) 45 | return 0; 46 | if(u > 1) 47 | return 1; 48 | return Math.pow(u+1, 3)/16.0 * (3*u*u - 9*u + 8); 49 | } 50 | 51 | @Override 52 | public double k2() 53 | { 54 | return 1.0/7.0; 55 | } 56 | 57 | @Override 58 | public double cutOff() 59 | { 60 | return Math.ulp(1)+1; 61 | } 62 | 63 | @Override 64 | public double kPrime(double u) 65 | { 66 | if(Math.abs(u) > 1) 67 | return 0; 68 | return (15.0/4.0)*u*(u*u-1); 69 | } 70 | 71 | @Override 72 | public String toString() 73 | { 74 | return "Biweight Kernel"; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /JSAT/src/jsat/distributions/empirical/kernelfunc/EpanechnikovKF.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.distributions.empirical.kernelfunc; 3 | 4 | /** 5 | * 6 | * @author Edward Raff 7 | */ 8 | public class EpanechnikovKF implements KernelFunction 9 | { 10 | 11 | private static final long serialVersionUID = 8688942176576932932L; 12 | 13 | private EpanechnikovKF() 14 | { 15 | } 16 | 17 | private static class SingletonHolder 18 | { 19 | 20 | public static final EpanechnikovKF INSTANCE = new EpanechnikovKF(); 21 | } 22 | 23 | /** 24 | * Returns the singleton instance of this class 25 | * @return the instance of this class 26 | */ 27 | public static EpanechnikovKF getInstance() 28 | { 29 | return SingletonHolder.INSTANCE; 30 | } 31 | 32 | @Override 33 | public double k(double u) 34 | { 35 | if(Math.abs(u) > 1) 36 | return 0; 37 | return (1-u*u)*(3.0/4.0); 38 | } 39 | 40 | @Override 41 | public double intK(double u) 42 | { 43 | if(u < -1) 44 | return 0; 45 | if( u > 1) 46 | return 1; 47 | return (-u*u*u + 3 *u + 2)/4; 48 | } 49 | 50 | @Override 51 | public double k2() 52 | { 53 | return 1.0/5.0; 54 | } 55 | 56 | @Override 57 | public double cutOff() 58 | { 59 | return Math.ulp(1)+1; 60 | } 61 | 62 | @Override 63 | public double kPrime(double u) 64 | { 65 | if(Math.abs(u) > 1) 66 | return 0; 67 | return - u *(3.0/2.0); 68 | } 69 | 70 | @Override 71 | public String toString() 72 | { 73 | return "Epanechnikov Kernel"; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /JSAT/src/jsat/distributions/empirical/kernelfunc/GaussKF.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.distributions.empirical.kernelfunc; 3 | import static java.lang.Math.*; 4 | import jsat.distributions.Normal; 5 | 6 | /** 7 | * 8 | * @author Edward Raff 9 | */ 10 | public class GaussKF implements KernelFunction 11 | { 12 | 13 | private static final long serialVersionUID = -6765390012694573184L; 14 | 15 | private GaussKF() 16 | { 17 | } 18 | 19 | private static class SingletonHolder 20 | { 21 | 22 | public static final GaussKF INSTANCE = new GaussKF(); 23 | } 24 | 25 | /** 26 | * Returns the singleton instance of this class 27 | * @return the instance of this class 28 | */ 29 | public static GaussKF getInstance() 30 | { 31 | return SingletonHolder.INSTANCE; 32 | } 33 | 34 | @Override 35 | public double k(double u) 36 | { 37 | return Normal.pdf(u, 0, 1); 38 | } 39 | 40 | @Override 41 | public double intK(double u) 42 | { 43 | return Normal.cdf(u, 0, 1); 44 | } 45 | 46 | @Override 47 | public double k2() 48 | { 49 | return 1; 50 | } 51 | 52 | @Override 53 | public double cutOff() 54 | { 55 | /* 56 | * This is not techincaly correct, as this value of k(u) is still 7.998827757006813E-38 57 | * However, this is very close to zero, and is so small that k(u)+x = x, for most values of x. 58 | * Unless this probability si going to be near zero, values past this point will have 59 | * no effect on the result 60 | */ 61 | return 13; 62 | } 63 | 64 | @Override 65 | public double kPrime(double u) 66 | { 67 | return -exp(-pow(u, 2)/2)*u/sqrt(2 * PI); 68 | } 69 | 70 | @Override 71 | public String toString() 72 | { 73 | return "Gaussian Kernel"; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /JSAT/src/jsat/distributions/empirical/kernelfunc/KernelFunction.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.distributions.empirical.kernelfunc; 3 | 4 | import java.io.Serializable; 5 | 6 | /** 7 | * Class for representing one dimensional kernel functions. Since they require 8 | * no parameters and have no need for duplication, its is advised to make 9 | * them singletons. 10 | * 11 | * See http://en.wikipedia.org/wiki/Kernel_(statistics) 12 | * 13 | * @author Edward Raff 14 | */ 15 | public interface KernelFunction extends Serializable 16 | { 17 | /** 18 | * Returns the weight to be applied to a sample for the normalized distance of two data points. 19 | * @param u the distance of the data points 20 | * @return the value in [0, 1) of the amount of weight to give to the sample based on its distance 21 | */ 22 | public double k(double u); 23 | /** 24 | * Computes the value of the finite integral from -Infinity up to the value u, of the function given by {@link #k(double) } 25 | * @param u the distance of the data points 26 | * @return the value of the integration 27 | */ 28 | public double intK(double u); 29 | 30 | /** 31 | * 32 | * Returns the value of the derivative at a point, k'(u) 33 | * @param u the distance of the data points 34 | * @return the value of the derivative at u 35 | */ 36 | public double kPrime(double u); 37 | 38 | /** 39 | * Returns the variance of the kernel function 40 | * @return the variance of the kernel function 41 | */ 42 | public double k2(); 43 | 44 | /** 45 | * As the value of |u| for the kernel function approaches infinity, the 46 | * value of k(u) approaches zero. This function returns the minimal 47 | * absolute value of u for which k(u) returns 0 48 | * 49 | * @return the first value for which k(u) = 0 50 | */ 51 | public double cutOff(); 52 | 53 | /** 54 | * Returns the name of this kernel function 55 | * @return the name of this kernel function 56 | */ 57 | @Override 58 | public String toString(); 59 | } 60 | -------------------------------------------------------------------------------- /JSAT/src/jsat/distributions/empirical/kernelfunc/TriweightKF.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.distributions.empirical.kernelfunc; 3 | 4 | /** 5 | * 6 | * @author Edward Raff 7 | */ 8 | public class TriweightKF implements KernelFunction 9 | { 10 | 11 | private static final long serialVersionUID = -9156392658970318676L; 12 | 13 | private TriweightKF() 14 | { 15 | } 16 | 17 | private static class SingletonHolder 18 | { 19 | 20 | public static final TriweightKF INSTANCE = new TriweightKF(); 21 | } 22 | 23 | /** 24 | * Returns the singleton instance of this class 25 | * @return the instance of this class 26 | */ 27 | public static TriweightKF getInstance() 28 | { 29 | return SingletonHolder.INSTANCE; 30 | } 31 | 32 | @Override 33 | public double k(double u) 34 | { 35 | if(Math.abs(u) > 1) 36 | return 0; 37 | return Math.pow(1 - u*u, 3)*(35.0/32.0); 38 | } 39 | 40 | @Override 41 | public double intK(double u) 42 | { 43 | if(u < -1) 44 | return 0; 45 | if(u > 1) 46 | return 1; 47 | return (-5*Math.pow(u, 7) + 21*Math.pow(u, 5) - 35 * Math.pow(u, 3) + 35 *u + 16)/32; 48 | } 49 | 50 | @Override 51 | public double k2() 52 | { 53 | return 1.0/9.0; 54 | } 55 | 56 | @Override 57 | public double cutOff() 58 | { 59 | return Math.ulp(1)+1; 60 | } 61 | 62 | @Override 63 | public double kPrime(double u) 64 | { 65 | if(Math.abs(u) > 1) 66 | return 0; 67 | return -u; 68 | } 69 | 70 | @Override 71 | public String toString() 72 | { 73 | return "Triweight Kernel"; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /JSAT/src/jsat/distributions/empirical/kernelfunc/UniformKF.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.distributions.empirical.kernelfunc; 3 | 4 | /** 5 | * 6 | * @author Edward Raff 7 | */ 8 | public class UniformKF implements KernelFunction 9 | { 10 | 11 | private static final long serialVersionUID = -6413579643511350896L; 12 | 13 | private UniformKF() 14 | { 15 | } 16 | 17 | private static class SingletonHolder 18 | { 19 | 20 | public static final UniformKF INSTANCE = new UniformKF(); 21 | } 22 | 23 | /** 24 | * Returns the singleton instance of this class 25 | * @return the instance of this class 26 | */ 27 | public static UniformKF getInstance() 28 | { 29 | return SingletonHolder.INSTANCE; 30 | } 31 | 32 | @Override 33 | public double k(double u) 34 | { 35 | if(Math.abs(u) > 1) 36 | return 0; 37 | return 0.5; 38 | } 39 | 40 | @Override 41 | public double intK(double u) 42 | { 43 | if(u < -1) 44 | return 0; 45 | if (u > 1) 46 | return 1; 47 | return (u+1)/2; 48 | } 49 | 50 | @Override 51 | public double k2() 52 | { 53 | return 1.0/3.0; 54 | } 55 | 56 | @Override 57 | public double cutOff() 58 | { 59 | return Math.ulp(1)+1; 60 | } 61 | 62 | @Override 63 | public double kPrime(double u) 64 | { 65 | return 0; 66 | } 67 | 68 | @Override 69 | public String toString() 70 | { 71 | return "Uniform Kernel"; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /JSAT/src/jsat/distributions/kernels/LinearKernel.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.distributions.kernels; 3 | 4 | import java.util.Arrays; 5 | import java.util.List; 6 | import jsat.linear.Vec; 7 | import jsat.parameters.DoubleParameter; 8 | import jsat.parameters.Parameter; 9 | 10 | /** 11 | * Provides a linear kernel function, which computes the normal dot product. 12 | * k(x,y) = x.y + c 13 | * 14 | * @author Edward Raff 15 | */ 16 | public class LinearKernel extends BaseKernelTrick 17 | { 18 | 19 | private static final long serialVersionUID = -1870181048970135367L; 20 | private double c; 21 | 22 | /** 23 | * Creates a new Linear Kernel that computes the dot product and offsets it by a specified value 24 | * @param c the positive bias term for the dot product 25 | */ 26 | public LinearKernel(double c) 27 | { 28 | this.c = c; 29 | } 30 | 31 | /** 32 | * Creates a new Linear Kernel with an added bias term of 1 33 | */ 34 | public LinearKernel() 35 | { 36 | this(1); 37 | } 38 | 39 | /** 40 | * The positive bias term added to the result of the dot product 41 | * @param c the added product term 42 | */ 43 | public void setC(double c) 44 | { 45 | if(c < 0 || Double.isInfinite(c) || Double.isNaN(c)) 46 | throw new IllegalArgumentException("C must be a positive constant, not " + c); 47 | this.c = c; 48 | } 49 | 50 | /** 51 | * Returns the positive additive term 52 | * @return the positive additive term 53 | */ 54 | public double getC() 55 | { 56 | return c; 57 | } 58 | 59 | 60 | @Override 61 | public double eval(Vec a, Vec b) 62 | { 63 | return a.dot(b) + c; 64 | } 65 | 66 | @Override 67 | public String toString() 68 | { 69 | return "Linear Kernel (c=" + c + ")"; 70 | } 71 | 72 | @Override 73 | public LinearKernel clone() 74 | { 75 | return new LinearKernel(c); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /JSAT/src/jsat/distributions/multivariate/MultivariateDistributionSkeleton.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.distributions.multivariate; 3 | 4 | import java.util.List; 5 | import java.util.concurrent.ExecutorService; 6 | import jsat.DataSet; 7 | import jsat.classifiers.DataPoint; 8 | import jsat.linear.DenseVector; 9 | import jsat.linear.Vec; 10 | 11 | /** 12 | * Common class for implementing a multivariate distribution. A number of methods are pre implemented, 13 | * building off of the implementation of the remaining methods.
14 | * Note: the default implementation for the multithreaded methods calls the non threaded version of the method. 15 | * The exception to this is the {@link #setUsingData(jsat.DataSet, java.util.concurrent.ExecutorService) } method, 16 | * which calls {@link #setUsingData(java.util.List, java.util.concurrent.ExecutorService) } 17 | * 18 | * @author Edward Raff 19 | */ 20 | public abstract class MultivariateDistributionSkeleton implements MultivariateDistribution 21 | { 22 | 23 | private static final long serialVersionUID = 4080753806798149915L; 24 | 25 | @Override 26 | public double logPdf(Vec x) 27 | { 28 | double logPDF = Math.log(pdf(x)); 29 | if(Double.isInfinite(logPDF) && logPDF < 0)//log(0) == -Infinty 30 | return -Double.MAX_VALUE; 31 | return logPDF; 32 | } 33 | 34 | 35 | @Override 36 | abstract public MultivariateDistribution clone(); 37 | } 38 | -------------------------------------------------------------------------------- /JSAT/src/jsat/distributions/multivariate/NormalMR.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 Edward Raff 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | package jsat.distributions.multivariate; 18 | 19 | import java.util.List; 20 | import jsat.linear.DenseMatrix; 21 | import jsat.linear.DenseVector; 22 | import jsat.linear.Matrix; 23 | import jsat.linear.MatrixStatistics; 24 | import jsat.linear.Vec; 25 | 26 | /** 27 | * This class implements the Multivariate Normal Distribution, but augments it 28 | * so that {@link #setUsingData(jsat.DataSet, boolean) fitting} the distribution 29 | * uses a robust estimate of the distribution parameters. This comes at 30 | * increased cost that is cubic with respect to the number of variables. 31 | * 32 | * @author Edward Raff 33 | */ 34 | public class NormalMR extends NormalM 35 | { 36 | 37 | @Override 38 | public boolean setUsingData(List dataSet, boolean parallel) 39 | { 40 | try 41 | { 42 | Vec mean = new DenseVector(dataSet.get(0).length()); 43 | Matrix cov = new DenseMatrix(mean.length(), mean.length()); 44 | MatrixStatistics.FastMCD(mean, cov, dataSet, parallel); 45 | 46 | setMeanCovariance(mean, cov); 47 | return true; 48 | } 49 | catch(Exception ex) 50 | { 51 | return false; 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /JSAT/src/jsat/driftdetectors/UnhandledDriftException.java: -------------------------------------------------------------------------------- 1 | package jsat.driftdetectors; 2 | 3 | /** 4 | * This exception is thrown when a drift detector receives new data even through 5 | * the drift was not handled. 6 | * 7 | * @author Edward Raff 8 | */ 9 | public class UnhandledDriftException extends RuntimeException 10 | { 11 | 12 | 13 | private static final long serialVersionUID = -5781626293819651067L; 14 | 15 | public UnhandledDriftException() 16 | { 17 | super(); 18 | } 19 | 20 | public UnhandledDriftException(String message) 21 | { 22 | super(message); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /JSAT/src/jsat/exceptions/FailedToFitException.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.exceptions; 3 | 4 | /** 5 | * 6 | * @author Edward Raff 7 | */ 8 | public class FailedToFitException extends RuntimeException 9 | { 10 | 11 | private static final long serialVersionUID = 2982189541225068993L; 12 | private Exception faultException; 13 | 14 | public FailedToFitException(Exception faultException, String message) 15 | { 16 | super(message); 17 | this.faultException = faultException; 18 | } 19 | 20 | public FailedToFitException(Exception faultException, Throwable cause) 21 | { 22 | super(cause); 23 | this.faultException = faultException; 24 | } 25 | 26 | public FailedToFitException(Exception faultException, String message, Throwable cause) 27 | { 28 | super(message, cause); 29 | this.faultException = faultException; 30 | } 31 | 32 | public FailedToFitException(Exception faultException) 33 | { 34 | super(faultException.getMessage()); 35 | this.faultException = faultException; 36 | } 37 | 38 | public FailedToFitException(String string) 39 | { 40 | super(string); 41 | } 42 | 43 | /** 44 | * Returns the exception that caused the issue. If no exception occurred 45 | * that caused the failure to fit, the value returned will be null. 46 | * @return the exception that caused the issue. 47 | */ 48 | public Exception getFaultException() 49 | { 50 | return faultException; 51 | } 52 | 53 | 54 | } 55 | -------------------------------------------------------------------------------- /JSAT/src/jsat/exceptions/ModelMismatchException.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.exceptions; 3 | 4 | /** 5 | * This exception is thrown when the input into a model does not match the expectation of the model. 6 | * @author Edward Raff 7 | */ 8 | public class ModelMismatchException extends RuntimeException 9 | { 10 | 11 | 12 | private static final long serialVersionUID = 6962636868667470816L; 13 | 14 | public ModelMismatchException(String message, Throwable cause) 15 | { 16 | super(message, cause); 17 | } 18 | 19 | public ModelMismatchException(Throwable cause) 20 | { 21 | super(cause); 22 | } 23 | 24 | public ModelMismatchException(String message) 25 | { 26 | super(message); 27 | } 28 | 29 | public ModelMismatchException() 30 | { 31 | super(); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /JSAT/src/jsat/exceptions/UntrainedModelException.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.exceptions; 3 | 4 | /** 5 | * This exception is thrown when someone attempts to use a model that has not been trained or constructed. 6 | * @author Edward Raff 7 | */ 8 | public class UntrainedModelException extends RuntimeException 9 | { 10 | 11 | 12 | private static final long serialVersionUID = 3693546100471013277L; 13 | 14 | public UntrainedModelException(String message, Throwable cause) 15 | { 16 | super(message, cause); 17 | } 18 | 19 | public UntrainedModelException(Throwable cause) 20 | { 21 | super(cause); 22 | } 23 | 24 | public UntrainedModelException(String message) 25 | { 26 | super(message); 27 | } 28 | 29 | public UntrainedModelException() 30 | { 31 | super(); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /JSAT/src/jsat/linear/IndexValue.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.linear; 3 | 4 | /** 5 | * The value at a specified index for one dimension. This is a tool mean for use with sparce data structures. 6 | * The values should not be backed by any list, and changes to the IndexValue should not alter any data 7 | * structures. This class is mean to be returned by an iterator, and the iterator may reuse the same 8 | * IndexValue object for efficency. 9 | * 10 | * @author Edward Raff 11 | */ 12 | public class IndexValue 13 | { 14 | private int index; 15 | private double value; 16 | 17 | /** 18 | * Creates a new IndexValue 19 | * @param index the index for the given value 20 | * @param value the value at the specified index 21 | */ 22 | public IndexValue(int index, double value) 23 | { 24 | this.index = index; 25 | this.value = value; 26 | } 27 | 28 | /** 29 | * Sets the index associated with the value. 30 | * @param index the new index 31 | */ 32 | public void setIndex(int index) 33 | { 34 | this.index = index; 35 | } 36 | 37 | /** 38 | * Sets the value associated with the index 39 | * @param value the new value 40 | */ 41 | public void setValue(double value) 42 | { 43 | this.value = value; 44 | } 45 | 46 | /** 47 | * Returns the index of the stored value 48 | * @return the index of the stored value 49 | */ 50 | public int getIndex() 51 | { 52 | return index; 53 | } 54 | 55 | /** 56 | * Returns the value of the stored index 57 | * @return the value of the stored index 58 | */ 59 | public double getValue() 60 | { 61 | return value; 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /JSAT/src/jsat/linear/TransposeView.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.linear; 3 | 4 | /** 5 | * This class provides a free view of the transpose of a matrix. This is done by accessing 6 | * the matrix elements in swapped order. This has a serious performance impact. If the base 7 | * matrix storage is row major order, then accessing the TransposeView in column major order 8 | * will provide the best performance. 9 | * 10 | * @author Edward Raff 11 | */ 12 | public class TransposeView extends GenericMatrix 13 | { 14 | 15 | private static final long serialVersionUID = 7762422292840392481L; 16 | private Matrix base; 17 | 18 | public TransposeView(Matrix base) 19 | { 20 | this.base = base; 21 | } 22 | 23 | @Override 24 | public Vec getColumnView(int j) 25 | { 26 | return base.getRowView(j); 27 | } 28 | 29 | @Override 30 | public Vec getRowView(int r) 31 | { 32 | return base.getColumnView(r); 33 | } 34 | 35 | @Override 36 | protected Matrix getMatrixOfSameType(int rows, int cols) 37 | { 38 | return new DenseMatrix(rows, cols); 39 | } 40 | 41 | @Override 42 | public double get(int i, int j) 43 | { 44 | return base.get(j, i); 45 | } 46 | 47 | @Override 48 | public void set(int i, int j, double value) 49 | { 50 | base.set(j, i, value); 51 | } 52 | 53 | @Override 54 | public int rows() 55 | { 56 | return base.cols(); 57 | } 58 | 59 | @Override 60 | public int cols() 61 | { 62 | return base.rows(); 63 | } 64 | 65 | @Override 66 | public boolean isSparce() 67 | { 68 | return base.isSparce(); 69 | } 70 | 71 | @Override 72 | public void changeSize(int newRows, int newCols) 73 | { 74 | base.changeSize(newCols, newRows); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /JSAT/src/jsat/linear/VecPairedComparable.java: -------------------------------------------------------------------------------- 1 | package jsat.linear; 2 | 3 | /** 4 | * Utility class for using {@link VecPaired} when the paired value is comparable 5 | * , and the vectors need to be sorted based on their paired value. This class 6 | * performs exactly the same, and its only modification is that it is comparable 7 | * based on the paired object type. 8 | * 9 | * @author Edward Raff 10 | */ 11 | public class VecPairedComparable> extends VecPaired implements Comparable> 12 | { 13 | 14 | 15 | private static final long serialVersionUID = -7061543870162459467L; 16 | 17 | public VecPairedComparable(V v, P p) 18 | { 19 | super(v, p); 20 | } 21 | 22 | @Override 23 | public int compareTo(VecPairedComparable o) 24 | { 25 | return this.getPair().compareTo(o.getPair()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /JSAT/src/jsat/linear/distancemetrics/ChebyshevDistance.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.linear.distancemetrics; 3 | 4 | import java.util.List; 5 | import java.util.concurrent.ExecutorService; 6 | import jsat.linear.Vec; 7 | 8 | /** 9 | * Chebyshev Distance is the L norm. 10 | * 11 | * @author Edward Raff 12 | */ 13 | public class ChebyshevDistance implements DistanceMetric 14 | { 15 | 16 | private static final long serialVersionUID = 2528153647402824790L; 17 | 18 | @Override 19 | public double dist(Vec a, Vec b) 20 | { 21 | if(a.length() != b.length()) 22 | throw new ArithmeticException("Vectors must have the same length"); 23 | double max = 0; 24 | 25 | for(int i = 0; i < a.length(); i++) 26 | max = Math.max(max, Math.abs(a.get(i)-b.get(i))); 27 | 28 | return max; 29 | } 30 | 31 | @Override 32 | public boolean isSymmetric() 33 | { 34 | return true; 35 | } 36 | 37 | @Override 38 | public boolean isSubadditive() 39 | { 40 | return true; 41 | } 42 | 43 | @Override 44 | public boolean isIndiscemible() 45 | { 46 | return true; 47 | } 48 | 49 | @Override 50 | public double metricBound() 51 | { 52 | return Double.POSITIVE_INFINITY; 53 | } 54 | 55 | @Override 56 | public String toString() 57 | { 58 | return "Chebyshev Distance"; 59 | } 60 | 61 | @Override 62 | public ChebyshevDistance clone() 63 | { 64 | return new ChebyshevDistance(); 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /JSAT/src/jsat/linear/distancemetrics/DenseSparseMetric.java: -------------------------------------------------------------------------------- 1 | package jsat.linear.distancemetrics; 2 | 3 | import jsat.linear.Vec; 4 | 5 | /** 6 | * Many algorithms require computing the distances from a small set of points to 7 | * many other points. In these scenarios, if the small set of points contain 8 | * dense vectors - and the large set contain sparse vectors, a large amount of 9 | * unnecessary computation may be done. A {@link DistanceMetric} that implements 10 | * this interface indicates that it supports more efficient computation of the 11 | * distances in these scenarios.
12 | * A distance metric that can efficiently handle dense to sparse distance 13 | * computations has no reason to implement this interface. 14 | * 15 | * @author Edward Raff 16 | */ 17 | public interface DenseSparseMetric extends DistanceMetric 18 | { 19 | /** 20 | * Computes a summary constant value for the vector that is based on the 21 | * distance metric in use. This value will be used to perform efficient 22 | * dense to sparse computations. 23 | * 24 | * @param vec the vector that will be used in many distance computations 25 | * @return the summary value for the vector 26 | */ 27 | public double getVectorConstant(Vec vec); 28 | 29 | /** 30 | * Efficiently computes the distance from one main vector that is used many 31 | * times, to some sparse target vector. If the target vector dose not return 32 | * true for {@link Vec#isSparse() }, the distance will be calculated using 33 | * {@link #dist(jsat.linear.Vec, jsat.linear.Vec) } instead. 34 | * 35 | * @param summaryConst the summary constant for the main vector obtained 36 | * with {@link #getVectorConstant(jsat.linear.Vec) } 37 | * @param main the main vector the summary constant is for 38 | * @param target the target vector to compute the distance to 39 | * @return the distance between the two vectors dist(main, target) 40 | */ 41 | public double dist(double summaryConst, Vec main, Vec target); 42 | } 43 | -------------------------------------------------------------------------------- /JSAT/src/jsat/linear/distancemetrics/ManhattanDistance.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.linear.distancemetrics; 3 | 4 | import java.util.List; 5 | import java.util.concurrent.ExecutorService; 6 | import jsat.linear.IndexValue; 7 | import jsat.linear.Vec; 8 | 9 | /** 10 | * Manhattan Distance is the L1 norm. 11 | * 12 | * @author Edward Raff 13 | */ 14 | public class ManhattanDistance implements DenseSparseMetric 15 | { 16 | 17 | private static final long serialVersionUID = 3028834823742743351L; 18 | 19 | @Override 20 | public double dist(Vec a, Vec b) 21 | { 22 | return a.pNormDist(1, b); 23 | } 24 | 25 | @Override 26 | public boolean isSymmetric() 27 | { 28 | return true; 29 | } 30 | 31 | @Override 32 | public boolean isSubadditive() 33 | { 34 | return true; 35 | } 36 | 37 | @Override 38 | public boolean isIndiscemible() 39 | { 40 | return true; 41 | } 42 | 43 | @Override 44 | public double metricBound() 45 | { 46 | return Double.POSITIVE_INFINITY; 47 | } 48 | 49 | @Override 50 | public String toString() 51 | { 52 | return "Manhattan Distance"; 53 | } 54 | 55 | @Override 56 | public ManhattanDistance clone() 57 | { 58 | return new ManhattanDistance(); 59 | } 60 | 61 | @Override 62 | public double getVectorConstant(Vec vec) 63 | { 64 | return vec.pNorm(1); 65 | } 66 | 67 | @Override 68 | public double dist(double summaryConst, Vec main, Vec target) 69 | { 70 | if(!target.isSparse()) 71 | return dist(main, target); 72 | /** 73 | * Summary contains the differences to the zero vec, only a few 74 | * of the indices are actually non zero - we correct those values 75 | */ 76 | double takeOut = 0.0; 77 | for(IndexValue iv : target) 78 | { 79 | int i = iv.getIndex(); 80 | double mainVal = main.get(i); 81 | takeOut += mainVal-Math.abs(mainVal-iv.getValue()); 82 | } 83 | return summaryConst-takeOut; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /JSAT/src/jsat/linear/vectorcollection/BaseCaseDT.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 Edward Raff 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | package jsat.linear.vectorcollection; 18 | 19 | /** 20 | * 21 | * @author Edward Raff 22 | */ 23 | public interface BaseCaseDT 24 | { 25 | public double base_case(int r_indx, int q_indx); 26 | } 27 | -------------------------------------------------------------------------------- /JSAT/src/jsat/linear/vectorcollection/IncrementalCollection.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017 Edward Raff 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | package jsat.linear.vectorcollection; 18 | 19 | import jsat.linear.Vec; 20 | 21 | /** 22 | * This interface is for Vector Collections that support incremental 23 | * construction. If all data is available at the onset, it is recommended to use 24 | * the appropriate constructor / bulk insertion as they may be more compute 25 | * efficient or produce better indexes. The incremental insertion of points is 26 | * not guaranteed to result in a collection that is equally as performant in 27 | * either construction or querying. However, it does allow for additions to the 28 | * collection without needing to re-build the entire collection. Efficiency and 29 | * performance of incremental additions will depend on the base implementation. 30 | * 31 | * @author Edward Raff 32 | * @param The type of vectors stored in this collection 33 | */ 34 | public interface IncrementalCollection extends VectorCollection 35 | { 36 | /** 37 | * Incrementally adds the given datapoint into the collection 38 | * @param x the vector to add to the collection 39 | */ 40 | public void insert(V x); 41 | 42 | @Override 43 | public IncrementalCollection clone(); 44 | } 45 | -------------------------------------------------------------------------------- /JSAT/src/jsat/linear/vectorcollection/IndexDistPair.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017 Edward Raff 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package jsat.linear.vectorcollection; 19 | 20 | /** 21 | * This class exists as a helper method for use with nearest neighbor 22 | * implementations. It stores an integer to represent the index of a vector, and 23 | * a double to store the distance of the index to a given query. 24 | * 25 | * @author Edward Raff 26 | */ 27 | public class IndexDistPair implements Comparable 28 | { 29 | /** 30 | * the index of a vector 31 | */ 32 | protected int indx; 33 | 34 | /** 35 | * the distance of this index to a query vector 36 | */ 37 | protected double dist; 38 | 39 | public IndexDistPair(int indx, double dist) 40 | { 41 | this.indx = indx; 42 | this.dist = dist; 43 | } 44 | 45 | public int getIndex() 46 | { 47 | return indx; 48 | } 49 | 50 | public void setIndex(int indx) 51 | { 52 | this.indx = indx; 53 | } 54 | 55 | public double getDist() 56 | { 57 | return dist; 58 | } 59 | 60 | public void setDist(double dist) 61 | { 62 | this.dist = dist; 63 | } 64 | 65 | @Override 66 | public int compareTo(IndexDistPair o) 67 | { 68 | return Double.compare(this.dist, o.dist); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /JSAT/src/jsat/linear/vectorcollection/IndexTuple.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 Edward Raff 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | 18 | package jsat.linear.vectorcollection; 19 | 20 | /** 21 | * 22 | * @author Edward Raff 23 | */ 24 | public class IndexTuple implements Comparable 25 | { 26 | public IndexNode a; 27 | public IndexNode b; 28 | double priority; 29 | 30 | public IndexTuple(IndexNode a, IndexNode b, double priority) 31 | { 32 | this.a = a; 33 | this.b = b; 34 | this.priority = priority; 35 | } 36 | 37 | 38 | 39 | @Override 40 | public int compareTo(IndexTuple o) 41 | { 42 | return Double.compare(this.priority, o.priority); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /JSAT/src/jsat/linear/vectorcollection/ScoreDT.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 Edward Raff 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | package jsat.linear.vectorcollection; 18 | 19 | /** 20 | * 21 | * @author Edward Raff 22 | */ 23 | public interface ScoreDT 24 | { 25 | /** 26 | * 27 | * @param query 28 | * @param ref 29 | * @return {@link Double#POSITIVE_INFINITY} if the node should be pruned. 30 | */ 31 | public double score(IndexNode ref, IndexNode query); 32 | 33 | /** 34 | * This method re-scores a given reference query node pair. By default this 35 | * simply returns the original score that was given and does no computation. 36 | * If the given original score does not look valid (is less than zero), the 37 | * score will be re-computed. Some algorithms may choose to implement this 38 | * method when pruning is best done after initial depth-first traversals 39 | * have already been completed of other branches. 40 | * 41 | * @param ref 42 | * @param query 43 | * @param origScore 44 | * @return 45 | */ 46 | default double score(IndexNode ref, IndexNode query, double origScore) 47 | { 48 | if(origScore < 0) 49 | return score(ref, query); 50 | else 51 | return origScore; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /JSAT/src/jsat/linear/vectorcollection/ScoreDTLazy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 Edward Raff 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | package jsat.linear.vectorcollection; 18 | 19 | /** 20 | * 21 | * @author Edward Raff 22 | */ 23 | public interface ScoreDTLazy extends ScoreDT 24 | { 25 | 26 | @Override 27 | public double score(IndexNode ref, IndexNode query, double origScore); 28 | 29 | @Override 30 | public default double score(IndexNode ref, IndexNode query) 31 | { 32 | return score(ref, query, -1); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /JSAT/src/jsat/lossfunctions/LossC.java: -------------------------------------------------------------------------------- 1 | package jsat.lossfunctions; 2 | 3 | import jsat.classifiers.CategoricalResults; 4 | 5 | /** 6 | * Specifies a loss function for binary classification problems. 7 | * 8 | * @author Edward Raff 9 | */ 10 | public interface LossC extends LossFunc 11 | { 12 | 13 | /** 14 | * Computes the getLoss for a classification problem. 15 | * 16 | * @param pred the predicted score in (-Infinity, Infinity) 17 | * @param y the true class label in {-1, 1} 18 | * @return the getLoss in [0, Inf) 19 | */ 20 | @Override 21 | public double getLoss(double pred, double y); 22 | 23 | /** 24 | * Computes the first derivative of the getLoss function. 25 | * 26 | * @param pred the predicted score in (-Infinity, Infinity) 27 | * @param y the true class label in {-1, 1} 28 | * @return the first derivative of the getLoss 29 | */ 30 | @Override 31 | public double getDeriv(double pred, double y); 32 | 33 | /** 34 | * Computes the second derivative of the getLoss function. 35 | * 36 | * @param pred the predicted score in (-Infinity, Infinity) 37 | * @param y the true class label in {-1, 1} 38 | * @return the second derivative of the getLoss function 39 | */ 40 | @Override 41 | public double getDeriv2(double pred, double y); 42 | 43 | /** 44 | * Given the score value of a data point, this returns the classification 45 | * results. 46 | * 47 | * @param score the score for a data point 48 | * @return the categorical results with the correct probability values for 49 | * this loss function. 50 | */ 51 | public CategoricalResults getClassification(double score); 52 | 53 | @Override 54 | public LossC clone(); 55 | } 56 | -------------------------------------------------------------------------------- /JSAT/src/jsat/lossfunctions/LossR.java: -------------------------------------------------------------------------------- 1 | package jsat.lossfunctions; 2 | 3 | /** 4 | * Specifies a getLoss function for regression problems. 5 | * 6 | * @author Edward Raff 7 | */ 8 | public interface LossR extends LossFunc 9 | { 10 | 11 | /** 12 | * Computes the getLoss for a regression problem. 13 | * 14 | * @param pred the predicted value in (-Infinity, Infinity) 15 | * @param y the true target value in (-Infinity, Infinity) 16 | * @return the getLoss in [0, Inf) 17 | */ 18 | @Override 19 | public double getLoss(double pred, double y); 20 | 21 | /** 22 | * Computes the first derivative of the getLoss function. 23 | * 24 | * @param pred the predicted value in (-Infinity, Infinity) 25 | * @param y the true target value in (-Infinity, Infinity) 26 | * @return the first derivative of the getLoss 27 | */ 28 | @Override 29 | public double getDeriv(double pred, double y); 30 | 31 | /** 32 | * Computes the second derivative of the getLoss function. 33 | * 34 | * @param pred the predicted value in (-Infinity, Infinity) 35 | * @param y the true target value in (-Infinity, Infinity) 36 | * @return the second derivative of the getLoss function 37 | */ 38 | @Override 39 | public double getDeriv2(double pred, double y); 40 | 41 | /** 42 | * Given the score value of a data point, this returns the correct numeric 43 | * result. For most regression problems this simply returns the score value. 44 | * 45 | * @param score the score for a data point 46 | * @return the correct numeric regression value for this loss function 47 | */ 48 | public double getRegression(double score); 49 | 50 | @Override 51 | public LossR clone(); 52 | } 53 | -------------------------------------------------------------------------------- /JSAT/src/jsat/lossfunctions/SoftmaxLoss.java: -------------------------------------------------------------------------------- 1 | package jsat.lossfunctions; 2 | 3 | import jsat.classifiers.CategoricalResults; 4 | import jsat.linear.Vec; 5 | import jsat.math.MathTricks; 6 | 7 | /** 8 | * The Softmax loss function is a multi-class generalization of the 9 | * {@link LogisticLoss Logistic loss}. 10 | * 11 | * @author Edward Raff 12 | */ 13 | public class SoftmaxLoss extends LogisticLoss implements LossMC 14 | { 15 | 16 | private static final long serialVersionUID = 3936898932252996024L; 17 | 18 | @Override 19 | public double getLoss(Vec processed, int y) 20 | { 21 | return -Math.log(processed.get(y)); 22 | } 23 | 24 | @Override 25 | public void process(Vec pred, Vec processed) 26 | { 27 | if(pred != processed) 28 | pred.copyTo(processed); 29 | MathTricks.softmax(processed, false); 30 | } 31 | 32 | @Override 33 | public void deriv(Vec processed, Vec derivs, int y) 34 | { 35 | for(int i = 0; i < processed.length(); i++) 36 | if(i == y) 37 | derivs.set(i, processed.get(i)-1);//-(1-p) 38 | else 39 | derivs.set(i, processed.get(i));//-(0-p) 40 | } 41 | 42 | @Override 43 | public CategoricalResults getClassification(Vec processed) 44 | { 45 | return new CategoricalResults(processed.arrayCopy()); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /JSAT/src/jsat/math/Function1D.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017 Edward Raff 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | package jsat.math; 18 | 19 | /** 20 | * This functional interface defines a one dimensional (single input) function. 21 | * 22 | * @author Edward Raff 23 | */ 24 | public interface Function1D 25 | { 26 | public double f(double x); 27 | } 28 | -------------------------------------------------------------------------------- /JSAT/src/jsat/math/FunctionMat.java: -------------------------------------------------------------------------------- 1 | package jsat.math; 2 | 3 | import java.util.concurrent.ExecutorService; 4 | import jsat.linear.Matrix; 5 | import jsat.linear.Vec; 6 | 7 | /** 8 | * Interface for representing a function that should return a Matrix object as 9 | * the result. 10 | * 11 | * @author Edward Raff 12 | */ 13 | public interface FunctionMat 14 | { 15 | /** 16 | * Computes a matrix based on multivariate input 17 | * @param x the variables to evaluate as part of the function 18 | * @return the matrix output of the function 19 | */ 20 | public Matrix f(double... x); 21 | 22 | /** 23 | * Computes a matrix based on multivariate input 24 | * @param x the variables to evaluate as part of the function 25 | * @return the matrix output of the function 26 | */ 27 | public Matrix f(Vec x); 28 | 29 | /** 30 | * Computes a matrix based on multivariate input 31 | * @param x the variables to evaluate as part of the function 32 | * @param s the matrix to store the result in, or {@code null} if a new 33 | * matrix should be allocated 34 | * @return the matrix containing the results. This is the same object as 35 | * {@code s} if {@code s} is not {@code null} 36 | */ 37 | public Matrix f(Vec x, Matrix s); 38 | 39 | /** 40 | * Computes a matrix based on multivariate input 41 | * @param x the variables to evaluate as part of the function 42 | * @param s the matrix to store the result in, or {@code null} if a new 43 | * matrix should be allocated 44 | * @param ex the source of threads to use for the computation 45 | * @return the matrix containing the results. This is the same object as 46 | * {@code s} if {@code s} is not {@code null} 47 | */ 48 | public Matrix f(Vec x, Matrix s, ExecutorService ex); 49 | } 50 | -------------------------------------------------------------------------------- /JSAT/src/jsat/math/FunctionVec.java: -------------------------------------------------------------------------------- 1 | package jsat.math; 2 | 3 | import jsat.linear.DenseVector; 4 | import jsat.linear.Vec; 5 | 6 | /** 7 | * Interface for representing a function that takes a vector as input should 8 | * return a vector as the output. 9 | * 10 | * @author Edward Raff 11 | */ 12 | public interface FunctionVec 13 | { 14 | /** 15 | * Computes the function value given the input {@code x} 16 | * @param x the input to compute the output from 17 | * @return the vector containing the results 18 | */ 19 | default public Vec f(double... x) 20 | { 21 | return f(DenseVector.toDenseVec(x)); 22 | } 23 | 24 | /** 25 | * Computes the function value given the input {@code x} 26 | * @param x the input to compute the output from 27 | * @return the vector containing the results 28 | */ 29 | default public Vec f(Vec x) 30 | { 31 | return f(x, null); 32 | } 33 | 34 | /** 35 | * Computes the function value given the input {@code x} 36 | * @param x the input to compute the output from 37 | * @param s the vector to store the result in, or {@code null} if a new 38 | * vector should be allocated 39 | * @return the vector containing the results. This is the same object as 40 | * {@code s} if {@code s} is not {@code null} 41 | */ 42 | default public Vec f(Vec x, Vec s) 43 | { 44 | return f(x, s, false); 45 | } 46 | 47 | /** 48 | * Computes the function value given the input {@code x} 49 | * @param x the input to compute the output from 50 | * @param s the vector to store the result in, or {@code null} if a new 51 | * vector should be allocated 52 | * @param parallel {@code true} if multiple threads should be used for 53 | * evaluation, {@code false} if only a single thread should. 54 | * @return the vector containing the results. This is the same object as 55 | * {@code s} of {@code s} is not {@code null} 56 | */ 57 | public Vec f(Vec x, Vec s, boolean parallel); 58 | } 59 | -------------------------------------------------------------------------------- /JSAT/src/jsat/math/IndexFunction.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.math; 3 | 4 | import jsat.linear.Vec; 5 | 6 | /** 7 | * 8 | * @author Edward Raff 9 | */ 10 | public interface IndexFunction extends Function 11 | { 12 | /** 13 | * An index function, meant to be applied to vectors where the 14 | * value to be computed may vary based on the position in the 15 | * vector of the value. 16 | * 17 | * @param value the value at the specified index 18 | * @param index the index the value is from 19 | * @return the computed result. If a negative index was given, 20 | * the function should return 0.0 if indexFunc(0,indx) would 21 | * return zero for all valid indices. If this is not the case, any non zero value should be returned. 22 | * 23 | */ 24 | abstract public double indexFunc(double value, int index); 25 | 26 | @Override 27 | default public double f(Vec x, boolean parallel) 28 | { 29 | return indexFunc(x.get(0), (int)x.get(1)); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /JSAT/src/jsat/math/SimpleLinearRegression.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.math; 3 | 4 | import jsat.linear.Vec; 5 | 6 | /** 7 | * 8 | * @author Edward Raff 9 | */ 10 | public class SimpleLinearRegression 11 | { 12 | /** 13 | * Performs a Simple Linear Regression on the data set, calculating the best fit a and b such that y = a + b * x

14 | * 15 | * @param yData the Y data set (to be predicted) 16 | * @param xData the X data set (the predictor) 17 | * @return an array containing the a and b, such that index 0 contains a and index 1 contains b 18 | */ 19 | static public double[] regres(Vec xData, Vec yData) 20 | { 21 | //find y = a + B *x 22 | double[] toReturn = new double[2]; 23 | 24 | //B value 25 | toReturn[1] = DescriptiveStatistics.sampleCorCoeff(xData, yData)*yData.standardDeviation()/xData.standardDeviation(); 26 | //a value 27 | toReturn[0] = yData.mean() - toReturn[1]*xData.mean(); 28 | 29 | return toReturn; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /JSAT/src/jsat/math/TrigMath.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.math; 3 | import static java.lang.Math.*; 4 | 5 | /** 6 | * This class includes additional trig and hyperbolic trig that 7 | * does not come with Java.Math by default. 8 | * 9 | * @author Edward Raff 10 | */ 11 | public class TrigMath 12 | { 13 | 14 | public static double coth(double x) 15 | { 16 | double eX = exp(x); 17 | double eNX = exp(-x); 18 | 19 | return (eX + eNX) / (eX - eNX); 20 | } 21 | 22 | public static double sech(double x) 23 | { 24 | return 2 / (exp(x) + exp(-x)); 25 | } 26 | 27 | public static double csch(double x) 28 | { 29 | return 2 / (exp(x) - exp(-x)); 30 | } 31 | 32 | public static double asinh(double x) 33 | { 34 | return log(x + sqrt(x*x + 1)); 35 | } 36 | 37 | public static double acosh(double x) 38 | { 39 | if(x < 1) 40 | return Double.NaN;//Complex result 41 | return log(x + sqrt(x*x - 1)); 42 | } 43 | 44 | public static double atanh(double x) 45 | { 46 | if(abs(x) >= 1) 47 | return Double.NaN; 48 | return 0.5* log((x+1) / (x-1)); 49 | } 50 | 51 | public static double asech(double x) 52 | { 53 | if(x <= 0 || x > 1) 54 | return Double.NaN; 55 | return log((1 + sqrt(1-x*x))/x); 56 | } 57 | 58 | public static double acsch(double x) 59 | { 60 | return log(1/x + sqrt(1+x*x)/abs(x)); 61 | } 62 | 63 | public static double acotch(double x) 64 | { 65 | if(abs(x) <= 1) 66 | return Double.NaN; 67 | return 0.5* log((x+1) / (x-1)); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /JSAT/src/jsat/math/decayrates/DecayRate.java: -------------------------------------------------------------------------------- 1 | package jsat.math.decayrates; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Many algorithms use a learning rate to adjust the step size by which the 7 | * search space is covered. In practice, it is often useful to reduce this 8 | * learning rate over time. In this way, large steps can be taken in the 9 | * beginning when we are far from the solution, and smaller steps when we have 10 | * gotten closer to the solution and do not want to step too far away. 11 | * 12 | * @author Edward Raff 13 | */ 14 | public interface DecayRate extends Serializable 15 | { 16 | /** 17 | * Decays the initial value over time. 18 | * 19 | * @param time the current time through the algorithm in the range 20 | * [0, maxTime] 21 | * @param maxTime the maximum time step that will be seen 22 | * @param initial the initial value 23 | * @return the decayed value over time of the initial value 24 | * @throws ArithmeticException if the time is negative 25 | */ 26 | public double rate(double time, double maxTime, double initial); 27 | 28 | /** 29 | * Decays the initial value over time. 30 | * 31 | * @param time the current time step to return a value for 32 | * @param initial the initial learning rate 33 | * @return the decayed value 34 | */ 35 | public double rate(double time, double initial); 36 | 37 | public DecayRate clone(); 38 | } 39 | -------------------------------------------------------------------------------- /JSAT/src/jsat/math/decayrates/NoDecay.java: -------------------------------------------------------------------------------- 1 | package jsat.math.decayrates; 2 | 3 | /** 4 | * A possible value for a decaying learning rate. NoDecay will perform no 5 | * decaying of the initial value, the initial value will always be returned 6 | * regardless of the input. 7 | * 8 | * @author Edward Raff 9 | */ 10 | public class NoDecay implements DecayRate 11 | { 12 | 13 | private static final long serialVersionUID = -4502356199281880268L; 14 | 15 | @Override 16 | public double rate(double time, double maxTime, double initial) 17 | { 18 | return rate(time, initial); 19 | } 20 | 21 | @Override 22 | public double rate(double time, double initial) 23 | { 24 | if(time < 0) 25 | throw new ArithmeticException("Negative time value given"); 26 | return initial; 27 | } 28 | 29 | @Override 30 | public DecayRate clone() 31 | { 32 | return new NoDecay(); 33 | } 34 | 35 | @Override 36 | public String toString() 37 | { 38 | return "NoDecay"; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /JSAT/src/jsat/math/integration/Trapezoidal.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.math.integration; 3 | 4 | import jsat.math.Function1D; 5 | 6 | /** 7 | * This class provides an implementation of the Trapezoidal method for 8 | * numerically computing an integral 9 | * 10 | * @author Edward Raff 11 | */ 12 | public class Trapezoidal 13 | { 14 | /** 15 | * Numerically computes the integral of the given function 16 | * 17 | * @param f the function to integrate 18 | * @param a the lower limit of the integral 19 | * @param b the upper limit of the integral 20 | * @param N the number of points in the integral to take, must be ≥ 2. 21 | * @return an approximation of the integral of 22 | * ∫abf(x) , dx 23 | */ 24 | static public double trapz(Function1D f, double a, double b, int N) 25 | { 26 | if(a == b) 27 | return 0; 28 | else if(a > b) 29 | throw new RuntimeException("Integral upper limit (" + b+") must be larger than the lower-limit (" + a + ")"); 30 | else if(N < 1) 31 | throw new RuntimeException("At least two integration parts must be used, not " + N); 32 | /* 33 | * b / N - 1 \ 34 | * / | ===== | 35 | * | b - a |f(a) + f(b) \ / k (b - a)\| 36 | * | f(x) dx = ----- |----------- + > f|a + ---------|| 37 | * | N | 2 / \ N /| 38 | * / | ===== | 39 | * a \ k = 1 / 40 | */ 41 | double sum =0; 42 | for(int k = 1; k < N; k++) 43 | sum += f.f(a+k*(b-a)/N); 44 | 45 | sum+= (f.f(a)+f.f(b))/2; 46 | 47 | return (b-a)/N*sum; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /JSAT/src/jsat/math/optimization/GoldenSearch.java: -------------------------------------------------------------------------------- 1 | package jsat.math.optimization; 2 | 3 | import jsat.math.Function1D; 4 | 5 | /** 6 | * Minimizes a single variate function in the same way that 7 | * 8 | * @author Edward Raff 9 | */ 10 | public class GoldenSearch 11 | { 12 | /** 13 | * Phi (golden ratio) minus 1 14 | */ 15 | private static final double tau = (Math.sqrt(5.0) - 1.0)/2.0; 16 | private static final double om_tau = 1-tau; 17 | 18 | /** 19 | * Finds the local minimum of the function {@code f}. 20 | * @param eps the desired accuracy of the result 21 | * @param maxIterations the maximum number of iterations to perform 22 | * @param a the left bound on the minimum 23 | * @param b the right bound on the minimum 24 | * @param f the function to find the minimize of 25 | 26 | * @return the value of variable {@code pos} that produces the local minima 27 | */ 28 | public static double minimize(double eps, int maxIterations, double a, double b, Function1D f) 29 | { 30 | if (a > b) 31 | { 32 | double tmp = b; 33 | b = a; 34 | a = tmp; 35 | } 36 | 37 | //Intitial values 38 | int iter = 0; 39 | 40 | double x1 = a + om_tau*(b-a); 41 | double f1 = f.f(x1); 42 | 43 | double x2 = a + tau*(b-a); 44 | double f2 = f.f(x2); 45 | 46 | while (b - a > 2 * eps && iter < maxIterations) 47 | { 48 | if(f1 > f2) 49 | { 50 | a = x1; 51 | x1 = x2; 52 | f1 = f2; 53 | x2 = a + tau*(b-a); 54 | f2 = f.f(x2); 55 | } 56 | else//f1 < f2 57 | { 58 | b = x2; 59 | x2 = x1; 60 | f2 = f1; 61 | x1 = a + om_tau*(b-a); 62 | f1 = f.f(x1); 63 | } 64 | iter++; 65 | } 66 | 67 | return (a + b) / 2.0; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /JSAT/src/jsat/math/optimization/stochastic/SimpleSGD.java: -------------------------------------------------------------------------------- 1 | package jsat.math.optimization.stochastic; 2 | 3 | import jsat.linear.Vec; 4 | 5 | /** 6 | * Performs unaltered Stochastic Gradient Decent updates computing 7 | * x = x- η grad
8 | *
9 | * Because the SimpleSGD requires no internal state, it is not necessary to call 10 | * {@link #setup(int) }. 11 | * 12 | * @author Edward Raff 13 | */ 14 | public class SimpleSGD implements GradientUpdater 15 | { 16 | 17 | 18 | private static final long serialVersionUID = 4022442467298319553L; 19 | 20 | /** 21 | * Creates a new SGD updater 22 | */ 23 | public SimpleSGD() 24 | { 25 | } 26 | 27 | @Override 28 | public void update(Vec x, Vec grad, double eta) 29 | { 30 | x.mutableSubtract(eta, grad); 31 | } 32 | 33 | @Override 34 | public double update(Vec x, Vec grad, double eta, double bias, double biasGrad) 35 | { 36 | x.mutableSubtract(eta, grad); 37 | return eta*biasGrad; 38 | } 39 | 40 | @Override 41 | public SimpleSGD clone() 42 | { 43 | return new SimpleSGD(); 44 | } 45 | 46 | @Override 47 | public void setup(int d) 48 | { 49 | //no setup to be done 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /JSAT/src/jsat/math/rootfinding/RootFinder.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.math.rootfinding; 3 | 4 | import java.io.Serializable; 5 | import jsat.math.Function1D; 6 | 7 | /** 8 | * This interface defines a general contract for the numerical computation of a 9 | * root of a given function. A root of a function {@code f} is a point {@code x} 10 | * for which {@code f(x) = 0}. A function may have any number of roots 11 | * (including no roots). 12 | 13 | * 14 | * @author Edward Raff 15 | */ 16 | public interface RootFinder extends Serializable 17 | { 18 | /** 19 | * Attempts to numerical compute the root of a given function, such that f(args) = 0. Only one variable may be altered at a time 20 | * 21 | * @param eps the accuracy desired for the solution 22 | * @param maxIterations the maximum number of steps allowed before forcing a return of the current solution. 23 | * @param initialGuesses an array containing the initial guess values 24 | * @param f the function to find the root of 25 | * @param pos the index of the argument that will be allowed to alter in order to find the root. Starts from 0 26 | * @param args the values to be passed to the function as arguments 27 | * @return the value of the variable at the index pos that makes the function return 0 28 | */ 29 | public double root(double eps, int maxIterations, double[] initialGuesses, Function1D f); 30 | 31 | /** 32 | * Different root finding methods require different numbers of initial guesses. 33 | * Some root finding methods require 2 guesses, each with values of opposite 34 | * sign so that they bracket the root. Others just need any 2 initial guesses 35 | * sufficiently close to the root. This method simply returns the number of 36 | * guesses that are needed. 37 | * 38 | * @return the number of initial guesses this root finding method needs 39 | */ 40 | public int guessesNeeded(); 41 | } 42 | -------------------------------------------------------------------------------- /JSAT/src/jsat/outlier/Outlier.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2018 Edward Raff 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | package jsat.outlier; 18 | 19 | import java.io.Serializable; 20 | import jsat.DataSet; 21 | import jsat.classifiers.DataPoint; 22 | 23 | /** 24 | * 25 | * @author Edward Raff 26 | */ 27 | public interface Outlier extends Serializable 28 | { 29 | default public void fit(DataSet d) 30 | { 31 | fit(d, false); 32 | } 33 | 34 | public void fit(DataSet d, boolean parallel); 35 | 36 | /** 37 | * Returns an unbounded anomaly/outlier score. Negative values indicate the 38 | * input is likely to be an outlier, and positive values that the input is 39 | * likely to be an inlier. 40 | * 41 | * @param x 42 | * @return 43 | */ 44 | public double score(DataPoint x); 45 | 46 | default public boolean isOutlier(DataPoint x) 47 | { 48 | return score(x) < 0 ; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /JSAT/src/jsat/parameters/BooleanParameter.java: -------------------------------------------------------------------------------- 1 | package jsat.parameters; 2 | 3 | /** 4 | * A boolean parameter that may be altered. 5 | * 6 | * @author Edward Raff 7 | */ 8 | public abstract class BooleanParameter extends Parameter 9 | { 10 | 11 | private static final long serialVersionUID = 4961692453234546675L; 12 | 13 | /** 14 | * Returns the current value for the parameter. 15 | * @return the value for this parameter. 16 | */ 17 | abstract public boolean getValue(); 18 | 19 | /** 20 | * Sets the value for this parameter. 21 | * @return true if the value was set, false if the value 22 | * was invalid, and thus ignored. 23 | */ 24 | abstract public boolean setValue(boolean val); 25 | 26 | @Override 27 | public String getValueString() 28 | { 29 | return Boolean.toString(getValue()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /JSAT/src/jsat/parameters/DecayRateParameter.java: -------------------------------------------------------------------------------- 1 | package jsat.parameters; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | import jsat.math.decayrates.DecayRate; 6 | import jsat.math.decayrates.ExponetialDecay; 7 | import jsat.math.decayrates.InverseDecay; 8 | import jsat.math.decayrates.LinearDecay; 9 | import jsat.math.decayrates.NoDecay; 10 | 11 | /** 12 | * A parameter for changing between the default {@link DecayRate decay rates}. 13 | * 14 | * @author Edward Raff 15 | */ 16 | public abstract class DecayRateParameter extends ObjectParameter 17 | { 18 | 19 | 20 | private static final long serialVersionUID = -3751128637789053385L; 21 | 22 | @Override 23 | public List parameterOptions() 24 | { 25 | return Arrays.asList(new NoDecay(), new LinearDecay(), new ExponetialDecay(), new InverseDecay()); 26 | } 27 | 28 | @Override 29 | public String getASCIIName() 30 | { 31 | return "Decay Rate"; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /JSAT/src/jsat/parameters/DoubleParameter.java: -------------------------------------------------------------------------------- 1 | package jsat.parameters; 2 | 3 | import jsat.DataSet; 4 | import jsat.classifiers.ClassificationDataSet; 5 | import jsat.distributions.Distribution; 6 | import jsat.regression.RegressionDataSet; 7 | 8 | /** 9 | * A double parameter that may be altered. 10 | * 11 | * @author Edward Raff 12 | */ 13 | public abstract class DoubleParameter extends Parameter 14 | { 15 | 16 | private static final long serialVersionUID = 4132422231433472554L; 17 | 18 | /** 19 | * Returns the current value for the parameter. 20 | * 21 | * @return the value for this parameter. 22 | */ 23 | abstract public double getValue(); 24 | 25 | /** 26 | * Sets the value for this parameter. 27 | * @return true if the value was set, false if the value 28 | * was invalid, and thus ignored. 29 | */ 30 | abstract public boolean setValue(double val); 31 | 32 | /** 33 | * This method allows one to obtain a distribution that represents a 34 | * reasonable "guess" at the range of values that would work for this 35 | * parameter. If the DataSet is an instance of {@link ClassificationDataSet} 36 | * or {@link RegressionDataSet}, the method may choose to assume that the 37 | * value is being guessed for the specified task and change its behavior
38 | *
39 | * Providing a getGuess is not required, and returns {@code null} if 40 | * guessing is not supported. 41 | * 42 | * @param data the data with which we want a reasonable guess for this 43 | * parameter 44 | * @return a distribution that represents a reasonable guess of a good value 45 | * for this parameter given the input data 46 | */ 47 | public Distribution getGuess(DataSet data) 48 | { 49 | return null; 50 | } 51 | 52 | @Override 53 | public String getValueString() 54 | { 55 | return Double.toString(getValue()); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /JSAT/src/jsat/parameters/IntParameter.java: -------------------------------------------------------------------------------- 1 | package jsat.parameters; 2 | 3 | import jsat.DataSet; 4 | import jsat.distributions.Distribution; 5 | 6 | /** 7 | * An integer parameter that may be altered. 8 | * 9 | * @author Edward Raff 10 | */ 11 | public abstract class IntParameter extends Parameter 12 | { 13 | 14 | private static final long serialVersionUID = -8467918069240345315L; 15 | 16 | /** 17 | * Returns the current value for the parameter. 18 | * 19 | * @return the value for this parameter. 20 | */ 21 | abstract public int getValue(); 22 | 23 | /** 24 | * Sets the value for this parameter. 25 | * @return true if the value was set, false if the value 26 | * was invalid, and thus ignored. 27 | */ 28 | abstract public boolean setValue(int val); 29 | 30 | /** 31 | * This method allows one to obtain a distribution that represents a 32 | * reasonable "guess" at the range of values that would work for this 33 | * parameter. If the DataSet is an instance of {@link ClassificationDataSet} 34 | * or {@link RegressionDataSet}, the method may choose to assume that the 35 | * value is being guessed for the specified task and change its behavior
36 | *
37 | * Providing a getGuess is not required, and returns {@code null} if 38 | * guessing is not supported. 39 | * 40 | * @param data the data with which we want a reasonable guess for this 41 | * parameter 42 | * @return a distribution that represents a reasonable guess of a good value 43 | * for this parameter given the input data 44 | */ 45 | public Distribution getGuess(DataSet data) 46 | { 47 | return null; 48 | } 49 | 50 | @Override 51 | public String getValueString() 52 | { 53 | return Integer.toString(getValue()); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /JSAT/src/jsat/parameters/KernelFunctionParameter.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.parameters; 3 | 4 | import java.util.*; 5 | import jsat.distributions.empirical.kernelfunc.*; 6 | 7 | /** 8 | * A default Parameter semi-implementation for classes that require a 9 | * {@link KernelFunction} to be specified. 10 | * 11 | * @author Edward Raff 12 | */ 13 | public abstract class KernelFunctionParameter extends ObjectParameter 14 | { 15 | 16 | private static final long serialVersionUID = 2100826688956817533L; 17 | private final static List kernelFuncs = Collections.unmodifiableList(new ArrayList() 18 | {/** 19 | * 20 | */ 21 | private static final long serialVersionUID = 4910454799262834767L; 22 | 23 | { 24 | add(UniformKF.getInstance()); 25 | add(EpanechnikovKF.getInstance()); 26 | add(GaussKF.getInstance()); 27 | add(BiweightKF.getInstance()); 28 | add(TriweightKF.getInstance()); 29 | }}); 30 | 31 | @Override 32 | public List parameterOptions() 33 | { 34 | return kernelFuncs; 35 | } 36 | 37 | @Override 38 | public String getASCIIName() 39 | { 40 | return "Kernel Function"; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /JSAT/src/jsat/parameters/MetricParameter.java: -------------------------------------------------------------------------------- 1 | package jsat.parameters; 2 | 3 | import jsat.linear.distancemetrics.DistanceMetric; 4 | 5 | /** 6 | * A MetricParameter is a parameter controller for the {@link DistanceMetric} 7 | * used by the current algorithm. 8 | * 9 | * @author Edward Raff 10 | */ 11 | public abstract class MetricParameter extends Parameter 12 | { 13 | 14 | private static final long serialVersionUID = -8525270531723322719L; 15 | 16 | /** 17 | * Sets the distance metric that should be sued 18 | * @param val the distance metric to use 19 | * @return true if the metric is valid and was set, false 20 | * if the metric was not valid for this learner and ignored. 21 | */ 22 | abstract public boolean setMetric(DistanceMetric val); 23 | 24 | /** 25 | * Returns the distance metric that was used for this learner 26 | * @return the current distance metric 27 | */ 28 | abstract public DistanceMetric getMetric(); 29 | 30 | @Override 31 | public String getASCIIName() 32 | { 33 | return "Distance Metric"; 34 | } 35 | 36 | @Override 37 | public String getValueString() 38 | { 39 | return getMetric().toString(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /JSAT/src/jsat/parameters/ObjectParameter.java: -------------------------------------------------------------------------------- 1 | package jsat.parameters; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * A parameter that could be one of a finite number of possible objects. 7 | * 8 | * @author Edward Raff 9 | */ 10 | public abstract class ObjectParameter extends Parameter 11 | { 12 | 13 | private static final long serialVersionUID = 7639067170001873762L; 14 | 15 | /** 16 | * Returns the current object value 17 | * @return the current object set for the parameter 18 | */ 19 | abstract public T getObject(); 20 | 21 | /** 22 | * Sets the parameter to the given object 23 | * @param obj the new parameter value 24 | * @return true if the value was set, false if the value 25 | * was invalid, and thus ignored. 26 | */ 27 | abstract public boolean setObject(T obj); 28 | 29 | /** 30 | * Returns a list of all possible objects that may be used as a parameter. 31 | * @return a list of all possible objects that may be used as a parameter. 32 | */ 33 | abstract public List parameterOptions(); 34 | 35 | @Override 36 | public String getValueString() 37 | { 38 | return getObject().toString(); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /JSAT/src/jsat/parameters/Parameterized.java: -------------------------------------------------------------------------------- 1 | package jsat.parameters; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * An algorithm may be Parameterized, meaning it has one or more parameters that 7 | * can be tuned or alter the results of the algorithm in question. 8 | * 9 | * @author Edward Raff 10 | */ 11 | public interface Parameterized 12 | { 13 | /** 14 | * Returns the list of parameters that can be altered for this learner. 15 | * @return the list of parameters that can be altered for this learner. 16 | */ 17 | default public List getParameters() 18 | { 19 | return Parameter.getParamsFromMethods(this); 20 | } 21 | 22 | /** 23 | * Returns the parameter with the given name. Two different strings may map 24 | * to a single Parameter object. An ASCII only string, and a Unicode style 25 | * string. 26 | * @param paramName the name of the parameter to obtain 27 | * @return the Parameter in question, or null if no such named Parameter exists. 28 | */ 29 | default public Parameter getParameter(String paramName) 30 | { 31 | return Parameter.toParameterMap(getParameters()).get(paramName); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /JSAT/src/jsat/regression/Regressor.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.regression; 3 | 4 | import java.io.Serializable; 5 | import java.util.concurrent.ExecutorService; 6 | import jsat.classifiers.DataPoint; 7 | 8 | /** 9 | * 10 | * @author Edward Raff 11 | */ 12 | public interface Regressor extends Cloneable, Serializable 13 | { 14 | public double regress(DataPoint data); 15 | 16 | public void train(RegressionDataSet dataSet, boolean parallel); 17 | 18 | default public void train(RegressionDataSet dataSet) 19 | { 20 | train(dataSet, false); 21 | } 22 | 23 | public boolean supportsWeightedData(); 24 | 25 | public Regressor clone(); 26 | } 27 | -------------------------------------------------------------------------------- /JSAT/src/jsat/regression/evaluation/CoefficientOfDetermination.java: -------------------------------------------------------------------------------- 1 | package jsat.regression.evaluation; 2 | 3 | /** 4 | * Uses the Coefficient of Determination, also known as R2, is an 5 | * evaluation score in [0,1]. 6 | * 7 | * @author Edward Raff 8 | */ 9 | public class CoefficientOfDetermination extends TotalHistoryRegressionScore 10 | { 11 | 12 | private static final long serialVersionUID = 1215708502913888821L; 13 | 14 | /** 15 | * Creates a new Coefficient of Determination object 16 | */ 17 | public CoefficientOfDetermination() 18 | { 19 | super(); 20 | } 21 | 22 | /** 23 | * Copy constructor 24 | * @param toCopy the object to copy 25 | */ 26 | public CoefficientOfDetermination(CoefficientOfDetermination toCopy) 27 | { 28 | super(toCopy); 29 | } 30 | 31 | @Override 32 | public double getScore() 33 | { 34 | double trueMean = truths.getVecView().mean(); 35 | double numer = 0, denom = 0; 36 | for(int i = 0; i < truths.size(); i++) 37 | { 38 | numer += Math.pow(predictions.getD(i)-truths.getD(i), 2); 39 | denom += Math.pow(trueMean-truths.getD(i), 2); 40 | } 41 | return 1-numer/denom; 42 | } 43 | 44 | @Override 45 | public boolean lowerIsBetter() 46 | { 47 | return true; 48 | } 49 | 50 | @Override 51 | public CoefficientOfDetermination clone() 52 | { 53 | return new CoefficientOfDetermination(this); 54 | } 55 | 56 | @Override 57 | public int hashCode() 58 | {//XXX this is a strange hashcode method 59 | return getName().hashCode(); 60 | } 61 | 62 | @Override 63 | public boolean equals(Object obj) 64 | {//XXX check for equality of fields and obj == null 65 | if(this.getClass().isAssignableFrom(obj.getClass()) && obj.getClass().isAssignableFrom(this.getClass())) 66 | { 67 | return true; 68 | } 69 | return false; 70 | } 71 | 72 | @Override 73 | public String getName() 74 | { 75 | return "Coefficient of Determination"; 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /JSAT/src/jsat/regression/evaluation/RelativeSquaredError.java: -------------------------------------------------------------------------------- 1 | package jsat.regression.evaluation; 2 | 3 | /** 4 | * Uses the Sum of Squared Errors divided by the sum of the squared true values 5 | * subtracted from their mean. This produces an error metric that has no units. 6 | * 7 | * @author Edward Raff 8 | */ 9 | public class RelativeSquaredError extends TotalHistoryRegressionScore 10 | { 11 | 12 | private static final long serialVersionUID = 8377798320269626429L; 13 | 14 | /** 15 | * Creates a new Relative Squared Error object 16 | */ 17 | public RelativeSquaredError() 18 | { 19 | super(); 20 | } 21 | 22 | /** 23 | * Copy constructor 24 | * @param toCopy the object to copy 25 | */ 26 | public RelativeSquaredError(RelativeSquaredError toCopy) 27 | { 28 | super(toCopy); 29 | } 30 | 31 | @Override 32 | public double getScore() 33 | { 34 | double trueMean = truths.getVecView().mean(); 35 | double numer = 0, denom = 0; 36 | for(int i = 0; i < truths.size(); i++) 37 | { 38 | numer += Math.pow(predictions.getD(i)-truths.getD(i), 2); 39 | denom += Math.pow(trueMean-truths.getD(i), 2); 40 | } 41 | return numer/denom; 42 | } 43 | 44 | @Override 45 | public boolean lowerIsBetter() 46 | { 47 | return true; 48 | } 49 | 50 | @Override 51 | public RelativeSquaredError clone() 52 | { 53 | return new RelativeSquaredError(this); 54 | } 55 | 56 | @Override 57 | public int hashCode() 58 | {//XXX this is a strange hashcode method 59 | return getName().hashCode(); 60 | } 61 | 62 | @Override 63 | public boolean equals(Object obj) 64 | {//XXX check for equality of fields and obj == null 65 | if(this.getClass().isAssignableFrom(obj.getClass()) && obj.getClass().isAssignableFrom(this.getClass())) 66 | { 67 | return true; 68 | } 69 | return false; 70 | } 71 | 72 | @Override 73 | public String getName() 74 | { 75 | return "Relative Squared Error"; 76 | } 77 | 78 | } 79 | -------------------------------------------------------------------------------- /JSAT/src/jsat/testing/StatisticTest.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.testing; 3 | 4 | /** 5 | * 6 | * @author Edward Raff 7 | */ 8 | public interface StatisticTest 9 | { 10 | public enum H1 11 | { 12 | 13 | LESS_THAN 14 | { 15 | 16 | @Override 17 | public String toString() 18 | { 19 | return "<"; 20 | } 21 | }, 22 | GREATER_THAN 23 | { 24 | 25 | @Override 26 | public String toString() 27 | { 28 | return ">"; 29 | } 30 | }, 31 | NOT_EQUAL 32 | { 33 | 34 | @Override 35 | public String toString() 36 | { 37 | return "\u2260"; 38 | } 39 | } 40 | }; 41 | 42 | /** 43 | * 44 | * @return an array of the valid alternate hypothesis for this test 45 | */ 46 | public H1[] validAlternate(); 47 | 48 | public void setAltHypothesis(H1 h1); 49 | 50 | 51 | /** 52 | * 53 | * @return a descriptive name for the statistical test 54 | */ 55 | public String testName(); 56 | 57 | public double pValue(); 58 | 59 | } 60 | -------------------------------------------------------------------------------- /JSAT/src/jsat/testing/onesample/OneSampleTest.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.testing.onesample; 3 | 4 | import jsat.linear.Vec; 5 | import jsat.testing.StatisticTest; 6 | 7 | /** 8 | * 9 | * @author Edward Raff 10 | */ 11 | public interface OneSampleTest extends StatisticTest 12 | { 13 | /** 14 | * Sets the statistics that will be tested against an alternate hypothesis. 15 | * 16 | * @param data 17 | */ 18 | public void setTestUsingData(Vec data); 19 | 20 | public String[] getTestVars(); 21 | public void setTestVars(double[] testVars); 22 | 23 | public String getAltVar(); 24 | public void setAltVar(double altVar); 25 | 26 | public String getNullVar(); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /JSAT/src/jsat/text/GreekLetters.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.text; 3 | 4 | /** 5 | * 6 | * @author Edward Raff 7 | */ 8 | public class GreekLetters 9 | { 10 | public static final String alpha = "\u03B1"; 11 | public static final String beta = "\u03B2"; 12 | public static final String gamma = "\u03B3"; 13 | public static final String delta = "\u03B4"; 14 | public static final String epsilon = "\u03B5"; 15 | public static final String zeta = "\u03B6"; 16 | public static final String eta = "\u03B7"; 17 | public static final String theta = "\u03B8"; 18 | public static final String iota = "\u03B9"; 19 | public static final String kappa = "\u03BA"; 20 | public static final String lamda = "\u03BB"; 21 | public static final String mu = "\u03BC"; 22 | public static final String nu = "\u03BD"; 23 | public static final String xi = "\u03BE"; 24 | public static final String omicron = "\u03BF"; 25 | public static final String pi = "\u03C0"; 26 | public static final String rho = "\u03C1"; 27 | public static final String finalSigma = "\u03C2"; 28 | public static final String sigma = "\u03C3"; 29 | public static final String tau = "\u03C4"; 30 | public static final String upsilon = "\u03C5"; 31 | public static final String phi = "\u03C6"; 32 | public static final String chi = "\u03C7"; 33 | public static final String psi = "\u03C8"; 34 | public static final String omega = "\u03C9"; 35 | 36 | /** 37 | * Puts an over line on top the string s. 38 | * @param s the character to put a line over 39 | * @return the input with a line over 40 | */ 41 | public static String bar(String s) 42 | { 43 | return s + "\u0305"; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /JSAT/src/jsat/text/TextVectorCreator.java: -------------------------------------------------------------------------------- 1 | package jsat.text; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | import jsat.linear.Vec; 6 | 7 | /** 8 | * A Text Vector Creator is an object that can convert a text string into a 9 | * {@link Vec} 10 | * 11 | * @author Edward Raff 12 | */ 13 | public interface TextVectorCreator extends Serializable 14 | { 15 | /** 16 | * Converts the given input text into a vector representation. 17 | * @param input the input string 18 | * @return a vector representation 19 | */ 20 | public Vec newText(String input); 21 | 22 | /** 23 | * Converts the given input text into a vector representation 24 | * @param input the input string 25 | * @param workSpace an already allocated (but empty) string builder than can 26 | * be used as a temporary work space. 27 | * @param storageSpace an already allocated (but empty) list to place the 28 | * tokens into 29 | * @return a vector representation 30 | */ 31 | public Vec newText(String input, StringBuilder workSpace, List storageSpace); 32 | } 33 | -------------------------------------------------------------------------------- /JSAT/src/jsat/text/stemming/Stemmer.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.text.stemming; 3 | 4 | import java.io.Serializable; 5 | import java.util.List; 6 | 7 | /** 8 | * Stemmers are algorithms that attempt reduce strings to their common stem or 9 | * root word. For example, a stemmer might idly reduce "runs" "running" and 10 | * "ran" to the single stem word "run". This reduces the feature space size, 11 | * and allows multiple words that have the same meaning to be counted together. 12 | *
13 | * Do not expect perfect results from stemming. This class provides the 14 | * contract for a stemmer that does not have any word history. 15 | * 16 | * @author Edward Raff 17 | */ 18 | public abstract class Stemmer implements Serializable 19 | { 20 | 21 | private static final long serialVersionUID = 1889842876393488149L; 22 | 23 | /** 24 | * Reduce the given input to its stem word 25 | * @param word the unstemmed input word 26 | * @return the stemmed version of the word 27 | */ 28 | abstract public String stem(String word); 29 | 30 | /** 31 | * Replaces each value in the list with the stemmed version of the word 32 | * @param list the list to apply stemming to 33 | */ 34 | public void applyTo(List list) 35 | { 36 | for(int i = 0; i < list.size(); i++) 37 | list.set(i, stem(list.get(i))); 38 | } 39 | 40 | /** 41 | * Replaces each value in the array with the stemmed version of the word 42 | * @param arr the array to apply stemming to 43 | */ 44 | public void applyTo(String[] arr) 45 | { 46 | for(int i = 0; i < arr.length; i++) 47 | arr[i] = stem(arr[i]); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /JSAT/src/jsat/text/stemming/VoidStemmer.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.text.stemming; 3 | 4 | /** 5 | * The most naive of stemming possible, this class simply returns whatever string is given to it. 6 | * @author Edward Raff 7 | */ 8 | public class VoidStemmer extends Stemmer 9 | { 10 | 11 | 12 | private static final long serialVersionUID = -5059926028932641447L; 13 | 14 | public String stem(String word) 15 | { 16 | return word; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /JSAT/src/jsat/text/tokenizer/StemmingTokenizer.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.text.tokenizer; 3 | 4 | import java.util.List; 5 | import jsat.text.stemming.Stemmer; 6 | 7 | /** 8 | * 9 | * @author Edward Raff 10 | */ 11 | public class StemmingTokenizer implements Tokenizer 12 | { 13 | 14 | private static final long serialVersionUID = 2883247633791522390L; 15 | private Stemmer stemmer; 16 | private Tokenizer baseTokenizer; 17 | 18 | public StemmingTokenizer(Stemmer stemmer, Tokenizer baseTokenizer) 19 | { 20 | this.stemmer = stemmer; 21 | this.baseTokenizer = baseTokenizer; 22 | } 23 | 24 | @Override 25 | public List tokenize(String input) 26 | { 27 | List tokens = baseTokenizer.tokenize(input); 28 | stemmer.applyTo(tokens); 29 | return tokens; 30 | } 31 | 32 | @Override 33 | public void tokenize(String input, StringBuilder workSpace, List storageSpace) 34 | { 35 | baseTokenizer.tokenize(input, workSpace, storageSpace); 36 | stemmer.applyTo(storageSpace); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /JSAT/src/jsat/text/tokenizer/Tokenizer.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.text.tokenizer; 3 | 4 | import java.io.Serializable; 5 | import java.util.List; 6 | 7 | /** 8 | * Interface for taking the text of a document and breaking it up into features. 9 | * For example "This doc" might become "this" and "doc" 10 | * 11 | * @author Edward Raff 12 | */ 13 | public interface Tokenizer extends Serializable 14 | { 15 | /** 16 | * Breaks the input string into a series of tokens that may be used as 17 | * features for a classifier. The returned tokens must be either new string 18 | * objects or interned strings. If a token is returned that is backed by 19 | * the original document, memory may get leaked by processes consuming the 20 | * token.
21 | * This method should be thread safe 22 | * 23 | * @param input the string to tokenize 24 | * @return an already allocated list to place the tokens into 25 | */ 26 | public List tokenize(String input); 27 | 28 | /** 29 | * Breaks the input string into a series of tokens that may be used as 30 | * features for a classifier. The returned tokens must be either new string 31 | * objects or interned strings. If a token is returned that is backed by 32 | * the original document, memory may get leaked by processes consuming the 33 | * token.
34 | * This method should be thread safe 35 | * 36 | * @param input the string to tokenize 37 | * @param workSpace an already allocated (but empty) string builder than can 38 | * be used as a temporary work space. 39 | * @param storageSpace an already allocated (but empty) list to place the 40 | * tokens into 41 | */ 42 | public void tokenize(String input, StringBuilder workSpace, List storageSpace); 43 | } 44 | -------------------------------------------------------------------------------- /JSAT/src/jsat/text/wordweighting/BinaryWordPresent.java: -------------------------------------------------------------------------------- 1 | package jsat.text.wordweighting; 2 | 3 | import java.util.List; 4 | import jsat.linear.Vec; 5 | 6 | /** 7 | * Provides a simple binary representation of bag-of-word vectors by simply 8 | * marking a value 1.0 if the token is present, and 0.0 if the token is not 9 | * present. Nothing else is taken into account.
10 | *
11 | * This class does not require any state or configuration, so it can be used 12 | * without calling {@link #setWeight(java.util.List, java.util.List) }. 13 | * 14 | * 15 | * @author Edward Raff 16 | */ 17 | public class BinaryWordPresent implements WordWeighting 18 | { 19 | 20 | private static final long serialVersionUID = 5633647387188363706L; 21 | 22 | @Override 23 | public void setWeight(List allDocuments, List df) { 24 | //No work needed 25 | } 26 | 27 | @Override 28 | public void applyTo(Vec vec) 29 | { 30 | vec.applyIndexFunction(this); 31 | } 32 | 33 | @Override 34 | public double indexFunc(double value, int index) 35 | { 36 | if(index < 0) 37 | return 0.0; 38 | else if(value > 0.0) 39 | return 1.0; 40 | else 41 | return 0.0; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /JSAT/src/jsat/text/wordweighting/WordCount.java: -------------------------------------------------------------------------------- 1 | package jsat.text.wordweighting; 2 | 3 | import java.util.List; 4 | import jsat.linear.Vec; 5 | 6 | /** 7 | * Provides a simple representation of bag-of-word vectors by simply using the 8 | * number of occurrences for a word in a document as the weight for said word. 9 | *

10 | * This class does not require any state or configuration, so it can be used 11 | * without calling {@link #setWeight(java.util.List, java.util.List) }. 12 | * 13 | * @author Edward Raff 14 | */ 15 | public class WordCount implements WordWeighting 16 | { 17 | private static final long serialVersionUID = 4665749166722300326L; 18 | 19 | @Override 20 | public void setWeight(List allDocuments, List df) 21 | { 22 | //No work needed 23 | } 24 | 25 | @Override 26 | public void applyTo(Vec vec) 27 | { 28 | vec.applyIndexFunction(this); 29 | } 30 | 31 | @Override 32 | public double indexFunc(double value, int index) 33 | { 34 | if(index < 0) 35 | return 0.0; 36 | else if(value > 0.0) 37 | return value; 38 | else 39 | return 0.0; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /JSAT/src/jsat/text/wordweighting/WordWeighting.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.text.wordweighting; 3 | 4 | import java.util.List; 5 | import jsat.linear.Vec; 6 | import jsat.math.IndexFunction; 7 | 8 | /** 9 | * WordWeighting is an index function specifically mean for modifying the values 10 | * of a vectors used for a bag-of-words representation of text data.
11 | * Some Word weighting schemes may need information about the document 12 | * collection as a whole before constructing the weightings, and this class 13 | * provides the facilities for this to be done in a standardized manner. 14 | * 15 | * @author Edward Raff 16 | */ 17 | public interface WordWeighting extends IndexFunction 18 | { 19 | 20 | /** 21 | * Prepares the word weighting to be performed on a data set. This should be 22 | * called once before being applied to any vectors. Different WordWeightings 23 | * may require different amounts of computation to set up. 24 | * 25 | * @param allDocuments the list of all vectors that make up the set of 26 | * documents. The word vectors should be unmodified, containing the value of 27 | * how many times a word appeared in the document for each index. 28 | * @param df a list mapping each integer index of a word to how many times 29 | * that word occurred in total 30 | */ 31 | public void setWeight(List allDocuments, List df); 32 | 33 | /** 34 | * The implementation may want to pre compute come values based on the 35 | * vector it is about to be applied to. This should be called in place of 36 | * {@link Vec#applyIndexFunction(jsat.math.IndexFunction) } . The vector 37 | * should be in a bag-of-words form where each index value indicates how 38 | * many times the word for that index occurred in the document represented 39 | * by the vector. 40 | * 41 | * @param vec the vector to set up for and then alter by invoking 42 | * {@link Vec#applyIndexFunction(jsat.math.IndexFunction) } on 43 | */ 44 | public void applyTo(Vec vec); 45 | } 46 | -------------------------------------------------------------------------------- /JSAT/src/jsat/utils/ArrayUtils.java: -------------------------------------------------------------------------------- 1 | package jsat.utils; 2 | 3 | import java.util.Arrays; 4 | import java.util.Random; 5 | 6 | /** 7 | * Extra utilities for working on array types 8 | * 9 | * @author Edward Raff 10 | */ 11 | public class ArrayUtils 12 | { 13 | 14 | /** 15 | * No constructing! 16 | */ 17 | private ArrayUtils() 18 | { 19 | } 20 | 21 | /** 22 | * Converts the return value from binarySearch code such as {@link Arrays#binarySearch(double[], double) 23 | * } into a value that is guaranteed to be positive. It will be the either 24 | * the index that had the exact value, or the next position at which the 25 | * search value should be inserted. 26 | * 27 | * @param indx 28 | * @return 29 | */ 30 | public static int bsIndex2Insert(int indx) 31 | { 32 | if (indx < 0) 33 | indx = -indx - 1; 34 | return indx; 35 | } 36 | 37 | /** 38 | * Swaps values in the given array 39 | * 40 | * @param array the array to swap values in 41 | * @param rand the source of randomness for shuffling 42 | */ 43 | static public void shuffle(int[] array, Random rand) 44 | { 45 | shuffle(array, 0, array.length, rand); 46 | } 47 | 48 | /** 49 | * Shuffles the values in the given array 50 | * @param array the array to shuffle values in 51 | * @param from the first index, inclusive, to shuffle from 52 | * @param to the last index, exclusive, to shuffle from 53 | * @param rand the source of randomness for shuffling 54 | */ 55 | static public void shuffle(int[] array, int from, int to, Random rand) 56 | { 57 | for(int i = to-1; i > from; i--) 58 | swap(array, i, rand.nextInt(i)); 59 | } 60 | 61 | /** 62 | * Swaps two indices in the given array 63 | * @param array the array to perform the sawp in 64 | * @param a the first index 65 | * @param b the second index 66 | */ 67 | static public void swap(int[] array, int a, int b) 68 | { 69 | int tmp = array[a]; 70 | array[a] = array[b]; 71 | array[b] = tmp; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /JSAT/src/jsat/utils/BoundedSortedSet.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.utils; 3 | 4 | import java.util.*; 5 | 6 | /** 7 | * 8 | * A Sorted set that has a maximum number of values it will hold. 9 | * 10 | * @author Edward Raff 11 | */ 12 | public class BoundedSortedSet extends TreeSet 13 | { 14 | 15 | private static final long serialVersionUID = -4774987058243433217L; 16 | private final int maxSize; 17 | 18 | public BoundedSortedSet(int max) 19 | { 20 | super(); 21 | this.maxSize = max; 22 | } 23 | 24 | public BoundedSortedSet(int max, Comparator cmp) 25 | { 26 | super(cmp); 27 | this.maxSize = max; 28 | } 29 | 30 | @Override 31 | public boolean add(V e) 32 | { 33 | super.add(e); 34 | 35 | if(size() > maxSize) 36 | remove(last()); 37 | return true; 38 | } 39 | 40 | @Override 41 | public boolean addAll(Collection clctn) 42 | { 43 | super.addAll(clctn); 44 | while (size() > maxSize) 45 | remove(last()); 46 | return true; 47 | } 48 | 49 | /** 50 | * Returns the maximum size allowed for the bounded set 51 | * @return the maximum size allowed for the bounded set 52 | */ 53 | public int getMaxSize() 54 | { 55 | return maxSize; 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /JSAT/src/jsat/utils/IterableIterator.java: -------------------------------------------------------------------------------- 1 | package jsat.utils; 2 | 3 | import java.util.Iterator; 4 | 5 | /** 6 | * Convenience object for being able to use the for each loop on an iterator. 7 | * 8 | * @author Edward Raff 9 | */ 10 | public final class IterableIterator implements Iterable 11 | { 12 | private final Iterator iterator; 13 | 14 | public IterableIterator(Iterator iterator) 15 | { 16 | this.iterator = iterator; 17 | } 18 | 19 | @Override 20 | public Iterator iterator() 21 | { 22 | return iterator; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /JSAT/src/jsat/utils/ModifiableCountDownLatch.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.utils; 3 | 4 | import java.util.concurrent.CountDownLatch; 5 | import java.util.concurrent.Semaphore; 6 | import java.util.concurrent.atomic.AtomicInteger; 7 | 8 | /** 9 | * Provides a {@link CountDownLatch} that can have the number of counts increased as well as decreased. 10 | * @author Edward Raff 11 | */ 12 | public class ModifiableCountDownLatch 13 | { 14 | private Semaphore awaitSemaphore; 15 | private AtomicInteger count; 16 | 17 | public ModifiableCountDownLatch(int count) 18 | { 19 | this.count = new AtomicInteger(count); 20 | awaitSemaphore = new Semaphore(0); 21 | } 22 | 23 | /** 24 | * Waits until the count gets reduced to zero, and then all threads waiting will get to run. 25 | * 26 | * @throws InterruptedException 27 | */ 28 | public void await() throws InterruptedException 29 | { 30 | awaitSemaphore.acquire(); 31 | awaitSemaphore.release(); 32 | } 33 | 34 | /** 35 | * Decrements the counter. Allowing threads that have called {@link #await() } to run once the count reaches zero. 36 | */ 37 | public void countDown() 38 | { 39 | if(count.get() == 0) 40 | return; 41 | else if( count.decrementAndGet() == 0) 42 | awaitSemaphore.release(); 43 | } 44 | 45 | /** 46 | * Increments the count. Once the count has reached zero once, incrementing the count back above zero will have no affect. 47 | */ 48 | public void countUp() 49 | { 50 | count.addAndGet(1); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /JSAT/src/jsat/utils/Pair.java: -------------------------------------------------------------------------------- 1 | package jsat.utils; 2 | 3 | import java.util.Objects; 4 | 5 | /** 6 | * A simple object to hold a pair of values 7 | * @author Edward Raff 8 | */ 9 | public class Pair 10 | { 11 | private X firstItem; 12 | private Y secondItem; 13 | 14 | public Pair(X firstItem, Y secondItem) 15 | { 16 | setFirstItem(firstItem); 17 | setSecondItem(secondItem); 18 | } 19 | 20 | public void setFirstItem(X firstItem) 21 | { 22 | this.firstItem = firstItem; 23 | } 24 | 25 | public X getFirstItem() 26 | { 27 | return firstItem; 28 | } 29 | 30 | public void setSecondItem(Y secondItem) 31 | { 32 | this.secondItem = secondItem; 33 | } 34 | 35 | public Y getSecondItem() 36 | { 37 | return secondItem; 38 | } 39 | 40 | @Override 41 | public String toString() 42 | { 43 | return "(" + firstItem + ", " + secondItem + ")"; 44 | } 45 | 46 | @Override 47 | public boolean equals(Object obj) 48 | { 49 | if (this == obj) 50 | return true; 51 | if (obj == null) 52 | return false; 53 | if (getClass() != obj.getClass()) 54 | return false; 55 | final Pair other = (Pair) obj; 56 | if (!Objects.equals(this.firstItem, other.firstItem)) 57 | return false; 58 | if (!Objects.equals(this.secondItem, other.secondItem)) 59 | return false; 60 | return true; 61 | } 62 | 63 | @Override 64 | public int hashCode() 65 | { 66 | int hash = 3; 67 | hash = 41 * hash + Objects.hashCode(this.firstItem); 68 | hash = 41 * hash + Objects.hashCode(this.secondItem); 69 | return hash; 70 | } 71 | 72 | 73 | } 74 | -------------------------------------------------------------------------------- /JSAT/src/jsat/utils/PairedReturn.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.utils; 3 | 4 | /** 5 | * 6 | * Utility class that allows the returning of 2 different objects as one. 7 | * 8 | * @author Edwartd Raff 9 | */ 10 | public class PairedReturn 11 | { 12 | private final T firstItem; 13 | private final V secondItem; 14 | 15 | public PairedReturn(T t, V v) 16 | { 17 | this.firstItem = t; 18 | this.secondItem = v; 19 | } 20 | 21 | /** 22 | * Returns the first object stored. 23 | * @return the first object stored 24 | */ 25 | public T getFirstItem() 26 | { 27 | return firstItem; 28 | } 29 | 30 | /** 31 | * Returns the second object stored 32 | * @return the second object stored 33 | */ 34 | public V getSecondItem() 35 | { 36 | return secondItem; 37 | } 38 | 39 | @Override 40 | public String toString() 41 | { 42 | return "(" + getFirstItem() + ", " + getSecondItem() + ")"; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /JSAT/src/jsat/utils/ProbailityMatch.java: -------------------------------------------------------------------------------- 1 | package jsat.utils; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Class allows the arbitrary association of some object type with a probability. 7 | * @author Edward Raff 8 | */ 9 | public class ProbailityMatch implements Comparable>, Serializable 10 | { 11 | 12 | 13 | private static final long serialVersionUID = -1544116376166946986L; 14 | private double probability; 15 | private T match; 16 | 17 | public ProbailityMatch(double probability, T match) 18 | { 19 | this.probability = probability; 20 | this.match = match; 21 | } 22 | 23 | public int compareTo(ProbailityMatch t) 24 | { 25 | return new Double(probability).compareTo(t.probability); 26 | } 27 | 28 | public double getProbability() 29 | { 30 | return probability; 31 | } 32 | 33 | public void setProbability(double probability) 34 | { 35 | this.probability = probability; 36 | } 37 | 38 | public T getMatch() 39 | { 40 | return match; 41 | } 42 | 43 | public void setMatch(T match) 44 | { 45 | this.match = match; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /JSAT/src/jsat/utils/RunnableConsumer.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.utils; 3 | 4 | import java.util.concurrent.BlockingQueue; 5 | import java.util.concurrent.ExecutorService; 6 | import java.util.logging.Level; 7 | import java.util.logging.Logger; 8 | 9 | /** 10 | * The RunnableConsumer is meant to be used in conjunction with 11 | * {@link PoisonRunnable} and an {@link ExecutorService} to implement a 12 | * consumer / produce model. It will consume runnables from a queue and 13 | * immediately call its run method. Termination occurs when a 14 | * {@link PoisonRunnable} is encountered, after it's run method is called. 15 | * 16 | * @author Edward Raff 17 | */ 18 | public class RunnableConsumer implements Runnable 19 | { 20 | final private BlockingQueue jobQueue; 21 | 22 | /** 23 | * Creates a new runnable that will consume and run other runnables. 24 | * @param jobQueue the queue from which to obtain runnable objects. 25 | */ 26 | public RunnableConsumer(BlockingQueue jobQueue) 27 | { 28 | this.jobQueue = jobQueue; 29 | } 30 | 31 | @Override 32 | public void run() 33 | { 34 | while(true) 35 | { 36 | try 37 | { 38 | Runnable toRun = jobQueue.take(); 39 | 40 | toRun.run(); 41 | 42 | if(toRun instanceof PoisonRunnable) 43 | return; 44 | } 45 | catch (InterruptedException ex) 46 | { 47 | Logger.getLogger(RunnableConsumer.class.getName()).log(Level.SEVERE, null, ex); 48 | } 49 | } 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /JSAT/src/jsat/utils/concurrent/IndexReducer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017 Edward Raff 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | package jsat.utils.concurrent; 18 | 19 | /** 20 | * 21 | * @author Edward Raff 22 | * @param The object type that the IndexReducer will return 23 | */ 24 | public interface IndexReducer 25 | { 26 | public T run(int indx); 27 | } 28 | -------------------------------------------------------------------------------- /JSAT/src/jsat/utils/concurrent/IndexRunnable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017 Edward Raff 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | package jsat.utils.concurrent; 18 | 19 | /** 20 | * 21 | * @author Edward Raff 22 | */ 23 | public interface IndexRunnable 24 | { 25 | public void run(int indx); 26 | } 27 | -------------------------------------------------------------------------------- /JSAT/src/jsat/utils/concurrent/LoopChunkReducer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017 Edward Raff 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | package jsat.utils.concurrent; 18 | 19 | import java.util.function.BinaryOperator; 20 | 21 | /** 22 | * This functional interface is similar to that of {@link LoopChunkReducer}, 23 | * allowing convieniently processing of a range of values for parallel 24 | * computing. However, the Reducer returns an object implementing the 25 | * {@link BinaryOperator} interface. The goal is that all chunks will eventually 26 | * be merged into a single result. This interface is preffered over using normal 27 | * streams to reduce unecessary object creation and reductions. 28 | * 29 | * @author Edward Raff 30 | * @param The object type that the Loop Chunk Reducer will return 31 | */ 32 | public interface LoopChunkReducer 33 | { 34 | /** 35 | * Runs a process over the given loop portion, returning a single object of type {@link T}. 36 | * @param start the starting index to process 37 | * @param end the ending index to process 38 | * @return the object to return 39 | */ 40 | public T run(int start, int end); 41 | } 42 | -------------------------------------------------------------------------------- /JSAT/src/jsat/utils/concurrent/LoopChunkRunner.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2017 Edward Raff 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | package jsat.utils.concurrent; 18 | 19 | /** 20 | * 21 | * @author Edward Raff 22 | */ 23 | public interface LoopChunkRunner 24 | { 25 | /** 26 | * Runs a process over the given loop portion 27 | * @param start the starting index to process 28 | * @param end the ending index to process 29 | */ 30 | public void run(int start, int end); 31 | } 32 | -------------------------------------------------------------------------------- /JSAT/src/jsat/utils/random/CMWC4096.java: -------------------------------------------------------------------------------- 1 | package jsat.utils.random; 2 | 3 | import java.util.Random; 4 | 5 | /** 6 | * A Complement-Multiply-With-Carry PRNG. It is a fast high quality generator 7 | * that passes the Diehard tests. It has a period of over 2131083 8 | *

9 | * See: Marsaglia, G. (2005). 10 | * On the randomness of Pi and other decimal expansions. Interstat 5 11 | * 12 | * @author Edward Raff 13 | */ 14 | public class CMWC4096 extends Random 15 | { 16 | 17 | private static final long serialVersionUID = -5061963074440046713L; 18 | private static final long a = 18782; 19 | private int c = 362436; 20 | private int i = 4095; 21 | private int[] Q; 22 | 23 | /** 24 | * Creates a new PRNG with a random seed 25 | */ 26 | public CMWC4096() 27 | { 28 | super(); 29 | } 30 | 31 | /** 32 | * Creates a new PRNG 33 | * @param seed the seed that controls the initial state of the PRNG 34 | * @see #setSeed(long) 35 | */ 36 | public CMWC4096(long seed) 37 | { 38 | super(seed); 39 | } 40 | 41 | 42 | @Override 43 | public synchronized void setSeed(long seed) 44 | { 45 | super.setSeed(seed); 46 | if(Q == null) 47 | Q = new int[4096]; 48 | for (int j = 0; j < Q.length; j++) 49 | Q[j] = super.next(32); 50 | } 51 | 52 | @Override 53 | protected int next(int bits) 54 | { 55 | long t; 56 | 57 | long x, r = 0xfffffffe; 58 | i = (i + 1) & 4095; 59 | t = a * Q[i] + c; 60 | c = (int) (t >>> 32); 61 | x = t + c; 62 | if (x < c) 63 | { 64 | x++; 65 | c++; 66 | } 67 | return (Q[i] = (int) (r - x)) >>> 32 - bits; 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /JSAT/src/jsat/utils/random/XOR128.java: -------------------------------------------------------------------------------- 1 | package jsat.utils.random; 2 | 3 | import java.util.Random; 4 | 5 | /** 6 | * A fast PRNG that produces medium quality random numbers that passes the 7 | * diehard tests. It has a period of 2128-1 8 | *

9 | * See: G. Marsaglia. Xorshift RNGs. Journal of Statistical Software, 8, 10 | * 14:1–9, 2003 11 | * @author Edward Raff 12 | */ 13 | public class XOR128 extends Random 14 | { 15 | 16 | private static final long serialVersionUID = -5218902638864900490L; 17 | private long x, y, z, w; 18 | 19 | /** 20 | * Creates a new PRNG with a random seed 21 | */ 22 | public XOR128() 23 | { 24 | super(); 25 | } 26 | 27 | /** 28 | * Creates a new PRNG 29 | * @param seed the seed that controls the initial state of the PRNG 30 | * @see #setSeed(long) 31 | */ 32 | public XOR128(long seed) 33 | { 34 | super(seed); 35 | } 36 | 37 | @Override 38 | public synchronized void setSeed(long seed) 39 | { 40 | super.setSeed(seed); 41 | x = super.next(32); 42 | x = x << 32; 43 | x += super.next(32); 44 | 45 | y = super.next(32); 46 | y = y << 32; 47 | y += super.next(32); 48 | 49 | z = super.next(32); 50 | z = z << 32; 51 | z += super.next(32); 52 | 53 | w = super.next(32); 54 | w = w << 32; 55 | w += super.next(32); 56 | } 57 | 58 | @Override 59 | protected int next(int bits) 60 | { 61 | return (int)(nextLong() >>> (64 - bits)); 62 | } 63 | 64 | @Override 65 | public long nextLong() 66 | { 67 | long t; 68 | t = (x ^ (x << 11)); 69 | x = y; 70 | y = z; 71 | z = w; 72 | w = (w ^ (w >>> 19)) ^ (t ^ (t >>> 8)); 73 | return w; 74 | } 75 | 76 | @Override 77 | public double nextDouble() 78 | { 79 | long l = nextLong() >>> 11; 80 | return l / (double)(1L << 53); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /JSAT/src/jsat/utils/random/XOR96.java: -------------------------------------------------------------------------------- 1 | package jsat.utils.random; 2 | 3 | import java.util.Random; 4 | 5 | /** 6 | * A fast PRNG that produces medium quality random numbers. It has a period of 7 | * 296-1 8 | *

9 | * See: G. Marsaglia. Xorshift RNGs. Journal of Statistical Software, 8, 10 | * 14:1–9, 2003 11 | * @author Edward Raff 12 | */ 13 | public class XOR96 extends Random 14 | { 15 | 16 | private static final long serialVersionUID = 1247900882148980639L; 17 | 18 | private static final long a = 13, b = 19, c = 3;//magic from paper 19 | 20 | private long x, y, z; 21 | 22 | /** 23 | * Creates a new PRNG with a random seed 24 | */ 25 | public XOR96() 26 | { 27 | super(); 28 | } 29 | 30 | /** 31 | * Creates a new PRNG 32 | * @param seed the seed that controls the initial state of the PRNG 33 | * @see #setSeed(long) 34 | */ 35 | public XOR96(long seed) 36 | { 37 | super(seed); 38 | } 39 | 40 | @Override 41 | public synchronized void setSeed(long seed) 42 | { 43 | super.setSeed(seed); 44 | x = super.next(32); 45 | x = x << 32; 46 | x += super.next(32); 47 | 48 | y = super.next(32); 49 | y = y << 32; 50 | y += super.next(32); 51 | 52 | z = super.next(32); 53 | z = z << 32; 54 | z += super.next(32); 55 | } 56 | 57 | @Override 58 | protected int next(int bits) 59 | { 60 | return (int)(nextLong() >>> (64 - bits)); 61 | } 62 | 63 | @Override 64 | public long nextLong() 65 | { 66 | long t = (x ^ (x << a)); 67 | x = y; 68 | y = z; 69 | z = (z ^ (z >>> c)) ^ (t ^ (t >>> b)); 70 | return z; 71 | } 72 | 73 | @Override 74 | public double nextDouble() 75 | { 76 | long l = nextLong() >>> 11; 77 | return l / (double)(1L << 53); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /JSAT/test/jsat/NormalClampedSample.java: -------------------------------------------------------------------------------- 1 | package jsat; 2 | 3 | import java.util.Random; 4 | import jsat.distributions.Normal; 5 | import jsat.linear.DenseVector; 6 | 7 | /** 8 | * Helper class to avoid issues with sampling from the normal distribution when 9 | * testing since the normal can have extreme values 10 | * @author Edward Raff 11 | */ 12 | public class NormalClampedSample extends Normal 13 | { 14 | 15 | private static final long serialVersionUID = 3970933766374506189L; 16 | double min, max; 17 | 18 | public NormalClampedSample(double mean, double stndDev) 19 | { 20 | this(mean, stndDev, mean-3*stndDev, mean+3*stndDev); 21 | } 22 | 23 | public NormalClampedSample(double mean, double stndDev, double min, double max) 24 | { 25 | super(mean, stndDev); 26 | this.min = Math.min(min, max); 27 | this.max = Math.max(min, max); 28 | } 29 | 30 | @Override 31 | public double invCdf(double d) 32 | { 33 | return Math.max(min, Math.min(max, super.invCdf(d))); 34 | } 35 | 36 | @Override 37 | public double[] sample(int numSamples, Random rand) 38 | { 39 | double[] ret = super.sample(numSamples, rand); 40 | for(int i = 0; i < ret.length; i++) 41 | ret[i] = Math.max(min, Math.min(max, ret[i])); 42 | return ret; 43 | } 44 | 45 | @Override 46 | public DenseVector sampleVec(int numSamples, Random rand) 47 | { 48 | DenseVector ret = super.sampleVec(numSamples, rand); 49 | for(int i = 0; i < ret.length(); i++) 50 | ret.set(i, Math.max(min, Math.min(max, ret.get(i)))); 51 | return ret; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /JSAT/test/jsat/classifiers/linear/ALMA2Test.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | package jsat.classifiers.linear; 6 | 7 | import java.util.Random; 8 | import jsat.FixedProblems; 9 | import jsat.classifiers.*; 10 | import jsat.utils.random.RandomUtil; 11 | import org.junit.After; 12 | import org.junit.AfterClass; 13 | import org.junit.Before; 14 | import org.junit.BeforeClass; 15 | import org.junit.Test; 16 | import static org.junit.Assert.*; 17 | 18 | /** 19 | * 20 | * @author Edward Raff 21 | */ 22 | public class ALMA2Test 23 | { 24 | 25 | public ALMA2Test() 26 | { 27 | } 28 | 29 | @BeforeClass 30 | public static void setUpClass() 31 | { 32 | } 33 | 34 | @AfterClass 35 | public static void tearDownClass() 36 | { 37 | } 38 | 39 | @Before 40 | public void setUp() 41 | { 42 | } 43 | 44 | @After 45 | public void tearDown() 46 | { 47 | } 48 | 49 | /** 50 | * Test of classify method, of class ALMA2. 51 | */ 52 | @Test 53 | public void testTrain_C() 54 | { 55 | System.out.println("classify"); 56 | 57 | ClassificationDataSet train = FixedProblems.get2ClassLinear(200, RandomUtil.getRandom()); 58 | 59 | ALMA2 alma = new ALMA2(); 60 | alma.setEpochs(1); 61 | 62 | alma.train(train); 63 | 64 | ClassificationDataSet test = FixedProblems.get2ClassLinear(200, RandomUtil.getRandom()); 65 | 66 | for(DataPointPair dpp : test.getAsDPPList()) 67 | assertEquals(dpp.getPair().longValue(), alma.classify(dpp.getDataPoint()).mostLikely()); 68 | 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /JSAT/test/jsat/classifiers/linear/AROWTest.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.classifiers.linear; 3 | 4 | import java.util.Random; 5 | import jsat.FixedProblems; 6 | import jsat.classifiers.ClassificationDataSet; 7 | import jsat.classifiers.DataPointPair; 8 | import jsat.utils.random.RandomUtil; 9 | import org.junit.After; 10 | import org.junit.AfterClass; 11 | import org.junit.Before; 12 | import org.junit.BeforeClass; 13 | import org.junit.Test; 14 | import static org.junit.Assert.*; 15 | 16 | /** 17 | * 18 | * @author Edward Raff 19 | */ 20 | public class AROWTest 21 | { 22 | 23 | public AROWTest() 24 | { 25 | } 26 | 27 | @BeforeClass 28 | public static void setUpClass() 29 | { 30 | } 31 | 32 | @AfterClass 33 | public static void tearDownClass() 34 | { 35 | } 36 | 37 | @Before 38 | public void setUp() 39 | { 40 | } 41 | 42 | @After 43 | public void tearDown() 44 | { 45 | } 46 | 47 | /** 48 | * Test of classify method, of class AROW. 49 | */ 50 | @Test 51 | public void testTrain_C() 52 | { 53 | System.out.println("train_C"); 54 | ClassificationDataSet train = FixedProblems.get2ClassLinear(200, RandomUtil.getRandom()); 55 | 56 | AROW arow0 = new AROW(1, true); 57 | AROW arow1 = new AROW(1, false); 58 | 59 | arow0.train(train); 60 | arow1.train(train); 61 | 62 | 63 | ClassificationDataSet test = FixedProblems.get2ClassLinear(200, RandomUtil.getRandom()); 64 | 65 | for(DataPointPair dpp : test.getAsDPPList()) 66 | assertEquals(dpp.getPair().longValue(), arow0.classify(dpp.getDataPoint()).mostLikely()); 67 | for(DataPointPair dpp : test.getAsDPPList()) 68 | assertEquals(dpp.getPair().longValue(), arow1.classify(dpp.getDataPoint()).mostLikely()); 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /JSAT/test/jsat/classifiers/linear/NHERDTest.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.classifiers.linear; 3 | 4 | import java.util.Random; 5 | import jsat.FixedProblems; 6 | import jsat.classifiers.ClassificationDataSet; 7 | import jsat.classifiers.DataPointPair; 8 | import org.junit.AfterClass; 9 | import org.junit.BeforeClass; 10 | import org.junit.Test; 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * 15 | * @author Edward Raff 16 | */ 17 | public class NHERDTest 18 | { 19 | 20 | public NHERDTest() 21 | { 22 | } 23 | 24 | @BeforeClass 25 | public static void setUpClass() 26 | { 27 | } 28 | 29 | @AfterClass 30 | public static void tearDownClass() 31 | { 32 | } 33 | 34 | @Test 35 | public void testTrain_C() 36 | { 37 | System.out.println("train_C"); 38 | ClassificationDataSet train = FixedProblems.get2ClassLinear(200, new Random(132)); 39 | 40 | ClassificationDataSet test = FixedProblems.get2ClassLinear(200, new Random(231)); 41 | 42 | for (NHERD.CovMode mode : NHERD.CovMode.values()) 43 | { 44 | NHERD nherd0 = new NHERD(1, mode); 45 | nherd0.train(train); 46 | 47 | for (DataPointPair dpp : test.getAsDPPList()) 48 | assertEquals(dpp.getPair().longValue(), nherd0.classify(dpp.getDataPoint()).mostLikely()); 49 | } 50 | 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /JSAT/test/jsat/classifiers/linear/ROMMATest.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.classifiers.linear; 3 | 4 | import java.util.Random; 5 | import jsat.FixedProblems; 6 | import jsat.classifiers.*; 7 | import jsat.utils.random.RandomUtil; 8 | import org.junit.After; 9 | import org.junit.AfterClass; 10 | import org.junit.Before; 11 | import org.junit.BeforeClass; 12 | import org.junit.Test; 13 | import static org.junit.Assert.*; 14 | 15 | /** 16 | * 17 | * @author Edward Raff 18 | */ 19 | public class ROMMATest 20 | { 21 | 22 | public ROMMATest() 23 | { 24 | } 25 | 26 | @BeforeClass 27 | public static void setUpClass() 28 | { 29 | } 30 | 31 | @AfterClass 32 | public static void tearDownClass() 33 | { 34 | } 35 | 36 | @Before 37 | public void setUp() 38 | { 39 | } 40 | 41 | @After 42 | public void tearDown() 43 | { 44 | } 45 | 46 | /** 47 | * Test of supportsWeightedData method, of class ROMMA. 48 | */ 49 | @Test 50 | public void testTrain_C() 51 | { 52 | System.out.println("supportsWeightedData"); 53 | ROMMA nonAggro = new ROMMA(); 54 | ROMMA aggro = new ROMMA(); 55 | ClassificationDataSet train = FixedProblems.get2ClassLinear(200, RandomUtil.getRandom()); 56 | 57 | nonAggro.setEpochs(1); 58 | nonAggro.train(train); 59 | 60 | aggro.setEpochs(1); 61 | aggro.train(train); 62 | 63 | ClassificationDataSet test = FixedProblems.get2ClassLinear(200, RandomUtil.getRandom()); 64 | 65 | for(DataPointPair dpp : test.getAsDPPList()) 66 | assertEquals(dpp.getPair().longValue(), aggro.classify(dpp.getDataPoint()).mostLikely()); 67 | 68 | for(DataPointPair dpp : test.getAsDPPList()) 69 | assertEquals(dpp.getPair().longValue(), nonAggro.classify(dpp.getDataPoint()).mostLikely()); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /JSAT/test/jsat/clustering/evaluation/AdjustedRandIndexTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | 7 | package jsat.clustering.evaluation; 8 | 9 | import jsat.classifiers.CategoricalData; 10 | import jsat.classifiers.ClassificationDataSet; 11 | import jsat.linear.Vec; 12 | import org.junit.After; 13 | import org.junit.AfterClass; 14 | import org.junit.Before; 15 | import org.junit.BeforeClass; 16 | import org.junit.Test; 17 | import static org.junit.Assert.*; 18 | 19 | /** 20 | * 21 | * @author Edward Raff 22 | */ 23 | public class AdjustedRandIndexTest 24 | { 25 | 26 | public AdjustedRandIndexTest() 27 | { 28 | } 29 | 30 | @BeforeClass 31 | public static void setUpClass() 32 | { 33 | } 34 | 35 | @AfterClass 36 | public static void tearDownClass() 37 | { 38 | } 39 | 40 | @Before 41 | public void setUp() 42 | { 43 | } 44 | 45 | @After 46 | public void tearDown() 47 | { 48 | } 49 | 50 | /** 51 | * Test of evaluate method, of class AdjustedRandIndex. 52 | */ 53 | @Test 54 | public void testEvaluate_intArr_DataSet() 55 | { 56 | System.out.println("evaluate"); 57 | //using example from http://www.otlet-institute.org/wikics/Clustering_Problems.html 58 | ClassificationDataSet cds = new ClassificationDataSet(1, new CategoricalData[0], new CategoricalData(3)); 59 | for(int i = 0; i < 3; i++) 60 | for(int j = 0; j < 3; j++) 61 | cds.addDataPoint(Vec.random(1), new int[0], i); 62 | int[] d = new int[9]; 63 | d[0] = d[1] = 0; 64 | d[2] = d[3] = d[4] = d[5] = 1; 65 | d[6] = d[7] = 2; 66 | d[8] = 3; 67 | 68 | AdjustedRandIndex ari = new AdjustedRandIndex(); 69 | double score = ari.evaluate(d, cds); 70 | //conver tot ARI 71 | score = 1.0-score; 72 | assertEquals(0.46, score, 0.005); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /JSAT/test/jsat/datatransform/featureselection/ReliefFTest.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.datatransform.featureselection; 3 | 4 | import java.util.*; 5 | 6 | import jsat.classifiers.ClassificationDataSet; 7 | import jsat.linear.distancemetrics.EuclideanDistance; 8 | import jsat.utils.IntSet; 9 | import jsat.utils.random.XORWOW; 10 | 11 | import org.junit.After; 12 | import org.junit.AfterClass; 13 | import org.junit.Before; 14 | import org.junit.BeforeClass; 15 | import org.junit.Test; 16 | 17 | import static org.junit.Assert.*; 18 | 19 | /** 20 | * 21 | * @author Edward Raff 22 | */ 23 | public class ReliefFTest 24 | { 25 | 26 | public ReliefFTest() 27 | { 28 | } 29 | 30 | @BeforeClass 31 | public static void setUpClass() 32 | { 33 | } 34 | 35 | @AfterClass 36 | public static void tearDownClass() 37 | { 38 | } 39 | 40 | @Before 41 | public void setUp() 42 | { 43 | } 44 | 45 | @After 46 | public void tearDown() 47 | { 48 | } 49 | 50 | /** 51 | * Test of transform method, of class ReliefF. 52 | */ 53 | @Test 54 | public void testTransformC() 55 | { 56 | System.out.println("transformC"); 57 | Random rand = new XORWOW(13); 58 | int t0 = 1, t1 = 5, t2 = 8; 59 | Set shouldHave = new IntSet(); 60 | shouldHave.addAll(Arrays.asList(t0, t1, t2)); 61 | 62 | ClassificationDataSet cds = SFSTest. 63 | generate3DimIn10(rand, t0, t1, t2); 64 | 65 | ReliefF relieff = new ReliefF(3, 50, 7, new EuclideanDistance()).clone(); 66 | relieff.fit(cds); 67 | Set found = new IntSet(relieff.getKeptNumeric()); 68 | 69 | assertEquals(shouldHave.size(), found.size()); 70 | assertTrue(shouldHave.containsAll(found)); 71 | cds.applyTransform(relieff); 72 | assertEquals(shouldHave.size(), cds.getNumFeatures()); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /JSAT/test/jsat/distributions/KolmogorovTest.java: -------------------------------------------------------------------------------- 1 | package jsat.distributions; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.After; 6 | import org.junit.AfterClass; 7 | import org.junit.Before; 8 | import org.junit.BeforeClass; 9 | import org.junit.Test; 10 | 11 | public class KolmogorovTest { 12 | 13 | @BeforeClass 14 | public static void setUpBeforeClass() throws Exception { 15 | } 16 | 17 | @AfterClass 18 | public static void tearDownAfterClass() throws Exception { 19 | } 20 | 21 | @Before 22 | public void setUp() throws Exception { 23 | } 24 | 25 | @After 26 | public void tearDown() throws Exception { 27 | } 28 | 29 | @Test 30 | public void testEquals(){ 31 | System.out.println("equals"); 32 | ContinuousDistribution d1 = new Kolmogorov(); 33 | ContinuousDistribution d4 = new Kolmogorov(); 34 | Integer i = new Integer(1); 35 | assertFalse(d1.equals(i)); 36 | assertFalse(d1.equals(null)); 37 | assertEquals(d1, d1); 38 | assertEquals(d1, d4); 39 | assertEquals(d1, d1.clone()); 40 | } 41 | 42 | @Test 43 | public void testHashCode(){ 44 | System.out.println("hashCode"); 45 | ContinuousDistribution d1 = new Kolmogorov(); 46 | ContinuousDistribution d4 = new Kolmogorov(); 47 | assertEquals(d1.hashCode(), d4.hashCode()); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /JSAT/test/jsat/distributions/LaplaceTest.java: -------------------------------------------------------------------------------- 1 | package jsat.distributions; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.After; 6 | import org.junit.AfterClass; 7 | import org.junit.Before; 8 | import org.junit.BeforeClass; 9 | import org.junit.Test; 10 | 11 | public class LaplaceTest { 12 | 13 | @BeforeClass 14 | public static void setUpBeforeClass() throws Exception { 15 | } 16 | 17 | @AfterClass 18 | public static void tearDownAfterClass() throws Exception { 19 | } 20 | 21 | @Before 22 | public void setUp() throws Exception { 23 | } 24 | 25 | @After 26 | public void tearDown() throws Exception { 27 | } 28 | 29 | @Test 30 | public void testEquals(){ 31 | System.out.println("equals"); 32 | ContinuousDistribution d1 = new Laplace(0.5, 0.5); 33 | ContinuousDistribution d2 = new Laplace(0.6, 0.5); 34 | ContinuousDistribution d3 = new Laplace(0.5, 0.6); 35 | ContinuousDistribution d4 = new Laplace(0.5, 0.5); 36 | Integer i = new Integer(1); 37 | assertFalse(d1.equals(d2)); 38 | assertFalse(d1.equals(d3)); 39 | assertFalse(d2.equals(d3)); 40 | assertFalse(d1.equals(i)); 41 | assertFalse(d1.equals(null)); 42 | assertEquals(d1, d1); 43 | assertEquals(d1, d4); 44 | assertEquals(d1, d1.clone()); 45 | } 46 | 47 | @Test 48 | public void testHashCode(){ 49 | System.out.println("hashCode"); 50 | ContinuousDistribution d1 = new Laplace(0.5, 0.5); 51 | ContinuousDistribution d2 = new Laplace(0.6, 0.5); 52 | ContinuousDistribution d4 = new Laplace(0.5, 0.5); 53 | assertEquals(d1.hashCode(), d4.hashCode()); 54 | assertFalse(d1.hashCode()==d2.hashCode()); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /JSAT/test/jsat/distributions/LevyTest.java: -------------------------------------------------------------------------------- 1 | package jsat.distributions; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.After; 6 | import org.junit.AfterClass; 7 | import org.junit.Before; 8 | import org.junit.BeforeClass; 9 | import org.junit.Test; 10 | 11 | public class LevyTest { 12 | 13 | @BeforeClass 14 | public static void setUpBeforeClass() throws Exception { 15 | } 16 | 17 | @AfterClass 18 | public static void tearDownAfterClass() throws Exception { 19 | } 20 | 21 | @Before 22 | public void setUp() throws Exception { 23 | } 24 | 25 | @After 26 | public void tearDown() throws Exception { 27 | } 28 | 29 | @Test 30 | public void testEquals(){ 31 | System.out.println("equals"); 32 | ContinuousDistribution d1 = new Levy(0.5, 0.5); 33 | ContinuousDistribution d2 = new Levy(0.6, 0.5); 34 | ContinuousDistribution d3 = new Levy(0.5, 0.6); 35 | ContinuousDistribution d4 = new Levy(0.5, 0.5); 36 | Integer i = new Integer(1); 37 | assertFalse(d1.equals(d2)); 38 | assertFalse(d1.equals(d3)); 39 | assertFalse(d2.equals(d3)); 40 | assertFalse(d1.equals(i)); 41 | assertFalse(d1.equals(null)); 42 | assertEquals(d1, d1); 43 | assertEquals(d1, d4); 44 | assertEquals(d1, d1.clone()); 45 | } 46 | 47 | @Test 48 | public void testHashCode(){ 49 | System.out.println("hashCode"); 50 | ContinuousDistribution d1 = new Levy(0.5, 0.5); 51 | ContinuousDistribution d2 = new Levy(0.6, 0.5); 52 | ContinuousDistribution d4 = new Levy(0.5, 0.5); 53 | assertEquals(d1.hashCode(), d4.hashCode()); 54 | assertFalse(d1.hashCode()==d2.hashCode()); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /JSAT/test/jsat/distributions/LogNormalTest.java: -------------------------------------------------------------------------------- 1 | package jsat.distributions; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.After; 6 | import org.junit.AfterClass; 7 | import org.junit.Before; 8 | import org.junit.BeforeClass; 9 | import org.junit.Test; 10 | 11 | public class LogNormalTest { 12 | 13 | @BeforeClass 14 | public static void setUpBeforeClass() throws Exception { 15 | } 16 | 17 | @AfterClass 18 | public static void tearDownAfterClass() throws Exception { 19 | } 20 | 21 | @Before 22 | public void setUp() throws Exception { 23 | } 24 | 25 | @After 26 | public void tearDown() throws Exception { 27 | } 28 | 29 | @Test 30 | public void testEquals(){ 31 | System.out.println("equals"); 32 | ContinuousDistribution d1 = new LogNormal(0.5, 0.5); 33 | ContinuousDistribution d2 = new LogNormal(0.6, 0.5); 34 | ContinuousDistribution d3 = new LogNormal(0.5, 0.6); 35 | ContinuousDistribution d4 = new LogNormal(0.5, 0.5); 36 | Integer i = new Integer(1); 37 | assertFalse(d1.equals(d2)); 38 | assertFalse(d1.equals(d3)); 39 | assertFalse(d2.equals(d3)); 40 | assertFalse(d1.equals(i)); 41 | assertFalse(d1.equals(null)); 42 | assertEquals(d1, d1); 43 | assertEquals(d1, d4); 44 | assertEquals(d1, d1.clone()); 45 | } 46 | 47 | @Test 48 | public void testHashCode(){ 49 | System.out.println("hashCode"); 50 | ContinuousDistribution d1 = new LogNormal(0.5, 0.5); 51 | ContinuousDistribution d2 = new LogNormal(0.6, 0.5); 52 | ContinuousDistribution d4 = new LogNormal(0.5, 0.5); 53 | assertEquals(d1.hashCode(), d4.hashCode()); 54 | assertFalse(d1.hashCode()==d2.hashCode()); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /JSAT/test/jsat/distributions/LogisticTest.java: -------------------------------------------------------------------------------- 1 | package jsat.distributions; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.After; 6 | import org.junit.AfterClass; 7 | import org.junit.Before; 8 | import org.junit.BeforeClass; 9 | import org.junit.Test; 10 | 11 | public class LogisticTest { 12 | 13 | @BeforeClass 14 | public static void setUpBeforeClass() throws Exception { 15 | } 16 | 17 | @AfterClass 18 | public static void tearDownAfterClass() throws Exception { 19 | } 20 | 21 | @Before 22 | public void setUp() throws Exception { 23 | } 24 | 25 | @After 26 | public void tearDown() throws Exception { 27 | } 28 | 29 | @Test 30 | public void testEquals(){ 31 | System.out.println("equals"); 32 | ContinuousDistribution d1 = new Logistic(0.5, 0.5); 33 | ContinuousDistribution d2 = new Logistic(0.6, 0.5); 34 | ContinuousDistribution d3 = new Logistic(0.5, 0.6); 35 | ContinuousDistribution d4 = new Logistic(0.5, 0.5); 36 | Integer i = new Integer(1); 37 | assertFalse(d1.equals(d2)); 38 | assertFalse(d1.equals(d3)); 39 | assertFalse(d2.equals(d3)); 40 | assertFalse(d1.equals(i)); 41 | assertFalse(d1.equals(null)); 42 | assertEquals(d1, d1); 43 | assertEquals(d1, d4); 44 | assertEquals(d1, d1.clone()); 45 | } 46 | 47 | @Test 48 | public void testHashCode(){ 49 | System.out.println("hashCode"); 50 | ContinuousDistribution d1 = new Logistic(0.5, 0.5); 51 | ContinuousDistribution d2 = new Logistic(0.6, 0.5); 52 | ContinuousDistribution d4 = new Logistic(0.5, 0.5); 53 | assertEquals(d1.hashCode(), d4.hashCode()); 54 | assertFalse(d1.hashCode()==d2.hashCode()); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /JSAT/test/jsat/distributions/ParetoTest.java: -------------------------------------------------------------------------------- 1 | package jsat.distributions; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.After; 6 | import org.junit.AfterClass; 7 | import org.junit.Before; 8 | import org.junit.BeforeClass; 9 | import org.junit.Test; 10 | 11 | public class ParetoTest { 12 | 13 | @BeforeClass 14 | public static void setUpBeforeClass() throws Exception { 15 | } 16 | 17 | @AfterClass 18 | public static void tearDownAfterClass() throws Exception { 19 | } 20 | 21 | @Before 22 | public void setUp() throws Exception { 23 | } 24 | 25 | @After 26 | public void tearDown() throws Exception { 27 | } 28 | @Test 29 | public void testEquals(){ 30 | System.out.println("equals"); 31 | ContinuousDistribution d1 = new Pareto(0.5, 0.5); 32 | ContinuousDistribution d2 = new Pareto(0.6, 0.5); 33 | ContinuousDistribution d3 = new Pareto(0.5, 0.6); 34 | ContinuousDistribution d4 = new Pareto(0.5, 0.5); 35 | Integer i = new Integer(1); 36 | assertFalse(d1.equals(d2)); 37 | assertFalse(d1.equals(d3)); 38 | assertFalse(d2.equals(d3)); 39 | assertFalse(d1.equals(i)); 40 | assertFalse(d1.equals(null)); 41 | assertEquals(d1, d1); 42 | assertEquals(d1, d4); 43 | assertEquals(d1, d1.clone()); 44 | } 45 | 46 | @Test 47 | public void testHashCode(){ 48 | System.out.println("hashCode"); 49 | ContinuousDistribution d1 = new Pareto(0.5, 0.5); 50 | ContinuousDistribution d2 = new Pareto(0.6, 0.5); 51 | ContinuousDistribution d4 = new Pareto(0.5, 0.5); 52 | assertEquals(d1.hashCode(), d4.hashCode()); 53 | assertFalse(d1.hashCode()==d2.hashCode()); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /JSAT/test/jsat/distributions/UniformTest.java: -------------------------------------------------------------------------------- 1 | package jsat.distributions; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.After; 6 | import org.junit.AfterClass; 7 | import org.junit.Before; 8 | import org.junit.BeforeClass; 9 | import org.junit.Test; 10 | 11 | public class UniformTest { 12 | 13 | @BeforeClass 14 | public static void setUpBeforeClass() throws Exception { 15 | } 16 | 17 | @AfterClass 18 | public static void tearDownAfterClass() throws Exception { 19 | } 20 | 21 | @Before 22 | public void setUp() throws Exception { 23 | } 24 | 25 | @After 26 | public void tearDown() throws Exception { 27 | } 28 | 29 | @Test 30 | public void testEquals(){ 31 | System.out.println("equals"); 32 | ContinuousDistribution d1 = new Uniform(0.5, 0.5); 33 | ContinuousDistribution d2 = new Uniform(0.6, 0.5); 34 | ContinuousDistribution d3 = new Uniform(0.5, 0.6); 35 | ContinuousDistribution d4 = new Uniform(0.5, 0.5); 36 | Integer i = new Integer(1); 37 | assertFalse(d1.equals(d2)); 38 | assertFalse(d1.equals(d3)); 39 | assertFalse(d1.equals(i)); 40 | assertFalse(d1.equals(null)); 41 | assertEquals(d2,d3); 42 | assertEquals(d1, d1); 43 | assertEquals(d1, d4); 44 | assertEquals(d1, d1.clone()); 45 | } 46 | 47 | @Test 48 | public void testHashCode(){ 49 | System.out.println("hashCode"); 50 | ContinuousDistribution d1 = new Uniform(0.5, 0.5); 51 | ContinuousDistribution d2 = new Uniform(0.6, 0.5); 52 | ContinuousDistribution d4 = new Uniform(0.5, 0.5); 53 | assertEquals(d1.hashCode(), d4.hashCode()); 54 | assertFalse(d1.hashCode()==d2.hashCode()); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /JSAT/test/jsat/distributions/WeibullTest.java: -------------------------------------------------------------------------------- 1 | package jsat.distributions; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.After; 6 | import org.junit.AfterClass; 7 | import org.junit.Before; 8 | import org.junit.BeforeClass; 9 | import org.junit.Test; 10 | 11 | public class WeibullTest { 12 | 13 | @BeforeClass 14 | public static void setUpBeforeClass() throws Exception { 15 | } 16 | 17 | @AfterClass 18 | public static void tearDownAfterClass() throws Exception { 19 | } 20 | 21 | @Before 22 | public void setUp() throws Exception { 23 | } 24 | 25 | @After 26 | public void tearDown() throws Exception { 27 | } 28 | 29 | @Test 30 | public void testEquals(){ 31 | System.out.println("equals"); 32 | ContinuousDistribution d1 = new LogNormal(0.5, 0.5); 33 | ContinuousDistribution d2 = new LogNormal(0.6, 0.5); 34 | ContinuousDistribution d3 = new LogNormal(0.5, 0.6); 35 | ContinuousDistribution d4 = new LogNormal(0.5, 0.5); 36 | Integer i = new Integer(1); 37 | assertFalse(d1.equals(d2)); 38 | assertFalse(d1.equals(d3)); 39 | assertFalse(d2.equals(d3)); 40 | assertFalse(d1.equals(i)); 41 | assertFalse(d1.equals(null)); 42 | assertEquals(d1, d1); 43 | assertEquals(d1, d4); 44 | assertEquals(d1, d1.clone()); 45 | } 46 | 47 | @Test 48 | public void testHashCode(){ 49 | System.out.println("hashCode"); 50 | ContinuousDistribution d1 = new LogNormal(0.5, 0.5); 51 | ContinuousDistribution d2 = new LogNormal(0.6, 0.5); 52 | ContinuousDistribution d4 = new LogNormal(0.5, 0.5); 53 | assertEquals(d1.hashCode(), d4.hashCode()); 54 | assertFalse(d1.hashCode()==d2.hashCode()); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /JSAT/test/jsat/linear/TruncatedSVDTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This implementation contributed under the Public Domain. 3 | */ 4 | package jsat.linear; 5 | 6 | import java.util.Arrays; 7 | import java.util.Random; 8 | import jsat.utils.random.RandomUtil; 9 | import org.junit.After; 10 | import org.junit.AfterClass; 11 | import org.junit.Before; 12 | import org.junit.BeforeClass; 13 | import org.junit.Test; 14 | import static org.junit.Assert.*; 15 | 16 | /** 17 | * 18 | * @author edwardraff 19 | */ 20 | public class TruncatedSVDTest { 21 | 22 | public TruncatedSVDTest() { 23 | } 24 | 25 | @BeforeClass 26 | public static void setUpClass() { 27 | } 28 | 29 | @AfterClass 30 | public static void tearDownClass() { 31 | } 32 | 33 | @Before 34 | public void setUp() { 35 | } 36 | 37 | @After 38 | public void tearDown() { 39 | } 40 | 41 | @Test 42 | public void testSomeMethod() 43 | { 44 | Random rand = RandomUtil.getRandom(123); 45 | Matrix X_tall = DenseMatrix.random(100, 40, rand); 46 | Matrix X_wide = DenseMatrix.random(40, 100, rand); 47 | 48 | for(Matrix X : Arrays.asList(X_tall, X_wide)) 49 | { 50 | double origNorm = X.frobenius(); 51 | double prevNorm = X.frobenius(); 52 | for(int k = 1; k < 40; k+= 4) 53 | { 54 | 55 | TruncatedSVD svd = new TruncatedSVD(X, k); 56 | 57 | Matrix U = svd.getU(); 58 | Matrix V = svd.getV(); 59 | 60 | Matrix R = U.clone(); 61 | Matrix.diagMult(R, DenseVector.toDenseVec(svd.getSingularValues())); 62 | R = R.multiply(V); 63 | 64 | 65 | double cur_rec_cost = R.subtract(X).frobenius(); 66 | assertTrue(cur_rec_cost < prevNorm); 67 | assertTrue(cur_rec_cost < origNorm); 68 | prevNorm = cur_rec_cost; 69 | } 70 | } 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /JSAT/test/jsat/math/optimization/BFGSTest.java: -------------------------------------------------------------------------------- 1 | package jsat.math.optimization; 2 | 3 | import java.util.Random; 4 | import jsat.linear.DenseVector; 5 | import jsat.linear.Vec; 6 | import jsat.math.FunctionVec; 7 | import jsat.utils.random.RandomUtil; 8 | import org.junit.After; 9 | import org.junit.AfterClass; 10 | import org.junit.Before; 11 | import org.junit.BeforeClass; 12 | import org.junit.Test; 13 | import static org.junit.Assert.*; 14 | 15 | /** 16 | * 17 | * @author Edward Raff 18 | */ 19 | public class BFGSTest 20 | { 21 | 22 | public BFGSTest() 23 | { 24 | } 25 | 26 | @BeforeClass 27 | public static void setUpClass() 28 | { 29 | } 30 | 31 | @AfterClass 32 | public static void tearDownClass() 33 | { 34 | } 35 | 36 | @Before 37 | public void setUp() 38 | { 39 | } 40 | 41 | @After 42 | public void tearDown() 43 | { 44 | } 45 | 46 | /** 47 | * Test of optimize method, of class BFGS. 48 | */ 49 | @Test 50 | public void testOptimize() 51 | { 52 | System.out.println("optimize"); 53 | Random rand = RandomUtil.getRandom(); 54 | Vec x0 = new DenseVector(3);//D=3 means one local minima for easy evaluation 55 | for(int i = 0; i < x0.length(); i++) 56 | x0.set(i, rand.nextDouble()+0.5);//make sure we get to the right local optima 57 | 58 | RosenbrockFunction f = new RosenbrockFunction(); 59 | FunctionVec fp = f.getDerivative(); 60 | BFGS instance = new BFGS(); 61 | 62 | for(LineSearch lineSearch : new LineSearch[]{new BacktrackingArmijoLineSearch(), new WolfeNWLineSearch()}) 63 | { 64 | instance.setLineSearch(lineSearch); 65 | Vec w = new DenseVector(x0.length()); 66 | instance.optimize(1e-4, w, x0, f, fp); 67 | 68 | for(int i = 0; i 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | package jsat.math.optimization.oned; 18 | 19 | import jsat.linear.Vec; 20 | import jsat.math.Function1D; 21 | import org.junit.After; 22 | import org.junit.AfterClass; 23 | import org.junit.Before; 24 | import org.junit.BeforeClass; 25 | import org.junit.Test; 26 | import static org.junit.Assert.*; 27 | 28 | /** 29 | * 30 | * @author Edward Raff 31 | */ 32 | public class GoldenSearchTest 33 | { 34 | private static final Function1D easyMin_at_0 = (double x) -> 1+Math.pow(x, 2); 35 | 36 | public GoldenSearchTest() 37 | { 38 | } 39 | 40 | @BeforeClass 41 | public static void setUpClass() 42 | { 43 | } 44 | 45 | @AfterClass 46 | public static void tearDownClass() 47 | { 48 | } 49 | 50 | @Before 51 | public void setUp() 52 | { 53 | } 54 | 55 | @After 56 | public void tearDown() 57 | { 58 | } 59 | 60 | /** 61 | * Test of findMin method, of class GoldenSearch. 62 | */ 63 | @Test 64 | public void testFindMin() 65 | { 66 | System.out.println("findMin"); 67 | 68 | for(double tol = 2; tol >= 1e-8; tol/=2) 69 | { 70 | double result = GoldenSearch.findMin(-10, 10, easyMin_at_0, tol, 1000); 71 | assertEquals(0.0, result, tol*1.5); 72 | } 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /JSAT/test/jsat/text/tokenizer/NGramTokenizerTest.java: -------------------------------------------------------------------------------- 1 | 2 | package jsat.text.tokenizer; 3 | 4 | import java.util.Arrays; 5 | import java.util.List; 6 | import org.junit.After; 7 | import org.junit.AfterClass; 8 | import org.junit.Before; 9 | import org.junit.BeforeClass; 10 | import org.junit.Test; 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * 15 | * @author Edward Raff 16 | */ 17 | public class NGramTokenizerTest 18 | { 19 | 20 | public NGramTokenizerTest() 21 | { 22 | } 23 | 24 | @BeforeClass 25 | public static void setUpClass() 26 | { 27 | } 28 | 29 | @AfterClass 30 | public static void tearDownClass() 31 | { 32 | } 33 | 34 | @Before 35 | public void setUp() 36 | { 37 | } 38 | 39 | @After 40 | public void tearDown() 41 | { 42 | } 43 | 44 | /** 45 | * Test of tokenize method, of class NGramTokenizer. 46 | */ 47 | @Test 48 | public void testTokenize_String() 49 | { 50 | System.out.println("tokenize"); 51 | String input = "the dog barked"; 52 | NaiveTokenizer naiveToken = new NaiveTokenizer(); 53 | NGramTokenizer instance = new NGramTokenizer(3, naiveToken, true); 54 | List expResult = Arrays.asList("the", "dog", "barked", "the dog", "dog barked", "the dog barked"); 55 | List result = instance.tokenize(input); 56 | 57 | assertEquals(expResult.size(), result.size()); 58 | for(int i = 0; i < expResult.size(); i++) 59 | assertEquals(expResult.get(i), result.get(i)); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /JSAT/test/jsat/utils/IntListTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Edward Raff 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | package jsat.utils; 18 | 19 | import java.util.List; 20 | import org.junit.After; 21 | import org.junit.AfterClass; 22 | import org.junit.Before; 23 | import org.junit.BeforeClass; 24 | import org.junit.Test; 25 | import static org.junit.Assert.*; 26 | 27 | /** 28 | * 29 | * @author edwardraff 30 | */ 31 | public class IntListTest 32 | { 33 | 34 | public IntListTest() 35 | { 36 | } 37 | 38 | @BeforeClass 39 | public static void setUpClass() 40 | { 41 | } 42 | 43 | @AfterClass 44 | public static void tearDownClass() 45 | { 46 | } 47 | 48 | @Before 49 | public void setUp() 50 | { 51 | } 52 | 53 | @After 54 | public void tearDown() 55 | { 56 | } 57 | 58 | /** 59 | * Test of clear method, of class IntList. 60 | */ 61 | @Test 62 | public void testAdd_int_int() 63 | { 64 | System.out.println("clear"); 65 | IntList list = new IntList(); 66 | 67 | for(int sizes = 2; sizes < 100; sizes++) 68 | { 69 | for(int i = sizes-1; i >= 0; i--) 70 | list.add(0, i); 71 | for(int i = 0; i < sizes; i++) 72 | assertEquals(i, list.getI(i)); 73 | } 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /JSAT/test/jsat/utils/LongListTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2016 Edward Raff 3 | * 4 | * This program is free software: you can redistribute it and/or modify 5 | * it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * This program is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with this program. If not, see . 16 | */ 17 | package jsat.utils; 18 | 19 | import java.util.List; 20 | import org.junit.After; 21 | import org.junit.AfterClass; 22 | import org.junit.Before; 23 | import org.junit.BeforeClass; 24 | import org.junit.Test; 25 | import static org.junit.Assert.*; 26 | 27 | /** 28 | * 29 | * @author edwardraff 30 | */ 31 | public class LongListTest 32 | { 33 | 34 | public LongListTest() 35 | { 36 | } 37 | 38 | @BeforeClass 39 | public static void setUpClass() 40 | { 41 | } 42 | 43 | @AfterClass 44 | public static void tearDownClass() 45 | { 46 | } 47 | 48 | @Before 49 | public void setUp() 50 | { 51 | } 52 | 53 | @After 54 | public void tearDown() 55 | { 56 | } 57 | 58 | @Test 59 | public void testAdd_int_long() 60 | { 61 | System.out.println("clear"); 62 | LongList list = new LongList(); 63 | 64 | for(int sizes = 2; sizes < 100; sizes++) 65 | { 66 | for(int i = sizes-1; i >= 0; i--) 67 | list.add(0, i); 68 | for(int i = 0; i < sizes; i++) 69 | assertEquals(i, list.getL(i)); 70 | } 71 | } 72 | 73 | } 74 | --------------------------------------------------------------------------------