Esempio n. 1
0
  public void outputMatrix(String file) throws Exception {
    int row = lpsolver.getNrows() + 1;
    int col = lpsolver.getNcolumns() + 1;
    ArrayRealVector f = new ArrayRealVector(col - 1);
    for (int j = 1; j < col; j++) {
      f.setEntry(j - 1, lpsolver.getMat(0, j));
    }
    Array2DRowRealMatrix Aeq = new Array2DRowRealMatrix(numOfBus, col - 1);
    ArrayRealVector beq = new ArrayRealVector(numOfBus);

    for (int i = 1; i < numOfBus + 1; i++) {
      for (int j = 1; j < col; j++) {
        Aeq.setEntry(i - 1, j - 1, lpsolver.getMat(i, j));
      }
      beq.setEntry(i - 1, lpsolver.getRh(i));
    }

    Array2DRowRealMatrix Aiq = new Array2DRowRealMatrix(row - numOfBus - 1, col - 1);
    ArrayRealVector biq = new ArrayRealVector(row - numOfBus - 1);

    int cnt = 0;
    for (int i = numOfBus + 1; i < row; i++) {
      for (int j = 1; j < col; j++) {
        Aiq.setEntry(cnt, j - 1, lpsolver.getMat(i, j));
      }
      biq.setEntry(cnt, lpsolver.getRh(i));
      cnt++;
    }

    ArrayRealVector lb = new ArrayRealVector(col - 1);
    for (int i = 1; i < col - 1; i++) {
      lb.setEntry(i - 1, lpsolver.getLowbo(i));
    }

    ArrayRealVector ub = new ArrayRealVector(col - 1);
    for (int i = 1; i < col; i++) {
      ub.setEntry(i - 1, lpsolver.getUpbo(i));
    }

    writeMatlabInputFile(file, f, Aeq, beq, Aiq, biq, ub, lb);
  }
