List of usage examples for org.apache.commons.math3.linear ArrayRealVector getDimension
@Override public int getDimension()
From source file:gamlss.utilities.MatrixFunctions.java
/** * Sum all vector entries.//from w w w. j a v a 2s . c o m * @param v - vector * @return sum of vector entries */ public static double sumV(final ArrayRealVector v) { double tempD = 0; for (int j = 0; j < v.getDimension(); j++) { tempD = tempD + v.getEntry(j); } return tempD; }
From source file:gamlss.utilities.MatrixFunctions.java
/** * Add value to each entry of vector./*from ww w. j a v a2s. c om*/ * @param v - vector * @param value - value to add * @return new vector */ public static ArrayRealVector addValueToVector(final ArrayRealVector v, final double value) { double[] tempArr = new double[v.getDimension()]; for (int i = 0; i < tempArr.length; i++) { tempArr[i] = v.getEntry(i) + value; } return new ArrayRealVector(tempArr, false); }
From source file:gamlss.utilities.MatrixFunctions.java
/** * Create a matrix with length of y number of rows * and one column, all entries of the matrix are 1. * @param y - vector //from w w w . jav a 2 s . com * @return matrix */ public static BlockRealMatrix setInterceptMatrix(final ArrayRealVector y) { BlockRealMatrix designMatrix = new BlockRealMatrix(y.getDimension(), 1); for (int i = 0; i < y.getDimension(); i++) { designMatrix.setEntry(i, 0, 1.0); } return designMatrix; }
From source file:gamlss.utilities.MatrixFunctions.java
public static double dotProduct(ArrayRealVector v1, ArrayRealVector v2) { double out = 0; for (int i = 0; i < v1.getDimension(); i++) { out = out + v1.getEntry(i) * v2.getEntry(i); }//from w ww . j a v a2s . co m return out; }
From source file:gamlss.utilities.MatrixFunctions.java
/** * if the length of the return vector is zero, * then the function was not completed properly. * Check the 'diff' operator. /* w w w.j a v a2s.c o m*/ * @param data - matrix * @param diff - difference value * @return matrix */ public static ArrayRealVector diffV(final ArrayRealVector data, final int diff) { ArrayRealVector newdata = new ArrayRealVector(data.getDimension() - 1); for (int i = 0; i < diff; i++) { if (i == 0) { newdata = getDiff(data); } else { newdata = getDiff(newdata); } } return newdata; }
From source file:gamlss.utilities.MatrixFunctions.java
/** * Calculate this: (x - t) ^ p * (x > t). * @param v1 vector 1/* w ww . j a v a2 s. c om*/ * @param v2 vector 2 * @param p - power * @return matrix C with Cij = v1i * v2j. */ //tpower <- function(x, t, p) public static BlockRealMatrix outertpowerPB(final ArrayRealVector v1, final ArrayRealVector v2, final int p) { double[][] newdata = new double[v1.getDimension()][v2.getDimension()]; for (int j = 0; j < v1.getDimension(); j++) { for (int j2 = 0; j2 < v2.getDimension(); j2++) { if (v1.getEntry(j) > v2.getEntry(j2)) { newdata[j][j2] = FastMath.pow((v1.getEntry(j) - v2.getEntry(j2)), p); } else { newdata[j][j2] = 0d; } } } return new BlockRealMatrix(newdata); }
From source file:gamlss.smoothing.PB.java
/** * Constructs the base matrix./*from ww w .jav a 2 s . c om*/ * @param colValues - values of the certain column of * the smooth matrix which corresponds to the * currently fitting distribution parameter * @return - base matrix */ //bbase <- function(x, xl, xr, ndx, deg, quantiles=FALSE) private static BlockRealMatrix formX(final ArrayRealVector colValues) { //control$inter <- if (lx<99) 10 else control$inter # //this is to prevent singularities when length(x) is small if (colValues.getDimension() < 99) { Controls.INTER = 10; } //xl <- min(x) double xl = colValues.getMinValue(); //xr <- max(x) double xr = colValues.getMaxValue(); //xmax <- xr + 0.01 * (xr - xl) double xmax = xr + 0.01 * (xr - xl); //xmin <- xl - 0.01 * (xr - xl) double xmin = xl - 0.01 * (xr - xl); //dx <- (xr - xl) / ndx double dx = (xmax - xmin) / Controls.INTER; //if (quantiles) # if true use splineDesign if (Controls.QUANTILES) { //knots <- sort(c(seq(xl-deg*dx, xl, dx),quantile(x, //prob=seq(0, 1, length=ndx)), seq(xr, xr+deg*dx, dx))) ArrayRealVector kts = null; //B <- splineDesign(knots, x = x, outer.ok = TRUE, ord=deg+1) //return(B) return null; } else { //kts <- seq(xl - deg * dx, xr + deg * dx, by = dx) //ArrayRealVector kts = new ArrayRealVector( //ArithmeticSeries.getSeries(xl-deg*dx, xr+deg*dx, dx),false); rConnection.assingVar("min", new double[] { xmin - Controls.DEGREE * dx }); rConnection.assingVar("max", new double[] { xmax + Controls.DEGREE * dx }); rConnection.assingVar("step", new double[] { dx }); ArrayRealVector kts = new ArrayRealVector( rConnection.runEvalDoubles("knots <- seq(min, max, by = step)")); //P <- outer(x, kts, FUN = tpower, deg) BlockRealMatrix pM = MatrixFunctions.outertpowerPB(colValues, kts, Controls.DEGREE); //D <- diff(diag(dim(P)[2]), //diff = deg + 1) / (gamma(deg + 1) * dx ^ deg) BlockRealMatrix tempM = MatrixFunctions .diff(MatrixFunctions.buildIdentityMatrix(pM.getColumnDimension()), Controls.DEGREE + 1); double[][] tempArrArr = new double[tempM.getRowDimension()][tempM.getColumnDimension()]; for (int i = 0; i < tempArrArr.length; i++) { for (int j = 0; j < tempArrArr[i].length; j++) { tempArrArr[i][j] = tempM.getEntry(i, j) / ((FastMath.exp(Gamma.logGamma(Controls.DEGREE + 1))) * FastMath.pow(dx, Controls.DEGREE)); } } tempM = new BlockRealMatrix(tempArrArr); //B <- (-1) ^ (deg + 1) * P %*% t(D) return (BlockRealMatrix) pM.multiply(tempM.transpose()) .scalarMultiply(FastMath.pow(-1, (Controls.DEGREE + 1))); } }
From source file:edu.utexas.cs.tactex.subscriptionspredictors.LWRCustOldAppache.java
/** * Compute the n-fold Cross validation error with LWR and a given Tau * //from w w w . j a va2 s. c o m * @param tau * @param x * @param y * @return */ private Double CrossValidationError(Double tau, ArrayRealVector x, ArrayRealVector y) { int n = x.getDimension(); double totalError = 0.0; for (int i = 0; i < n; ++i) { // CV fold for i double x_i = x.getEntry(i); double y_i = y.getEntry(i); ArrayRealVector Xcv = new ArrayRealVector((ArrayRealVector) x.getSubVector(0, i), x.getSubVector(i + 1, n - (i + 1))); ArrayRealVector Ycv = new ArrayRealVector((ArrayRealVector) y.getSubVector(0, i), y.getSubVector(i + 1, n - (i + 1))); Double y_predicted = LWRPredict(Xcv, Ycv, x_i, tau); if (null == y_predicted) { log.error(" cp LWR cannot predict - returning NULL"); return null; } double predictionError = y_predicted - y_i; totalError += predictionError * predictionError; } return totalError; }
From source file:gamlss.algorithm.GlimFitCG.java
private ArrayRealVector setAdj(ArrayRealVector in1, ArrayRealVector in2, ArrayRealVector in3, ArrayRealVector in4, ArrayRealVector in5, ArrayRealVector in6, ArrayRealVector in7, ArrayRealVector in8, ArrayRealVector in9, ArrayRealVector in10) { double[] out = new double[in10.getDimension()]; for (int i = 0; i < out.length; i++) { out[i] = -(in1.getEntry(i) * (in2.getEntry(i) - in3.getEntry(i)) + in4.getEntry(i) * (in5.getEntry(i) - in6.getEntry(i)) + in7.getEntry(i) * (in8.getEntry(i) - in9.getEntry(i))) / in10.getEntry(i); }/*from w w w . j a v a2 s.c om*/ return new ArrayRealVector(out, false); }
From source file:hivemall.anomaly.SDAR2D.java
/** * @param x series of input in LIFO order * @param k AR window size/*w ww .j av 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; }