dataset1[3][0] = 0.55; MultivariateEstimator mv1 = new MultivariateGaussianEstimator(); mv1.estimate(dataset1, new double[]{0.7, 0.2, 0.05, 0.05}); dataset[3][2] = 0.54; MultivariateEstimator mv = new MultivariateGaussianEstimator(); mv.estimate(dataset, new double[]{2, 0.2, 0.05, 0.05}); dataset3[3][2] = 0.54; MultivariateEstimator mv3 = new MultivariateGaussianEstimator(); mv3.estimate(dataset3, new double[]{1, 0.2, 0.05, 0.05, 1}); weights[1] = new double[] {2, 1, 1}; MultivariateGaussianEstimator mv4 = new MultivariateGaussianEstimator(); mv4.estimatePooled(dataset4, weights); weights2[1] = new double[] {1, 1, 1, 1}; MultivariateGaussianEstimator mv5 = new MultivariateGaussianEstimator(); mv5.estimatePooled(dataset5, weights2);
/** * Generates the estimator based on the given observations and weight vector. * Equal weights are assumed if the weight vector is null. */ @Override public void estimate(double[][] observations, double[] weights) { if (weights == null) { weights = new double[observations.length]; for (int i = 0; i < weights.length; i++) { weights[i] = 1.0; } } DenseVector weightVector = new DenseVector(weights); weightVector = weightVector.scale(1.0 / weightVector.norm(Vector.Norm.One)); mean = weightedMean(observations, weightVector); Matrix cov = weightedCovariance(observations, weightVector, mean); // Compute inverse of covariance matrix DenseCholesky chol = new DenseCholesky(observations[0].length, true).factor((UpperSPDDenseMatrix)cov); covarianceInverse = new UpperSPDDenseMatrix(chol.solve(Matrices.identity(observations[0].length))); double logDeterminant = 0; for (int i = 0; i < observations[0].length; i++) { logDeterminant += Math.log(chol.getU().get(i, i)); } logDeterminant *= 2; lnconstant = -(Log2PI * observations[0].length + logDeterminant) * 0.5; }
m_Estimator = new MultivariateGaussianEstimator(); m_Estimator.setRidge(getRidge()); m_Means = m_Estimator.estimatePooled(data, weights); m_GlobalMean = m_Estimator.getMean();
for (int i = 0; i < insts.numClasses(); i++) { if (sumOfWeightsPerClass[i] > 0) { m_Estimators[i] = new MultivariateGaussianEstimator(); m_Estimators[i].setRidge(getRidge()); m_Estimators[i].estimate(data[i], weights[i]);
/** * Output class probabilities using Bayes' rule. */ public double[] distributionForInstance(Instance inst) throws Exception { // Filter instance m_RemoveUseless.input(inst); inst = m_RemoveUseless.output(); // Convert instance to array double[] values = new double[inst.numAttributes() - 1]; int index = 0; for (int i = 0; i < m_Data.numAttributes(); i++) { if (i != m_Data.classIndex()) { values[index++] = inst.value(i); } } double[] posteriorProbs = new double[m_Data.numClasses()]; for (int i = 0; i < m_Data.numClasses(); i++) { if (m_Estimators[i] != null) { posteriorProbs[i] = m_Estimators[i].logDensity(values) + m_LogPriors[i]; } else { posteriorProbs[i] = -Double.MAX_VALUE; } } posteriorProbs = Utils.logs2probs(posteriorProbs); return posteriorProbs; }
/** * Output class probabilities using Bayes' rule. */ public double[] distributionForInstance(Instance inst) throws Exception { // Filter instance m_RemoveUseless.input(inst); inst = m_RemoveUseless.output(); // Convert instance to array double[] posteriorProbs = new double[m_Data.numClasses()]; double[] values = new double[inst.numAttributes() - 1]; for (int i = 0; i < m_Data.numClasses(); i++) { if (m_Means[i] != null) { int index = 0; for (int j = 0; j < m_Data.numAttributes(); j++) { if (j != m_Data.classIndex()) { values[index] = inst.value(j) - m_Means[i][index] + m_GlobalMean[index]; index++; } } posteriorProbs[i] = m_Estimator.logDensity(values) + m_LogPriors[i]; } else { posteriorProbs[i] = -Double.MAX_VALUE; } } posteriorProbs = Utils.logs2probs(posteriorProbs); return posteriorProbs; }
dataset1[3][0] = 0.55; MultivariateEstimator mv1 = new MultivariateGaussianEstimator(); mv1.estimate(dataset1, new double[]{0.7, 0.2, 0.05, 0.05}); dataset[3][2] = 0.54; MultivariateEstimator mv = new MultivariateGaussianEstimator(); mv.estimate(dataset, new double[]{2, 0.2, 0.05, 0.05}); dataset3[3][2] = 0.54; MultivariateEstimator mv3 = new MultivariateGaussianEstimator(); mv3.estimate(dataset3, new double[]{1, 0.2, 0.05, 0.05, 1}); weights[1] = new double[] {2, 1, 1}; MultivariateGaussianEstimator mv4 = new MultivariateGaussianEstimator(); mv4.estimatePooled(dataset4, weights); weights2[1] = new double[] {1, 1, 1, 1}; MultivariateGaussianEstimator mv5 = new MultivariateGaussianEstimator(); mv5.estimatePooled(dataset5, weights2);
/** * Generates the estimator based on the given observations and weight vector. * Equal weights are assumed if the weight vector is null. */ @Override public void estimate(double[][] observations, double[] weights) { if (weights == null) { weights = new double[observations.length]; for (int i = 0; i < weights.length; i++) { weights[i] = 1.0; } } DenseVector weightVector = new DenseVector(weights); weightVector = weightVector.scale(1.0 / weightVector.norm(Vector.Norm.One)); mean = weightedMean(observations, weightVector); Matrix cov = weightedCovariance(observations, weightVector, mean); // Compute inverse of covariance matrix DenseCholesky chol = new DenseCholesky(observations[0].length, true).factor((UpperSPDDenseMatrix)cov); covarianceInverse = new UpperSPDDenseMatrix(chol.solve(Matrices.identity(observations[0].length))); double logDeterminant = 0; for (int i = 0; i < observations[0].length; i++) { logDeterminant += Math.log(chol.getU().get(i, i)); } logDeterminant *= 2; lnconstant = -(Log2PI * observations[0].length + logDeterminant) * 0.5; }
DenseVector weightVector = new DenseVector(weights[i]); weightVector = weightVector.scale(1.0 / weightVector.norm(Vector.Norm.One)); groupMean[i] = weightedMean(observations[i], weightVector); groupCovariance[i] = weightedCovariance(observations[i], weightVector, groupMean[i]); groupWeights[i] = Utils.sum(weights[i]);
DenseVector weightVector = new DenseVector(weights[i]); weightVector = weightVector.scale(1.0 / weightVector.norm(Vector.Norm.One)); groupMean[i] = weightedMean(observations[i], weightVector); groupCovariance[i] = weightedCovariance(observations[i], weightVector, groupMean[i]); groupWeights[i] = Utils.sum(weights[i]);