本文整理汇总了Java中org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression类的典型用法代码示例。如果您正苦于以下问题:Java OLSMultipleLinearRegression类的具体用法?Java OLSMultipleLinearRegression怎么用?Java OLSMultipleLinearRegression使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
OLSMultipleLinearRegression类属于org.apache.commons.math3.stat.regression包,在下文中一共展示了OLSMultipleLinearRegression类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: mutipleLineRegress_OLS
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; //导入依赖的package包/类
/**
* Implements ordinary least squares (OLS) to estimate the parameters of a
* multiple linear regression model.
* @param y Y sample data - one dimension array
* @param x X sample data - two dimension array
* @return Estimated regression parameters and residuals
*/
public static Array[] mutipleLineRegress_OLS(Array y, Array x) {
OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
double[] yy = (double[])ArrayUtil.copyToNDJavaArray(y);
double[][] xx = (double[][])ArrayUtil.copyToNDJavaArray(x);
regression.newSampleData(yy, xx);
double[] para = regression.estimateRegressionParameters();
double[] residuals = regression.estimateResiduals();
int k = para.length;
int n = residuals.length;
Array aPara = Array.factory(DataType.DOUBLE, new int[]{k});
Array aResiduals = Array.factory(DataType.DOUBLE, new int[]{n});
for (int i = 0; i < k; i++){
aPara.setDouble(i, para[i]);
}
for (int i = 0; i < k; i++){
aResiduals.setDouble(i, residuals[i]);
}
return new Array[]{aPara, aResiduals};
}
示例2: testMLR
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; //导入依赖的package包/类
@Test(dataProvider = "thresholds")
public void testMLR(double threshold) {
final Range<String> rowKeys = Range.of(0, 5000).map(i -> "R" + i);
final DataFrame<String,String> frame = DataFrame.of(rowKeys, String.class, columns -> {
columns.add("Y", Double.class).applyDoubles(v -> v.rowOrdinal() + 1d + (Math.random() * 2));
columns.add("X1", Double.class).applyDoubles(v -> v.rowOrdinal() + 1d + (Math.random() * 3));
columns.add("X2", Double.class).applyDoubles(v -> v.rowOrdinal() + 1d + (Math.random() * 4));
columns.add("X3", Double.class).applyDoubles(v -> v.rowOrdinal() + 1d + (Math.random() * 5));
columns.add("X4", Double.class).applyDoubles(v -> v.rowOrdinal() + 1d + (Math.random() * 6));
});
final OLSMultipleLinearRegression model = new OLSMultipleLinearRegression(threshold);
final double[] dependent = frame.col("Y").toDoubleStream().toArray();
final double[][] independent = new double[frame.rows().count()][3];
frame.rows().forEach(row -> {
independent[row.ordinal()][0] = row.getDouble("X1");
independent[row.ordinal()][1] = row.getDouble("X2");
independent[row.ordinal()][2] = row.getDouble("X3");
});
model.newSampleData(dependent, independent);
frame.regress().ols("Y", Arrays.asList("X1", "X2", "X3"), true, mlr -> {
System.out.println(mlr);
assertResultsMatch(mlr, model);
return Optional.empty();
});
}
示例3: setValues
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; //导入依赖的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));
}
示例4: calculateRegressionParamsByHandForWeaponsAndMinions
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; //导入依赖的package包/类
double [] calculateRegressionParamsByHandForWeaponsAndMinions() {
OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
double [] manaCost = {4,6,0,2,1,5}; // mana cost is y intercept
double [] [] x = new double[6][];
//atk,charge,durability,health
double [] korkonElite = new double []{4,1,0,3};
x[0] = korkonElite;
double [] recklessRocketeer = new double []{5,1,0,2};
x[1] = recklessRocketeer;
double [] wisp = new double []{1,0,0,1};
x[2] = wisp;
double [] riverCrocolisk = new double []{2,0,0,3};
x[3] = riverCrocolisk;
double [] lightsJustice = new double []{1,1,4,0};
x[4] = lightsJustice;
double [] arcaniteReaper = new double []{5,1,2,0};
x[5] = arcaniteReaper;
ols.newSampleData(manaCost, x);
return ols.estimateRegressionParameters();
}
示例5: calculateRegressionParamsByHandForStealthAndPoison
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; //导入依赖的package包/类
double [] calculateRegressionParamsByHandForStealthAndPoison() {
OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
double [] manaCost = {2,5,3,2,3}; // mana cost is y intercept
double [] [] x = new double[5][];
//atk, health, poison, stealth
double [] gilblinStalker = new double []{2,3,0,1};
x[0] = gilblinStalker;
double [] stranglethornTiger = new double []{5,5,0,1};
x[1] = stranglethornTiger;
double [] junglePanther = new double []{4,2,0,1};
x[2] = junglePanther;
double [] patientAssassin = new double []{1,1,1,1};
x[3] = patientAssassin;
double [] emperorCobra = new double []{2,3,1,0};
x[4] = emperorCobra;
ols.newSampleData(manaCost, x);
return ols.estimateRegressionParameters();
}
示例6: helpMeUnderstandEstimateRegressionParamters
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; //导入依赖的package包/类
@Test
public void helpMeUnderstandEstimateRegressionParamters(){
OLSMultipleLinearRegression regression2 = new OLSMultipleLinearRegression();
double[] y = {
4,
8,
13,
18
};
double[][] x2 =
{
{ 1, 1, 1 },
{ 1, 2, 4 },
{ 1, 3, 9 },
{ 1, 4, 16 },
};
regression2.newSampleData(y, x2);
regression2.setNoIntercept(true);
regression2.newSampleData(y, x2);
double[] beta = regression2.estimateRegressionParameters();
for (double d : beta) {
System.out.println("D: " + d);
}
}
示例7: updateLinearParametersGivenRegression
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; //导入依赖的package包/类
@Override
protected void updateLinearParametersGivenRegression(
int i, OLSMultipleLinearRegression regression, double[][] x) {
//compute variance as well
double[] residuals = regression.estimateResiduals();
//square them then log them
for(int j=0; j<residuals.length; j++)
residuals[j] = Math.log(residuals[j] * residuals[j]);
//regress
OLSMultipleLinearRegression variance = new OLSMultipleLinearRegression();
variance.setNoIntercept(true);
variance.newSampleData(residuals,x);
temporaryVarianceCoefficients[i] = variance.estimateRegressionParameters();
super.updateLinearParametersGivenRegression(i, regression, x);
}
示例8: train
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; //导入依赖的package包/类
@Override
public double[][] train(double[][] ARx,
double[][] MAx, double[] y) {
regression = new OLSMultipleLinearRegression();
regression.setNoIntercept(true);
final DataTuple data = super.transfer(ARx, MAx, y);
regression.newSampleData(data.y, data.x);
intercept = regression.isNoIntercept()? 0 : regression.estimateRegressionParameters()[0];
double[] result = null;
if(regression.isNoIntercept()){
result = regression.estimateRegressionParameters();
} else {
double[] para = regression.estimateRegressionParameters();
double[] array = new double[para.length-1];
System.arraycopy(para, 1, array, 0, para.length);
result = array;
}
return transfer(result);
}
示例9: updateModelCoefficient
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; //导入依赖的package包/类
/**
* Trigger the calculation of the model parameters.
* @return true if the parameters are generated, otherwise false;
*/
public synchronized boolean updateModelCoefficient() {
if (validBuckets().size() < MIN_CPU_UTIL_OBSERVATION_BUCKETS) {
return false;
}
try {
OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
regression.setNoIntercept(true);
boolean ignoreLeaderBytesOut = !isLeaderBytesInAndOutRatioDiverseEnough();
regression.newSampleData(aggregateSampleCpuUtilData(),
aggregateSampleBytesRateData(ignoreLeaderBytesOut));
double[] parameters = regression.estimateRegressionParameters();
int leaderBytesInIndex = 0;
int leaderBytesOutIndex = 1;
int followerBytesInIndex = ignoreLeaderBytesOut ? 1 : 2;
_coefficients.put(ModelCoefficient.LEADER_BYTES_IN, parameters[leaderBytesInIndex]);
if (!ignoreLeaderBytesOut) {
_coefficients.put(ModelCoefficient.LEADER_BYTES_OUT, parameters[leaderBytesOutIndex]);
}
_coefficients.put(ModelCoefficient.FOLLOWER_BYTES_IN, parameters[followerBytesInIndex]);
LOG.info("Coefficient generated: leader_bytes_in: {}, leader_bytes_out: {}, follower_bytes_in: {}",
_coefficients.get(ModelCoefficient.LEADER_BYTES_IN),
_coefficients.get(ModelCoefficient.LEADER_BYTES_OUT),
_coefficients.get(ModelCoefficient.FOLLOWER_BYTES_IN));
return true;
} catch (Exception e) {
LOG.warn("received exception {}", e);
}
return false;
}
示例10: calculateOlsRegression
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; //导入依赖的package包/类
public void calculateOlsRegression(double[][] x, double[] y){
OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
regression.newSampleData(y, x);
double[] beta = regression.estimateRegressionParameters();
double[] residuals = regression.estimateResiduals();
double[][] parametersVariance = regression.estimateRegressionParametersVariance();
double regressandVariance = regression.estimateRegressandVariance();
double rSquared = regression.calculateRSquared();
double sigma = regression.estimateRegressionStandardError();
}
示例11: assertResultsMatch
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; //导入依赖的package包/类
/**
* Checks that the Morpheus OLS model yields the same results as Apache Math
* @param actual the Morpheus results
* @param expected the Apache results
*/
private <R,C> void assertResultsMatch(DataFrameLeastSquares<R,C> actual, OLSMultipleLinearRegression expected) {
Assert.assertEquals(actual.getResidualSumOfSquares(), expected.calculateResidualSumOfSquares(), 0.0000001, "Residual sum of squares matches");
Assert.assertEquals(actual.getTotalSumOfSquares(), expected.calculateTotalSumOfSquares(), actual.getTotalSumOfSquares() * 0.000001, "Total sum of squares matches");
Assert.assertEquals(actual.getRSquared(), expected.calculateRSquared(), 0.0000001, "R^2 values match");
Assert.assertEquals(actual.getStdError(), expected.estimateRegressionStandardError(), 0.0000001, "Std error matches");
final DataFrame<C,Field> params1 = actual.getBetas();
final double[] params2 = expected.estimateRegressionParameters();
Assert.assertEquals(params1.rows().count(), params2.length-1, "Same number of parameters");
for (int i=0; i<params1.rows().count(); ++i) {
final double actualParam = params1.data().getDouble(i, Field.PARAMETER);
final double expectedParam = params2[i+1];
Assert.assertEquals(actualParam, expectedParam, 0.000000001, "Parameters match at index " + i);
}
final double intercept = expected.estimateRegressionParameters()[0];
final double interceptStdError = expected.estimateRegressionParametersStandardErrors()[0];
Assert.assertEquals(actual.getInterceptValue(Field.PARAMETER), intercept, 0.0000001, "The intercepts match");
Assert.assertEquals(actual.getInterceptValue(Field.STD_ERROR), interceptStdError, 0.000000001, "The intercept std errors match");
final DataFrame<R,String> residuals1 = actual.getResiduals();
final double[] residuals2 = expected.estimateResiduals();
Assert.assertEquals(residuals1.rows().count(), residuals2.length, "Same number of residuals");
for (int i=0; i<residuals1.rows().count(); ++i) {
Assert.assertEquals(residuals1.data().getDouble(i, 0), residuals2[i], 0.00000001, "Residuals match at index " + i);
}
final DataFrame<C,Field> stdErrs1 = actual.getBetas().cols().select(c -> c.key() == Field.STD_ERROR);
final double[] stdErrs2 = expected.estimateRegressionParametersStandardErrors();
Assert.assertEquals(stdErrs1.rows().count(), stdErrs2.length-1, "Same number of parameter standard errors");
for (int i=0; i<stdErrs1.cols().count(); ++i) {
Assert.assertEquals(stdErrs1.data().getDouble(0, i), stdErrs2[i+1], 0.00000001, "Standard errors match at index " + i);
}
}
示例12: train
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; //导入依赖的package包/类
@Override
public IModel<Sample, Double> train(final double[][] features, final double[] expected,
final String[] featureNames) {
final OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
regression.newSampleData(expected, features);
return new LinearRegressionModel(regression.estimateRegressionParameters(), featureNames);
}
示例13: setValues
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; //导入依赖的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();
}
示例14: calculateRegressionParameters
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; //导入依赖的package包/类
double[] calculateRegressionParameters(List<Card> cards) {
double[] manaCost = cards.stream().mapToDouble(Card :: getCostAsDouble).toArray();
double[][] inputParams = new double[cards.size()][];
//cards.stream().map(card -> card.getRawAttributeValues().values().stream())
for (int i = 0; i < cards.size(); i++) {
inputParams [i] = cards.get(i).getRawAttributeValues().values().stream().mapToDouble(Integer::doubleValue).toArray();
}
OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
ols.newSampleData(manaCost, inputParams);
return ols.estimateRegressionParameters();
}
示例15: calculateRegressionParamsByHandForChargeAndDivineShield
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; //导入依赖的package包/类
double [] calculateRegressionParamsByHandForChargeAndDivineShield() {
OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
double [] manaCost = {4,6,6,3,1,0,4,2}; // mana cost is y intercept
double [] [] x = new double[8][];
//atk,charge,divine,health
double [] korkonElite = new double []{4,1,0,3};
x[0] = korkonElite;
double [] recklessRocketeer = new double []{5,1,0,2};
x[1] = recklessRocketeer;
double [] argentCommander = new double []{4,1,1,2};
x[2] = argentCommander;
double [] scarletCrusader = new double []{3,0,1,1};
x[3] = scarletCrusader;
double [] argentSquire = new double []{1,0,1,1};
x[4] = argentSquire;
double [] wisp = new double []{1,0,0,1};
x[5] = wisp;
double [] silvermoonGuardian = new double []{3,0,1,3};
x[6] = silvermoonGuardian;
double [] riverCrocolisk = new double []{2,0,0,3};
x[7] = riverCrocolisk;
ols.newSampleData(manaCost, x);
return ols.estimateRegressionParameters();
}