I am working on a Java project, and I have to compute a multiple linear regression, but I want the gotten parameters to be non-negative. Is there an existing commercial-friendly-licensed library to do such a thing? I've been looking for Non-Negative Least Squares libs, without success.
4 Answers
Well, I could not find any pure java library so I built it myself from the article of [1], wich can be found in [2] and [3]. I give the algorithm:
P, R are the active and the passive sets. t() is transpose
The problem is to solve Ax = b under the condition x>0
P=null
R = {1,2,...,m}
x = 0
w = t(A)*(b-A*x)
while R<>null and max{wi|i in R}>0 do:
j = argmax{wi|i in R}
P = P U {j}
R = R\{j}
s[P] = invert[t(A[P])A[P]]t(A[P])b
while sp<=0 do:
a = -min{xi/(di-xi)|i in P and di<0}
x = x + a*s -x
update(P)
update(R)
sP = invert[t(A[P])A[P]]t(A[P])b
sR = 0
x = s
w = t(A)*(b-A*x)
return x
For the other definitions, I strongly advise to read the papers [2] and [3], which are online (see below for the links ;) )
[1] Lawson, C. L., & Hanson, R. J. (1974). Solving least squares problems (Vol. 161). Englewood Cliffs, NJ: Prentice-hall. [2] Rasmus Bro et Sijmen De Jong : A fast non-negativity-constrained least squares algorithm. Journal of chemometrics, 11(5) :393–401, 1997. http://www.researchgate.net/publication/230554373_A_fast_non-negativity-constrained_least_squares_algorithm/file/79e41501a40da0224e.pdf [3] Donghui Chen et Robert J Plemmons : Nonnegativity constraints in numerical analysis. In Symposium on the Birth of Numerical Analysis, pages 109–140, 2009. http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.157.9203&rep=rep1&type=pdf

- 95
- 1
- 10
You can use Apache Commons Math, making your constraints an additional cost to the objective function. See section 14.4 here: http://commons.apache.org/proper/commons-math/userguide/leastsquares.html

- 600
- 5
- 7
Have your tried Weka? It's Java and under GNU General Public License. It's mainly a GUI-Tool for experiments, but you can use it as a library too. It should have implementations of linear regressions.

- 30,811
- 12
- 60
- 80
-
Thank you for answer but Weka does not seem to have the features I need. I have not found non-negative linear regression, nor multiple linear regression. Maybe I've missed something? – Cedric Buron Nov 26 '13 at 11:32
-
not sure what "logistic" means, but what about http://weka.sourceforge.net/doc.packages/supervisedAttributeScaling/weka/classifiers/functions/NonNegativeLogisticRegression.html? – Thomas Uhrig Nov 26 '13 at 12:43
-
Thank you much. Logistic regression is different from linear regression, quite far actually. It is actually a binary predictor. – Cedric Buron Nov 26 '13 at 13:02
As George Foreman pointed out you can use apache commons math. In particular there is the object OLSMultipleLinearRegression which provides tha methods for performing multiple regression analysis.
Here is some code on how to do it.
OLSMultipleLinearRegression ols = new OLSMultipleLinearRegression();
double[] data = new double[75];
int numberOfObservations = 25;
int numberOfIndependentVariables = 3;
try {
ols.newSampleData(data, numberOfObservations, numberOfIndependentVariables);
} catch (IllegalArgumentException e) {
e.printStackTrace();
return;
}
And here is the whole github project from where you can download a working example on how to use multiple regression in Java : https://github.com/tekrar/MultRegressionInJava

- 117
- 1
- 11