예제 #1
0
 @Override
 public Numeric gradient(Var y, Var fx) {
   Numeric delta = Numeric.empty();
   for (int i = 0; i < y.rowCount(); i++) {
     delta.addValue(y.value(i) - fx.value(i));
   }
   return delta;
 }
예제 #2
0
 @Override
 public Numeric gradient(Var y, Var fx) {
   Numeric gradient = Numeric.empty();
   for (int i = 0; i < y.rowCount(); i++) {
     gradient.addValue(y.value(i) - fx.value(i) < 0 ? -1. : 1.);
   }
   return gradient;
 }
예제 #3
0
  public BoxPlot(Var x, Var factor, GOpt... opts) {

    Map<String, List<Double>> map =
        x.stream().collect(groupingBy(s -> factor.label(s.row()), mapping(VSpot::value, toList())));
    names = factor.streamLevels().filter(map::containsKey).toArray(String[]::new);
    vars = Arrays.stream(names).map(map::get).map(Numeric::copy).toArray(Var[]::new);

    this.options.apply(opts);
    initialize();
  }
예제 #4
0
 @Override
 public double findMinimum(Var y, Var fx) {
   Numeric values = Numeric.empty();
   for (int i = 0; i < y.rowCount(); i++) {
     values.addValue(y.value(i) - fx.value(i));
   }
   double result = Quantiles.from(values, new double[] {0.5}).values()[0];
   if (Double.isNaN(result)) {
     WS.println();
   }
   return result;
 }
예제 #5
0
 @Override
 public Range buildRange() {
   Range range = new Range();
   range.union(0, Double.NaN);
   range.union(vars.length, Double.NaN);
   for (Var v : vars) {
     for (int i = 0; i < v.rowCount(); i++) {
       if (v.missing(i)) continue;
       range.union(Double.NaN, v.value(i));
     }
   }
   return range;
 }
예제 #6
0
    @Override
    public Numeric gradient(Var y, Var fx) {

      // compute absolute residuals

      Numeric absResidual = Numeric.empty();
      for (int i = 0; i < y.rowCount(); i++) {
        absResidual.addValue(Math.abs(y.value(i) - fx.value(i)));
      }

      // compute rho as an alpha-quantile of absolute residuals

      double rho = Quantiles.from(absResidual, new double[] {alpha}).values()[0];

      // now compute gradient

      Numeric gradient = Numeric.empty();
      for (int i = 0; i < y.rowCount(); i++) {
        if (absResidual.value(i) <= rho) {
          gradient.addValue(y.value(i) - fx.value(i));
        } else {
          gradient.addValue(rho * ((y.value(i) - fx.value(i) <= 0) ? -1 : 1));
        }
      }

      // return gradient

      return gradient;
    }
예제 #7
0
 private Minimum(Var var) {
   this.varName = var.name();
   for (int i = 0; i < var.rowCount(); i++) {
     if (var.missing(i)) {
       missingCount++;
     } else {
       completeCount++;
     }
   }
   if (var.stream().complete().count() == 0) {
     value = Double.NaN;
   } else {
     value = var.stream().complete().mapToDouble().min().getAsDouble();
   }
 }
예제 #8
0
    @Override
    public double findMinimum(Var y, Var fx) {

      // compute residuals

      Numeric residual = Numeric.empty();
      for (int i = 0; i < y.rowCount(); i++) {
        residual.addValue(y.value(i) - fx.value(i));
      }

      // compute median of residuals

      double r_bar = Quantiles.from(residual, new double[] {0.5}).values()[0];

      // compute absolute residuals

      Numeric absResidual = Numeric.empty();
      for (int i = 0; i < y.rowCount(); i++) {
        absResidual.addValue(Math.abs(y.value(i) - fx.value(i)));
      }

      // compute rho as an alpha-quantile of absolute residuals

      double rho = Quantiles.from(absResidual, new double[] {alpha}).values()[0];

      // compute one-iteration approximation

      double gamma = r_bar;
      double count = y.rowCount();
      for (int i = 0; i < y.rowCount(); i++) {
        gamma +=
            (residual.value(i) - r_bar <= 0 ? -1 : 1)
                * Math.min(rho, Math.abs(residual.value(i) - r_bar))
                / count;
      }
      return gamma;
    }
