Example usage for org.apache.commons.math3.linear ArrayRealVector setEntry

List of usage examples for org.apache.commons.math3.linear ArrayRealVector setEntry

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear ArrayRealVector setEntry.

Prototype

@Override
public void setEntry(int index, double value) throws OutOfRangeException 

Source Link

Usage

From source file:edu.utexas.cs.tactex.servercustomers.factoredcustomer.DefaultCapacityOriginator.java

/**
 * NOTE: subtle conversion that caused a bug - we use predictions
 * starting next time-step, but forecastCapacities are assumed
 * to be from current - so to return data to broker we need to
 * offset the record by 1// w  w  w . j ava2  s . c o  m
 */
protected ArrayRealVector convertEnergyProfileFromServerToBroker(CapacityProfile predictedEnergyProfile,
        int recordLength) throws Exception {
    //log.info("scaleEnergyProfile()");
    //log.info("predictedEnergyProfile" + Arrays.toString(predictedEnergyProfile.values.toArray()));
    int profileLength = predictedEnergyProfile.NUM_TIMESLOTS;
    // verify divides
    boolean divides = (recordLength / profileLength * profileLength) == recordLength;
    if (!divides) {
        throw new Exception("How come profileLength doesn't divide recordLength");
    }
    //log.info("recordLength=" + recordLength);
    ArrayRealVector result = new ArrayRealVector(recordLength);

    for (int i = 0; i < recordLength; ++i) {
        result.setEntry(i, predictedEnergyProfile.getCapacity((i + 1) % profileLength));
        //log.info("appending " + predictedEnergyProfile.getCapacity( i % profileLength ) + " at " + i);
    }

    //log.info("result" + Arrays.toString(result.toArray()));
    return result;
}

From source file:gamlss.algorithm.TEST.java

private void twoParTest() {

    String fileName = "Data/distTest.csv";
    CSVFileReader readData = new CSVFileReader(fileName);
    readData.readFile();/*from ww  w .  j  a  va2s  .co  m*/
    ArrayList<String> data = readData.storeValues;

    ArrayRealVector y = new ArrayRealVector(data.size());
    ArrayRealVector mu = new ArrayRealVector(data.size());
    ArrayRealVector sigma = new ArrayRealVector(data.size());
    ArrayRealVector nu = new ArrayRealVector(data.size());
    ArrayRealVector tau = new ArrayRealVector(data.size());

    for (int i = 0; i < data.size(); i++) {
        String[] line = data.get(i).split(",");
        y.setEntry(i, Double.parseDouble(line[0]));
        mu.setEntry(i, Double.parseDouble(line[1]));
        sigma.setEntry(i, Double.parseDouble(line[2]));
        nu.setEntry(i, Double.parseDouble(line[3]));
        tau.setEntry(i, Double.parseDouble(line[4]));
    }

    double[] outA = new double[y.getDimension()];
    String folder = "C:\\Users\\Daniil\\Desktop\\Gamlss_exp/outDistG.csv";
    ArrayRealVector out = null;
    GA dist = new GA();

    dist.setDistributionParameter(DistributionSettings.MU, mu);
    dist.setDistributionParameter(DistributionSettings.SIGMA, sigma);
    dist.setDistributionParameter(DistributionSettings.NU, nu);
    dist.setDistributionParameter(DistributionSettings.TAU, tau);
    //1      
    out = dist.firstDerivative(DistributionSettings.MU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, false);
    //2
    out = dist.secondDerivative(DistributionSettings.MU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //3
    out = dist.firstDerivative(DistributionSettings.SIGMA, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //4
    out = dist.secondDerivative(DistributionSettings.SIGMA, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //5
    //         out = dist.secondCrossDerivative(DistributionSettings.MU, DistributionSettings.SIGMA,  y);
    //         MatrixFunctions.vectorWriteCSV(folder, out, true);
    //6
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.dGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //7
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.dGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), true);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //8
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), true, true);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //9
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), false, true);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //10
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), true, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //11

    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), false, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //12
    for (int i = 0; i < y.getDimension(); i++) {
        //            outA[i] = dist.qGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), true, true);
    }
    //         MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //13
    for (int i = 0; i < y.getDimension(); i++) {
        //            outA[i] = dist.qGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), false, true);
    }
    //         MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //14
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.qGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), true, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //15
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.qGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), false, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //16
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.dGA(y.getEntry(i));
    }
    //         MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //17
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pGA(y.getEntry(i));
    }
    //         MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //18
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.qGA(y.getEntry(i));
    }
    //         MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);

    System.out.println("Done !!!");

}

From source file:gamlss.utilities.TestingDistributions.java

