/** * Construct an ellipse that encompasses the shape of a * {@link CachingMultivariateGaussian}. * * @param gaussian * the {@link CachingMultivariateGaussian} * @param scale * the relative size of the ellipse * @return the ellipse */ public static Ellipse ellipseFromGaussian(MultivariateGaussian gaussian, float scale) { final Matrix mean = gaussian.getMean(); final float x = (float) mean.get(0, 0); final float y = (float) mean.get(0, 1); final Matrix covar = gaussian.getCovariance(); return ellipseFromCovariance(x, y, covar, scale); } }
/** * Compute the log probability of the given data points belonging to each of * the gaussians * * @param x * the points * @return the log probability of each point belonging to each gaussian * distribution */ public double[][] logProbability(double[][] x) { final int nmix = gaussians.length; final int nsamples = x.length; final double[][] log_prob = new double[nsamples][nmix]; for (int i = 0; i < nmix; i++) { final double[] lp = gaussians[i].estimateLogProbability(x); for (int j = 0; j < nsamples; j++) { log_prob[j][i] = lp[j]; } } return log_prob; // return logProbability(x, gaussians); }
@Override public double compare(MultivariateGaussian o1, MultivariateGaussian o2) { final Matrix sig0 = o1.getCovariance(); final Matrix sig1 = o2.getCovariance(); final Matrix mu0 = o1.getMean(); final Matrix mu1 = o2.getMean(); final int K = o1.numDims(); final Matrix sig1inv = sig1.inverse(); final double sigtrace = MatrixUtils.trace(sig1inv.times(sig0)); final Matrix mudiff = mu1.minus(mu0); final double xt_s_x = mudiff.transpose().times(sig1inv).times(mudiff).get(0, 0); final double ln_norm_sig = Math.log(sig0.norm1() / sig1.norm1()); return 0.5 * (sigtrace + xt_s_x - K - ln_norm_sig); }
@Override public double[][] sample(int n_samples, Random rng) { final double[] weight_cdf = ArrayUtils.cumulativeSum(this.weights); final double[][] X = new double[n_samples][this.gaussians[0].getMean().getColumnDimension()]; // decide which component to use for each sample final int[] comps = new int[n_samples]; for (int i = 0; i < n_samples; i++) { comps[i] = Arrays.binarySearch(weight_cdf, rng.nextDouble()); if (comps[i] < 0) comps[i] = 0; if (comps[i] >= gaussians.length) comps[i] = gaussians.length - 1; } // generate samples for each component... doing it this way is more // efficient as it minimises the number of times you'd need to do the // cholesky decomposition for (int i = 0; i < gaussians.length; i++) { final int[] idxs = ArrayUtils.search(comps, i); if (idxs.length == 0) continue; // generate the sample final double[][] samples = gaussians[i].sample(idxs.length, rng); for (int j = 0; j < samples.length; j++) { X[idxs[j]] = samples[j]; } } return X; }
/** * Get the probability for a given points in space relative to the PDF * represented by the gaussian mixture. * * @param samples * the points * @return the probability */ @Override public double[] estimateLogProbability(double[][] samples) { if (samples[0].length != this.gaussians[0].getMean().getColumnDimension()) { throw new IllegalArgumentException( "The number of dimensions of the given data is not compatible with the model"); } final double[][] lpr = computeWeightedLogProb(samples); final double[] logprob = new double[samples.length]; for (int i = 0; i < samples.length; i++) { for (int j = 0; j < lpr[0].length; j++) { logprob[i] += Math.exp(lpr[i][j]); } logprob[i] = Math.log(logprob[i]); } return logprob; }
public static void main(String[] args) { final double[][] data = RandomData.getRandomDoubleArray(10000, 64, -1d, 1d); final GaussianMixtureModelEM gmmem = new GaussianMixtureModelEM(512, CovarianceType.Diagonal); final MixtureOfGaussians model = gmmem.estimate(data); System.out.println(MatrixUtils.toString(model.gaussians[0].getCovariance())); System.out.println(); System.out.println(MatrixUtils.toString(model.gaussians[1].getCovariance())); } }
/** * Compute the posterior distribution of the samples, and the overall log * probability of each sample as belonging to the model. * * @param samples * the samples * @return a pair of (log probabilities, log posterior probabilities) */ public IndependentPair<double[], double[][]> scoreSamples(double[][] samples) { if (samples[0].length != this.gaussians[0].getMean().getColumnDimension()) { throw new IllegalArgumentException( "The number of dimensions of the given data is not compatible with the model"); } final double[][] lpr = computeWeightedLogProb(samples); final double[] logprob = logsumexp(lpr); final double[][] responsibilities = new double[samples.length][gaussians.length]; for (int i = 0; i < samples.length; i++) { for (int j = 0; j < gaussians.length; j++) { responsibilities[i][j] = Math.exp(lpr[i][j] - logprob[i]); } } return IndependentPair.pair(logprob, responsibilities); }
public static void main(String[] args) { final double[][] data = RandomData.getRandomDoubleArray(10000, 64, -1d, 1d); final GaussianMixtureModelEM gmmem = new GaussianMixtureModelEM(512, CovarianceType.Diagonal); final MixtureOfGaussians model = gmmem.estimate(data); System.out.println(MatrixUtils.toString(model.gaussians[0].getCovariance())); System.out.println(); System.out.println(MatrixUtils.toString(model.gaussians[1].getCovariance())); } }
final double[] mean = gauss.getMean().getArray()[0]; final double var = gauss.getCovariance(j, j); final double diff = (xp[j] - mean[j]) / Math.sqrt(var);
/** * Predict the log-posterior for the given samples; this is the * log-probability of each sample point belonging to each of the gaussians * in the mixture. * * @param samples * the samples * @return the log-probability for each gaussian */ public double[][] predictLogPosterior(double[][] samples) { if (samples[0].length != this.gaussians[0].getMean().getColumnDimension()) { throw new IllegalArgumentException( "The number of dimensions of the given data is not compatible with the model"); } final double[][] lpr = computeWeightedLogProb(samples); final double[] logprob = logsumexp(lpr); final double[][] responsibilities = new double[samples.length][gaussians.length]; for (int i = 0; i < samples.length; i++) { for (int j = 0; j < gaussians.length; j++) { responsibilities[i][j] = lpr[i][j] - logprob[i]; // note no exp // as want // log prob } } return responsibilities; }
final double[] mean = gauss.getMean().getArray()[0]; final double var = gauss.getCovariance(j, j); final double diff = (xp[j] - mean[j]) / Math.sqrt(var);
final double[] mv = g.getMean().getArray()[0]; final double x = mv[mv.length - 2] * w + (w / 2); final double y = mv[mv.length - 1] * h + (h / 2); final double xc = g.getCovariance(mv.length - 2, mv.length - 2); final double yc = g.getCovariance(mv.length - 1, mv.length - 1);
final double[] mv = g.getMean().getArray()[0]; final double x = mv[mv.length - 2] * w + (w / 2); final double y = mv[mv.length - 1] * h + (h / 2); final double xc = g.getCovariance(mv.length - 2, mv.length - 2); final double yc = g.getCovariance(mv.length - 1, mv.length - 1);
final Matrix mu = gaussians[i].getMean(); final Matrix cv = gaussians[i].getCovariance();