List of usage examples for org.apache.commons.math3.linear RealVector outerProduct
public RealMatrix outerProduct(RealVector v)
From source file:hivemall.utils.math.MatrixUtils.java
/** * QR decomposition for a tridiagonal matrix T. * * @see https://gist.github.com/lightcatcher/8118181 * @see http://www.ericmart.in/blog/optimizing_julia_tridiag_qr * @param T target tridiagonal matrix/*from w w w . j a v a 2 s . c o m*/ * @param R output matrix for R which is the same shape as T * @param Qt output matrix for Q.T which is the same shape an T */ public static void tridiagonalQR(@Nonnull final RealMatrix T, @Nonnull final RealMatrix R, @Nonnull final RealMatrix Qt) { int n = T.getRowDimension(); Preconditions.checkArgument(n == R.getRowDimension() && n == R.getColumnDimension(), "T and R must be the same shape"); Preconditions.checkArgument(n == Qt.getRowDimension() && n == Qt.getColumnDimension(), "T and Qt must be the same shape"); // initial R = T R.setSubMatrix(T.getData(), 0, 0); // initial Qt = identity Qt.setSubMatrix(eye(n), 0, 0); for (int i = 0; i < n - 1; i++) { // Householder projection for a vector x // https://en.wikipedia.org/wiki/Householder_transformation RealVector x = T.getSubMatrix(i, i + 1, i, i).getColumnVector(0); x = unitL2norm(x); RealMatrix subR = R.getSubMatrix(i, i + 1, 0, n - 1); R.setSubMatrix(subR.subtract(x.outerProduct(subR.preMultiply(x)).scalarMultiply(2)).getData(), i, 0); RealMatrix subQt = Qt.getSubMatrix(i, i + 1, 0, n - 1); Qt.setSubMatrix(subQt.subtract(x.outerProduct(subQt.preMultiply(x)).scalarMultiply(2)).getData(), i, 0); } }
From source file:com.opengamma.strata.math.impl.matrix.CommonsMatrixAlgebra.java
@Override public DoubleMatrix getOuterProduct(Matrix m1, Matrix m2) { ArgChecker.notNull(m1, "m1"); ArgChecker.notNull(m2, "m2"); if (m1 instanceof DoubleArray && m2 instanceof DoubleArray) { RealVector t1 = CommonsMathWrapper.wrap((DoubleArray) m1); RealVector t2 = CommonsMathWrapper.wrap((DoubleArray) m2); return CommonsMathWrapper.unwrap(t1.outerProduct(t2)); }/*from w w w .java 2 s .co m*/ throw new IllegalArgumentException( "Can only find outer product of DoubleArray; have " + m1.getClass() + " and " + m2.getClass()); }
From source file:edu.oregonstate.eecs.mcplan.ml.LinearDiscriminantAnalysis.java
/** * @param data The elements of 'data' will be modified. * @param label/*from w w w. j a va 2s. co m*/ * @param Nclasses * @param shrinkage_intensity */ public LinearDiscriminantAnalysis(final ArrayList<double[]> data, final int[] label, final int Nclasses, final double shrinkage) { assert (data.size() == label.length); final int Ndata = data.size(); final int Ndim = data.get(0).length; // Partition data by class final ArrayList<ArrayList<double[]>> classes = new ArrayList<ArrayList<double[]>>(Nclasses); for (int i = 0; i < Nclasses; ++i) { classes.add(new ArrayList<double[]>()); } for (int i = 0; i < data.size(); ++i) { classes.get(label[i]).add(data.get(i)); } // Mean center the data final VectorMeanVarianceAccumulator mv = new VectorMeanVarianceAccumulator(Ndim); for (int i = 0; i < Ndata; ++i) { mv.add(data.get(i)); } mean = mv.mean(); // Subtract global mean for (final double[] x : data) { Fn.vminus_inplace(x, mean); } // Calculate class means and covariances final double[][] class_mean = new double[Nclasses][Ndim]; final RealMatrix[] class_cov = new RealMatrix[Nclasses]; for (int i = 0; i < Nclasses; ++i) { final ArrayList<double[]> Xc = classes.get(i); final VectorMeanVarianceAccumulator mv_i = new VectorMeanVarianceAccumulator(Ndim); final StorelessCovariance cov = new StorelessCovariance(Ndim); for (int j = 0; j < Xc.size(); ++j) { final double[] x = Xc.get(j); mv_i.add(x); cov.increment(x); } class_mean[i] = mv_i.mean(); class_cov[i] = cov.getCovarianceMatrix(); } // Between-class scatter. // Note that 'data' is mean-centered, so the global mean is 0. RealMatrix Sb_builder = new Array2DRowRealMatrix(Ndim, Ndim); for (int i = 0; i < Nclasses; ++i) { final RealVector mu_i = new ArrayRealVector(class_mean[i]); final RealMatrix xxt = mu_i.outerProduct(mu_i); Sb_builder = Sb_builder.add(xxt.scalarMultiply(classes.get(i).size() / ((double) Ndata - 1))); } this.Sb = Sb_builder; Sb_builder = null; // Within-class scatter with shrinkage estimate: // Sw = (1.0 - shrinkage) * \sum Sigma_i + shrinkage * I RealMatrix Sw_builder = new Array2DRowRealMatrix(Ndim, Ndim); for (int i = 0; i < Nclasses; ++i) { final RealMatrix Sigma_i = class_cov[i]; final RealMatrix scaled = Sigma_i.scalarMultiply((1.0 - shrinkage) * (classes.get(i).size() - 1)); Sw_builder = Sw_builder.add(scaled); } for (int i = 0; i < Ndim; ++i) { Sw_builder.setEntry(i, i, Sw_builder.getEntry(i, i) + shrinkage); } this.Sw = Sw_builder; Sw_builder = null; // Invert Sw System.out.println("[LDA] Sw inverse"); final RealMatrix Sw_inv = new LUDecomposition(Sw).getSolver().getInverse(); final RealMatrix F = Sw_inv.multiply(Sb); System.out.println("[LDA] Eigendecomposition"); eigenvalues = new double[Nclasses - 1]; eigenvectors = new ArrayList<RealVector>(Nclasses - 1); final EigenDecomposition evd = new EigenDecomposition(F); for (int j = 0; j < Nclasses - 1; ++j) { final double eigenvalue = evd.getRealEigenvalue(j); eigenvalues[j] = eigenvalue; // final double scale = 1.0 / Math.sqrt( eigenvalue ); // eigenvectors.add( evd.getEigenvector( j ).mapMultiply( scale ) ); eigenvectors.add(evd.getEigenvector(j)); } }
From source file:hivemall.anomaly.SDAR2D.java
/** * @param x series of input in LIFO order * @param k AR window size// ww w.j a v a 2s.c om * @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:com.analog.lyric.dimple.solvers.sumproduct.customFactors.CustomComplexGaussianPolynomial.java
private Object[] calculateWeightedSums(Complex[] in) { double[] means = new double[] { 0, 0 }; for (int i = 0; i < in.length; i++) { means[0] += in[i].getReal();/*from w w w .j ava 2 s .c o m*/ means[1] += in[i].getImaginary(); } means[0] /= in.length; means[1] /= in.length; RealMatrix cm = createRealMatrix(2, 2); RealVector mm = wrapRealVector(means); for (int i = 0; i < in.length; i++) { RealVector m = createRealVector(new double[] { in[i].getReal(), in[i].getImaginary() }); RealVector m2 = m.subtract(mm); cm = cm.add(m2.outerProduct(m2)); } cm = cm.scalarMultiply(1.0 / in.length); return new Object[] { new Complex(means[0], means[1]), matrixGetDataRef(cm) }; }
From source file:edu.oregonstate.eecs.mcplan.ml.GaussianMixtureModel.java
@Override public void run() { init();/*www . ja v a2s . co m*/ System.out.println("Init"); for (int i = 0; i < mu().length; ++i) { System.out.println("Mu " + i + ": " + mu()[i]); System.out.println("Sigma " + i + ": " + Sigma()[i]); } int iterations = 0; while (!converged_ && iterations++ < max_iterations_) { // Expectation makeMixture(); for (int i = 0; i < n_; ++i) { for (int j = 0; j < k_; ++j) { c_[i][j] = posterior(data_[i], j); } Fn.normalize_inplace(c_[i]); } // Maximization for (int j = 0; j < k_; ++j) { double Z = 0.0; final RealVector mu_j = new ArrayRealVector(d_); RealMatrix Sigma_j = new Array2DRowRealMatrix(d_, d_); for (int i = 0; i < n_; ++i) { final double c_ij = c_[i][j]; Z += c_ij; final RealVector x_i = new ArrayRealVector(data_[i]); // mu_j += c_ij * x_i mu_j.combineToSelf(1.0, 1.0, x_i.mapMultiply(c_ij)); final RealVector v = x_i.subtract(mu_[j]); // Sigma_j += c_ij * |v><v| Sigma_j = Sigma_j.add(v.outerProduct(v).scalarMultiply(c_ij)); } final double Zinv = 1.0 / Z; final double pi_j = Z / n_; mu_j.mapMultiplyToSelf(Zinv); Sigma_j = Sigma_j.scalarMultiply(Zinv); // converged &= hasConverged( j, pi_j, mu_j, Sigma_j ); pi_[j] = pi_j; mu_[j] = mu_j; Sigma_[j] = Sigma_j; } // debug(); final double log_likelihood = logLikelihood(); if (Math.abs(log_likelihood - old_log_likelihood_) < epsilon_) { converged_ = true; } old_log_likelihood_ = log_likelihood; } }
From source file:edu.oregonstate.eecs.mcplan.ml.SequentialProjectionHashLearner.java
@Override public void run() { final RealMatrix cov_reg = MatrixUtils.createRealIdentityMatrix(X.getRowDimension()) .scalarMultiply(shrinkage);/* w ww . j a v a 2 s .c o m*/ for (int k = 0; k < K; ++k) { System.out.println("k = " + k); System.out.println("\tCovariance"); final RealMatrix cov = Xi_.multiply(Xi_.transpose()).add(cov_reg); // System.out.println( cov ); System.out.println("\tM"); final RealMatrix M = cov; // XL.multiply( Si_ ).multiply( XLt ).add( cov.scalarMultiply( eta ) ); // TODO: You only need the largest eigenvalue, so the full // decomposition is a ton of unnecessary work. System.out.println("\tM[" + M.getRowDimension() + "x" + M.getColumnDimension() + "]"); final EigenDecomposition evd = new EigenDecomposition(M); final RealVector w = evd.getEigenvector(0); w.mapMultiplyToSelf(1.0 / w.getNorm()); // if( Math.abs( w.getNorm() - 1.0 ) > 1e-2 ) { // System.out.println( "! Non-unit eigenvector: ||w|| = " + w.getNorm() ); // System.out.println( Math.abs( w.getNorm() - 1.0 ) ); // assert( false ); // } W.add(w); final RealMatrix w_wt = w.outerProduct(w); final RealMatrix S_tilde = XLt.multiply(w_wt).multiply(XL); T(S_tilde, Si_); Si_ = Si_.subtract(S_tilde.scalarMultiply(alpha)); Xi_ = Xi_.subtract(w_wt.multiply(Xi_)); } }
From source file:com.joptimizer.functions.SOCPLogarithmicBarrier.java
public double[][] hessian(double[] X) { RealVector x = new ArrayRealVector(X); RealMatrix ret = new Array2DRowRealMatrix(dim, dim); for (int i = 0; i < socpConstraintParametersList.size(); i++) { SOCPConstraintParameters param = socpConstraintParametersList.get(i); double t = this.buildT(param, x); RealVector u = this.buildU(param, x); double t2uu = t * t - u.dotProduct(u); RealVector t2u = u.mapMultiply(-2 * t); RealMatrix Jacob = this.buildJ(param, x); int k = u.getDimension(); RealMatrix H = new Array2DRowRealMatrix(k + 1, k + 1); RealMatrix ID = MatrixUtils.createRealIdentityMatrix(k); H.setSubMatrix(ID.scalarMultiply(t2uu).add(u.outerProduct(u).scalarMultiply(2)).getData(), 0, 0); H.setSubMatrix(new double[][] { t2u.toArray() }, k, 0); for (int j = 0; j < k; j++) { H.setEntry(j, k, t2u.getEntry(j)); }/*from w w w . j a v a 2s. c o m*/ H.setEntry(k, k, t * t + u.dotProduct(u)); RealMatrix ret_i = Jacob.multiply(H).multiply(Jacob.transpose()).scalarMultiply(2. / Math.pow(t2uu, 2)); ret = ret.add(ret_i); } return ret.getData(); }
From source file:edu.oregonstate.eecs.mcplan.ml.GaussianMixtureModel.java
private void init() { final int step = Math.max(1, n_ / k_); final double unif = 1.0 / k_; double acc = 0.0; final RandomPermutationIterator<double[]> r = new RandomPermutationIterator<double[]>(data_, rng_); final RandomPermutationIterator<double[]> rrepeat = new RandomPermutationIterator<double[]>(data_, r.permutation());//from w w w . j ava 2s.com for (int i = 0; i < k_; ++i) { final RealVector mu = new ArrayRealVector(d_); for (int j = 0; j < step; ++j) { final double[] x = r.next(); final RealVector v = new ArrayRealVector(x); mu.combineToSelf(1.0, 1.0, v); } final double Zinv = 1.0 / step; mu.mapMultiplyToSelf(Zinv); RealMatrix Sigma = new Array2DRowRealMatrix(d_, d_); for (int j = 0; j < step; ++j) { final double[] x = rrepeat.next(); final RealVector v = new ArrayRealVector(x); v.combineToSelf(1.0, -1.0, mu); Sigma = Sigma.add(v.outerProduct(v)); } Sigma = Sigma.scalarMultiply(Zinv); pi_[i] = unif; acc += unif; mu_[i] = mu; Sigma_[i] = Sigma; //MatrixUtils.createRealIdentityMatrix( d_ ); } pi_[k_ - 1] += (1.0 - acc); // Round-off error }
From source file:org.grouplens.samantha.modeler.reinforce.LinearUCB.java
public List<StochasticOracle> getStochasticOracle(List<LearningInstance> instances) { List<StochasticOracle> oracles = new ArrayList<>(instances.size()); for (LearningInstance ins : instances) { StochasticOracle orc = new StochasticOracle(); StandardLearningInstance instance = (StandardLearningInstance) ins; orc.setValues(-instance.getLabel(), instance.getLabel(), instance.getWeight()); int dim = features.size(); RealVector x = extractDenseVector(dim, ins); RealMatrix increA = x.outerProduct(x); RealVector increB = x.mapMultiplyToSelf(instance.getLabel()); for (int i = 0; i < dim; i++) { orc.addScalarOracle(LinearUCBKey.B.get(), i, -increB.getEntry(i)); orc.addVectorOracle(LinearUCBKey.A.get(), i, increA.getRowVector(i).mapMultiplyToSelf(-1.0)); }//from w w w. j a va2 s .c o m oracles.add(orc); } return oracles; }