Exemple #1
0
  /**
   * Computes the value of h(x) given the mixture.
   *
   * @param AHat the value
   * @return the value of h(x)
   */
  public double h(double AHat) {

    if (AHat == 0.0) return 0.0;
    DoubleVector points = mixingDistribution.getPointValues();
    DoubleVector values = mixingDistribution.getFunctionValues();

    double aHat = Math.sqrt(AHat);
    DoubleVector aStar = points.sqrt();
    DoubleVector d1 = Maths.dnorm(aHat, aStar, 1).timesEquals(values);
    DoubleVector d2 = Maths.dnorm(-aHat, aStar, 1).timesEquals(values);

    return points.minus(aHat / 2).innerProduct(d1) - points.plus(aHat / 2).innerProduct(d2);
  }
Exemple #2
0
  /**
   * Method to test this class
   *
   * @param args the commandline arguments
   */
  public static void main(String args[]) {

    int n1 = 50;
    int n2 = 50;
    double ncp1 = 0;
    double ncp2 = 10;
    double mu1 = Math.sqrt(ncp1);
    double mu2 = Math.sqrt(ncp2);
    DoubleVector a = Maths.rnorm(n1, mu1, 1, new Random());
    a = a.cat(Maths.rnorm(n2, mu2, 1, new Random()));
    DoubleVector aNormal = a;
    a = a.square();
    a.sort();

    DoubleVector means = (new DoubleVector(n1, mu1)).cat(new DoubleVector(n2, mu2));

    System.out.println("==========================================================");
    System.out.println(
        "This is to test the estimation of the mixing\n"
            + "distribution of the mixture of non-central Chi-square\n"
            + "distributions. The example mixture used is of the form: \n\n"
            + "   0.5 * Chi^2_1(ncp1) + 0.5 * Chi^2_1(ncp2)\n");

    System.out.println(
        "It also tests the PACE estimators. Quadratic losses of the\n"
            + "estimators are given, measuring their performance.");
    System.out.println("==========================================================");
    System.out.println("ncp1 = " + ncp1 + " ncp2 = " + ncp2 + "\n");

    System.out.println(a.size() + " observations are: \n\n" + a);

    System.out.println("\nQuadratic loss of the raw data (i.e., the MLE) = " + aNormal.sum2(means));
    System.out.println("==========================================================");

    // find the mixing distribution
    ChisqMixture d = new ChisqMixture();
    d.fit(a, NNMMethod);
    System.out.println("The estimated mixing distribution is\n" + d);

    DoubleVector pred = d.pace2(a.rev()).rev();
    System.out.println("\nThe PACE2 Estimate = \n" + pred);
    System.out.println("Quadratic loss = " + pred.sqrt().times(aNormal.sign()).sum2(means));

    pred = d.pace4(a);
    System.out.println("\nThe PACE4 Estimate = \n" + pred);
    System.out.println("Quadratic loss = " + pred.sqrt().times(aNormal.sign()).sum2(means));

    pred = d.pace6(a);
    System.out.println("\nThe PACE6 Estimate = \n" + pred);
    System.out.println("Quadratic loss = " + pred.sqrt().times(aNormal.sign()).sum2(means));
  }
Exemple #3
0
  /**
   * Computes the value of f(x) given the mixture.
   *
   * @param x the value
   * @return the value of f(x)
   */
  public double f(double x) {

    DoubleVector points = mixingDistribution.getPointValues();
    DoubleVector values = mixingDistribution.getFunctionValues();

    return Maths.dchisq(x, points).timesEquals(values).sum();
  }
  /**
   * Method to test this class
   *
   * @param args the commandline arguments - ignored
   */
  public static void main(String args[]) {
    int n1 = 50;
    int n2 = 50;
    double mu1 = 0;
    double mu2 = 5;
    DoubleVector a = Maths.rnorm(n1, mu1, 1, new Random());
    a = a.cat(Maths.rnorm(n2, mu2, 1, new Random()));
    DoubleVector means = (new DoubleVector(n1, mu1)).cat(new DoubleVector(n2, mu2));

    System.out.println("==========================================================");
    System.out.println(
        "This is to test the estimation of the mixing\n"
            + "distribution of the mixture of unit variance normal\n"
            + "distributions. The example mixture used is of the form: \n\n"
            + "   0.5 * N(mu1, 1) + 0.5 * N(mu2, 1)\n");

    System.out.println(
        "It also tests three estimators: the subset\n"
            + "selector, the nested model selector, and the empirical Bayes\n"
            + "estimator. Quadratic losses of the estimators are given, \n"
            + "and are taken as the measure of their performance.");
    System.out.println("==========================================================");
    System.out.println("mu1 = " + mu1 + " mu2 = " + mu2 + "\n");

    System.out.println(a.size() + " observations are: \n\n" + a);

    System.out.println("\nQuadratic loss of the raw data (i.e., the MLE) = " + a.sum2(means));
    System.out.println("==========================================================");

    // find the mixing distribution
    NormalMixture d = new NormalMixture();
    d.fit(a, NNMMethod);
    System.out.println("The estimated mixing distribution is:\n" + d);

    DoubleVector pred = d.nestedEstimate(a.rev()).rev();
    System.out.println("\nThe Nested Estimate = \n" + pred);
    System.out.println("Quadratic loss = " + pred.sum2(means));

    pred = d.subsetEstimate(a);
    System.out.println("\nThe Subset Estimate = \n" + pred);
    System.out.println("Quadratic loss = " + pred.sum2(means));

    pred = d.empiricalBayesEstimate(a);
    System.out.println("\nThe Empirical Bayes Estimate = \n" + pred);
    System.out.println("Quadratic loss = " + pred.sum2(means));
  }
 /**
  * Return true if a value can be considered for mixture estimatino separately from the data
  * indexed between i0 and i1
  *
  * @param data the data supposedly generated from the mixture
  * @param i0 the index of the first element in the group
  * @param i1 the index of the last element in the group
  * @param x the value
  * @return true if the value can be considered
  */
 public boolean separable(DoubleVector data, int i0, int i1, double x) {
   double p = 0;
   for (int i = i0; i <= i1; i++) {
     p += Maths.pnorm(-Math.abs(x - data.get(i)));
   }
   if (p < separatingThreshold) return true;
   else return false;
 }
