Example usage for org.apache.commons.math3.linear BlockRealMatrix BlockRealMatrix

List of usage examples for org.apache.commons.math3.linear BlockRealMatrix BlockRealMatrix

Introduction

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

Prototype

public BlockRealMatrix(final int rows, final int columns) throws NotStrictlyPositiveException 

Source Link

Document

Create a new matrix with the supplied row and column dimensions.

Usage

From source file:gamlss.algorithm.Gamlss.java

/**
 * Main method.//from w ww. java2  s.co  m
 * @param args -  command-line arguments
 */
public static void main(final String[] args) {

    //String fileName = "Data/dataReduced.csv";
    String fileName = "Data/oil.csv";
    // String fileName = "Data/sp.csv";
    //String fileName = "Data/dataReduced.csv";
    CSVFileReader readData = new CSVFileReader(fileName);
    readData.readFile();
    ArrayList<String> data = readData.storeValues;

    ArrayRealVector y = new ArrayRealVector(data.size());
    BlockRealMatrix muX = new BlockRealMatrix(data.size(), 1);
    BlockRealMatrix sigmaX = new BlockRealMatrix(data.size(), 1);
    BlockRealMatrix nuX = new BlockRealMatrix(data.size(), 1);
    BlockRealMatrix tauX = new BlockRealMatrix(data.size(), 1);
    ArrayRealVector w = new ArrayRealVector(data.size());

    BlockRealMatrix muS = new BlockRealMatrix(data.size(), 1);
    BlockRealMatrix sigmaS = new BlockRealMatrix(data.size(), 1);
    BlockRealMatrix nuS = new BlockRealMatrix(data.size(), 1);
    BlockRealMatrix tauS = new BlockRealMatrix(data.size(), 1);

    for (int i = 0; i < data.size(); i++) {
        String[] line = data.get(i).split(",");
        y.setEntry(i, Double.parseDouble(line[0]));
        muX.setEntry(i, 0, Double.parseDouble(line[1]));
        muS.setEntry(i, 0, Double.parseDouble(line[1]));
        sigmaX.setEntry(i, 0, Double.parseDouble(line[1]));
        sigmaS.setEntry(i, 0, Double.parseDouble(line[1]));
        nuX.setEntry(i, 0, Double.parseDouble(line[1]));
        nuS.setEntry(i, 0, Double.parseDouble(line[1]));
        tauX.setEntry(i, 0, Double.parseDouble(line[1]));
        tauS.setEntry(i, 0, Double.parseDouble(line[1]));
    }

    Hashtable<Integer, BlockRealMatrix> designMatrices = new Hashtable<Integer, BlockRealMatrix>();
    designMatrices.put(DistributionSettings.MU, muX);
    designMatrices.put(DistributionSettings.SIGMA, sigmaX);
    designMatrices.put(DistributionSettings.NU, nuX);
    designMatrices.put(DistributionSettings.TAU, tauX);

    HashMap<Integer, BlockRealMatrix> smoothMatrices = new HashMap<Integer, BlockRealMatrix>();
    smoothMatrices.put(DistributionSettings.MU, muS);
    smoothMatrices.put(DistributionSettings.SIGMA, sigmaS);
    smoothMatrices.put(DistributionSettings.NU, nuS);
    smoothMatrices.put(DistributionSettings.TAU, tauS);

    //smoothMatrices.put(DistributionSettings.MU, null);
    //smoothMatrices.put(DistributionSettings.SIGMA, null);
    //smoothMatrices.put(DistributionSettings.NU, null);
    //smoothMatrices.put(DistributionSettings.TAU, null);

    DistributionSettings.DISTR = DistributionSettings.SST;
    Controls.GLOB_DEVIANCE_TOL = 5500;
    Controls.INTER = 50;//only for the PB smoother
    Controls.SMOOTHER = Controls.PB; //or PB
    Controls.IS_SVD = true;
    Controls.BIG_DATA = true;
    Controls.JAVA_OPTIMIZATION = false;
    Controls.GAMLSS_NUM_CYCLES = 50;
    //Gamlss gamlss = new Gamlss(y, designMatrices, null);
    Gamlss gamlss = new Gamlss(y, designMatrices, null);
    //Gamlss gamlss = new Gamlss(y, null, smoothMatrices);      
    gamlss.saveFittedDistributionParameters("Data/oilresults.csv");
}

