//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testEvaluate()
        public virtual void testEvaluate()
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double x = RANDOM.nextDouble();
            double x = RANDOM.NextDouble();

            assertEquals(C[3] * Math.Pow(x, 3) + C[2] * Math.Pow(x, 2) + C[1] * x + C[0], F.applyAsDouble(x), EPS);
        }
Exemple #2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void test()
        public virtual void test()
        {
            const int n = 20;

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[][] x = new double[n][5];
//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] x = new double[n][5];
            double[][] x = RectangularArrays.ReturnRectangularDoubleArray(n, 5);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] y1 = new double[n];
            double[] y1 = new double[n];
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] y2 = new double[n];
            double[] y2 = new double[n];
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] a1 = new double[] {3.4, 1.2, -0.62, -0.44, 0.65 };
            double[] a1 = new double[] { 3.4, 1.2, -0.62, -0.44, 0.65 };
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] a2 = new double[] {0.98, 3.4, 1.2, -0.62, -0.44, 0.65 };
            double[] a2 = new double[] { 0.98, 3.4, 1.2, -0.62, -0.44, 0.65 };
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    x[i][j] = RANDOM.NextDouble() + (RANDOM.NextDouble() - 0.5) / FACTOR;
                }
                y1[i] = a1[0] * x[i][0] + a1[1] * x[i][1] + a1[2] * x[i][2] + a1[3] * x[i][3] + a1[4] * x[i][4] + RANDOM.NextDouble() / FACTOR;
                y2[i] = a2[0] + a2[1] * x[i][0] + a2[2] * x[i][1] + a2[3] * x[i][2] + a2[4] * x[i][3] + a2[5] * x[i][4] + RANDOM.NextDouble() / FACTOR;
            }
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final LeastSquaresRegressionResult result1 = REGRESSION.regress(x, null, y1, false);
            LeastSquaresRegressionResult result1 = REGRESSION.regress(x, null, y1, false);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final LeastSquaresRegressionResult result2 = REGRESSION.regress(x, null, y2, true);
            LeastSquaresRegressionResult result2 = REGRESSION.regress(x, null, y2, true);

            assertRegression(result1, a1);
            assertRegression(result2, a2);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] residuals1 = result1.getResiduals();
            double[] residuals1 = result1.Residuals;
            for (int i = 0; i < n; i++)
            {
                assertEquals(y1[i], a1[0] * x[i][0] + a1[1] * x[i][1] + a1[2] * x[i][2] + a1[3] * x[i][3] + a1[4] * x[i][4] + residuals1[i], 10 * EPS);
            }
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] residuals2 = result2.getResiduals();
            double[] residuals2 = result2.Residuals;
            for (int i = 0; i < n; i++)
            {
                assertEquals(y2[i], a2[0] + a2[1] * x[i][0] + a2[2] * x[i][1] + a2[3] * x[i][2] + a2[4] * x[i][3] + a2[5] * x[i][4] + residuals2[i], 10 * EPS);
            }
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void test()
        public virtual void test()
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double x = RANDOM.nextDouble();
            double x = RANDOM.NextDouble();

            assertEquals(Math.Log(GAMMA.applyAsDouble(x)), LN_GAMMA.apply(x), EPS);
        }
