norm() public static method

public static norm ( Vector v, int size, double h ) : double
v Vector
size int
h double
return double
コード例 #1
0
        public void testOperatorConsistency()
        {
            //("Testing differential operators...");

            NormalDistribution           normal = new NormalDistribution(average, sigma);
            CumulativeNormalDistribution cum    = new CumulativeNormalDistribution(average, sigma);

            double xMin = average - 4 * sigma,
                   xMax = average + 4 * sigma;
            int    N    = 10001;
            double h    = (xMax - xMin) / (N - 1);

            Vector x    = new Vector(N),
                   y    = new Vector(N),
                   yi   = new Vector(N),
                   yd   = new Vector(N),
                   temp = new Vector(N),
                   diff = new Vector(N);

            for (int i = 0; i < N; i++)
            {
                x[i] = xMin + h * i;
            }

            for (int i = 0; i < x.Count; i++)
            {
                y[i] = normal.value(x[i]);
            }
            for (int i = 0; i < x.Count; i++)
            {
                yi[i] = cum.value(x[i]);
            }

            for (int i = 0; i < x.size(); i++)
            {
                yd[i] = normal.derivative(x[i]);
            }

            // define the differential operators
            DZero       D  = new DZero(N, h);
            DPlusDMinus D2 = new DPlusDMinus(N, h);

            // check that the derivative of cum is Gaussian
            temp = D.applyTo(yi);

            for (int i = 0; i < y.Count; i++)
            {
                diff[i] = y[i] - temp[i];
            }
            double e = Utilities.norm(diff, diff.size(), h);

            if (e > 1.0e-6)
            {
                Assert.Fail("norm of 1st derivative of cum minus Gaussian: " + e + "\ntolerance exceeded");
            }

            // check that the second derivative of cum is normal.derivative
            temp = D2.applyTo(yi);

            for (int i = 0; i < yd.Count; i++)
            {
                diff[i] = yd[i] - temp[i];
            }

            e = Utilities.norm(diff, diff.size(), h);
            if (e > 1.0e-4)
            {
                Assert.Fail("norm of 2nd derivative of cum minus Gaussian derivative: " + e + "\ntolerance exceeded");
            }
        }