Esempio n. 2
0
  /**
   * The main fitting method of Random Walk smoother.
   *
   * @param additiveFit - object of AdditiveFit class
   * @return residuals
   */
  public ArrayRealVector solve(AdditiveFit additiveFit) {

    ArrayRealVector y = additiveFit.getZ();
    ArrayRealVector w = additiveFit.getW();
    int whichDistParameter = additiveFit.getWhichDistParameter();

    // if (any(is.na(y)))
    for (int i = 0; i < y.getDimension(); i++) {
      if (Double.isNaN(y.getEntry(i))) {

        // weights[is.na(y)] <- 0
        w.setEntry(i, 0.0);

        // y[is.na(y)] <- 0
        y.setEntry(i, 0.0);
      }
    }

    // N <- length(y)
    n = y.getDimension();
    // -------------------------------------------------------  BECOMES VERY SLOW HERE
    // W <- diag.spam(x=weights)  # weights
    wM = (BlockRealMatrix) MatrixUtils.createRealDiagonalMatrix(w.getDataRef());

    // E <- diag.spam(N)
    eM = MatrixFunctions.buildIdentityMatrix(n);

    // D <- diff(E, diff = order) # order
    dM = MatrixFunctions.diff(eM, Controls.RW_ORDER);
    // -------------------------------------------------------  FREEZES HERE
    // G <- t(D)%*%D
    gM = dM.transpose().multiply(dM);

    // logsig2e <- log(sig2e) # getting logs
    double logsig2e = FastMath.log(sigmasHash.get(whichDistParameter)[0]);

    // logsig2b <- log(sig2b)
    double logsig2b = FastMath.log(sigmasHash.get(whichDistParameter)[1]);

    // fv <- solve(exp(-logsig2e)*W + exp(-logsig2b)*G,
    // (exp(-logsig2e)*W)%*% as.vector(y))
    solver =
        new QRDecomposition(
                wM.scalarMultiply(FastMath.exp(-logsig2e))
                    .add(gM.scalarMultiply(FastMath.exp(-logsig2b))))
            .getSolver();

    fv = (ArrayRealVector) solver.solve(wM.scalarMultiply(FastMath.exp(-logsig2e)).operate(y));

    // if (sig2e.fix==FALSE && sig2b.fix==FALSE) # both estimated{
    if (Controls.SIG2E_FIX) {
      if (Controls.SIG2B_FIX) {

        // out <- list()
        // par <- c(logsig2e, logsig2b)
        // out$par <- par
        // value.of.Q <- -Qf(par)
        valueOfQ = -qF(new double[] {logsig2e, logsig2b}, y, w);

        // se <- NULL
        se = null;

      } else {

        // par <- log(sum((D%*%fv)^2)/N)
        tempV = (ArrayRealVector) dM.operate(fv);
        logsig2b = FastMath.log(MatrixFunctions.sumV(tempV.ebeMultiply(tempV)) / n);

        // Qf1 <- function(par)  Qf(c(logsig2e, par))
        // out<-nlminb(start = par,objective = Qf1,
        // lower = c(-20), upper = c(20))

        nonLinObj1.setResponse(y);
        nonLinObj1.setWeights(w);
        nonLinObj1.setLogsig2e(logsig2e);

        maxiter = new MaxIter(Controls.BOBYQA_MAX_ITER);
        maxeval = new MaxEval(Controls.BOBYQA_MAX_EVAL);
        initguess = new InitialGuess(new double[] {logsig2b, logsig2b});
        bounds =
            new SimpleBounds(
                new double[] {-Double.MAX_VALUE, -Double.MAX_VALUE},
                new double[] {Double.MAX_VALUE, Double.MAX_VALUE});
        obj = new ObjectiveFunction(nonLinObj1);
        PointValuePair values =
            optimizer.optimize(maxiter, maxeval, obj, initguess, bounds, GoalType.MINIMIZE);

        logsig2b = values.getPoint()[0];

        if (logsig2b > 20.0) {
          logsig2b = 20.0;
          valueOfQ = -qF(new double[] {logsig2e, logsig2b}, y, w);
        } else if (logsig2b < -20.0) {
          logsig2b = -20.0;
          valueOfQ = -qF(new double[] {logsig2e, logsig2b}, y, w);
        } else {
          valueOfQ = -values.getValue();
        }

        // out$hessian <- optimHess(out$par, Qf1)
        // value.of.Q <- -out$objective
        // shes <- 1/out$hessian
        // se1 <- ifelse(shes>0, sqrt(shes), NA)
        // par <- c(logsig2e, out$par)
        // names(par) <- c("logsig2e", "logsig2b")
        /// out$par <- par
        // se <- c(NA, se1)
        // System.out.println(logsig2e+"   "+logsig2b +"   "+qF(new double[]{logsig2e, logsig2b}, y,
        // w));
      }
    } else {
      if (Controls.SIG2B_FIX) {

        // par <- log(sum(weights*(y-fv)^2)/N)
        tempV = y.subtract(fv);
        logsig2e = FastMath.log(MatrixFunctions.sumV(tempV.ebeMultiply(tempV).ebeMultiply(w)) / n);

        // Qf2 <- function(par)  Qf(c(par, logsig2b))
        // out <- nlminb(start = par, objective = Qf2,
        // lower = c(-20),  upper = c(20))
        nonLinObj2.setResponse(y);
        nonLinObj2.setWeights(w);
        nonLinObj2.setLogsig2b(logsig2b);

        maxiter = new MaxIter(Controls.BOBYQA_MAX_ITER);
        maxeval = new MaxEval(Controls.BOBYQA_MAX_EVAL);
        initguess = new InitialGuess(new double[] {logsig2e, logsig2e});
        bounds =
            new SimpleBounds(
                new double[] {-Double.MAX_VALUE, -Double.MAX_VALUE},
                new double[] {Double.MAX_VALUE, Double.MAX_VALUE});
        obj = new ObjectiveFunction(nonLinObj2);
        PointValuePair values =
            optimizer.optimize(maxiter, maxeval, obj, initguess, bounds, GoalType.MINIMIZE);

        logsig2e = values.getPoint()[0];

        // out$hessian <- optimHess(out$par, Qf2)
        // value.of.Q <- -out$objective
        if (logsig2e > 20.0) {
          logsig2e = 20.0;
          valueOfQ = -qF(new double[] {logsig2e, logsig2b}, y, w);
        } else if (logsig2e < -20.0) {
          logsig2e = -20.0;
          valueOfQ = -qF(new double[] {logsig2e, logsig2b}, y, w);
        } else {
          valueOfQ = -values.getValue();
        }

        // shes <- 1/out$hessian
        // se1 <- ifelse(shes>0, sqrt(shes), NA)
        // par <- c( out$par, logsig2b)
        // names(par) <- c("logsig2e", "logsig2b")
        // out$par <- par
        // se <- c(se1, NA)
        // System.out.println(logsig2e+"   "+logsig2b +"   "+qF(new double[]{logsig2e, logsig2b}, y,
        // w));

      } else {

        // par <- c(logsig2e <- log(sum(weights*(y-fv)^2)/N),
        // logsig2b <-log(sum((D%*%fv)^2)/N))
        tempV = y.subtract(fv);
        logsig2e = FastMath.log(MatrixFunctions.sumV(w.ebeMultiply(tempV).ebeMultiply(tempV)) / n);

        tempV = (ArrayRealVector) dM.operate(fv);
        logsig2b = FastMath.log(MatrixFunctions.sumV(tempV.ebeMultiply(tempV)) / n);

        nonLinObj.setResponse(y);
        nonLinObj.setWeights(w);

        // out <- nlminb(start = par, objective = Qf,
        // lower = c(-20, -20),  upper = c(20, 20))
        maxiter = new MaxIter(Controls.BOBYQA_MAX_ITER);
        maxeval = new MaxEval(Controls.BOBYQA_MAX_EVAL);
        initguess = new InitialGuess(new double[] {logsig2e, logsig2b});
        bounds = new SimpleBounds(new double[] {-20.0, -20.0}, new double[] {20.0, 20.0});
        obj = new ObjectiveFunction(nonLinObj);
        PointValuePair values =
            optimizer.optimize(maxiter, maxeval, obj, initguess, bounds, GoalType.MINIMIZE);

        logsig2e = values.getPoint()[0];
        logsig2b = values.getPoint()[1];

        // System.out.println(logsig2e+"   "+logsig2b +"   "+values.getValue());

        // out$hessian <- optimHess(out$par, Qf) !!! Missing in Java
        // value.of.Q <- -out$objective
        valueOfQ = -values.getValue();

        // shes <- try(solve(out$hessian))
        // se <- if (any(class(shes)%in%"try-error"))
        // rep(NA_real_,2) else sqrt(diag(shes))
      }
    }

    // fv <- solve(exp(-out$par[1])*W + exp(-out$par[2])*G,
    // (exp(-out$par[1])*W)%*% as.vector(y))
    solver =
        new QRDecomposition(
                wM.scalarMultiply(FastMath.exp(-logsig2e))
                    .add(gM.scalarMultiply(FastMath.exp(-logsig2b))))
            .getSolver();

    fv = (ArrayRealVector) solver.solve(wM.scalarMultiply(FastMath.exp(-logsig2e)).operate(y));

    sigmasHash.put(
        whichDistParameter, new Double[] {FastMath.exp(logsig2e), FastMath.exp(logsig2b)});

    // b <- diff(fv, differences=order)
    b = MatrixFunctions.diffV(fv, Controls.RW_ORDER);

    // tr1 <- order + sum(b^2)/(exp(out$par[2])) # this always right
    // attributes(tr1) <- NULL
    double tr1 =
        Controls.RW_ORDER + MatrixFunctions.sumV(b.ebeMultiply(b)) / (FastMath.exp(logsig2b));

    tempV = null;
    return y.subtract(fv);
  }