Github user myui commented on a diff in the pull request:

    https://github.com/apache/incubator-hivemall/pull/92#discussion_r125170771
  
    --- Diff: 
core/src/main/java/hivemall/anomaly/SingularSpectrumTransform.java ---
    @@ -41,161 +41,160 @@
     
     final class SingularSpectrumTransform implements 
SingularSpectrumTransformInterface {
     
    -    @Nonnull
    -    private final PrimitiveObjectInspector oi;
    -    @Nonnull
    -    private final ScoreFunction scoreFunc;
    -
    -    @Nonnull
    -    private final int window;
    -    @Nonnull
    -    private final int nPastWindow;
    -    @Nonnull
    -    private final int nCurrentWindow;
    -    @Nonnull
    -    private final int pastSize;
    -    @Nonnull
    -    private final int currentSize;
    -    @Nonnull
    -    private final int currentOffset;
    -    @Nonnull
    -    private final int r;
    -    @Nonnull
    -    private final int k;
    -
    -    @Nonnull
    -    private final DoubleRingBuffer xRing;
    -    @Nonnull
    -    private final double[] xSeries;
    -
    -    @Nonnull
    -    private final double[] q;
    -
    -    SingularSpectrumTransform(@Nonnull Parameters params, @Nonnull 
PrimitiveObjectInspector oi) {
    -        this.oi = oi;
    -        this.scoreFunc = params.scoreFunc;
    -
    -        this.window = params.w;
    -        this.nPastWindow = params.n;
    -        this.nCurrentWindow = params.m;
    -        this.pastSize = window + nPastWindow;
    -        this.currentSize = window + nCurrentWindow;
    -        this.currentOffset = params.g;
    -        this.r = params.r;
    -        this.k = params.k;
    -        Preconditions.checkArgument(params.k >= params.r);
    -
    -        // (w + n) past samples for the n-past-windows
    -        // (w + m) current samples for the m-current-windows, starting 
from offset g
    -        // => need to hold past (w + n + g + w + m) samples from the 
latest sample
    -        int holdSampleSize = pastSize + currentOffset + currentSize;
    -
    -        this.xRing = new DoubleRingBuffer(holdSampleSize);
    -        this.xSeries = new double[holdSampleSize];
    -
    -        this.q = new double[window];
    -        double norm = 0.d;
    -        for (int i = 0; i < window; i++) {
    -            this.q[i] = Math.random();
    -            norm += q[i] * q[i];
    -        }
    -        norm = Math.sqrt(norm);
    -        // normalize
    -        for (int i = 0; i < window; i++) {
    -            this.q[i] = q[i] / norm;
    -        }
    +  @Nonnull
    +  private final PrimitiveObjectInspector oi;
    +  @Nonnull
    +  private final ScoreFunction scoreFunc;
    +
    +  @Nonnull
    +  private final int window;
    +  @Nonnull
    +  private final int nPastWindow;
    +  @Nonnull
    +  private final int nCurrentWindow;
    +  @Nonnull
    +  private final int pastSize;
    +  @Nonnull
    +  private final int currentSize;
    +  @Nonnull
    +  private final int currentOffset;
    +  @Nonnull
    +  private final int r;
    +  @Nonnull
    +  private final int k;
    +
    +  @Nonnull
    +  private final DoubleRingBuffer xRing;
    +  @Nonnull
    +  private final double[] xSeries;
    +
    +  @Nonnull
    +  private final double[] q;
    +
    +  SingularSpectrumTransform(@Nonnull Parameters params, @Nonnull 
PrimitiveObjectInspector oi) {
    +    this.oi = oi;
    +    this.scoreFunc = params.scoreFunc;
    +
    +    this.window = params.w;
    +    this.nPastWindow = params.n;
    +    this.nCurrentWindow = params.m;
    +    this.pastSize = window + nPastWindow;
    +    this.currentSize = window + nCurrentWindow;
    +    this.currentOffset = params.g;
    +    this.r = params.r;
    +    this.k = params.k;
    +    Preconditions.checkArgument(params.k >= params.r);
    +
    +    // (w + n) past samples for the n-past-windows
    +    // (w + m) current samples for the m-current-windows, starting from 
offset g
    +    // => need to hold past (w + n + g + w + m) samples from the latest 
sample
    +    int holdSampleSize = pastSize + currentOffset + currentSize;
    +
    +    this.xRing = new DoubleRingBuffer(holdSampleSize);
    +    this.xSeries = new double[holdSampleSize];
    +
    +    this.q = new double[window];
    +    double norm = 0.d;
    +    for (int i = 0; i < window; i++) {
    +      this.q[i] = Math.random();
    +      norm += q[i] * q[i];
         }
    -
    -    @Override
    -    public void update(@Nonnull final Object arg, @Nonnull final double[] 
outScores)
    -            throws HiveException {
    -        double x = PrimitiveObjectInspectorUtils.getDouble(arg, oi);
    -        xRing.add(x).toArray(xSeries, true /* FIFO */);
    -
    -        // need to wait until the buffer is filled
    -        if (!xRing.isFull()) {
    -            outScores[0] = 0.d;
    -        } else {
    -            // create past trajectory matrix and find its left singular 
vectors
    -            RealMatrix H = new Array2DRowRealMatrix(window, nPastWindow);
    -            for (int i = 0; i < nPastWindow; i++) {
    -                H.setColumn(i, Arrays.copyOfRange(xSeries, i, i + window));
    -            }
    -
    -            // create current trajectory matrix and find its left singular 
vectors
    -            RealMatrix G = new Array2DRowRealMatrix(window, 
nCurrentWindow);
    -            int currentHead = pastSize + currentOffset;
    -            for (int i = 0; i < nCurrentWindow; i++) {
    -                G.setColumn(i,
    -                    Arrays.copyOfRange(xSeries, currentHead + i, 
currentHead + i + window));
    -            }
    -
    -            switch (scoreFunc) {
    -                case svd:
    -                    outScores[0] = computeScoreSVD(H, G);
    -                    break;
    -                case ika:
    -                    outScores[0] = computeScoreIKA(H, G);
    -                    break;
    -                default:
    -                    throw new IllegalStateException("Unexpected score 
function: " + scoreFunc);
    -            }
    -        }
    +    norm = Math.sqrt(norm);
    +    // normalize
    +    for (int i = 0; i < window; i++) {
    +      this.q[i] = q[i] / norm;
         }
    -
    -    /**
    -     * Singular Value Decomposition (SVD) based naive scoring.
    -     */
    -    private double computeScoreSVD(@Nonnull final RealMatrix H, @Nonnull 
final RealMatrix G) {
    -        SingularValueDecomposition svdH = new 
SingularValueDecomposition(H);
    -        RealMatrix UT = svdH.getUT();
    -
    -        SingularValueDecomposition svdG = new 
SingularValueDecomposition(G);
    -        RealMatrix Q = svdG.getU();
    -
    -        // find the largest singular value for the r principal components
    -        RealMatrix UTQ = UT.getSubMatrix(0, r - 1, 0, window - 1).multiply(
    -            Q.getSubMatrix(0, window - 1, 0, r - 1));
    -        SingularValueDecomposition svdUTQ = new 
SingularValueDecomposition(UTQ);
    -        double[] s = svdUTQ.getSingularValues();
    -
    -        return 1.d - s[0];
    +  }
    +
    +  @Override
    +  public void update(@Nonnull final Object arg, @Nonnull final double[] 
outScores)
    +      throws HiveException {
    +    double x = PrimitiveObjectInspectorUtils.getDouble(arg, oi);
    +    xRing.add(x).toArray(xSeries, true /* FIFO */);
    +
    +    // need to wait until the buffer is filled
    +    if (!xRing.isFull()) {
    +      outScores[0] = 0.d;
    +    } else {
    +      // create past trajectory matrix and find its left singular vectors
    +      RealMatrix H = new Array2DRowRealMatrix(window, nPastWindow);
    +      for (int i = 0; i < nPastWindow; i++) {
    +        H.setColumn(i, Arrays.copyOfRange(xSeries, i, i + window));
    +      }
    +
    +      // create current trajectory matrix and find its left singular 
vectors
    +      RealMatrix G = new Array2DRowRealMatrix(window, nCurrentWindow);
    +      int currentHead = pastSize + currentOffset;
    +      for (int i = 0; i < nCurrentWindow; i++) {
    +        G.setColumn(i, Arrays.copyOfRange(xSeries, currentHead + i, 
currentHead + i + window));
    +      }
    +
    +      switch (scoreFunc) {
    +      case svd:
    --- End diff --
    
    `case` better to be indented as seen in Google coding style.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to