From source file:edu.macalester.tagrelatedness.KendallsCorrelation.java

/**
 * This code is taken from a patch submitted for the Apache Commons 3.3
 * library by The Apache Software Foundation 
 * at: https://issues.apache.org/jira/browse/MATH-814
 * //from  w w  w.  jav  a 2  s .  com
 * 
 * Computes the Kendall's Tau rank correlation matrix for the columns of
 * the input matrix.
 *
 * @param matrix matrix with columns representing variables to correlate
 * @return correlation matrix
 * @author Matt Adereth
 */
static public RealMatrix computeCorrelationMatrix(final RealMatrix matrix) {
    int nVars = matrix.getColumnDimension();
    RealMatrix outMatrix = new BlockRealMatrix(nVars, nVars);
    for (int i = 0; i < nVars; i++) {
        for (int j = 0; j < i; j++) {
            double corr = correlation(matrix.getColumn(i), matrix.getColumn(j));
            outMatrix.setEntry(i, j, corr);
            outMatrix.setEntry(j, i, corr);
        }
        outMatrix.setEntry(i, i, 1d);
    }
    return outMatrix;
}

From source file:com.simiacryptus.mindseye.test.PCAUtil.java

/**
 * Forked from Apache Commons Math//w w  w  . ja va  2  s .c o m
 *
 * @param stream the stream
 * @return covariance covariance
 */
@Nonnull
public static RealMatrix getCovariance(@Nonnull final Supplier<Stream<double[]>> stream) {
    final int dimension = stream.get().findAny().get().length;
    final List<DoubleStatistics> statList = IntStream.range(0, dimension * dimension)
            .mapToObj(i -> new DoubleStatistics()).collect(Collectors.toList());
    stream.get().forEach(array -> {
        for (int i = 0; i < dimension; i++) {
            for (int j = 0; j <= i; j++) {
                statList.get(i * dimension + j).accept(array[i] * array[j]);
            }
        }
        RecycleBin.DOUBLES.recycle(array, array.length);
    });
    @Nonnull
    final RealMatrix covariance = new BlockRealMatrix(dimension, dimension);
    for (int i = 0; i < dimension; i++) {
        for (int j = 0; j <= i; j++) {
            final double v = statList.get(i + dimension * j).getAverage();
            covariance.setEntry(i, j, v);
            covariance.setEntry(j, i, v);
        }
    }
    return covariance;
}

From source file:com.datumbox.common.dataobjects.MatrixDataset.java

/**
 * Method used to generate a Dataset to a MatrixDataset and extracts its contents
 * to Matrixes. It populates the featureIdsReference map with the mappings
 * between the feature names and the column ids of the matrix. Typically used
 * to convert the training dataset./*from w w w  . j av  a2  s  .com*/
 * 
 * @param dataset
 * @param addConstantColumn
 * @param featureIdsReference
 * @return 
 */
public static MatrixDataset newInstance(Dataset dataset, boolean addConstantColumn,
        Map<Object, Integer> featureIdsReference) {
    if (!featureIdsReference.isEmpty()) {
        throw new RuntimeException("The featureIdsReference map should be empty.");
    }

    int n = dataset.size();
    int d = dataset.getColumnSize();

    if (addConstantColumn) {
        ++d;
    }

    MatrixDataset m = new MatrixDataset(new ArrayRealVector(n), new BlockRealMatrix(n, d), featureIdsReference);

    if (dataset.isEmpty()) {
        return m;
    }

    boolean extractY = (Dataset
            .value2ColumnType(dataset.iterator().next().getY()) == Dataset.ColumnType.NUMERICAL);

    int previousFeatureId = 0;
    if (addConstantColumn) {
        for (int row = 0; row < n; ++row) {
            m.X.setEntry(row, previousFeatureId, 1.0); //put the constant in evey row
        }
        m.feature2ColumnId.put(Dataset.constantColumnName, previousFeatureId);
        ++previousFeatureId;
    }

    for (Record r : dataset) {
        int row = r.getId();

        if (extractY) {
            m.Y.setEntry(row, Dataset.toDouble(r.getY()));
        }

        for (Map.Entry<Object, Object> entry : r.getX().entrySet()) {
            Object feature = entry.getKey();
            Integer featureId = m.feature2ColumnId.get(feature);
            if (featureId == null) {
                featureId = previousFeatureId;
                m.feature2ColumnId.put(feature, featureId);
                ++previousFeatureId;
            }

            Double value = Dataset.toDouble(entry.getValue());
            if (value != null) {
                m.X.setEntry(row, featureId, value);
            } else {
                //else the X matrix maintains the 0.0 default value
            }
        }
    }

    return m;
}

