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);
        }
コード例 #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()
        {
            double       chi2   = 13.234324;
            double       pen    = 2.3445;
            int          nParms = 12;
            DoubleArray  parms  = DoubleArray.filled(nParms, 0.5);
            DoubleMatrix cov    = DoubleMatrix.filled(nParms, nParms);

            LeastSquareWithPenaltyResults res = new LeastSquareWithPenaltyResults(chi2, pen, parms, cov);

            assertEquals(chi2, res.ChiSq);
            assertEquals(pen, res.Penalty);

            DoubleMatrix invJac = DoubleMatrix.filled(nParms, 5);

            res = new LeastSquareWithPenaltyResults(chi2, pen, parms, cov, invJac);
            assertEquals(chi2, res.ChiSq);
            assertEquals(pen, res.Penalty);
        }