本文整理汇总了Java中org.apache.commons.math3.linear.MatrixUtils.createColumnRealMatrix方法的典型用法代码示例。如果您正苦于以下问题:Java MatrixUtils.createColumnRealMatrix方法的具体用法?Java MatrixUtils.createColumnRealMatrix怎么用?Java MatrixUtils.createColumnRealMatrix使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.math3.linear.MatrixUtils
的用法示例。
在下文中一共展示了MatrixUtils.createColumnRealMatrix方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setValues
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
@Override
public void setValues(double[] y, double[] x) {
if (x.length != y.length) {
throw new IllegalArgumentException(String.format("The numbers of y and x values must be equal (%d != %d)", y.length, x.length));
}
double[][] xData = new double[x.length][];
for (int i = 0; i < x.length; i++) {
// the implementation determines how to produce a vector of predictors from a single x
xData[i] = xVector(x[i]);
}
if (logY()) { // in some models we are predicting ln y, so we replace each y with ln y
y = Arrays.copyOf(y, y.length); // user might not be finished with the array we were given
for (int i = 0; i < x.length; i++) {
y[i] = Math.log(y[i]);
}
}
final OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
ols.setNoIntercept(true); // let the implementation include a constant in xVector if desired
ols.newSampleData(y, xData); // provide the data to the model
coef = MatrixUtils.createColumnRealMatrix(ols.estimateRegressionParameters()); // get our coefs
last_error_rate = ols.estimateErrorVariance();
Log.d(TAG, getClass().getSimpleName() + " Forecast Error rate: errorvar:"
+ JoH.qs(last_error_rate, 4)
+ " regssionvar:" + JoH.qs(ols.estimateRegressandVariance(), 4)
+ " stderror:" + JoH.qs(ols.estimateRegressionStandardError(), 4));
}
示例2: calculateKernelVector
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
private RealMatrix calculateKernelVector(final double[][] training,
final double[] test, final KernelFunction kernelFunction) {
final double[] result = new double[training.length];
for (int r = 0; r < training.length; r++) {
result[r] = kernelFunction.calculate(training[r], test);
}
return MatrixUtils.createColumnRealMatrix(result);
}
示例3: HybridLinUCB
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
public HybridLinUCB(double alpha) {
this.alpha = alpha;
AMap = new HashMap<String, RealMatrix>();
bMap = new HashMap<String, RealMatrix>();
BMap = new HashMap<String, RealMatrix>();
// Need to double check that it is 6 long
double[] zeroArrayKLong = new double[36];
Double zero = new Double(0);
Arrays.fill(zeroArrayKLong, zero);
A0 = MatrixUtils.createRealIdentityMatrix(36);
b0 = MatrixUtils.createColumnRealMatrix(zeroArrayKLong);
BetaHat = MatrixUtils.inverse(A0).multiply(b0);
}
示例4: updateReward
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的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);
}
}
示例5: makeZta
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
public RealMatrix makeZta(RealMatrix userFeature, RealMatrix articleFeature) {
RealMatrix product = userFeature.multiply(articleFeature.transpose());
double[][] productData = product.getData();
double[] productVector = new double[36];
int count = 0;
for (int row = 0; row < 6; row++) {
for (int col = 0; col < 6; col++) {
productVector[count] = productData[row][col];
count++;
}
}
return MatrixUtils.createColumnRealMatrix(productVector);
}
示例6: updateReward
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
public void updateReward(User user, Article a, boolean clicked) {
String aId = a.getId();
RealMatrix xta = MatrixUtils
.createColumnRealMatrix(a.getFeatures());
AMap.put(aId, AMap.get(aId).add(xta.multiply(xta.transpose())));
if (clicked){
bMap.put(aId, bMap.get(aId).add(xta));
}
}
示例7: setValues
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
@Override
public void setValues(Array y, Array x) {
if (x.getSize() != y.getSize()) {
throw new IllegalArgumentException(String.format("The numbers of y and x values must be equal (%d != %d)",y.getSize(),x.getSize()));
}
double[][] xData = new double[(int)x.getSize()][];
for (int i = 0; i < x.getSize(); i++) {
// the implementation determines how to produce a vector of predictors from a single x
xData[i] = xVector(x.getDouble(i));
}
double[] yy = new double[(int)y.getSize()];
if(logY()) { // in some models we are predicting ln y, so we replace each y with ln y
for (int i = 0; i < yy.length; i++) {
if (i < x.getSize())
yy[i] = Math.log(y.getDouble(i));
else
yy[i] = y.getDouble(i);
}
} else {
for (int i = 0; i < yy.length; i++) {
yy[i] = y.getDouble(i);
}
}
// double[] yy = (double[])y.copyTo1DJavaArray();
// if(logY()) { // in some models we are predicting ln y, so we replace each y with ln y
// yy = Arrays.copyOf(yy, yy.length); // user might not be finished with the array we were given
// for (int i = 0; i < x.getSize(); i++) {
// yy[i] = Math.log(yy[i]);
// }
// }
OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
ols.setNoIntercept(true); // let the implementation include a constant in xVector if desired
ols.newSampleData(yy, xData); // provide the data to the model
coef = MatrixUtils.createColumnRealMatrix(ols.estimateRegressionParameters()); // get our coefs
rs = ols.calculateRSquared();
}
示例8: looPredLogLikelihood
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
@Override
public double looPredLogLikelihood(){
double[][] Y = getY();
double[][] dY = getdY();
double[][] mask = getMask();
int ns = Y.length;
int ny = Y[0].length;
RealMatrix Ky = MatrixUtils.createRealMatrix(this.Ky);
RealMatrix invK = new LUDecomposition(Ky).getSolver().getInverse();
RealMatrix dYmat = MatrixUtils.createRealMatrix(dY);
double[] LOOPredLL = new double[ny];
for(int j=0;j<ny; j++){
RealMatrix dy = MatrixUtils.createColumnRealMatrix(dYmat.getColumn(j));
RealMatrix invKdy = invK.multiply(dy);
double sum=0;
for(int i=0; i<ns; i++){
double mu_i = dYmat.getEntry(i, j) - invKdy.getEntry(i, 0)/invK.getEntry(i, i);
double sigma_i2 = 1/invK.getEntry(i, i);
double logLL = StatUtils.logProbaNormal(dYmat.getEntry(i, j), mu_i, Math.sqrt(sigma_i2));
sum += logLL * mask[i][j];
}
LOOPredLL[j] = sum;
}
double sumLOOPredLL=0;
for(int j=0;j<ny; j++){
sumLOOPredLL += LOOPredLL[j];
}
return sumLOOPredLL;
}
示例9: looPredLogLikelihood
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
public double looPredLogLikelihood(){
double[][] Y = getY();
double[][] dY = getdY();
int ns = X.length;
int ny = Y[0].length;
RealMatrix Ky = MatrixUtils.createRealMatrix(this.Ky);
RealMatrix invKy = new LUDecomposition(Ky).getSolver().getInverse();
RealMatrix dYmat = MatrixUtils.createRealMatrix(dY);
double[] LOOPredLL = new double[ny];
for(int j=0;j<ny; j++){
RealMatrix dy = MatrixUtils.createColumnRealMatrix(dYmat.getColumn(j));
RealMatrix invKdy = invKy.multiply(dy);
double sum=0;
for(int i=0; i<ns; i++){
double mu_i = dYmat.getEntry(i, j) - invKdy.getEntry(i, 0)/invKy.getEntry(i, i);
double sigma_i2 = 1/invKy.getEntry(i, i);
double logLL = StatUtils.logProbaNormal(dYmat.getEntry(i, j), mu_i, Math.sqrt(sigma_i2));
sum+=logLL;
}
LOOPredLL[j] = sum;
}
double sumLOOPredLL=0;
for(int j=0;j<ny; j++){
sumLOOPredLL += LOOPredLL[j];
}
return sumLOOPredLL;
}
示例10: chooseArm
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的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;
}
示例11: chooseArm
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
public Article chooseArm(User user, List<Article> articles) {
Article bestA = null;
double bestArmP = Double.MIN_VALUE;
RealMatrix Aa;
RealMatrix ba;
for (Article a : articles) {
String aId = a.getId();
double[] articleFeatureV = a.getFeatures();
// If not contained, then make new identity matrix and zero vector
if (!AMap.containsKey(aId)) {
Aa = MatrixUtils
.createRealIdentityMatrix(articleFeatureV.length);
double[] zeros = new double[articleFeatureV.length];
AMap.put(aId, Aa); //set as identity for now and we will update in reward
ba = MatrixUtils.createColumnRealMatrix(zeros);
bMap.put(aId, ba);
} else {
Aa = AMap.get(aId);
ba = bMap.get(aId);
}
// Make column vector out of features
RealMatrix xta = MatrixUtils
.createColumnRealMatrix(articleFeatureV);
RealMatrix theta = MatrixUtils.inverse(Aa).multiply(ba);
// Will have to index into matrix of one value after multiplication
double newP = theta.transpose().multiply(xta).getData()[0][0]
+ alpha
* Math.sqrt(xta.transpose()
.multiply(MatrixUtils.inverse(Aa)).multiply(xta)
.getData()[0][0]);
// Update argmax
if (newP > bestArmP) {
bestArmP = newP;
bestA = a;
}
}
return bestA;
}
示例12: setValues
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
@Override
public void setValues(double[] yIn, double[] xIn) {
if (xIn.length != yIn.length) {
throw new IllegalArgumentException(String.format("The numbers of y and x values must be equal (%d != %d)",yIn.length,xIn.length));
}
ArrayList<Double> xArr = new ArrayList<>();
ArrayList<Double> yArr = new ArrayList<>();
Predicate<Double> dom = getDomainPredicate();
Predicate<Double> ran = getRangePredicate();
for(int i = 0; i < xIn.length; i++) {
if(ran.apply(yIn[i]) && dom.apply(xIn[i])) {
yArr.add(yIn[i]);
xArr.add(xIn[i]);
}
}
//todo: super ugly conversion back to arr of primitive type
double[] x = new double[xArr.size()];
double[] y = new double[xArr.size()];
for(int i = 0; i< x.length; i++) {
x[i] = xArr.get(i);
y[i] = yArr.get(i);
}
double[][] xData = new double[x.length][];
for (int i = 0; i < x.length; i++) {
// the implementation determines how to produce a vector of predictors from a single x
xData[i] = xVector(x[i]);
}
if(logY()) { // in some models we are predicting ln y, so we replace each y with ln y
y = Arrays.copyOf(y, y.length); // user might not be finished with the array we were given
for (int i = 0; i < x.length; i++) {
y[i] = Math.log(y[i]);
}
}
OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
ols.setNoIntercept(true); // let the implementation include a constant in xVector if desired
ols.newSampleData(y, xData); // provide the data to the model
coef = MatrixUtils.createColumnRealMatrix(ols.estimateRegressionParameters()); // get our coefs
//set r^2
this.RSquared = ols.calculateRSquared();
this.adjustedRSquared = ols.calculateAdjustedRSquared();
}
示例13: initializeCMA
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
/**
* Initialization of the dynamic search parameters
*
* @param guess Initial guess for the arguments of the fitness function.
*/
private void initializeCMA(double[] guess) {
if (lambda <= 0) {
// XXX Line below to replace the current one in 4.0 (MATH-879).
// throw new NotStrictlyPositiveException(lambda);
lambda = 4 + (int) (3 * FastMath.log(dimension));
}
// initialize sigma
final double[][] sigmaArray = new double[guess.length][1];
for (int i = 0; i < guess.length; i++) {
// XXX Line below to replace the current one in 4.0 (MATH-868).
// sigmaArray[i][0] = inputSigma[i];
sigmaArray[i][0] = inputSigma == null ? 0.3 : inputSigma[i];
}
final RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
sigma = max(insigma); // overall standard deviation
// initialize termination criteria
stopTolUpX = 1e3 * max(insigma);
stopTolX = 1e-11 * max(insigma);
stopTolFun = 1e-12;
stopTolHistFun = 1e-13;
// initialize selection strategy parameters
mu = lambda / 2; // number of parents/points for recombination
logMu2 = FastMath.log(mu + 0.5);
weights = log(sequence(1, mu, 1)).scalarMultiply(-1).scalarAdd(logMu2);
double sumw = 0;
double sumwq = 0;
for (int i = 0; i < mu; i++) {
double w = weights.getEntry(i, 0);
sumw += w;
sumwq += w * w;
}
weights = weights.scalarMultiply(1 / sumw);
mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i
// initialize dynamic strategy parameters and constants
cc = (4 + mueff / dimension) /
(dimension + 4 + 2 * mueff / dimension);
cs = (mueff + 2) / (dimension + mueff + 3.);
damps = (1 + 2 * FastMath.max(0, FastMath.sqrt((mueff - 1) /
(dimension + 1)) - 1)) *
FastMath.max(0.3,
1 - dimension / (1e-6 + maxIterations)) + cs; // minor increment
ccov1 = 2 / ((dimension + 1.3) * (dimension + 1.3) + mueff);
ccovmu = FastMath.min(1 - ccov1, 2 * (mueff - 2 + 1 / mueff) /
((dimension + 2) * (dimension + 2) + mueff));
ccov1Sep = FastMath.min(1, ccov1 * (dimension + 1.5) / 3);
ccovmuSep = FastMath.min(1 - ccov1, ccovmu * (dimension + 1.5) / 3);
chiN = FastMath.sqrt(dimension) *
(1 - 1 / ((double) 4 * dimension) + 1 / ((double) 21 * dimension * dimension));
// intialize CMA internal values - updated each generation
xmean = MatrixUtils.createColumnRealMatrix(guess); // objective variables
diagD = insigma.scalarMultiply(1 / sigma);
diagC = square(diagD);
pc = zeros(dimension, 1); // evolution paths for C and sigma
ps = zeros(dimension, 1); // B defines the coordinate system
normps = ps.getFrobeniusNorm();
B = eye(dimension, dimension);
D = ones(dimension, 1); // diagonal D defines the scaling
BD = times(B, repmat(diagD.transpose(), dimension, 1));
C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
historySize = 10 + (int) (3 * 10 * dimension / (double) lambda);
fitnessHistory = new double[historySize]; // history of fitness values
for (int i = 0; i < historySize; i++) {
fitnessHistory[i] = Double.MAX_VALUE;
}
}
示例14: initializeCMA
import org.apache.commons.math3.linear.MatrixUtils; //导入方法依赖的package包/类
/**
* Initialization of the dynamic search parameters
*
* @param guess Initial guess for the arguments of the fitness function.
*/
private void initializeCMA(double[] guess) {
if (lambda <= 0) {
throw new NotStrictlyPositiveException(lambda);
}
// initialize sigma
final double[][] sigmaArray = new double[guess.length][1];
for (int i = 0; i < guess.length; i++) {
sigmaArray[i][0] = inputSigma[i];
}
final RealMatrix insigma = new Array2DRowRealMatrix(sigmaArray, false);
sigma = max(insigma); // overall standard deviation
// initialize termination criteria
stopTolUpX = 1e3 * max(insigma);
stopTolX = 1e-11 * max(insigma);
stopTolFun = 1e-12;
stopTolHistFun = 1e-13;
// initialize selection strategy parameters
mu = lambda / 2; // number of parents/points for recombination
logMu2 = FastMath.log(mu + 0.5);
weights = log(sequence(1, mu, 1)).scalarMultiply(-1).scalarAdd(logMu2);
double sumw = 0;
double sumwq = 0;
for (int i = 0; i < mu; i++) {
double w = weights.getEntry(i, 0);
sumw += w;
sumwq += w * w;
}
weights = weights.scalarMultiply(1 / sumw);
mueff = sumw * sumw / sumwq; // variance-effectiveness of sum w_i x_i
// initialize dynamic strategy parameters and constants
cc = (4 + mueff / dimension) /
(dimension + 4 + 2 * mueff / dimension);
cs = (mueff + 2) / (dimension + mueff + 3.);
damps = (1 + 2 * FastMath.max(0, FastMath.sqrt((mueff - 1) /
(dimension + 1)) - 1)) *
FastMath.max(0.3,
1 - dimension / (1e-6 + maxIterations)) + cs; // minor increment
ccov1 = 2 / ((dimension + 1.3) * (dimension + 1.3) + mueff);
ccovmu = FastMath.min(1 - ccov1, 2 * (mueff - 2 + 1 / mueff) /
((dimension + 2) * (dimension + 2) + mueff));
ccov1Sep = FastMath.min(1, ccov1 * (dimension + 1.5) / 3);
ccovmuSep = FastMath.min(1 - ccov1, ccovmu * (dimension + 1.5) / 3);
chiN = FastMath.sqrt(dimension) *
(1 - 1 / ((double) 4 * dimension) + 1 / ((double) 21 * dimension * dimension));
// intialize CMA internal values - updated each generation
xmean = MatrixUtils.createColumnRealMatrix(guess); // objective variables
diagD = insigma.scalarMultiply(1 / sigma);
diagC = square(diagD);
pc = zeros(dimension, 1); // evolution paths for C and sigma
ps = zeros(dimension, 1); // B defines the coordinate system
normps = ps.getFrobeniusNorm();
B = eye(dimension, dimension);
D = ones(dimension, 1); // diagonal D defines the scaling
BD = times(B, repmat(diagD.transpose(), dimension, 1));
C = B.multiply(diag(square(D)).multiply(B.transpose())); // covariance
historySize = 10 + (int) (3 * 10 * dimension / (double) lambda);
fitnessHistory = new double[historySize]; // history of fitness values
for (int i = 0; i < historySize; i++) {
fitnessHistory[i] = Double.MAX_VALUE;
}
}