From source file:gamlss.algorithm.RSAlgorithm.java

/**
 * this is to emulate the RSAlgorithm iterative algorithm.
 * @param distr - object of the fitted distribution belonging
 *  to the gamlss family/*  www.  ja  v  a  2 s .  c o m*/
 * @param response  -  vector of response variable values
 * @param designMatrices - design matrices for 
 * each of the distribution parameters
 * @param smoothMatrices - smoother matrices for each 
 * of the distribution parameters
 * @param w - vector of the weight values
 *  the original data or not
 *  distribution parameters
 */
public RSAlgorithm(final GAMLSSFamilyDistribution distr, final ArrayRealVector response,
        final Hashtable<Integer, BlockRealMatrix> designMatrices,
        final HashMap<Integer, BlockRealMatrix> smoothMatrices, final ArrayRealVector w) {

    glimfit = new GlimFit(distr, response, designMatrices, smoothMatrices, w);

    if (Controls.SMOOTHING) {
        for (int i = 1; i < distr.getNumberOfDistribtionParameters() + 1; i++) {
            if (smoothMatrices.get(i) != null) {
                // Create smoother matrices of zeros
                glimfit.setMatrixS(i, new BlockRealMatrix(smoothMatrices.get(i).getRowDimension(),
                        smoothMatrices.get(i).getColumnDimension()));
            } else {
                glimfit.setMatrixS(i, null);
            }
        }
    } else {
        for (int i = 1; i < distr.getNumberOfDistribtionParameters() + 1; i++) {
            glimfit.setMatrixS(i, null);
        }
    }
}

From source file:edu.cmu.tetrad.util.ApacheTetradMatrix.java

private ApacheTetradMatrix(int m, int n) {
    if (m == 0) {
        this.apacheData = new Array2DRowRealMatrix();
    } else {//w w  w  .  ja  v a 2s.c o m
        this.apacheData = new BlockRealMatrix(m, n);
    }
}

From source file:hivemall.anomaly.SDAR2D.java

/**
 * @param x series of input in LIFO order
 * @param k AR window size// w ww  .jav  a  2s . c o  m
 * @return x_hat predicted x
 * @link https://en.wikipedia.org/wiki/Matrix_multiplication#Outer_product
 */
