本文整理汇总了Java中org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression.setNoIntercept方法的典型用法代码示例。如果您正苦于以下问题:Java OLSMultipleLinearRegression.setNoIntercept方法的具体用法?Java OLSMultipleLinearRegression.setNoIntercept怎么用?Java OLSMultipleLinearRegression.setNoIntercept使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression
的用法示例。
在下文中一共展示了OLSMultipleLinearRegression.setNoIntercept方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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));
}
示例2: 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);
}
}
示例3: 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);
}
示例4: 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);
}
示例5: 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;
}
示例6: 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();
}
示例7: modelState
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; //导入方法依赖的package包/类
public synchronized LinearRegressionModelState modelState() {
Map<Integer, Double> detailCompleteness = new HashMap<>();
for (Map.Entry<Integer, AtomicInteger> entry : INDICES.entrySet()) {
detailCompleteness.put(entry.getKey(),
Math.min((double) entry.getValue().get() / NUM_OBSERVATIONS_PER_UTIL_BUCKET, 1.0));
}
Map<Integer, Integer> usedLeaderToFollowerRatio = new HashMap<>();
Map<Integer, Integer> usedLeaderBytesInToBytesOutRatio = new HashMap<>();
Map<ModelCoefficient, Double> coefficientFromAvailableData = new HashMap<>(_coefficients);
OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
regression.setNoIntercept(true);
boolean ignoreLeaderBytesOutRate = !isLeaderBytesInAndOutRatioDiverseEnough();
double[][] sampleBytesRateData = aggregateSampleBytesRateData(ignoreLeaderBytesOutRate);
int leaderBytesInIndex = 0;
int leaderBytesOutIndex = 1;
int followerBytesInIndex = ignoreLeaderBytesOutRate ? 1 : 2;
for (int i = 0; i < sampleBytesRateData.length; i++) {
int leaderToFollowerRatio = sampleBytesRateData[i][followerBytesInIndex] == 0.0 ? 10000000 :
(int) ((sampleBytesRateData[i][leaderBytesInIndex] / sampleBytesRateData[i][followerBytesInIndex]) * 10);
int count = usedLeaderToFollowerRatio.getOrDefault(leaderToFollowerRatio, 0);
usedLeaderToFollowerRatio.put(leaderToFollowerRatio, count + 1);
if (!ignoreLeaderBytesOutRate) {
int leaderBytesInToBytesOutRatio = sampleBytesRateData[i][leaderBytesOutIndex] == 0.0 ? 10000000 :
(int) ((sampleBytesRateData[i][leaderBytesInIndex] / sampleBytesRateData[i][leaderBytesOutIndex]) * 10);
count = usedLeaderBytesInToBytesOutRatio.getOrDefault(leaderBytesInToBytesOutRatio, 0);
usedLeaderBytesInToBytesOutRatio.put(leaderBytesInToBytesOutRatio, count + 1);
}
}
regression.newSampleData(aggregateSampleCpuUtilData(), sampleBytesRateData);
double[] parameters = regression.estimateRegressionParameters();
coefficientFromAvailableData.put(ModelCoefficient.LEADER_BYTES_IN, parameters[leaderBytesInIndex]);
if (ignoreLeaderBytesOutRate) {
coefficientFromAvailableData.put(ModelCoefficient.FOLLOWER_BYTES_IN, parameters[followerBytesInIndex]);
} else {
coefficientFromAvailableData.put(ModelCoefficient.LEADER_BYTES_OUT, parameters[leaderBytesOutIndex]);
coefficientFromAvailableData.put(ModelCoefficient.FOLLOWER_BYTES_IN, parameters[followerBytesInIndex]);
}
return new LinearRegressionModelState(detailCompleteness, coefficientFromAvailableData,
OBSERVED_LEADER_TO_FOLLOWER_BYTES_RATIO,
OBSERVED_LEADER_BYTES_IN_TO_BYTES_OUT_RATIO,
usedLeaderToFollowerRatio, usedLeaderBytesInToBytesOutRatio,
CPU_UTIL_ESTIMATION_ERROR_STATS);
}
示例8: setValues
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; //导入方法依赖的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();
}
示例9: computePriorProbability
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; //导入方法依赖的package包/类
@Override
public PriorProbability computePriorProbability() {
OLSMultipleLinearRegression multipleLinearRegression = new OLSMultipleLinearRegression();
multipleLinearRegression.setNoIntercept(true); // no intercept -- constant value
List<String> groupNames = new ArrayList<>(table.getGroupsNames());
int groupCount = groupNames.size();
List<String> allMaskValues = new ArrayList<>(table.getTable().keySet());
int maskCount = allMaskValues.size();
double[] observedFrequencies = new double[maskCount];
double[][] libraryFrequencies = new double[maskCount][groupCount];
for (int i = 0; i < allMaskValues.size(); i++) {
BigDecimal maskFrequency = maskToFrequency.get(allMaskValues.get(i));
if (maskFrequency == null) maskFrequency = BigDecimal.ZERO;
observedFrequencies[i] = maskFrequency.doubleValue();
}
observedFrequencies = normalize(observedFrequencies);
for (int i = 0; i < allMaskValues.size(); i++) {
ClassificationRow row = table.getTable().get(allMaskValues.get(i));
// remember, NO row.normalize();
for (int j = 0; j < groupNames.size(); j++) {
BigDecimal maskGroupProbability = row.getValues().get(groupNames.get(j));
if (maskGroupProbability == null) {
// impossible mask
maskGroupProbability = BigDecimal.ZERO;
}
libraryFrequencies[i][j] = maskGroupProbability.doubleValue();
}
}
for (int groupIndex = 0; groupIndex < groupCount; groupIndex++) {
double sum = 0d;
for (int i = 0; i < maskCount; i++) {
sum += libraryFrequencies[i][groupIndex];
}
for (int i = 0; i < maskCount; i++) {
libraryFrequencies[i][groupIndex] /= sum;
}
}
multipleLinearRegression.newSampleData(observedFrequencies, libraryFrequencies);
double[] parameters = multipleLinearRegression.estimateRegressionParameters();
parameters = normalize(parameters);
for (double parameter : parameters) {
System.out.println(parameter);
}
PriorProbability priorProbability = new PriorProbability();
for (int i = 0; i < groupCount; i++) {
priorProbability.put(groupNames.get(i), BigDecimal.valueOf(parameters[i]));
}
return priorProbability;
}
示例10: computeADFStatistics
import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression; //导入方法依赖的package包/类
private void computeADFStatistics() {
final double[] y = diff(ts);
RealMatrix designMatrix = null;
final int k = lag + 1;
final int n = ts.length - 1;
final RealMatrix z = MatrixUtils.createRealMatrix(laggedMatrix(y, k)); //has rows length(ts) - 1 - k + 1
final RealVector zcol1 = z.getColumnVector(0); //has length length(ts) - 1 - k + 1
final double[] xt1 = subsetArray(ts, k - 1, n - 1); //ts[k:(length(ts) - 1)], has length length(ts) - 1 - k + 1
final double[] trend = sequence(k, n); //trend k:n, has length length(ts) - 1 - k + 1
if (k > 1) {
final RealMatrix yt1 = z.getSubMatrix(0, ts.length - 1 - k, 1, k - 1); //same as z but skips first column
//build design matrix as cbind(xt1, 1, trend, yt1)
designMatrix = MatrixUtils.createRealMatrix(ts.length - 1 - k + 1, 3 + k - 1);
designMatrix.setColumn(0, xt1);
designMatrix.setColumn(1, ones(ts.length - 1 - k + 1));
designMatrix.setColumn(2, trend);
designMatrix.setSubMatrix(yt1.getData(), 0, 3);
} else {
//build design matrix as cbind(xt1, 1, tt)
designMatrix = MatrixUtils.createRealMatrix(ts.length - 1 - k + 1, 3);
designMatrix.setColumn(0, xt1);
designMatrix.setColumn(1, ones(ts.length - 1 - k + 1));
designMatrix.setColumn(2, trend);
}
final OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression();
regression.setNoIntercept(true);
try {
regression.newSampleData(zcol1.toArray(), designMatrix.getData());
} catch (final MathIllegalArgumentException e) {
//not enough data
return;
}
final double[] beta = regression.estimateRegressionParameters();
final double[] sd = regression.estimateRegressionParametersStandardErrors();
// final RidgeRegression regression = new RidgeRegression(designMatrix.getData(), zcol1.toArray());
// regression.updateCoefficients(L2PENALTY);
// final double[] beta = regression.getCoefficients();
// final double[] sd = regression.getStandarderrors();
final double result = beta[0] / sd[0];
if (Double.isInfinite(result)) {
this.testStatistic = null;
} else {
this.testStatistic = result;
}
}