/** * 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() { double x = 0.0; for (int i = 0; i < nu; i++) { double norm = GaussianDistribution.getInstance().rand(); x += norm * norm; } return x; }
@Override public double p(double x) { if (sigma == 0) { if (x == mu) { return 1.0; } else { return 0.0; } } return Math.exp(logp(x)); }
@Override public double rand() { if (gaussian == null) { gaussian = new GaussianDistribution(mu, sigma); } return Math.exp(gaussian.rand()); }
double alph = GaussianDistribution.getInstance().quantile(0.999); double errMax = 0.001; e[0] = GaussianDistribution.getInstance().cdf(v[0] / sigmaL.get(0, 0)); f[0] = e[0]; double[] w = Math.random(dim - 1); for (int i = 1; i < dim; i++) { y[i - 1] = GaussianDistribution.getInstance().quantile(w[i - 1] * e[i - 1]); double q = 0.0; for (int j = 0; j < i; j++) { e[i] = GaussianDistribution.getInstance().cdf((v[i] - q) / sigmaL.get(i, i)); f[i] = e[i] * f[i - 1];
/** * Create a plot canvas with the one sample Q-Q plot to standard normal * distribution. The x-axis is the quantiles of x and the y-axis is the * quantiles of normal distribution. * @param x a sample set. */ public static PlotCanvas plot(double[] x) { double[] lowerBound = {Math.min(x), GaussianDistribution.getInstance().quantile(1 / (x.length + 1.0))}; double[] upperBound = {Math.max(x), GaussianDistribution.getInstance().quantile(x.length / (x.length + 1.0))}; PlotCanvas canvas = new PlotCanvas(lowerBound, upperBound); canvas.add(new QQPlot(x)); return canvas; }
/** * Calculates the Anderson-Darling statistic for one-dimensional normality test. * * @param x the samples to test if drawn from a Gaussian distribution. */ private static double AndersonDarling(double[] x) { int n = x.length; Arrays.sort(x); for (int i = 0; i < n; i++) { x[i] = GaussianDistribution.getInstance().cdf(x[i]); // in case overflow when taking log later. if (x[i] == 0) x[i] = 0.0000001; if (x[i] == 1) x[i] = 0.9999999; } double A = 0.0; for (int i = 0; i < n; i++) { A -= (2*i+1) * (Math.log(x[i]) + Math.log(1-x[n-i-1])); } A = A / n - n; A *= (1 + 4.0/n - 25.0/(n*n)); return A; }
/** * Constructor of one sample Q-Q plot to standard normal distribution. */ public QQPlot(double[] x) { data = quantile(x, GaussianDistribution.getInstance()); }
/** * 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; }
/** * 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; }
GaussianDistribution gaussian = new GaussianDistribution(0.0, 0.0001); for (int i = 0; i < n; i++) { Arrays.fill(gains[i], 1.0); double[] Yi = Y[i]; for (int j = 0; j < d; j++) { Yi[j] = gaussian.rand();
/** * Constructor. */ @SuppressWarnings("unchecked") Hash() { a = new double[k][d]; b = new double[k]; for (int i = 0; i < k; i++) { for (int j = 0; j < d; j++) { a[i][j] = GaussianDistribution.getInstance().rand(); } b[i] = Math.random(0, w); } LinkedList<Item> list = new LinkedList<>(); table = (LinkedList<Item>[]) java.lang.reflect.Array.newInstance(list.getClass(), H); }
/** * 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; }
GaussianDistribution gauss = GaussianDistribution.getInstance(); for (int i = 0; i < p; i++) { for (int j = 0; j < n; j++) { proj[i][j] = gauss.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; } }
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);