private void twoParTest() {

    String fileName = "Data/distTest.csv";
    CSVFileReader readData = new CSVFileReader(fileName);
    readData.readFile();//from w ww. j  av  a  2 s  .c om
    ArrayList<String> data = readData.storeValues;

    ArrayRealVector y = new ArrayRealVector(data.size());
    ArrayRealVector mu = new ArrayRealVector(data.size());
    ArrayRealVector sigma = new ArrayRealVector(data.size());
    ArrayRealVector nu = new ArrayRealVector(data.size());
    ArrayRealVector tau = new ArrayRealVector(data.size());

    for (int i = 0; i < data.size(); i++) {
        String[] line = data.get(i).split(",");
        y.setEntry(i, Double.parseDouble(line[0]));
        mu.setEntry(i, Double.parseDouble(line[1]));
        sigma.setEntry(i, Double.parseDouble(line[2]));
        nu.setEntry(i, Double.parseDouble(line[3]));
        tau.setEntry(i, Double.parseDouble(line[4]));
    }

    double[] outA = new double[y.getDimension()];
    String folder = "C:\\Users\\Daniil\\Desktop\\Gamlss_exp/outDistG.csv";
    ArrayRealVector out = null;
    GA dist = new GA();

    dist.setDistributionParameter(DistributionSettings.MU, mu);
    dist.setDistributionParameter(DistributionSettings.SIGMA, sigma);
    dist.setDistributionParameter(DistributionSettings.NU, nu);
    dist.setDistributionParameter(DistributionSettings.TAU, tau);
    //1      
    out = dist.firstDerivative(DistributionSettings.MU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, false);
    //2
    out = dist.secondDerivative(DistributionSettings.MU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //3
    out = dist.firstDerivative(DistributionSettings.SIGMA, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //4
    out = dist.secondDerivative(DistributionSettings.SIGMA, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //5
    //            out = dist.secondCrossDerivative(DistributionSettings.MU, DistributionSettings.SIGMA,  y);
    //            MatrixFunctions.vectorWriteCSV(folder, out, true);
    //6
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.dGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //7
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.dGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), true);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //8
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), true, true);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //9
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), false, true);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //10
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), true, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //11

    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), false, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //12
    for (int i = 0; i < y.getDimension(); i++) {
        //               outA[i] = dist.qGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), true, true);
    }
    //            MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //13
    for (int i = 0; i < y.getDimension(); i++) {
        //               outA[i] = dist.qGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), false, true);
    }
    //            MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //14
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.qGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), true, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //15
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.qGA(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), false, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //16
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.dGA(y.getEntry(i));
    }
    //            MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //17
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pGA(y.getEntry(i));
    }
    //            MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //18
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.qGA(y.getEntry(i));
    }
    //            MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);

    System.out.println("Done !!!");

}

From source file:gamlss.algorithm.TEST.java

