本文整理汇总了Java中org.apache.commons.math3.linear.RealMatrix.transpose方法的典型用法代码示例。如果您正苦于以下问题:Java RealMatrix.transpose方法的具体用法?Java RealMatrix.transpose怎么用?Java RealMatrix.transpose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.math3.linear.RealMatrix
的用法示例。
在下文中一共展示了RealMatrix.transpose方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: updateReward
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
public void updateReward(User user, Article a, boolean clicked) {
String aId = a.getId();
// Collect Variables
RealMatrix xta = MatrixUtils.createColumnRealMatrix(a.getFeatures());
RealMatrix zta = makeZta(
MatrixUtils.createColumnRealMatrix(user.getFeatures()), xta);
RealMatrix Aa = AMap.get(aId);
RealMatrix ba = bMap.get(aId);
RealMatrix Ba = BMap.get(aId);
// Find common transpose/inverse to save computation
RealMatrix AaInverse = MatrixUtils.inverse(Aa);
RealMatrix BaTranspose = Ba.transpose();
RealMatrix xtaTranspose = xta.transpose();
RealMatrix ztaTranspose = zta.transpose();
// Update
A0 = A0.add(BaTranspose.multiply(AaInverse).multiply(Ba));
b0 = b0.add(BaTranspose.multiply(AaInverse).multiply(ba));
Aa = Aa.add(xta.multiply(xtaTranspose));
AMap.put(aId, Aa);
Ba = Ba.add(xta.multiply(ztaTranspose));
BMap.put(aId, Ba);
if (clicked) {
ba = ba.add(xta);
bMap.put(aId, ba);
}
// Update A0 and b0 with the new values
A0 = A0.add(zta.multiply(ztaTranspose)).subtract(
Ba.transpose().multiply(MatrixUtils.inverse(Aa).multiply(Ba)));
b0 = b0.subtract(Ba.transpose().multiply(MatrixUtils.inverse(Aa))
.multiply(ba));
if (clicked) {
b0 = b0.add(zta);
}
}
示例2: computeBeta
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
/**
*
* @param y the response vector
* @param x the design matrix
*/
private RealMatrix computeBeta(RealVector y, RealMatrix x) {
if (solver == Solver.QR) {
return computeBetaQR(y, x);
} else {
final int n = x.getRowDimension();
final int p = x.getColumnDimension();
final int offset = hasIntercept() ? 1 : 0;
final RealMatrix xT = x.transpose();
final RealMatrix xTxInv = new LUDecomposition(xT.multiply(x)).getSolver().getInverse();
final RealVector betaVector = xTxInv.multiply(xT).operate(y);
final RealVector residuals = y.subtract(x.operate(betaVector));
this.rss = residuals.dotProduct(residuals);
this.errorVariance = rss / (n - p);
this.stdError = Math.sqrt(errorVariance);
this.residuals = createResidualsFrame(residuals);
final RealMatrix covMatrix = xTxInv.scalarMultiply(errorVariance);
final RealMatrix result = new Array2DRowRealMatrix(p, 2);
if (hasIntercept()) {
result.setEntry(0, 0, betaVector.getEntry(0)); //Intercept coefficient
result.setEntry(0, 1, covMatrix.getEntry(0, 0)); //Intercept variance
}
for (int i = 0; i < getRegressors().size(); i++) {
final int index = i + offset;
final double variance = covMatrix.getEntry(index, index);
result.setEntry(index, 1, variance);
result.setEntry(index, 0, betaVector.getEntry(index));
}
return result;
}
}
示例3: weightedLinearCorr
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
/**
*
* @param y
* @param x
* @param sigmaRhoY
* @return
*/
public static WeightedLinearCorrResults weightedLinearCorr(double[] y, double[] x, double[][] sigmaRhoY) {
WeightedLinearCorrResults weightedLinearCorrResults = new WeightedLinearCorrResults();
RealMatrix omega = new BlockRealMatrix(convertCorrelationsToCovariances(sigmaRhoY));
RealMatrix invOmega = MatrixUtils.inverse(omega);
int n = y.length;
double mX = 0;
double pX = 0;
double pY = 0;
double pXY = 0;
double w = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
double invOm = invOmega.getEntry(i, j);
w += invOm;
pX += (invOm * (x[i] + x[j]));
pY += (invOm * (y[i] + y[j]));
pXY += (invOm * (((x[i] * y[j]) + (x[j] * y[i]))));
mX += (invOm * x[i] * x[j]);
}
}
double slope = ((2 * pXY * w) - (pX * pY)) / ((4 * mX * w) - (pX * pX));
double intercept = (pY - (slope * pX)) / (2 * w);
RealMatrix fischer = new BlockRealMatrix(new double[][]{{mX, pX / 2.0}, {pX / 2.0, w}});
RealMatrix fischerInv = MatrixUtils.inverse(fischer);
double slopeSig = Math.sqrt(fischerInv.getEntry(0, 0));
double interceptSig = Math.sqrt(fischerInv.getEntry(1, 1));
double slopeInterceptCov = fischerInv.getEntry(0, 1);
RealMatrix resid = new BlockRealMatrix(n, 1);
for (int i = 0; i < n; i++) {
resid.setEntry(i, 0, y[i] - (slope * x[i]) - intercept);
}
RealMatrix residT = resid.transpose();
RealMatrix mM = residT.multiply(invOmega).multiply(resid);
double sumSqWtdResids = mM.getEntry(0, 0);
double mswd = sumSqWtdResids / (n - 2);
// http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/distribution/FDistribution.html
FDistribution fdist = new org.apache.commons.math3.distribution.FDistribution((n - 2), 1E9);
double prob = 1.0 - fdist.cumulativeProbability(mswd);
weightedLinearCorrResults.setBad(false);
weightedLinearCorrResults.setSlope(slope);
weightedLinearCorrResults.setIntercept(intercept);
weightedLinearCorrResults.setSlopeSig(slopeSig);
weightedLinearCorrResults.setInterceptSig(interceptSig);
weightedLinearCorrResults.setSlopeInterceptCov(slopeInterceptCov);
weightedLinearCorrResults.setMswd(mswd);
weightedLinearCorrResults.setProb(prob);
return weightedLinearCorrResults;
}
示例4: wtdAvCorr
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
/**
*
* @param values
* @param varCov
* @return
*/
public static WtdAvCorrResults wtdAvCorr(double[] values, double[][] varCov) {
// assume varCov is variance-covariance matrix (i.e. SigRho = false)
WtdAvCorrResults results = new WtdAvCorrResults();
int n = varCov.length;
RealMatrix omegaInv = new BlockRealMatrix(varCov);
RealMatrix omega = MatrixUtils.inverse(omegaInv);
double numer = 0.0;
double denom = 0.0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
numer += (values[i] + values[j]) * omega.getEntry(i, j);
denom += omega.getEntry(i, j);
}
}
// test denom
if (denom > 0.0) {
double meanVal = numer / denom / 2.0;
double meanValSigma = Math.sqrt(1.0 / denom);
double[][] unwtdResidsArray = new double[n][1];
for (int i = 0; i < n; i++) {
unwtdResidsArray[i][0] = values[i] - meanVal;
}
RealMatrix unwtdResids = new BlockRealMatrix(unwtdResidsArray);
RealMatrix transUnwtdResids = unwtdResids.transpose();
RealMatrix product = transUnwtdResids.multiply(omega);
RealMatrix sumWtdResids = product.multiply(unwtdResids);
double mswd = 0.0;
double prob = 0.0;
if (n > 1) {
mswd = sumWtdResids.getEntry(0, 0) / (n - 1);
// http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/distribution/FDistribution.html
FDistribution fdist = new org.apache.commons.math3.distribution.FDistribution((n - 1), 1E9);
prob = 1.0 - fdist.cumulativeProbability(mswd);
}
results.setBad(false);
results.setMeanVal(meanVal);
results.setSigmaMeanVal(meanValSigma);
results.setMswd(mswd);
results.setProb(prob);
}
return results;
}
示例5: solve
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
private double[] solve(float tc, float m, float w, Range range) {
kernel.set_tcmw(tc, m, w);
kernel.execute(range);
final float[] result = kernel.getResult();
double[][] _A = new double[4][4];
double[] _b = new double[4];
_A[0][0] = N;
/*
A = [[ N, result[fi], result[gi], result[hi] ],
[ result[fi], result[fi2], result[figi], result[fihi] ],
[ result[gi], result[figi], result[gi2], result[gihi] ],
[ result[hi], result[fihi], result[gihi], result[hi2] ]]
b = [ sum_yi, sum_yi_fi, sum_yi_gi, sum_yi_hi ]
*/
for (int i = 0; i < N; i++) {
int offset = i * v;
_A[0][1] += result[offset + FI];
_A[0][2] += result[offset + GI];
_A[0][3] += result[offset + HI];
_A[1][0] += result[offset + FI];
_A[1][1] += result[offset + FI2];
_A[1][2] += result[offset + FIGI];
_A[1][3] += result[offset + FIHI];
_A[2][0] += result[offset + GI];
_A[2][1] += result[offset + FIGI];
_A[2][2] += result[offset + GI2];
_A[2][3] += result[offset + GIHI];
_A[3][0] += result[offset + HI];
_A[3][1] += result[offset + FIHI];
_A[3][2] += result[offset + GIHI];
_A[3][3] += result[offset + HI2];
_b[0] += result[offset + YI];
_b[1] += result[offset + YIFI];
_b[2] += result[offset + YIGI];
_b[3] += result[offset + YIHI];
}
RealMatrix A = new Array2DRowRealMatrix(_A, false);
RealMatrix b = new Array2DRowRealMatrix(new double[][]{_b}, false).transpose();
RealMatrix At = A.transpose();
RealMatrix x = MatrixUtils.inverse(At.multiply(A)).multiply(At).multiply(b);
return x.getColumn(0);
}
示例6: chooseArm
import org.apache.commons.math3.linear.RealMatrix; //导入方法依赖的package包/类
public Article chooseArm(User user, List<Article> articles) {
Article bestA = null;
double bestArmP = Double.MIN_VALUE;
RealMatrix Aa;
RealMatrix Ba;
RealMatrix ba;
for (Article a : articles) {
String aId = a.getId();
if (!AMap.containsKey(aId)) {
Aa = MatrixUtils.createRealIdentityMatrix(6);
AMap.put(aId, Aa); // set as identity for now and we will update
// in reward
double[] zeros = { 0, 0, 0, 0, 0, 0 };
ba = MatrixUtils.createColumnRealMatrix(zeros);
bMap.put(aId, ba);
double[][] BMapZeros = new double[6][36];
for (double[] row : BMapZeros) {
Arrays.fill(row, 0.0);
}
Ba = MatrixUtils.createRealMatrix(BMapZeros);
BMap.put(aId, Ba);
} else {
Aa = AMap.get(aId);
ba = bMap.get(aId);
Ba = BMap.get(aId);
}
// Make column vector out of features
RealMatrix xta = MatrixUtils
.createColumnRealMatrix(a.getFeatures());
RealMatrix zta = makeZta(
MatrixUtils.createColumnRealMatrix(user.getFeatures()), xta);
// Set up common variables
RealMatrix A0Inverse = MatrixUtils.inverse(A0);
RealMatrix AaInverse = MatrixUtils.inverse(Aa);
RealMatrix ztaTranspose = zta.transpose();
RealMatrix BaTranspose = Ba.transpose();
RealMatrix xtaTranspose = xta.transpose();
// Find theta
RealMatrix theta = AaInverse.multiply(ba.subtract(Ba
.multiply(BetaHat)));
// Find sta
RealMatrix staMatrix = ztaTranspose.multiply(A0Inverse).multiply(
zta);
staMatrix = staMatrix.subtract(ztaTranspose.multiply(A0Inverse)
.multiply(BaTranspose).multiply(AaInverse).multiply(xta)
.scalarMultiply(2));
staMatrix = staMatrix.add(xtaTranspose.multiply(AaInverse)
.multiply(xta));
staMatrix = staMatrix.add(xtaTranspose.multiply(AaInverse)
.multiply(Ba).multiply(A0Inverse).multiply(BaTranspose)
.multiply(AaInverse).multiply(xta));
// Find pta for arm
RealMatrix ptaMatrix = ztaTranspose.multiply(BetaHat);
ptaMatrix = ptaMatrix.add(xtaTranspose.multiply(theta));
double ptaVal = ptaMatrix.getData()[0][0];
double staVal = staMatrix.getData()[0][0];
ptaVal = ptaVal + alpha * Math.sqrt(staVal);
// Update argmax
if (ptaVal > bestArmP) {
bestArmP = ptaVal;
bestA = a;
}
}
return bestA;
}