@Nonnull
public RealVector update(@Nonnull final ArrayRealVector[] x, final int k) {
    Preconditions.checkArgument(x.length >= 1, "x.length MUST be greater than 1: " + x.length);
    Preconditions.checkArgument(k >= 0, "k MUST be greater than or equals to 0: ", k);
    Preconditions.checkArgument(k < _C.length,
            "k MUST be less than |C| but " + "k=" + k + ", |C|=" + _C.length);

    final ArrayRealVector x_t = x[0];
    final int dims = x_t.getDimension();

    if (_initialized == false) {
        this._mu = x_t.copy();
        this._sigma = new BlockRealMatrix(dims, dims);
        assert (_sigma.isSquare());
        this._initialized = true;
        return new ArrayRealVector(dims);
    }
    Preconditions.checkArgument(k >= 1, "k MUST be greater than 0: ", k);

    // old parameters are accessible to compute the Hellinger distance
    this._muOld = _mu.copy();
    this._sigmaOld = _sigma.copy();

    // update mean vector
    // \hat{mu} := (1-r) \hat{} + r x_t
    this._mu = _mu.mapMultiply(1.d - _r).add(x_t.mapMultiply(_r));

    // compute residuals (x - \hat{})
    final RealVector[] xResidual = new RealVector[k + 1];
    for (int j = 0; j <= k; j++) {
        xResidual[j] = x[j].subtract(_mu);
    }

    // update covariance matrices
    // C_j := (1-r) C_j + r (x_t - \hat{}) (x_{t-j} - \hat{})'
    final RealMatrix[] C = this._C;
    final RealVector rxResidual0 = xResidual[0].mapMultiply(_r); // r (x_t - \hat{})
    for (int j = 0; j <= k; j++) {
        RealMatrix Cj = C[j];
        if (Cj == null) {
            C[j] = rxResidual0.outerProduct(x[j].subtract(_mu));
        } else {
            C[j] = Cj.scalarMultiply(1.d - _r).add(rxResidual0.outerProduct(x[j].subtract(_mu)));
        }
    }

    // solve A in the following Yule-Walker equation
    // C_j = _{i=1}^{k} A_i C_{j-i} where j = 1..k, C_{-i} = C_i'
    /*
     * /C_1\     /A_1\  /C_0     |C_1'    |C_2'    | .  .  .   |C_{k-1}' \
     * |---|     |---|  |--------+--------+--------+           +---------|
     * |C_2|     |A_2|  |C_1     |C_0     |C_1'    |               .     |
     * |---|     |---|  |--------+--------+--------+               .     |
     * |C_3|  =  |A_3|  |C_2     |C_1     |C_0     |               .     |
     * | . |     | . |  |--------+--------+--------+                     |
     * | . |     | . |  |   .                            .               |
     * | . |     | . |  |   .                            .               |
     * |---|     |---|  |--------+                              +--------|
     * \C_k/     \A_k/  \C_{k-1} | .  .  .                      |C_0     /
     */
    RealMatrix[][] rhs = MatrixUtils.toeplitz(C, k);
    RealMatrix[] lhs = Arrays.copyOfRange(C, 1, k + 1);
    RealMatrix R = MatrixUtils.combinedMatrices(rhs, dims);
    RealMatrix L = MatrixUtils.combinedMatrices(lhs);
    RealMatrix A = MatrixUtils.solve(L, R, false);

    // estimate x
    // \hat{x} = \hat{} + _{i=1}^k A_i (x_{t-i} - \hat{})
    RealVector x_hat = _mu.copy();
    for (int i = 0; i < k; i++) {
        int offset = i * dims;
        RealMatrix Ai = A.getSubMatrix(offset, offset + dims - 1, 0, dims - 1);
        x_hat = x_hat.add(Ai.operate(xResidual[i + 1]));
    }

    // update model covariance
    //  := (1-r)  + r (x - \hat{x}) (x - \hat{x})'
    RealVector xEstimateResidual = x_t.subtract(x_hat);
    this._sigma = _sigma.scalarMultiply(1.d - _r)
            .add(xEstimateResidual.mapMultiply(_r).outerProduct(xEstimateResidual));

    return x_hat;
}

From source file:gamlss.utilities.MatrixFunctions.java

/**
* if the length of the return matrix is zero, 
* then the function was not completed properly. 
* Check the 'diff' operator. // ww  w  .  j  a  v a2s.  com
* @param data - matrix
* @param diff - difference value
* @return matrix
*/
public static BlockRealMatrix diff(final BlockRealMatrix data, final int diff) {
    BlockRealMatrix newdata = new BlockRealMatrix(data.getRowDimension() - 1, data.getColumnDimension());
    for (int i = 0; i < diff; i++) {
        if (i == 0) {
            newdata = getDiff(data);
        } else {
            newdata = getDiff(newdata);
        }
    }
    return newdata;
}

From source file:edu.cmu.tetrad.util.TetradMatrix.java

public TetradMatrix(int m, int n) {
    if (m == 0 || n == 0) {
        this.apacheData = new Array2DRowRealMatrix();
    } else {//from   w w  w .  j a v a2 s  .  c  o m
        //            this.apacheData = new OpenMapRealMatrix(m, n);
        this.apacheData = new BlockRealMatrix(m, n);
    }

    this.m = m;
    this.n = n;
}

From source file:mase.spec.SilhouetteDistanceCalculator.java

private RealMatrix computeDistanceMatrix(List<BehaviourResult> brs) {
    RealMatrix mat = new BlockRealMatrix(brs.size(), brs.size());
    for (int i = 0; i < brs.size(); i++) {
        for (int j = i + 1; j < brs.size(); j++) {
            double d = brs.get(i).distanceTo(brs.get(j));
            mat.setEntry(i, j, d);//from  ww  w  .ja v a2 s. c  o m
            mat.setEntry(j, i, d);
        }
    }
    return mat;
}