private void threeParTest() {

    String fileName = "Data/distTest.csv";
    CSVFileReader readData = new CSVFileReader(fileName);
    readData.readFile();/*  w  w  w.jav a 2s .c  om*/
    ArrayList<String> data = readData.storeValues;

    ArrayRealVector y = new ArrayRealVector(data.size());
    ArrayRealVector mu = new ArrayRealVector(data.size());
    ArrayRealVector sigma = new ArrayRealVector(data.size());
    ArrayRealVector nu = new ArrayRealVector(data.size());
    ArrayRealVector tau = new ArrayRealVector(data.size());

    for (int i = 0; i < data.size(); i++) {
        String[] line = data.get(i).split(",");
        y.setEntry(i, Double.parseDouble(line[0]));
        mu.setEntry(i, Double.parseDouble(line[1]));
        sigma.setEntry(i, Double.parseDouble(line[2]));
        nu.setEntry(i, Double.parseDouble(line[3]));
        tau.setEntry(i, Double.parseDouble(line[4]));
    }

    double[] outA = new double[y.getDimension()];
    String folder = "C:\\Users\\Daniil\\Desktop\\Gamlss_exp/outDistG.csv";
    ArrayRealVector out = null;

    TF2 dist = new TF2();

    dist.setDistributionParameter(DistributionSettings.MU, mu);
    dist.setDistributionParameter(DistributionSettings.SIGMA, sigma);
    dist.setDistributionParameter(DistributionSettings.NU, nu);
    dist.setDistributionParameter(DistributionSettings.TAU, tau);
    //1      
    out = dist.firstDerivative(DistributionSettings.MU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, false);
    //2
    out = dist.secondDerivative(DistributionSettings.MU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //3
    out = dist.firstDerivative(DistributionSettings.SIGMA, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //4
    out = dist.secondDerivative(DistributionSettings.SIGMA, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //5
    out = dist.secondCrossDerivative(DistributionSettings.MU, DistributionSettings.SIGMA, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //6
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.dTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //7
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.dTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), true);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //8
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), true, true);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //9
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), false, true);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //10
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), true, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //11

    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), false, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //12
    for (int i = 0; i < y.getDimension(); i++) {
        //         outA[i] = dist.qTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i),nu.getEntry(i), true, true);
    }
    //      MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //13
    for (int i = 0; i < y.getDimension(); i++) {
        //         outA[i] = dist.qTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i),nu.getEntry(i), false, true);
    }
    //      MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //14
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.qTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), true, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //15
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.qTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), false, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //16
    //for(int i = 0; i < y.getDimension(); i++) { 
    //   outA[i] = dist.dTF2(y.getEntry(i));
    //}
    //MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //17
    //for(int i = 0; i < y.getDimension(); i++) { 
    //   outA[i] = dist.pTF2(y.getEntry(i));
    //}
    //MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //18
    //for(int i = 0; i < y.getDimension(); i++) { 
    //   outA[i] = dist.qTF2(y.getEntry(i));
    //}
    //MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //19
    out = dist.firstDerivative(DistributionSettings.NU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //20
    out = dist.secondDerivative(DistributionSettings.NU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //21
    out = dist.secondCrossDerivative(DistributionSettings.MU, DistributionSettings.NU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //23
    out = dist.secondCrossDerivative(DistributionSettings.SIGMA, DistributionSettings.NU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //

    System.out.println("Done !!!");

}

From source file:gamlss.utilities.TestingDistributions.java

private void threeParTest() {

    String fileName = "Data/distTest.csv";
    CSVFileReader readData = new CSVFileReader(fileName);
    readData.readFile();//from w ww. j  a va2  s  .c o  m
    ArrayList<String> data = readData.storeValues;

    ArrayRealVector y = new ArrayRealVector(data.size());
    ArrayRealVector mu = new ArrayRealVector(data.size());
    ArrayRealVector sigma = new ArrayRealVector(data.size());
    ArrayRealVector nu = new ArrayRealVector(data.size());
    ArrayRealVector tau = new ArrayRealVector(data.size());

    for (int i = 0; i < data.size(); i++) {
        String[] line = data.get(i).split(",");
        y.setEntry(i, Double.parseDouble(line[0]));
        mu.setEntry(i, Double.parseDouble(line[1]));
        sigma.setEntry(i, Double.parseDouble(line[2]));
        nu.setEntry(i, Double.parseDouble(line[3]));
        tau.setEntry(i, Double.parseDouble(line[4]));
    }

    double[] outA = new double[y.getDimension()];
    String folder = "C:\\Users\\Daniil\\Desktop\\Gamlss_exp/outDistG.csv";
    ArrayRealVector out = null;

    TF2 dist = new TF2();

    dist.setDistributionParameter(DistributionSettings.MU, mu);
    dist.setDistributionParameter(DistributionSettings.SIGMA, sigma);
    dist.setDistributionParameter(DistributionSettings.NU, nu);
    dist.setDistributionParameter(DistributionSettings.TAU, tau);
    //1      
    out = dist.firstDerivative(DistributionSettings.MU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, false);
    //2
    out = dist.secondDerivative(DistributionSettings.MU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //3
    out = dist.firstDerivative(DistributionSettings.SIGMA, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //4
    out = dist.secondDerivative(DistributionSettings.SIGMA, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //5
    out = dist.secondCrossDerivative(DistributionSettings.MU, DistributionSettings.SIGMA, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //6
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.dTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //7
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.dTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), true);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //8
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), true, true);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //9
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), false, true);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //10
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), true, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //11

    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), false, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //12
    for (int i = 0; i < y.getDimension(); i++) {
        //            outA[i] = dist.qTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i),nu.getEntry(i), true, true);
    }
    //         MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //13
    for (int i = 0; i < y.getDimension(); i++) {
        //            outA[i] = dist.qTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i),nu.getEntry(i), false, true);
    }
    //         MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //14
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.qTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), true, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //15
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.qTF2(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), false, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //16
    //for(int i = 0; i < y.getDimension(); i++) { 
    //   outA[i] = dist.dTF2(y.getEntry(i));
    //}
    //MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //17
    //for(int i = 0; i < y.getDimension(); i++) { 
    //   outA[i] = dist.pTF2(y.getEntry(i));
    //}
    //MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //18
    //for(int i = 0; i < y.getDimension(); i++) { 
    //   outA[i] = dist.qTF2(y.getEntry(i));
    //}
    //MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //19
    out = dist.firstDerivative(DistributionSettings.NU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //20
    out = dist.secondDerivative(DistributionSettings.NU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //21
    out = dist.secondCrossDerivative(DistributionSettings.MU, DistributionSettings.NU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //23
    out = dist.secondCrossDerivative(DistributionSettings.SIGMA, DistributionSettings.NU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //

    System.out.println("Done !!!");

}

From source file:gamlss.algorithm.TEST.java

private void fourParTest() {

    String fileName = "Data/distTest2.csv";
    CSVFileReader readData = new CSVFileReader(fileName);
    readData.readFile();//w  w w.j a v a  2s.  c  o  m
    ArrayList<String> data = readData.storeValues;

    ArrayRealVector y = new ArrayRealVector(data.size());
    ArrayRealVector mu = new ArrayRealVector(data.size());
    ArrayRealVector sigma = new ArrayRealVector(data.size());
    ArrayRealVector nu = new ArrayRealVector(data.size());
    ArrayRealVector tau = new ArrayRealVector(data.size());

    for (int i = 0; i < data.size(); i++) {
        String[] line = data.get(i).split(",");
        y.setEntry(i, Double.parseDouble(line[0]));
        mu.setEntry(i, Double.parseDouble(line[1]));
        sigma.setEntry(i, Double.parseDouble(line[2]));
        nu.setEntry(i, Double.parseDouble(line[3]));
        tau.setEntry(i, Double.parseDouble(line[4]));
    }

    double[] outA = new double[y.getDimension()];
    String folder = "C:\\Users\\Daniil\\Desktop\\Gamlss_exp/outDistG.csv";
    ArrayRealVector out = null;

    SST dist = new SST();

    dist.setDistributionParameter(DistributionSettings.MU, mu);
    dist.setDistributionParameter(DistributionSettings.SIGMA, sigma);
    dist.setDistributionParameter(DistributionSettings.NU, nu);
    dist.setDistributionParameter(DistributionSettings.TAU, tau);
    //0   
    out = dist.firstDerivative(DistributionSettings.MU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, false);
    //1
    out = dist.secondDerivative(DistributionSettings.MU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //2
    out = dist.firstDerivative(DistributionSettings.SIGMA, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //3
    out = dist.secondDerivative(DistributionSettings.SIGMA, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //4
    out = dist.secondCrossDerivative(DistributionSettings.MU, DistributionSettings.SIGMA, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //5
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.dSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), tau.getEntry(i),
                false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //6
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.dSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), tau.getEntry(i),
                true);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //7
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), tau.getEntry(i),
                true, false);
        //System.out.println(outA[i]);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //8
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), tau.getEntry(i),
                false, true);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //9
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), tau.getEntry(i),
                true, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //10
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), tau.getEntry(i),
                false, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //11
    for (int i = 0; i < y.getDimension(); i++) {
        //         outA[i] = dist.qSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i),nu.getEntry(i),tau.getEntry(i), true, true);
    }
    //      MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //12
    for (int i = 0; i < y.getDimension(); i++) {
        //         outA[i] = dist.qSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i),nu.getEntry(i),tau.getEntry(i), false, true);
    }
    //      MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //13
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.qSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), tau.getEntry(i),
                true, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //14
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.qSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), tau.getEntry(i),
                false, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //15
    out = dist.firstDerivative(DistributionSettings.NU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //16
    out = dist.secondDerivative(DistributionSettings.NU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //17
    out = dist.secondCrossDerivative(DistributionSettings.MU, DistributionSettings.NU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //18
    out = dist.secondCrossDerivative(DistributionSettings.SIGMA, DistributionSettings.NU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //19
    out = dist.firstDerivative(DistributionSettings.TAU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //20
    out = dist.secondDerivative(DistributionSettings.TAU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //21
    out = dist.secondCrossDerivative(DistributionSettings.MU, DistributionSettings.TAU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //22
    out = dist.secondCrossDerivative(DistributionSettings.SIGMA, DistributionSettings.TAU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //23
    out = dist.secondCrossDerivative(DistributionSettings.NU, DistributionSettings.TAU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);

    System.out.println("Done !!!");

}

From source file:gamlss.utilities.TestingDistributions.java

private void fourParTest() {

    String fileName = "Data/distTest2.csv";
    CSVFileReader readData = new CSVFileReader(fileName);
    readData.readFile();/*from   ww w  . j  a  v a 2s .c  o m*/
    ArrayList<String> data = readData.storeValues;

    ArrayRealVector y = new ArrayRealVector(data.size());
    ArrayRealVector mu = new ArrayRealVector(data.size());
    ArrayRealVector sigma = new ArrayRealVector(data.size());
    ArrayRealVector nu = new ArrayRealVector(data.size());
    ArrayRealVector tau = new ArrayRealVector(data.size());

    for (int i = 0; i < data.size(); i++) {
        String[] line = data.get(i).split(",");
        y.setEntry(i, Double.parseDouble(line[0]));
        mu.setEntry(i, Double.parseDouble(line[1]));
        sigma.setEntry(i, Double.parseDouble(line[2]));
        nu.setEntry(i, Double.parseDouble(line[3]));
        tau.setEntry(i, Double.parseDouble(line[4]));
    }

    double[] outA = new double[y.getDimension()];
    String folder = "C:\\Users\\Daniil\\Desktop\\Gamlss_exp/outDistG.csv";
    ArrayRealVector out = null;

    SST dist = new SST();

    dist.setDistributionParameter(DistributionSettings.MU, mu);
    dist.setDistributionParameter(DistributionSettings.SIGMA, sigma);
    dist.setDistributionParameter(DistributionSettings.NU, nu);
    dist.setDistributionParameter(DistributionSettings.TAU, tau);
    //0   
    out = dist.firstDerivative(DistributionSettings.MU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, false);
    //1
    out = dist.secondDerivative(DistributionSettings.MU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //2
    out = dist.firstDerivative(DistributionSettings.SIGMA, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //3
    out = dist.secondDerivative(DistributionSettings.SIGMA, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //4
    out = dist.secondCrossDerivative(DistributionSettings.MU, DistributionSettings.SIGMA, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //5
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.dSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), tau.getEntry(i),
                false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //6
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.dSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), tau.getEntry(i),
                true);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //7
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), tau.getEntry(i),
                true, false);
        //System.out.println(outA[i]);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //8
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), tau.getEntry(i),
                false, true);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //9
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), tau.getEntry(i),
                true, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //10
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.pSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), tau.getEntry(i),
                false, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //11
    for (int i = 0; i < y.getDimension(); i++) {
        //            outA[i] = dist.qSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i),nu.getEntry(i),tau.getEntry(i), true, true);
    }
    //         MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //12
    for (int i = 0; i < y.getDimension(); i++) {
        //            outA[i] = dist.qSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i),nu.getEntry(i),tau.getEntry(i), false, true);
    }
    //         MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //13
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.qSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), tau.getEntry(i),
                true, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //14
    for (int i = 0; i < y.getDimension(); i++) {
        outA[i] = dist.qSST(y.getEntry(i), mu.getEntry(i), sigma.getEntry(i), nu.getEntry(i), tau.getEntry(i),
                false, false);
    }
    MatrixFunctions.vectorWriteCSV(folder, new ArrayRealVector(outA, false), true);
    //15
    out = dist.firstDerivative(DistributionSettings.NU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //16
    out = dist.secondDerivative(DistributionSettings.NU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //17
    out = dist.secondCrossDerivative(DistributionSettings.MU, DistributionSettings.NU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //18
    out = dist.secondCrossDerivative(DistributionSettings.SIGMA, DistributionSettings.NU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //19
    out = dist.firstDerivative(DistributionSettings.TAU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //20
    out = dist.secondDerivative(DistributionSettings.TAU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //21
    out = dist.secondCrossDerivative(DistributionSettings.MU, DistributionSettings.TAU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //22
    out = dist.secondCrossDerivative(DistributionSettings.SIGMA, DistributionSettings.TAU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);
    //23
    out = dist.secondCrossDerivative(DistributionSettings.NU, DistributionSettings.TAU, y);
    MatrixFunctions.vectorWriteCSV(folder, out, true);

    System.out.println("Done !!!");

}

From source file:edu.stanford.cfuller.imageanalysistools.filter.LocalMaximumSeparabilityThresholdingFilter.java

/**
 * Applies the filter to an Image, optionally turning on adaptive determination of the increment size used to find the threshold, and specifying a size for the threshold determination increment.
 * Turning on adaptive determination of the increment will generally make the threshold slightly less optimal, but can sometimes speed up the filtering, especially
 * for images with a large dynamic range.
 * <p>//from w  w w.  java2 s .  com
 * The increment size specified (in greylevels) will be used to determine the threshold only if adaptive determination is off; otherwise this parameter will be ignored.
 * @param im    The Image to be thresholded; this will be overwritten by the thresholded Image.
 * @param adaptiveincrement     true to turn on adaptive determination of the threshold increment; false to turn it off and use the default value
 * @param increment             the increment size (in greylevels) to use for determining the threshold; must be positive.
 */
public void apply_ext(WritableImage im, boolean adaptiveincrement, int increment) {

    Histogram h = new Histogram(im);

    int thresholdValue = 0;

    final int numSteps = 1000;

    double best_eta = 0;
    int best_index = Integer.MAX_VALUE;

    int nonzerocounts = h.getTotalCounts() - h.getCounts(0);

    double meannonzero = h.getMeanNonzero();

    ArrayRealVector omega_v = new ArrayRealVector(h.getMaxValue());
    ArrayRealVector mu_v = new ArrayRealVector(h.getMaxValue());
    ArrayRealVector eta_v = new ArrayRealVector(h.getMaxValue());

    int c = 0;

    if (adaptiveincrement) {
        increment = (int) ((h.getMaxValue() - h.getMinValue() + 1) * 1.0 / numSteps);
        if (increment < 1)
            increment = 1;
    }

    for (int k = h.getMinValue(); k < h.getMaxValue() + 1; k += increment) {

        if (k == 0)
            continue;

        omega_v.setEntry(c, h.getCumulativeCounts(k) * 1.0 / nonzerocounts);

        if (c == 0) {
            mu_v.setEntry(c, k * omega_v.getEntry(c));
        } else {

            mu_v.setEntry(c, mu_v.getEntry(c - 1) + k * h.getCounts(k) * 1.0 / nonzerocounts);
            for (int i = k - increment + 1; i < k; i++) {
                mu_v.setEntry(c, mu_v.getEntry(c) + h.getCounts(i) * i * 1.0 / nonzerocounts);
            }

        }

        double omega = omega_v.getEntry(c);
        double mu = mu_v.getEntry(c);

        if (omega > 1e-8 && 1 - omega > 1e-8) {

            double eta = omega * (1 - omega) * Math.pow((meannonzero - mu) / (1 - omega) - mu / omega, 2);

            eta_v.setEntry(c, eta);

            if (eta >= best_eta) {
                best_eta = eta;
                best_index = k;
            }

        } else {
            eta_v.setEntry(c, 0);
        }

        c++;

    }

    int orig_method_best_index = best_index;

    c = 1;

    ArrayList<Integer> maxima = new ArrayList<Integer>();
    Map<Integer, Integer> k_by_c = new HashMap<Integer, Integer>();
    Map<Integer, Integer> c_by_k = new HashMap<Integer, Integer>();

    for (int k = h.getMinValue() + 1; k < h.getMaxValue(); k += increment) {

        //detect if this is a local maximum

        k_by_c.put(c, k);
        c_by_k.put(k, c);

        int lastEntryNotEqual = c - 1;
        int nextEntryNotEqual = c + 1;

        while (lastEntryNotEqual > 0 && eta_v.getEntry(lastEntryNotEqual) == eta_v.getEntry(c)) {
            --lastEntryNotEqual;
        }
        while (nextEntryNotEqual < (eta_v.getDimension() - 1)
                && eta_v.getEntry(nextEntryNotEqual) == eta_v.getEntry(c)) {
            ++nextEntryNotEqual;
        }

        if (eta_v.getEntry(c) > eta_v.getEntry(lastEntryNotEqual)
                && eta_v.getEntry(c) > eta_v.getEntry(nextEntryNotEqual)) {

            maxima.add(k);

        }

        c++;

    }

    //now that we have maxima, try doing a gaussian fit to find the positions.  If there's only one, we need to guess at a second

    RealVector parameters = new ArrayRealVector(6, 0.0);

    int position0 = 0;
    int position1 = h.getMaxValue();

    if (maxima.size() > 1) {

        double best_max = 0;
        double second_best_max = 0;

        int best_pos = 0;
        int second_best_pos = 0;

        for (Integer k : maxima) {

            int ck = c_by_k.get(k);

            double eta_k = eta_v.getEntry(ck);

            if (eta_k > best_max) {

                second_best_max = best_max;
                second_best_pos = best_pos;

                best_max = eta_k;
                best_pos = ck;

            } else if (eta_k > second_best_max) {

                second_best_max = eta_k;
                second_best_pos = ck;

            }

        }

        position0 = best_pos;

        position1 = second_best_pos;

    } else {

        position0 = c_by_k.get(maxima.get(0));
        position1 = (eta_v.getDimension() - position0) / 2 + position0;

    }

    //make sure that position 1 is larger than position 0

    if (position1 < position0) {

        int temp = position0;
        position0 = position1;
        position1 = temp;

    }

    double s = (position1 - position0) / 4.0;

    parameters.setEntry(0, eta_v.getEntry(position0));//*Math.sqrt(2*Math.PI)*s);
    parameters.setEntry(1, position0);
    parameters.setEntry(2, s);
    parameters.setEntry(3, eta_v.getEntry(position1));//*Math.sqrt(2*Math.PI)*s);
    parameters.setEntry(4, position1);
    parameters.setEntry(5, s);

    DoubleGaussianObjectiveFunction dgof = new DoubleGaussianObjectiveFunction();

    dgof.setEta(eta_v);

    NelderMeadMinimizer nmm = new NelderMeadMinimizer();

    RealVector result = nmm.optimize(dgof, parameters);

    best_index = (int) result.getEntry(4);

    if (k_by_c.containsKey(best_index)) {
        best_index = k_by_c.get(best_index);
    } else {
        //fall back to the normal global maximum if the fitting seems to have found an invalid value.
        best_index = orig_method_best_index;

    }

    thresholdValue = best_index;

    if (thresholdValue == Integer.MAX_VALUE) {
        thresholdValue = 0;
    }
    for (ImageCoordinate coord : im) {
        if (im.getValue(coord) < thresholdValue)
            im.setValue(coord, 0);
    }

}

From source file:com.itemanalysis.psychometrics.optimization.BOBYQAOptimizer.java

/**
 *     The arguments N, NPT, XPT, XOPT, BMAT, ZMAT, NDIM, SL and SU all have
 *       the same meanings as the corresponding arguments of BOBYQB.
 *     KOPT is the index of the optimal interpolation point.
 *     KNEW is the index of the interpolation point that is going to be moved.
 *     ADELT is the current trust region bound.
 *     XNEW will be set to a suitable new position for the interpolation point
 *       XPT(KNEW,.). Specifically, it satisfies the SL, SU and trust region
 *       bounds and it should provide a large denominator in the next call of
 *       UPDATE. The step XNEW-XOPT from XOPT is restricted to moves along the
 *       straight lines through XOPT and another interpolation point.
 *     XALT also provides a large value of the modulus of the KNEW-th Lagrange
 *       function subject to the constraints that have been mentioned, its main
 *       difference from XNEW being that XALT-XOPT is a constrained version of
 *       the Cauchy step within the trust region. An exception is that XALT is
 *       not calculated if all components of GLAG (see below) are zero.
 *     ALPHA will be set to the KNEW-th diagonal element of the H matrix.
 *     CAUCHY will be set to the square of the KNEW-th Lagrange function at
 *       the step XALT-XOPT from XOPT for the vector XALT that is returned,
 *       except that CAUCHY is set to zero if XALT is not calculated.
 *     GLAG is a working space vector of length N for the gradientAt of the
 *       KNEW-th Lagrange function at XOPT.
 *     HCOL is a working space vector of length NPT for the second derivative
 *       coefficients of the KNEW-th Lagrange function.
 *     W is a working space vector of length 2N that is going to hold the
 *       constrained Cauchy step from XOPT of the Lagrange function, followed
 *       by the downhill version of XALT when the uphill step is calculated.
 *
 *     Set the first NPT components of W to the leading elements of the
 *     KNEW-th column of the H matrix./*  w  w  w  .  j av  a  2s  .  co m*/
 * @param knew
 * @param adelt
 */
private double[] altmov(int knew, double adelt) {
    printMethod(); // XXX

    final int n = currentBest.getDimension();
    final int npt = numberOfInterpolationPoints;

    final ArrayRealVector glag = new ArrayRealVector(n);
    final ArrayRealVector hcol = new ArrayRealVector(npt);

    final ArrayRealVector work1 = new ArrayRealVector(n);
    final ArrayRealVector work2 = new ArrayRealVector(n);

    for (int k = 0; k < npt; k++) {
        hcol.setEntry(k, ZERO);
    }
    for (int j = 0, max = npt - n - 1; j < max; j++) {
        final double tmp = zMatrix.getEntry(knew, j);
        for (int k = 0; k < npt; k++) {
            hcol.setEntry(k, hcol.getEntry(k) + tmp * zMatrix.getEntry(k, j));
        }
    }
    final double alpha = hcol.getEntry(knew);
    final double ha = HALF * alpha;

    // Calculate the gradientAt of the KNEW-th Lagrange function at XOPT.

    for (int i = 0; i < n; i++) {
        glag.setEntry(i, bMatrix.getEntry(knew, i));
    }
    for (int k = 0; k < npt; k++) {
        double tmp = ZERO;
        for (int j = 0; j < n; j++) {
            tmp += interpolationPoints.getEntry(k, j) * trustRegionCenterOffset.getEntry(j);
        }
        tmp *= hcol.getEntry(k);
        for (int i = 0; i < n; i++) {
            glag.setEntry(i, glag.getEntry(i) + tmp * interpolationPoints.getEntry(k, i));
        }
    }

    // Search for a large denominator along the straight lines through XOPT
    // and another interpolation point. SLBD and SUBD will be lower and upper
    // bounds on the step along each of these lines in turn. PREDSQ will be
    // set to the square of the predicted denominator for each line. PRESAV
    // will be set to the largest admissible value of PREDSQ that occurs.

    double presav = ZERO;
    double step = Double.NaN;
    int ksav = 0;
    int ibdsav = 0;
    double stpsav = 0;
    for (int k = 0; k < npt; k++) {
        if (k == trustRegionCenterInterpolationPointIndex) {
            continue;
        }
        double dderiv = ZERO;
        double distsq = ZERO;
        for (int i = 0; i < n; i++) {
            final double tmp = interpolationPoints.getEntry(k, i) - trustRegionCenterOffset.getEntry(i);
            dderiv += glag.getEntry(i) * tmp;
            distsq += tmp * tmp;
        }
        double subd = adelt / Math.sqrt(distsq);
        double slbd = -subd;
        int ilbd = 0;
        int iubd = 0;
        final double sumin = Math.min(ONE, subd);

        // Revise SLBD and SUBD if necessary because of the bounds in SL and SU.

        for (int i = 0; i < n; i++) {
            final double tmp = interpolationPoints.getEntry(k, i) - trustRegionCenterOffset.getEntry(i);
            if (tmp > ZERO) {
                if (slbd * tmp < lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) {
                    slbd = (lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp;
                    ilbd = -i - 1;
                }
                if (subd * tmp > upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) {
                    // Computing MAX
                    subd = Math.max(sumin,
                            (upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp);
                    iubd = i + 1;
                }
            } else if (tmp < ZERO) {
                if (slbd * tmp > upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) {
                    slbd = (upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp;
                    ilbd = i + 1;
                }
                if (subd * tmp < lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) {
                    // Computing MAX
                    subd = Math.max(sumin,
                            (lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i)) / tmp);
                    iubd = -i - 1;
                }
            }
        }

        // Seek a large modulus of the KNEW-th Lagrange function when the index
        // of the other interpolation point on the line through XOPT is KNEW.

        step = slbd;
        int isbd = ilbd;
        double vlag = Double.NaN;
        if (k == knew) {
            final double diff = dderiv - ONE;
            vlag = slbd * (dderiv - slbd * diff);
            final double d1 = subd * (dderiv - subd * diff);
            if (Math.abs(d1) > Math.abs(vlag)) {
                step = subd;
                vlag = d1;
                isbd = iubd;
            }
            final double d2 = HALF * dderiv;
            final double d3 = d2 - diff * slbd;
            final double d4 = d2 - diff * subd;
            if (d3 * d4 < ZERO) {
                final double d5 = d2 * d2 / diff;
                if (Math.abs(d5) > Math.abs(vlag)) {
                    step = d2 / diff;
                    vlag = d5;
                    isbd = 0;
                }
            }

            // Search along each of the other lines through XOPT and another point.

        } else {
            vlag = slbd * (ONE - slbd);
            final double tmp = subd * (ONE - subd);
            if (Math.abs(tmp) > Math.abs(vlag)) {
                step = subd;
                vlag = tmp;
                isbd = iubd;
            }
            if (subd > HALF) {
                if (Math.abs(vlag) < ONE_OVER_FOUR) {
                    step = HALF;
                    vlag = ONE_OVER_FOUR;
                    isbd = 0;
                }
            }
            vlag *= dderiv;
        }

        // Calculate PREDSQ for the current line search and maintain PRESAV.

        final double tmp = step * (ONE - step) * distsq;
        final double predsq = vlag * vlag * (vlag * vlag + ha * tmp * tmp);
        if (predsq > presav) {
            presav = predsq;
            ksav = k;
            stpsav = step;
            ibdsav = isbd;
        }
    }

    // Construct XNEW in a way that satisfies the bound constraints exactly.

    for (int i = 0; i < n; i++) {
        final double tmp = trustRegionCenterOffset.getEntry(i)
                + stpsav * (interpolationPoints.getEntry(ksav, i) - trustRegionCenterOffset.getEntry(i));
        newPoint.setEntry(i, Math.max(lowerDifference.getEntry(i), Math.min(upperDifference.getEntry(i), tmp)));
    }
    if (ibdsav < 0) {
        newPoint.setEntry(-ibdsav - 1, lowerDifference.getEntry(-ibdsav - 1));
    }
    if (ibdsav > 0) {
        newPoint.setEntry(ibdsav - 1, upperDifference.getEntry(ibdsav - 1));
    }

    // Prepare for the iterative method that assembles the constrained Cauchy
    // step in W. The sum of squares of the fixed components of W is formed in
    // WFIXSQ, and the free components of W are set to BIGSTP.

    final double bigstp = adelt + adelt;
    int iflag = 0;
    double cauchy = Double.NaN;
    double csave = ZERO;
    while (true) {
        double wfixsq = ZERO;
        double ggfree = ZERO;
        for (int i = 0; i < n; i++) {
            final double glagValue = glag.getEntry(i);
            work1.setEntry(i, ZERO);
            if (Math.min(trustRegionCenterOffset.getEntry(i) - lowerDifference.getEntry(i), glagValue) > ZERO
                    || Math.max(trustRegionCenterOffset.getEntry(i) - upperDifference.getEntry(i),
                            glagValue) < ZERO) {
                work1.setEntry(i, bigstp);
                // Computing 2nd power
                ggfree += glagValue * glagValue;
            }
        }
        if (ggfree == ZERO) {
            return new double[] { alpha, ZERO };
        }

        // Investigate whether more components of W can be fixed.
        final double tmp1 = adelt * adelt - wfixsq;
        if (tmp1 > ZERO) {
            step = Math.sqrt(tmp1 / ggfree);
            ggfree = ZERO;
            for (int i = 0; i < n; i++) {
                if (work1.getEntry(i) == bigstp) {
                    final double tmp2 = trustRegionCenterOffset.getEntry(i) - step * glag.getEntry(i);
                    if (tmp2 <= lowerDifference.getEntry(i)) {
                        work1.setEntry(i, lowerDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i));
                        // Computing 2nd power
                        final double d1 = work1.getEntry(i);
                        wfixsq += d1 * d1;
                    } else if (tmp2 >= upperDifference.getEntry(i)) {
                        work1.setEntry(i, upperDifference.getEntry(i) - trustRegionCenterOffset.getEntry(i));
                        // Computing 2nd power
                        final double d1 = work1.getEntry(i);
                        wfixsq += d1 * d1;
                    } else {
                        // Computing 2nd power
                        final double d1 = glag.getEntry(i);
                        ggfree += d1 * d1;
                    }
                }
            }
        }

        // Set the remaining free components of W and all components of XALT,
        // except that W may be scaled later.

        double gw = ZERO;
        for (int i = 0; i < n; i++) {
            final double glagValue = glag.getEntry(i);
            if (work1.getEntry(i) == bigstp) {
                work1.setEntry(i, -step * glagValue);
                final double min = Math.min(upperDifference.getEntry(i),
                        trustRegionCenterOffset.getEntry(i) + work1.getEntry(i));
                alternativeNewPoint.setEntry(i, Math.max(lowerDifference.getEntry(i), min));
            } else if (work1.getEntry(i) == ZERO) {
                alternativeNewPoint.setEntry(i, trustRegionCenterOffset.getEntry(i));
            } else if (glagValue > ZERO) {
                alternativeNewPoint.setEntry(i, lowerDifference.getEntry(i));
            } else {
                alternativeNewPoint.setEntry(i, upperDifference.getEntry(i));
            }
            gw += glagValue * work1.getEntry(i);
        }

        // Set CURV to the curvature of the KNEW-th Lagrange function along W.
        // Scale W by a factor less than one if that can reduce the modulus of
        // the Lagrange function at XOPT+W. Set CAUCHY to the final value of
        // the square of this function.

        double curv = ZERO;
        for (int k = 0; k < npt; k++) {
            double tmp = ZERO;
            for (int j = 0; j < n; j++) {
                tmp += interpolationPoints.getEntry(k, j) * work1.getEntry(j);
            }
            curv += hcol.getEntry(k) * tmp * tmp;
        }
        if (iflag == 1) {
            curv = -curv;
        }
        if (curv > -gw && curv < -gw * (ONE + Math.sqrt(TWO))) {
            final double scale = -gw / curv;
            for (int i = 0; i < n; i++) {
                final double tmp = trustRegionCenterOffset.getEntry(i) + scale * work1.getEntry(i);
                alternativeNewPoint.setEntry(i,
                        Math.max(lowerDifference.getEntry(i), Math.min(upperDifference.getEntry(i), tmp)));
            }
            // Computing 2nd power
            final double d1 = HALF * gw * scale;
            cauchy = d1 * d1;
        } else {
            // Computing 2nd power
            final double d1 = gw + HALF * curv;
            cauchy = d1 * d1;
        }

        // If IFLAG is zero, then XALT is calculated as before after reversing
        // the sign of GLAG. Thus two XALT vectors become available. The one that
        // is chosen is the one that gives the larger value of CAUCHY.

        if (iflag == 0) {
            for (int i = 0; i < n; i++) {
                glag.setEntry(i, -glag.getEntry(i));
                work2.setEntry(i, alternativeNewPoint.getEntry(i));
            }
            csave = cauchy;
            iflag = 1;
        } else {
            break;
        }
    }
    if (csave > cauchy) {
        for (int i = 0; i < n; i++) {
            alternativeNewPoint.setEntry(i, work2.getEntry(i));
        }
        cauchy = csave;
    }

    return new double[] { alpha, cauchy };
}

From source file:com.itemanalysis.psychometrics.optimization.BOBYQAOptimizer.java

/**
 *     The arrays BMAT and ZMAT are updated, as required by the new position
 *     of the interpolation point that has the index KNEW. The vector VLAG has
 *     N+NPT components, set on entry to the first NPT and last N components
 *     of the product Hw in equation (4.11) of the Powell (2006) paper on
 *     NEWUOA. Further, BETA is set on entry to the value of the parameter
 *     with that name, and DENOM is set to the denominator of the updating
 *     formula. Elements of ZMAT may be treated as zero if their moduli are
 *     at most ZTEST. The first NDIM elements of W are used for working space.
 * @param beta//from  w  w w . j ava2  s. c  o  m
 * @param denom
 * @param knew
 */
private void update(double beta, double denom, int knew) {
    printMethod(); // XXX

    final int n = currentBest.getDimension();
    final int npt = numberOfInterpolationPoints;
    final int nptm = npt - n - 1;

    // XXX Should probably be split into two arrays.
    final ArrayRealVector work = new ArrayRealVector(npt + n);

    double ztest = ZERO;
    for (int k = 0; k < npt; k++) {
        for (int j = 0; j < nptm; j++) {
            // Computing MAX
            ztest = Math.max(ztest, Math.abs(zMatrix.getEntry(k, j)));
        }
    }
    ztest *= 1e-20;

    // Apply the rotations that put zeros in the KNEW-th row of ZMAT.

    for (int j = 1; j < nptm; j++) {
        final double d1 = zMatrix.getEntry(knew, j);
        if (Math.abs(d1) > ztest) {
            // Computing 2nd power
            final double d2 = zMatrix.getEntry(knew, 0);
            // Computing 2nd power
            final double d3 = zMatrix.getEntry(knew, j);
            final double d4 = Math.sqrt(d2 * d2 + d3 * d3);
            final double d5 = zMatrix.getEntry(knew, 0) / d4;
            final double d6 = zMatrix.getEntry(knew, j) / d4;
            for (int i = 0; i < npt; i++) {
                final double d7 = d5 * zMatrix.getEntry(i, 0) + d6 * zMatrix.getEntry(i, j);
                zMatrix.setEntry(i, j, d5 * zMatrix.getEntry(i, j) - d6 * zMatrix.getEntry(i, 0));
                zMatrix.setEntry(i, 0, d7);
            }
        }
        zMatrix.setEntry(knew, j, ZERO);
    }

    // Put the first NPT components of the KNEW-th column of HLAG into W,
    // and calculate the parameters of the updating formula.

    for (int i = 0; i < npt; i++) {
        work.setEntry(i, zMatrix.getEntry(knew, 0) * zMatrix.getEntry(i, 0));
    }
    final double alpha = work.getEntry(knew);
    final double tau = lagrangeValuesAtNewPoint.getEntry(knew);
    lagrangeValuesAtNewPoint.setEntry(knew, lagrangeValuesAtNewPoint.getEntry(knew) - ONE);

    // Complete the updating of ZMAT.

    final double sqrtDenom = Math.sqrt(denom);
    final double d1 = tau / sqrtDenom;
    final double d2 = zMatrix.getEntry(knew, 0) / sqrtDenom;
    for (int i = 0; i < npt; i++) {
        zMatrix.setEntry(i, 0, d1 * zMatrix.getEntry(i, 0) - d2 * lagrangeValuesAtNewPoint.getEntry(i));
    }

    // Finally, update the matrix BMAT.

    for (int j = 0; j < n; j++) {
        final int jp = npt + j;
        work.setEntry(jp, bMatrix.getEntry(knew, j));
        final double d3 = (alpha * lagrangeValuesAtNewPoint.getEntry(jp) - tau * work.getEntry(jp)) / denom;
        final double d4 = (-beta * work.getEntry(jp) - tau * lagrangeValuesAtNewPoint.getEntry(jp)) / denom;
        for (int i = 0; i <= jp; i++) {
            bMatrix.setEntry(i, j,
                    bMatrix.getEntry(i, j) + d3 * lagrangeValuesAtNewPoint.getEntry(i) + d4 * work.getEntry(i));
            if (i >= npt) {
                bMatrix.setEntry(jp, (i - npt), bMatrix.getEntry(i, j));
            }
        }
    }
}