Java: Apache Regression gives me absolutly wrong regression parameters - java
I wanted to get regression parameters by using Apache's Commons.Math3 library and the OLSMultipleLinearRegression.
The regression should be polynomial with a power of 2.
It worked fine with test data but when I use this experimental data the methode gives me an absolutely wrong regression.
public static void poly (){
OLSMultipleLinearRegression quadRegression = new OLSMultipleLinearRegression();
double [] y = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,
26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,
51,52,53,54,55,56,57,58,59};
double [][] x = {{1.00,1.00},{1.00,1.00},{1.00,1.00},{1.00,1.00},{1.00,1.00},{1.00,1.00},{1.00,1.00},{1.00,1.00},{1.00,1.00},{0.95,0.90},{0.96,0.91},{0.96,0.92},{0.96,0.92},{0.96,0.92},{0.92,0.84},{0.92,0.85},
{0.92,0.86},{0.93,0.86},{0.93,0.87},{0.89,0.80},{0.90,0.81},{0.90,0.81},{0.90,0.82},{0.89,0.80},{0.90,0.81},{0.90,0.82},{0.91,0.82},{0.91,0.83},{0.90,0.80},{0.90,0.80},{0.90,0.81},{0.91,0.82},
{0.89,0.79},{0.89,0.80},{0.90,0.80},{0.90,0.81},{0.88,0.77},{0.88,0.77},{0.88,0.78},{0.88,0.78},{0.86,0.73},{0.86,0.74},{0.86,0.74},{0.86,0.74},{0.84,0.71},{0.85,0.72},{0.85,0.72},{0.85,0.73},
{0.84,0.71},{0.84,0.71},{0.84,0.71},{0.84,0.71},{0.83,0.69},{0.83,0.69},{0.83,0.69},{0.82,0.68},{0.82,0.68},{0.82,0.68},{0.82,0.68}};
quadRegression.newSampleData(y, x);
quadRegression.setNoIntercept(false);
double [] results = quadRegression.estimateRegressionParameters();}
For this input data I get the equation y=117.54x²-504.83x+389.088 which would result in a y-value of 379.760.85 for x=59 - way beyond my input value.
So I either handled the class absolutly wrong or I got stuck in a mathematical pitfall.
If someone please could explain me what I did wrong or misinterpreted - this problem drives me insane.
Related
Issue with using array in a class Java
I am trying to build a Java program based on this UML: UML of Polygon Class But I ran into a few hiccups along the way. This is my basic code: import java.util.Scanner; public class Polygon { private int[] side; private double perimeter; public double addSide(double length[]) { int i = 0; double perimeter = 0; while(length[i] > 0){ perimeter += (double)length[i]; i++; } return perimeter; } public int[] getSides() {return side;} public double getPerimeter() {return perimeter;} public static void main(String[] args) { Polygon polygon=new Polygon(); polygon.side = new int[99]; int i=0; do{ System.out.print("Side length(0 when done): "); Scanner in = new Scanner(System.in); polygon.side[i] = in.nextInt(); i++; }while(polygon.side[i]>0); //polygon.perimeter = addSide((double)polygon.side); System.out.println("Perimeter of " + i + "-sided polygon: " + polygon.getPerimeter()); } } There's a couple of issues. I got it to compile but when it accepts the first side[0], it immediately stops and gives me the perimeter. Exiting the loop eventhough the conditions haven't been met for it to so. So there's an issue with my while-loop. I want it to keep accepting values into the side[] array until a non-positive value is entered. Also the UML requires I use double parameter-type for the addSide method. I tried to cast it in the argument and tried a couple of other different things with no success. How would one transition an int-array into a double-array for the perimeter calucalation which has to be double as per the requirements. I wouldn't surprised if I made other issues since I'm new to Java so feel free to point them out to me or if you have a better way to go about this, I would love to learn your thinking. Any advice is appreciated!
There are a number of issues with your code. First, differences from the UML specification: You haven't used the given signature for addSide. The UML says that it takes a single double parameter, and returns nothing, i.e. void in Java. You are passing an array of double and returning a double. You are directly accessing sides in your main method. Java allows you to do this, because your main method is part of the Polygon class, but the UML shows that the field is private. What does direct manipulation of sides do to the validity of the value in perimeter? The UML shows the class having a field sides of type int. Your field sides is of type int[]. Similarly you haven't used the given signature for getSides, which should probably have been named getNumberOfSides. Your code has quite a few other issues, but I think you should fix the issues above first. A futher hint: The only things that the Polygon class can do is to tell you how many sides it has and what its total perimeter is. It does not care about the details of individual sides. (Off topic, it is strange to include main in the UML description of Polygon)
Trying to convert this formula into an arithmetic expression in Java
I'm trying to take user input in the form of myMonthlyPayment, myAnnualInterestRate, and myPrincipal in order to calculate the number of months needed to pay off debt by using The formula I've attached to this post. What I have in eclipse for the formula right now is: monthsNeeded = ((Math.log(myMonthlyPayment) - Math.log(myMonthlyPayment) - ((myAnnualInterestRate / 1200.0) * myPrincipal)) / ((Math.log(myAnnualInterestRate) / 1200.0) + 1.0)); I should be getting an output of 79 months with the inputs I'm using but instead I'm getting -62. I know the formula is correct, I'm almost positive I've made a mistake somewhere in the translation of it into Java. If someone could point it out that would be greatly appreciated!
So I've fixed it, with a sample input and output. I didn't put much effort into making this code beautiful but you can see that even separating it into 3 parts using method extraction (although I didn't know how to name them, lacking the domain knowledge) made the code easier to understand. public class Example { public static void main(String[] args) { double myMonthlyPayment = 2000; double myAnnualInterestRate = 5; double myPrincipal = 200000; System.out.println(a(myMonthlyPayment)); System.out.println(b(myPrincipal, myAnnualInterestRate, myMonthlyPayment)); System.out.println(c(myAnnualInterestRate)); double monthsNeeded = (a(myMonthlyPayment) - b(myPrincipal, myAnnualInterestRate, myMonthlyPayment)) / c(myAnnualInterestRate); System.out.println(monthsNeeded); } private static double c(double myAnnualInterestRate) { return Math.log((myAnnualInterestRate / 1200.0) + 1); } private static double b(double myPrinicipal, double myAnnualInterestRate, double myMonthlyPayment) { return Math.log(myMonthlyPayment - (myAnnualInterestRate / 1200.0) * myPrinicipal); } private static double a(double myMonthlyPayment) { return Math.log(myMonthlyPayment); } }
I think this is what you're looking for: monthsNeeded = (Math.log(myMonthlyPayment) - Math.log(myMonthlyPayment - myAnnualInterestRate / 1200d * myPrincipal)) / Math.log(myAnnualInterestRate / 1200d + 1); It seems that, in your solution, you weren't calculating your myAnnualInterestRate/1200*myPrincipal inside your second Math.log(...). You had also left some calculations outside of Math.log(...) in the bottom half of your equation. If you have an equation that does an operation inside a natural log, when you convert that equation to Java code, the operation needs to still be done, inside the natural log: ln(someNumber + 10) would be converted to: Math.log(someNumber + 10), NOT: Math.log(someNumber) + 10 Hope this helps and good luck. :)
Polynomial Regression with Apache Maths 3.6.1
Can someone let me know how I can do Polynomial Regression with Apache Maths 3.6.1 Below are the data points I used for my testing 60735214881.391304 1520254800000.000000 60697824142.469570 1520258400000.000000 60651182200.208694 1520262000000.000000 60684367132.939130 1520265600000.000000 60676588613.008700 1520269200000.000000 60641816564.869570 1520272800000.000000 60604714824.233510 1520276400000.000000 60580042814.330440 1520280000000.000000 60536134542.469570 1520283600000.000000 60566323732.034780 1520287200000.000000 60578775249.252174 1520290800000.000000 60547382844.104350 1520294400000.000000 60536776546.802160 1520298000000.000000 60474342718.330440 1520301600000.000000 60452725477.286960 1520305200000.000000 60486821569.669560 1520308800000.000000 60247997139.995674 1520312400000.000000 60248432181.426090 1520316000000.000000 60217476247.373920 1520319600000.000000 60170744493.634780 1520323200000.000000 My code looks like below private void polynomialFitter(List<List<Double>> pointlist) { final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2); final WeightedObservedPoints obs = new WeightedObservedPoints(); for (List<Double> point : pointlist) { obs.add(point.get(1), point.get(0)); } double[] fit = fitter.fit(obs.toList()); System.out.printf("\nCoefficient %f, %f, %f", fit[0], fit[1], fit[2]); } The coefficients are reported as Coefficient 12.910025, 0.000000, 0.000000 But these does not seem to be quite correct. If I use the same dataset in Online Polynimal Regression and in archanoid online regression - both reports same value as 654623237474.68250993904929103762, 28.75921919628759991574, -0.00000000023885199278 Can someone let me know what is going wrong? I have seen this question but that is not helping me.
This has been answered in apache-commons mailing list Polynomial regression is not the same as curve fitting. To do polynomial regression in Commons Math, use the OLSMultipleLinearRegression class, using, X, X^2 etc as the independent variables (as your second reference above shows). A sample code is like below private OLSMultipleLinearRegression getMultipleLinearRegression(List<List<Double>> pointlist) { OLSMultipleLinearRegression regression = new OLSMultipleLinearRegression(); double y[] = new double[pointlist.size()]; double x[][] = new double[pointlist.size()][2]; int c = 0; for (List<Double> point : pointlist) { y[c] = point.get(0); x[c][0] = point.get(1); x[c][1] = Math.pow(point.get(1), 2); regression.newSampleData(y, x); c++; } System.out.printf("\tR2 = %f", regression.calculateRSquared()); return regression; }
With Commons Math 3.6.1, I get almost identical parameters when fitting a cubic to data using PolynomialCurveFitter and tuk's OLSMultipleLinearRegression suggestion. The curve gives correct interpolations in the range of the data, but you've got to be careful about extrapolations. The PolynomialCurveFitter code was half the number of lines of the OLSMultipleLinearRegression code.
How to use SimplexSolver or SimplexOptimizer in java apache math?
I'm trying to use the apache commons math library version 3.5+ to solve an optimization problem. Basically, I'm trying to fit a (gamma) distribution to some data points. I can't seem to find any simple examples of how to use the new (version 3.5) optimization tools, such as SimplexSolver, SimplexOptimizer, or OptimizationData, to solve a trivial optimization problem. Similar questions have been asked here before, but all the answers seem to be for older version of apache math - in 3.5 things were restructured and none of the example code I could find works. Does anyone have a working example how to use the new optimizers or solvers? I'm most interested in SimplexOptimizer, but at this point anything would be useful.
Indeed, the optimizers may be hard to use: Lots of parameters, of which different combinations are required for the different types of optimizers, and they are all hidden in the generic OptimizationData array that they receive. Unless you start matching the code with the papers that they refer to, you can hardly get any results out of them whatsoever. I also wanted to use some of thes solvers/optimizers a try occasionally, the main source of reliable, working ""examples"" for me turned out to be the unit tests of these classes, which usually are quite elaborate and cover many cases. For example, regarding the SimplexOptimizer, you may want to have a look at the org/apache/commons/math4/optim/nonlinear/scalar/noderiv/ test cases, containing the test classes SimplexOptimizerMultiDirectionalTest.java and SimplexOptimizerNelderMeadTest.java. (Sorry, maybe this is not what you expected or hoped for, but ... I found these tests tremendously helpful when I tried to figure out which OptimizationData these optimizers actually need...) EDIT Just for reference, a complete example, extracted from one of the basic unit tests: import java.util.Arrays; import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.optim.InitialGuess; import org.apache.commons.math3.optim.MaxEval; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction; import org.apache.commons.math3.optim.nonlinear.scalar.noderiv.NelderMeadSimplex; import org.apache.commons.math3.optim.nonlinear.scalar.noderiv.SimplexOptimizer; import org.apache.commons.math3.util.FastMath; public class SimplexOptimizerExample { public static void main(String[] args) { SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30); final FourExtrema fourExtrema = new FourExtrema(); final PointValuePair optimum = optimizer.optimize( new MaxEval(100), new ObjectiveFunction(fourExtrema), GoalType.MINIMIZE, new InitialGuess(new double[]{ -3, 0 }), new NelderMeadSimplex(new double[]{ 0.2, 0.2 })); System.out.println(Arrays.toString(optimum.getPoint()) + " : " + optimum.getSecond()); } private static class FourExtrema implements MultivariateFunction { // The following function has 4 local extrema. final double xM = -3.841947088256863675365; final double yM = -1.391745200270734924416; final double xP = 0.2286682237349059125691; final double yP = -yM; final double valueXmYm = 0.2373295333134216789769; // Local maximum. final double valueXmYp = -valueXmYm; // Local minimum. final double valueXpYm = -0.7290400707055187115322; // Global minimum. final double valueXpYp = -valueXpYm; // Global maximum. public double value(double[] variables) { final double x = variables[0]; final double y = variables[1]; return (x == 0 || y == 0) ? 0 : FastMath.atan(x) * FastMath.atan(x + 2) * FastMath.atan(y) * FastMath.atan(y) / (x * y); } } }
Java Using Arguments in a Complex Formula
I am new to Java, and I am reading a book on it now. The book does not give me the answer. I am using the following code: package loanpayments; public class LoanPayments { public static void main(String[] args) { double years = Double.parseDouble(args[0]); double P = Double.parseDouble(args[1]); double r = Double.parseDouble(args[2]); double R = r / 100; double A = P*(Math.E*Math.exp(R*years)); System.out.println(A); } } I am testing the code with the following values: years = 3 P = 2340 r = 3.1 First I have to divide r by 100 to get a correct value (in this case it becomes 0.031). The new value of 0.031 becomes capitalized R. Then I use the formula to find A. I am getting an incorrect output of ~6980.712, when the output should instead be ~2568.060. I am thinking that I put in the formula wrong, it should be this: Pe^R(years) In this case e is Euler's number (~2.71828) If anyone could advise me on how to fix the formula, or some other mistake, I would much appreciate it, thanks.
Not needed to multiply with another e because Math.exp() is already the exponential function.