Exemple #4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void transformTest()
        public virtual void transformTest()
        {
            for (int n = 2; n < 13; n++)
            {
                double[] from = new double[n - 1];
                for (int j = 0; j < n - 1; j++)
                {
                    from[j] = RANDOM.NextDouble() * Math.PI / 2;
                }
                SumToOne    trans = new SumToOne(n);
                DoubleArray to    = trans.transform(DoubleArray.copyOf(from));
                assertEquals(n, to.size());
                double sum = 0;
                for (int i = 0; i < n; i++)
                {
                    sum += to.get(i);
                }
                assertEquals("vector length " + n, 1.0, sum, 1e-9);
            }
        }
        public virtual void linearTest()
        {
            bool print = false;

            if (print)
            {
                Console.WriteLine("NonLinearLeastSquareWithPenaltyTest.linearTest");
            }
            int          nWeights  = 20;
            int          diffOrder = 2;
            double       lambda    = 100.0;
            DoubleMatrix penalty   = (DoubleMatrix)MA.scale(getPenaltyMatrix(nWeights, diffOrder), lambda);

            int[]    onIndex = new int[] { 1, 4, 11, 12, 15, 17 };
            double[] obs     = new double[] { 0, 1.0, 1.0, 1.0, 0.0, 0.0 };
            int      n       = onIndex.Length;

            System.Func <DoubleArray, DoubleArray> func = (DoubleArray x) =>
            {
                return(DoubleArray.of(n, i => x.get(onIndex[i])));
            };

            System.Func <DoubleArray, DoubleMatrix> jac = (DoubleArray x) =>
            {
                return(DoubleMatrix.of(n, nWeights, (i, j) => j == onIndex[i] ? 1d : 0d));
            };

            Well44497b  random = new Well44497b(0L);
            DoubleArray start  = DoubleArray.of(nWeights, i => random.NextDouble());

            LeastSquareWithPenaltyResults lsRes = NLLSWP.solve(DoubleArray.copyOf(obs), DoubleArray.filled(n, 0.01), func, jac, start, penalty);

            if (print)
            {
                Console.WriteLine("chi2: " + lsRes.ChiSq);
                Console.WriteLine(lsRes.FitParameters);
            }
            for (int i = 0; i < n; i++)
            {
                assertEquals(obs[i], lsRes.FitParameters.get(onIndex[i]), 0.01);
            }
            double expPen = 20.87912357454752;

            assertEquals(expPen, lsRes.Penalty, 1e-9);
        }
Exemple #6
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void test()
        public virtual void test()
        {
            const double a0 = 2.3;
            const double a1 = -4.5;
            const double a2 = 0.76;
            const double a3 = 3.4;
            const int    n  = 30;

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[][] x = new double[n][3];
//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] x = new double[n][3];
            double[][] x = RectangularArrays.ReturnRectangularDoubleArray(n, 3);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] yIntercept = new double[n];
            double[] yIntercept = new double[n];
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] yNoIntercept = new double[n];
            double[] yNoIntercept = new double[n];
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[][] w1 = new double[n][n];
//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] w1 = new double[n][n];
            double[][] w1 = RectangularArrays.ReturnRectangularDoubleArray(n, n);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] w2 = new double[n];
            double[] w2 = new double[n];
            double   y, x1, x2, x3;

            for (int i = 0; i < n; i++)
            {
                x1              = i;
                x2              = x1 * x1;
                x3              = Math.Sqrt(x1);
                x[i]            = new double[] { x1, x2, x3 };
                y               = x1 * a1 + x2 * a2 + x3 * a3;
                yNoIntercept[i] = y;
                yIntercept[i]   = y + a0;
                for (int j = 0; j < n; j++)
                {
                    w1[i][j] = RANDOM.NextDouble();
                }
                w1[i][i] = 1.0;
                w2[i]    = 1.0;
            }
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final WeightedLeastSquaresRegression wlsRegression = new WeightedLeastSquaresRegression();
            WeightedLeastSquaresRegression wlsRegression = new WeightedLeastSquaresRegression();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final OrdinaryLeastSquaresRegression olsRegression = new OrdinaryLeastSquaresRegression();
            OrdinaryLeastSquaresRegression olsRegression = new OrdinaryLeastSquaresRegression();

            try
            {
                wlsRegression.regress(x, (double[])null, yNoIntercept, false);
                Assert.fail();
            }
