/** * 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; }
/** * 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); }