Exemple #6
0
  /**
   * Contructs the probability matrix for mixture estimation, given a set of support points and a
   * set of intervals.
   *
   * @param s the set of support points
   * @param intervals the intervals
   * @return the probability matrix
   */
  public PaceMatrix probabilityMatrix(DoubleVector s, PaceMatrix intervals) {

    int ns = s.size();
    int nr = intervals.getRowDimension();
    PaceMatrix p = new PaceMatrix(nr, ns);

    for (int i = 0; i < nr; i++) {
      for (int j = 0; j < ns; j++) {
        p.set(
            i,
            j,
            Maths.pchisq(intervals.get(i, 1), s.get(j))
                - Maths.pchisq(intervals.get(i, 0), s.get(j)));
      }
    }

    return p;
  }
  /**
   * Returns the empirical Bayes estimate of a single value.
   *
   * @param x the value
   * @return the empirical Bayes estimate
   */
  public double empiricalBayesEstimate(double x) {
    if (Math.abs(x) > 10) return x; // pratical consideration; modify later
    DoubleVector d = Maths.dnormLog(x, mixingDistribution.getPointValues(), 1);

    d.minusEquals(d.max());
    d = d.map("java.lang.Math", "exp");
    d.timesEquals(mixingDistribution.getFunctionValues());
    return mixingDistribution.getPointValues().innerProduct(d) / d.sum();
  }
Exemple #8
0
  /**
   * Computes the value of h(x) / f(x) given the mixture. The implementation avoided overflow.
   *
   * @param AHat the value
   * @return the value of h(x) / f(x)
   */
  public double hf(double AHat) {

    DoubleVector points = mixingDistribution.getPointValues();
    DoubleVector values = mixingDistribution.getFunctionValues();

    double x = Math.sqrt(AHat);
    DoubleVector mean = points.sqrt();
    DoubleVector d1 = Maths.dnormLog(x, mean, 1);
    double d1max = d1.max();
    d1.minusEquals(d1max);
    DoubleVector d2 = Maths.dnormLog(-x, mean, 1);
    d2.minusEquals(d1max);

    d1 = d1.map("java.lang.Math", "exp");
    d1.timesEquals(values);
    d2 = d2.map("java.lang.Math", "exp");
    d2.timesEquals(values);

    return ((points.minus(x / 2)).innerProduct(d1) - (points.plus(x / 2)).innerProduct(d2))
        / (d1.sum() + d2.sum());
  }
  /**
   * Computes the value of h(x) / f(x) given the mixture. The implementation avoided overflow.
   *
   * @param x the value
   * @return the value of h(x) / f(x)
   */
  public double hf(double x) {
    DoubleVector points = mixingDistribution.getPointValues();
    DoubleVector values = mixingDistribution.getFunctionValues();

    DoubleVector d = Maths.dnormLog(x, points, 1);
    d.minusEquals(d.max());

    d = (DoubleVector) d.map("java.lang.Math", "exp");
    d.timesEquals(values);

    return ((DoubleVector) points.times(2 * x).minusEquals(x * x)).innerProduct(d) / d.sum();
  }
Exemple #10
0
  /**
   * Returns the pace6 estimate of a single value.
   *
   * @param x the value
   * @return the pace6 estimate
   */
  public double pace6(double x) {

    if (x > 100) return x; // pratical consideration. will modify later
    DoubleVector points = mixingDistribution.getPointValues();
    DoubleVector values = mixingDistribution.getFunctionValues();
    DoubleVector mean = points.sqrt();

    DoubleVector d = Maths.dchisqLog(x, points);
    d.minusEquals(d.max());
    d = d.map("java.lang.Math", "exp").timesEquals(values);
    double atilde = mean.innerProduct(d) / d.sum();
    return atilde * atilde;
  }
Exemple #11
0
 /**
  * Returns sqrt(a^2 + b^2) without under/overflow.
  *
  * @param a length of one side of rectangular triangle
  * @param b length of other side of rectangular triangle
  * @return lenght of third side
  */
 protected static double hypot(double a, double b) {
   return weka.core.matrix.Maths.hypot(a, b);
 }
 /**
  * Computes the value of h(x) given the mixture.
  *
  * @param x the value
  * @return the value of h(x)
  */
 public double h(double x) {
   DoubleVector points = mixingDistribution.getPointValues();
   DoubleVector values = mixingDistribution.getFunctionValues();
   DoubleVector d = (DoubleVector) Maths.dnorm(x, points, 1).timesEquals(values);
   return ((DoubleVector) points.times(2 * x).minusEquals(x * x)).innerProduct(d);
 }