예제 #9
0
파일: FFT.java 프로젝트: padreati/rapaio
  // compute the FFT of x[], assuming its length is a power of 2
  public static Pair<Var, Var> fft(Pair<Var, Var> x) {

    int N = x._1.rowCount();

    // base case
    if (N == 1) return x;

    // radix 2 Cooley-Tukey FFT
    if (N % 2 != 0) {
      throw new RuntimeException("N is not a power of 2");
    }

    // fft of even terms
    Mapping evenMap = Mapping.empty();
    for (int k = 0; k < N / 2; k++) {
      evenMap.add(2 * k);
    }
    Pair<Var, Var> even = Pair.from(x._1.mapRows(evenMap), x._2.mapRows(evenMap));
    Pair<Var, Var> q = fft(even);

    // fft of odd terms
    Mapping oddMap = Mapping.empty();
    for (int k = 0; k < N / 2; k++) {
      oddMap.add(2 * k + 1);
    }
    Pair<Var, Var> r = fft(Pair.from(x._1.mapRows(oddMap), x._2.mapRows(oddMap)));

    // combine
    Var rey = Numeric.fill(N, 0.0);
    Var imy = Numeric.fill(N, 0.0);
    for (int k = 0; k < N / 2; k++) {
      double kth = -2 * k * Math.PI / N;
      double coskth = Math.cos(kth);
      double sinkth = Math.sin(kth);

      rey.setValue(k, q._1.value(k) + coskth * r._1.value(k) - sinkth * r._2.value(k));
      imy.setValue(k, q._2.value(k) + coskth * r._2.value(k) + sinkth * r._1.value(k));

      rey.setValue(k + N / 2, q._1.value(k) - coskth * r._1.value(k) + sinkth * r._2.value(k));
      imy.setValue(k + N / 2, q._2.value(k) - coskth * r._2.value(k) - sinkth * r._1.value(k));
    }
    return Pair.from(rey, imy);
  }
예제 #10
0
  @Override
  public void paint(Graphics2D g2d, Rectangle rect) {
    super.paint(g2d, rect);

    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, options.getAlpha()));

    for (int i = 0; i < vars.length; i++) {
      Var v = vars[i];
      if (v.rowCount() == 0) {
        continue;
      }
      double[] p = new double[] {0.25, 0.5, 0.75};
      double[] q = Quantiles.from(v, p).values();
      double iqr = q[2] - q[0];
      double innerFence = 1.5 * iqr;
      double outerFence = 3 * iqr;

      double x1 = i + 0.5 - 0.3;
      double x2 = i + 0.5;
      double x3 = i + 0.5 + 0.3;

      // first we fill the space

      g2d.setColor(options.getColor(i));
      g2d.fill(
          new Rectangle2D.Double(
              xScale(x1), yScale(q[2]), xScale(x3) - xScale(x1), yScale(q[0]) - yScale(q[2])));

      g2d.setColor(ColorPalette.STANDARD.getColor(0));

      // median
      g2d.setStroke(new BasicStroke(options.getLwd() * 2));
      g2d.draw(new Line2D.Double(xScale(x1), yScale(q[1]), xScale(x3), yScale(q[1])));

      // box
      g2d.setStroke(new BasicStroke(options.getLwd()));

      g2d.draw(new Line2D.Double(xScale(x1), yScale(q[0]), xScale(x3), yScale(q[0])));
      g2d.draw(new Line2D.Double(xScale(x1), yScale(q[2]), xScale(x3), yScale(q[2])));
      g2d.draw(new Line2D.Double(xScale(x1), yScale(q[0]), xScale(x1), yScale(q[2])));
      g2d.draw(new Line2D.Double(xScale(x3), yScale(q[0]), xScale(x3), yScale(q[2])));

      // outliers
      double upperwhisker = q[2];
      double lowerqhisker = q[0];
      for (int j = 0; j < v.rowCount(); j++) {
        double point = v.value(j);
        if ((point > q[2] + outerFence) || (point < q[0] - outerFence)) {
          // big outlier
          g2d.setStroke(new BasicStroke(options.getLwd()));
          PchPalette.STANDARD.draw(
              g2d, xScale(x2), yScale(point), options.getSz(i), options.getPch(1));
          continue;
        }
        if ((point > q[2] + innerFence) || (point < q[0] - innerFence)) {
          // outlier
          g2d.setStroke(new BasicStroke(options.getLwd()));
          PchPalette.STANDARD.draw(
              g2d, xScale(x2), yScale(point), options.getSz(i), options.getPch(0));
          continue;
        }
        if ((point > upperwhisker) && (point < q[2] + innerFence)) {
          upperwhisker = Math.max(upperwhisker, point);
        }
        if ((point < lowerqhisker) && (point >= q[0] - innerFence)) {
          lowerqhisker = Math.min(lowerqhisker, point);
        }
      }

      // whiskers
      g2d.draw(
          new Line2D.Double(xScale(x1), yScale(upperwhisker), xScale(x3), yScale(upperwhisker)));
      g2d.draw(
          new Line2D.Double(xScale(x1), yScale(lowerqhisker), xScale(x3), yScale(lowerqhisker)));

      g2d.setStroke(
          new BasicStroke(
              options.getLwd(),
              BasicStroke.CAP_BUTT,
              BasicStroke.JOIN_MITER,
              1f,
              new float[] {8},
              0));
      g2d.draw(new Line2D.Double(xScale(x2), yScale(q[2]), xScale(x2), yScale(upperwhisker)));
      g2d.draw(new Line2D.Double(xScale(x2), yScale(q[0]), xScale(x2), yScale(lowerqhisker)));
    }
  }