Java Code Examples for org.apache.commons.math3.linear.RealMatrix#getColumnVector()

The following examples show how to use org.apache.commons.math3.linear.RealMatrix#getColumnVector() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XDataFrameLeastSquares.java    From morpheus-core with Apache License 2.0 6 votes vote down vote up
/**
 * Runs the regression model for the given dependent and independent variables
 * The Y and X variables must be transformed, if necessary, to meet Gauss Markov assumptions
 * @param y     the dependent variable, which may be a transformed version of the raw data
 * @param x     the independent variable(s), which may be a transformed version of the raw data
 */
protected void compute(RealVector y, RealMatrix x) {
    final int n = frame.rows().count();
    final int p = regressors.size() + (hasIntercept() ? 1 : 0);
    final int dfModel = regressors.size();
    final RealMatrix betaMatrix = computeBeta(y, x);
    final RealVector betaCoefficients = betaMatrix.getColumnVector(0);
    final RealVector betaVariance = betaMatrix.getColumnVector(1);
    this.tss = computeTSS(y);
    this.ess = tss - rss;
    this.fValue = (ess / dfModel) / (rss / (n - p));
    this.fValueProbability = 1d - new FDistribution(dfModel, n-p).cumulativeProbability(fValue);
    this.rSquared = 1d - (rss / tss);
    this.rSquaredAdj = 1d - (rss * (n - (hasIntercept() ? 1 : 0))) / (tss * (n - p));
    this.computeParameterStdErrors(betaVariance);
    this.computeParameterSignificance(betaCoefficients);
}
 
Example 2
Source File: TOVEC.java    From warp10-platform with Apache License 2.0 5 votes vote down vote up
@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
  
  Object o = stack.pop();
  
  if (o instanceof RealMatrix) {
    RealMatrix matrix = (RealMatrix) o;
    
    if (1 != matrix.getColumnDimension()) {
      throw new WarpScriptException(getName() + " expects a matrix with a single column on top of the stack.");
    }
    
    RealVector vector = matrix.getColumnVector(0);
    stack.push(vector);
    return stack;
  }
  
  if (!(o instanceof List)) {
    throw new WarpScriptException(getName() + " expects a list onto the stack.");
  }
  
  double[] data = new double[((List) o).size()];
  
  for (int i = 0; i < data.length; i++) {
    Object oo = ((List) o).get(i);
    if (!(oo instanceof Number)) {
      throw new WarpScriptException(getName() + " expects a list of numbers onto the stack.");
    }
    data[i] = ((Number) oo).doubleValue();
  }
  
  stack.push(MatrixUtils.createRealVector(data));
  
  return stack;
}
 
Example 3
Source File: UserProfileKDEModeler.java    From Eagle with Apache License 2.0 5 votes vote down vote up
private void computeStats(RealMatrix m){
    if(m.getColumnDimension() !=  this.cmdTypes.length){
        LOG.error("Please fix the commands list in config file");
    }

    statistics = new UserCommandStatistics[m.getColumnDimension()];

    for(int i=0; i<m.getColumnDimension(); i++){
        UserCommandStatistics stats = new UserCommandStatistics();
        stats.setCommandName(this.cmdTypes[i]);
        RealVector colData = m.getColumnVector(i);
        StandardDeviation deviation = new StandardDeviation();
        double stddev = deviation.evaluate(colData.toArray());

        if(LOG.isDebugEnabled()) LOG.debug("Stddev is NAN ? " + (Double.isNaN(stddev) ? "yes" : "no"));
        if(stddev <= lowVarianceVal)
            stats.setLowVariant(true);
        else
            stats.setLowVariant(false);

        stats.setStddev(stddev);
        Mean mean = new Mean();
        double mu = mean.evaluate(colData.toArray());
        if(LOG.isDebugEnabled()) LOG.debug("mu is NAN ? " + (Double.isNaN(mu)? "yes":"no"));

        stats.setMean(mu);
        statistics[i]=stats;
    }
}
 
Example 4
Source File: AlgebraUtils.java    From macrobase with Apache License 2.0 5 votes vote down vote up
public static RealVector flattenMatrixByColumns(RealMatrix matrix) {
    int m = matrix.getColumnDimension(); // returns the width of the matrix, i.e. number of columns.
    int n = matrix.getRowDimension(); // returns the height of the matrix, i.e. number of rows.
    RealVector vector = matrix.getColumnVector(0);
    for (int i = 1; i < m; i++) {
        vector = vector.append(matrix.getColumnVector(i));
    }
    return vector;
}
 
