/** * Create a new Normal distribution with the given mean and standard deviation. * * @param mean the mean of the distribution. * @param stdev the standard deviation of the distribution. */ public Normal(final double mean, final double stdev) { this.dist = new GaussianDistribution(mean, stdev); this.mean = mean; this.stdev = stdev; }
/** * Create a new Normal distribution with the given mean and standard deviation. * * @param mean the mean of the distribution. * @param stdev the standard deviation of the distribution. */ public Normal(final double mean, final double stdev) { this.dist = new GaussianDistribution(mean, stdev); this.mean = mean; this.stdev = stdev; }
/** * Create a new Normal distribution with the given mean and standard deviation. * * @param mean the mean of the distribution. * @param stdev the standard deviation of the distribution. */ public Normal(final double mean, final double stdev) { this.dist = new GaussianDistribution(mean, stdev); this.mean = mean; this.stdev = stdev; }
@Override public double rand() { if (gaussian == null) { gaussian = new GaussianDistribution(mu, sigma); } return Math.exp(gaussian.rand()); }
/** * Constructor. * @param x the samples to estimate the density function. * @param h a bandwidth parameter for smoothing. */ public KernelDensity(double[] x, double h) { if (h <= 0) { throw new IllegalArgumentException("Invalid bandwidth: " + h); } this.x = x; this.h = h; this.mean = Math.mean(x); this.var = Math.var(x); this.sd = Math.sqrt(var); gaussian = new GaussianDistribution(0, h); Arrays.sort(x); }
@Override public Mixture.Component M(double[] x, double[] posteriori) { double alpha = 0.0; double mean = 0.0; double sd = 0.0; for (int i = 0; i < x.length; i++) { alpha += posteriori[i]; mean += x[i] * posteriori[i]; } mean /= alpha; for (int i = 0; i < x.length; i++) { double d = x[i] - mean; sd += d * d * posteriori[i]; } sd = Math.sqrt(sd / alpha); Mixture.Component c = new Mixture.Component(); c.priori = alpha; c.distribution = new GaussianDistribution(mean, sd); return c; } }
/** * Returns a random matrix of normal distributed values with given mean and standard dev. */ public static DenseMatrix randn(int rows, int cols, double mu, double sigma) { DenseMatrix a = zeros(rows, cols); GaussianDistribution g = new GaussianDistribution(mu, sigma); for (int j = 0; j < cols; j++) { for (int i = 0; i < rows; i++) { a.set(i, j, g.rand()); } } return a; }
c.distribution = new GaussianDistribution(mu + delta/2, delta); mixture.add(c); c.distribution = new GaussianDistribution(mu - delta/2, delta); mixture.add(c);
/** * Constructor. The Gaussian mixture model will be learned from the given data * with the EM algorithm. * @param data the training data. * @param k the number of components. */ public GaussianMixture(double[] data, int k) { if (k < 2) throw new IllegalArgumentException("Invalid number of components in the mixture."); double min = Math.min(data); double max = Math.max(data); double step = (max - min) / (k+1); for (int i = 0; i < k; i++) { Component c = new Component(); c.priori = 1.0 / k; c.distribution = new GaussianDistribution(min+=step, step); components.add(c); } EM(components, data); }
/** * Constructor. The bandwidth of kernel will be estimated by the rule of thumb. * @param x the samples to estimate the density function. */ public KernelDensity(double[] x) { this.x = x; this.mean = Math.mean(x); this.var = Math.var(x); this.sd = Math.sqrt(var); Arrays.sort(x); int n = x.length; double iqr = x[n*3/4] - x[n/4]; h = 1.06 * Math.min(sd, iqr/1.34) / Math.pow(x.length, 0.2); gaussian = new GaussianDistribution(0, h); }
Component c = new Component(); c.priori = 1.0; c.distribution = new GaussianDistribution(data); mixture.add(c);
GaussianDistribution gaussian = new GaussianDistribution(0.0, 0.0001); for (int i = 0; i < n; i++) { Arrays.fill(gains[i], 1.0);