//JAVA TO C# CONVERTER WARNING: 'final' catch parameters are not available in C#:
//ORIGINAL LINE: catch (final IllegalArgumentException e)
            catch (legalArgumentException)
            {
                // Expected
            }
            LeastSquaresRegressionResult wls = wlsRegression.regress(x, w1, yIntercept, true);
            LeastSquaresRegressionResult ols = olsRegression.regress(x, yIntercept, true);

            assertRegressions(n, 4, wls, ols);
            wls = wlsRegression.regress(x, w1, yNoIntercept, false);
            ols = olsRegression.regress(x, yNoIntercept, false);
            assertRegressions(n, 3, wls, ols);
            wls = wlsRegression.regress(x, w2, yIntercept, true);
            ols = olsRegression.regress(x, yIntercept, true);
            assertRegressions(n, 4, wls, ols);
            wls = wlsRegression.regress(x, w2, yNoIntercept, false);
            ols = olsRegression.regress(x, yNoIntercept, false);
            assertRegressions(n, 3, wls, ols);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void test()
        public virtual void test()
        {
            const int    n     = 100;
            const double beta0 = 0.3;
            const double beta1 = 2.5;
            const double beta2 = -0.3;

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.function.DoubleBinaryOperator f1 = (x1, x2) -> beta1 * x1 + beta2 * x2;
            System.Func <double, double, double> f1 = (x1, x2) => beta1 * x1 + beta2 * x2;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.function.DoubleBinaryOperator f2 = (x1, x2) -> beta0 + beta1 * x1 + beta2 * x2;
            System.Func <double, double, double> f2 = (x1, x2) => beta0 + beta1 * x1 + beta2 * x2;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[][] x = new double[n][2];
//JAVA TO C# CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
//ORIGINAL LINE: double[][] x = new double[n][2];
            double[][] x = RectangularArrays.ReturnRectangularDoubleArray(n, 2);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] y1 = new double[n];
            double[] y1 = new double[n];
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final double[] y2 = new double[n];
            double[] y2 = new double[n];
            for (int i = 0; i < n; i++)
            {
                x[i][0] = RANDOM.NextDouble();
                x[i][1] = RANDOM.NextDouble();
                y1[i]   = f1(x[i][0], x[i][1]);
                y2[i]   = f2(x[i][0], x[i][1]);
            }
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final LeastSquaresRegression ols = new OrdinaryLeastSquaresRegression();
            LeastSquaresRegression ols = new OrdinaryLeastSquaresRegression();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.List<String> names = java.util.Arrays.asList("1", "2");
            IList <string> names = Arrays.asList("1", "2");
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final NamedVariableLeastSquaresRegressionResult result1 = new NamedVariableLeastSquaresRegressionResult(names, ols.regress(x, null, y1, false));
            NamedVariableLeastSquaresRegressionResult result1 = new NamedVariableLeastSquaresRegressionResult(names, ols.regress(x, null, y1, false));
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final NamedVariableLeastSquaresRegressionResult result2 = new NamedVariableLeastSquaresRegressionResult(names, ols.regress(x, null, y2, true));
            NamedVariableLeastSquaresRegressionResult result2 = new NamedVariableLeastSquaresRegressionResult(names, ols.regress(x, null, y2, true));

            try
            {
                result1.getPredictedValue((IDictionary <string, double>)null);
                Assert.fail();
            }
//JAVA TO C# CONVERTER WARNING: 'final' catch parameters are not available in C#:
//ORIGINAL LINE: catch (final IllegalArgumentException e)
            catch (legalArgumentException)
            {
                // Expected
            }
            assertEquals(result1.getPredictedValue(System.Linq.Enumerable.Empty <string, double>()), 0.0, 1e-16);
            try
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.Map<String, double> map = new java.util.HashMap<>();
                IDictionary <string, double> map = new Dictionary <string, double>();
                map["1"] = 0.0;
                result1.getPredictedValue(map);
                Assert.fail();
            }
//JAVA TO C# CONVERTER WARNING: 'final' catch parameters are not available in C#:
//ORIGINAL LINE: catch (final IllegalArgumentException e)
            catch (legalArgumentException)
            {
                // Expected
            }
            double x1, x2, x3;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.Map<String, double> var = new java.util.HashMap<>();
            IDictionary <string, double> var = new Dictionary <string, double>();

            for (int i = 0; i < 10; i++)
            {
                x1       = RANDOM.NextDouble();
                x2       = RANDOM.NextDouble();
                x3       = RANDOM.NextDouble();
                var["1"] = x1;
                var["2"] = x2;
                assertEquals(result1.getPredictedValue(var), f1(x1, x2), EPS);
                assertEquals(result2.getPredictedValue(var), f2(x1, x2), EPS);
                var["3"] = x3;
                assertEquals(result1.getPredictedValue(var), f1(x1, x2), EPS);
                assertEquals(result2.getPredictedValue(var), f2(x1, x2), EPS);
            }
        }