Пример #1
0
  // Qf <- function(par)
  private double qF(double[] par, ArrayRealVector y, ArrayRealVector w) {

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

    fv = (ArrayRealVector) solver.solve(wM.scalarMultiply(FastMath.exp(-par[0])).operate(y));

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

    // DT <-  determinant((exp(-par[1])*W) + (exp(-par[2])*G))$modulus
    double dt =
        new LUD(
                wM.scalarMultiply(FastMath.exp(-par[0]))
                    .add(gM.scalarMultiply(FastMath.exp(-par[1]))))
            .getDeterminant();

    // f <- -(N/2)*log(2*pi*exp(par[1])) +           # 1
    tempV = y.subtract(fv);
    double f =
        -(n / 2) * FastMath.log(2 * FastMath.PI * FastMath.exp(par[0]))

            //               .5*sum(log(weights))-                   # 2
            + 0.5 * MatrixFunctions.sumV(MatrixFunctions.logVec(w))

            //               sum(weights*(y-fv)^2)/(2*exp(par[1])) + # 3
            - MatrixFunctions.sumV(w.ebeMultiply(tempV).ebeMultiply(tempV))
                / (2 * FastMath.exp(par[0]))

            //               (N/2)*log(2*pi) -                       # 4
            + (n * FastMath.log(2 * FastMath.PI)) / 2

            //               ((N-order)/2)*log(2*pi*exp(par[2]))-    # 5
            - ((n - Controls.RW_ORDER) * FastMath.log(2 * FastMath.PI * FastMath.exp(par[1]))) / 2

            //              sum(b^2)/(2*exp(par[2])) -              # 6
            - MatrixFunctions.sumV(b.ebeMultiply(b)) / (2 * FastMath.exp(par[1]))

            //               .5*DT								   # 7
            - 0.5 * dt;

    // attributes(f) <- NULL
    // Pen <- if (penalty==TRUE) delta[1]*(par[1]-shift[1])^2
    // + delta[2]*(par[2]-shift[2])^2 else 0 -f+Pen
    double pen = 0;
    if (Controls.PENALTY) {
      pen =
          delta.getEntry(0) * (par[0] - Controls.RW_SHIFT[0]) * (par[0] - Controls.RW_SHIFT[0])
              + delta.getEntry(1)
                  * (par[1] - Controls.RW_SHIFT[1])
                  * (par[1] - Controls.RW_SHIFT[1]);
    }

    // -f+Pen  # no penalty  yet
    return (-f + pen);
  }
Пример #2
0
  /** Constructor of Random Walk Class. */
  public RW(GAMLSSFamilyDistribution distr, HashMap<Integer, BlockRealMatrix> smoothMatrices) {

    // if(order < 0) {
    if (Controls.RW_ORDER < 0) {
      Controls.RW_ORDER = 1;
      System.err.println(
          "the value of order supplied is zero"
              + " or negative the default value of 1 was used instead");
    }

    // if(sig2e < 0)
    if (Controls.RW_SIG2E < 0) {
      Controls.RW_SIG2E = 1.0;
      System.err.println(
          "the value of sig2e supplied is zero"
              + " or negative the default value of 1 was used instead");
    }

    // if(sig2b < 0)
    if (Controls.RW_SIG2B < 0) {
      Controls.RW_SIG2B = 1.0;
      System.err.println(
          "the value of sig2b supplied is zero"
              + " or negative the default value of 1 was used instead");
    }

    delta = new ArrayRealVector(Controls.RW_DELTA, false);

    // if(any(delta <= 0)||length(delta)!=2)
    if (delta.getMinValue() < 0 || delta.getDimension() != 2) {

      // delta <- c(0.01, 0.01)
      delta = MatrixFunctions.repV(0.01, 2);
      System.err.println(
          "delta should be positive and of length 2, "
              + "deltat is set at default values c(0.01,0.01)");
    }

    // if(length(shift)!=2)
    if (Controls.RW_SHIFT.length != 2) {

      // shift <- c(0, 0)
      Controls.RW_SHIFT = new double[2];
      System.err.println("shift  length should be 2, is it set" + "at default values c(0,0)");
    }

    sigmasHash = new Hashtable<Integer, Double[]>();
    for (int i = 1; i < distr.getNumberOfDistribtionParameters() + 1; i++) {
      if (smoothMatrices.get(i) != null) {
        sigmasHash.put(i, new Double[] {Controls.RW_SIG2E, Controls.RW_SIG2B});
      }
    }
    optimizer = new BOBYQAOptimizer(Controls.BOBYQA_INTERPOL_POINTS);

    if (Controls.SIG2E_FIX) {
      if (Controls.SIG2B_FIX) {
        nonLinObj = null;
      } else {
        nonLinObj1 = new NonLinObjFunction1();
      }
    } else {
      if (Controls.SIG2B_FIX) {
        nonLinObj2 = new NonLinObjFunction2();
      } else {
        nonLinObj = new NonLinObjFunction();
      }
    }
  }
Пример #3
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);
  }