@Override public final double quantile(final double prob) { return this.dist.quantile(prob); }
@Override public final double rand() { return this.dist.rand(); }
@Override public double cdf(double x) { if (components.isEmpty()) throw new IllegalStateException("Mixture is empty!"); double p = 0.0; for (Component c : components) p += c.priori * c.distribution.cdf(x); return p; }
@Override public double p(double x) { if (components.isEmpty()) throw new IllegalStateException("Mixture is empty!"); double p = 0.0; for (Component c : components) p += c.priori * c.distribution.p(x); return p; }
switch (model) { case GENERAL: logprob += prob[i][e.i].logp(e.x); break; case MULTINOMIAL:
@Override public double mean() { if (components.isEmpty()) throw new IllegalStateException("Mixture is empty!"); double mu = 0.0; for (Component c : components) mu += c.priori * c.distribution.mean(); return mu; }
@Override public int npara() { if (components.isEmpty()) throw new IllegalStateException("Mixture is empty!"); int f = components.size() - 1; // independent priori parameters for (int i = 0; i < components.size(); i++) f += components.get(i).distribution.npara(); return f; }
switch (model) { case GENERAL: logprob += prob[i][j].logp(x[j]); break; case MULTINOMIAL:
@Override public final double quantile(final double prob) { return this.dist.quantile(prob); }
@Override public final double rand() { return this.dist.rand(); }
/** * The one-sample KS test for the null hypothesis that the data set x * is drawn from the given distribution. Small values of p-value show that * the cumulative distribution function of x is significantly different from * the given distribution. The array x is modified by being sorted into * ascending order. */ public static KSTest test(double[] x, Distribution dist) { Arrays.sort(x); int n = x.length; double en = n; double d = 0.0; double fo = 0.0; for (int j = 0; j < n; j++) { double fn = (j + 1) / en; double ff = dist.cdf(x[j]); double dt = Math.max(Math.abs(fo - ff), Math.abs(fn - ff)); if (dt > d) { d = dt; } fo = fn; } en = Math.sqrt(en); double p = qks((en + 0.12 + 0.11 / en) * d); return new KSTest(d, p); }
@Override public final double quantile(final double prob) { return this.dist.quantile(prob); }
@Override public final double rand() { return this.dist.rand(); }
/** * Generate the quantile-quantile pairs. */ private static double[][] quantile(double[] x, Distribution d) { Arrays.sort(x); int n = x.length; double[][] q = new double[n][2]; for (int i = 0; i < n; i++) { double p = (i + 1) / (n + 1.0); q[i][0] = x[(int) Math.round(p * x.length)]; q[i][1] = d.quantile(p); } return q; }
@Override public double rand() { if (components.isEmpty()) throw new IllegalStateException("Mixture is empty!"); double r = Math.random(); double p = 0.0; for (Component g : components) { p += g.priori; if (r <= p) return g.distribution.rand(); } // we should not arrive here. return components.get(components.size()-1).distribution.rand(); }
/** * Create a plot canvas with the one sample Q-Q plot to given distribution. * The x-axis is the quantiles of x and the y-axis is the quantiles of * given distribution. * @param x a sample set. * @param d a distribution. */ public static PlotCanvas plot(double[] x, Distribution d) { double[] lowerBound = {Math.min(x), d.quantile(1 / (x.length + 1.0))}; double[] upperBound = {Math.max(x), d.quantile(x.length / (x.length + 1.0))}; PlotCanvas canvas = new PlotCanvas(lowerBound, upperBound); canvas.add(new QQPlot(x, d)); return canvas; }