Example 5
Source File: GMM.java    From pyramid with Apache License 2.0 5 votes vote down vote up
public GMM(int dimension, int numComponents, RealMatrix data) {
        this.numComponents = numComponents;
        this.mixtureCoefficients = new double[numComponents];
        Arrays.fill(this.mixtureCoefficients,1.0/numComponents);
        this.gaussianDistributions = new GaussianDistribution[numComponents];
        double[] mins = new double[data.getColumnDimension()];
        double[] maxs = new double[data.getColumnDimension()];
        double[] vars = new double[data.getColumnDimension()];
        for (int j=0;j<data.getColumnDimension();j++){
            RealVector column = data.getColumnVector(j);
            mins[j] = column.getMinValue();
            maxs[j] = column.getMaxValue();
            DescriptiveStatistics stats = new DescriptiveStatistics(column.toArray());
            vars[j] = stats.getVariance();
        }
//        System.out.println("mins = "+Arrays.toString(mins));
//        System.out.println("maxs = "+Arrays.toString(maxs));
        for (int k=0;k<numComponents;k++){
            int randomMeanInstance = Sampling.intUniform(0,data.getRowDimension()-1);

            RealVector mean = data.getRowVector(randomMeanInstance).copy();
//            RealVector mean = new ArrayRealVector(dimension);
//            for (int d=0;d<dimension;d++){
//                mean.setEntry(d, Sampling.doubleUniform(mins[d],maxs[d]));
//            }

            RealMatrix cov = new Array2DRowRealMatrix(dimension,dimension);
            for (int d=0;d<dimension;d++){
                cov.setEntry(d,d,vars[d]+0.00001);
            }
            gaussianDistributions[k] = new GaussianDistribution(mean, cov);
        }
    }
 
Example 6
Source File: AugmentedDickeyFuller.java    From Surus with Apache License 2.0 4 votes vote down vote up
private void computeADFStatistics() {
	double[] y = diff(ts);
	RealMatrix designMatrix = null;
	int k = lag+1;
	int n = ts.length - 1;
	
	RealMatrix z = MatrixUtils.createRealMatrix(laggedMatrix(y, k)); //has rows length(ts) - 1 - k + 1
	RealVector zcol1 = z.getColumnVector(0); //has length length(ts) - 1 - k + 1
	double[] xt1 = subsetArray(ts, k-1, n-1);  //ts[k:(length(ts) - 1)], has length length(ts) - 1 - k + 1
	double[] trend = sequence(k,n); //trend k:n, has length length(ts) - 1 - k + 1
	if (k > 1) {
		RealMatrix yt1 = z.getSubMatrix(0, ts.length - 1 - k, 1, k-1); //same as z but skips first column
		//build design matrix as cbind(xt1, 1, trend, yt1)
		designMatrix = MatrixUtils.createRealMatrix(ts.length - 1 - k + 1, 3 + k - 1);
		designMatrix.setColumn(0, xt1);
		designMatrix.setColumn(1, ones(ts.length - 1 - k + 1));
		designMatrix.setColumn(2, trend);
		designMatrix.setSubMatrix(yt1.getData(), 0, 3);
		
	} else {
		//build design matrix as cbind(xt1, 1, tt)
		designMatrix = MatrixUtils.createRealMatrix(ts.length - 1 - k + 1, 3);
		designMatrix.setColumn(0, xt1);
		designMatrix.setColumn(1, ones(ts.length - 1 - k + 1));
		designMatrix.setColumn(2, trend);
	}
	/*OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
	regression.setNoIntercept(true);
	regression.newSampleData(zcol1.toArray(), designMatrix.getData());
	double[] beta = regression.estimateRegressionParameters();
	double[] sd = regression.estimateRegressionParametersStandardErrors();
	*/
	RidgeRegression regression = new RidgeRegression(designMatrix.getData(), zcol1.toArray());
	regression.updateCoefficients(.0001);
	double[] beta = regression.getCoefficients();
	double[] sd = regression.getStandarderrors();
	
	double t = beta[0] / sd[0];
	if (t <= PVALUE_THRESHOLD) {
		this.needsDiff = true;
	} else {
		this.needsDiff = false;
	}	
}