コード例 #1
0
        public void Bukin()
        {
            // Burkin has a narrow valley, not aligned with any axis, punctuated with many tiny "wells" along its bottom.
            // The deepest well is at (-10,1)-> 0.
            Func <IList <double>, double> function = (IList <double> x) => 100.0 * Math.Sqrt(Math.Abs(x[1] - 0.01 * x[0] * x[0])) + 0.01 * Math.Abs(x[0] + 10.0);

            IList <Interval> box = new Interval[] { Interval.FromEndpoints(-15.0, 0.0), Interval.FromEndpoints(-3.0, 3.0) };

            EvaluationSettings settings = new EvaluationSettings()
            {
                RelativePrecision = 0.0, AbsolutePrecision = 1.0E-4, EvaluationBudget = 1000000
            };

            /*
             * settings.Update += (object result) => {
             *  MultiExtremum e = (MultiExtremum) result;
             *  Console.WriteLine("After {0} evaluations, best value {1}", e.EvaluationCount, e.Value);
             * };
             */
            MultiExtremum minimum = MultiFunctionMath.FindGlobalMinimum(function, box, settings);

            Console.WriteLine(minimum.EvaluationCount);
            Console.WriteLine("{0} ({1})", minimum.Value, minimum.Precision);
            Console.WriteLine("{0} {1}", minimum.Location[0], minimum.Location[1]);

            // We do not end up finding the global minimum.
        }
コード例 #2
0
        public void EllipticKIntegrals()
        {
            Interval i = Interval.FromEndpoints(0.0, 1.0);

            // http://mathworld.wolfram.com/CatalansConstant.html equation 37
            Assert.IsTrue(TestUtilities.IsNearlyEqual(
                              FunctionMath.Integrate(
                                  k => AdvancedMath.EllipticK(k),
                                  Interval.FromEndpoints(0.0, 1.0)),
                              2.0 * AdvancedMath.Catalan
                              ));

            Assert.IsTrue(TestUtilities.IsNearlyEqual(
                              FunctionMath.Integrate(
                                  k => AdvancedMath.EllipticK(k) * k,
                                  Interval.FromEndpoints(0.0, 1.0)),
                              1.0
                              ));

            Assert.IsTrue(TestUtilities.IsNearlyEqual(
                              FunctionMath.Integrate(
                                  k => AdvancedMath.EllipticK(k) / (1.0 + k),
                                  Interval.FromEndpoints(0.0, 1.0)),
                              Math.PI * Math.PI / 8.0
                              ));
        }
コード例 #3
0
        public void FitToFunctionPolynomialCompatibilityTest()
        {
            // specify a cubic function
            Interval r = Interval.FromEndpoints(-10.0, 10.0);
            Func <double, double> fv = delegate(double x) {
                return(0.0 - 1.0 * x + 2.0 * x * x - 3.0 * x * x * x);
            };
            Func <double, double> fu = delegate(double x) {
                return(1.0 + 0.5 * Math.Cos(x));
            };

            // create a data set from it
            UncertainMeasurementSample set = CreateDataSet(r, fv, fu, 60);

            // fit it to a cubic polynomial
            UncertainMeasurementFitResult pFit = set.FitToPolynomial(3);

            // fit it to a cubic polynomial
            Func <double[], double, double> ff = delegate(double[] p, double x) {
                return(p[0] + p[1] * x + p[2] * x * x + p[3] * x * x * x);
            };
            UncertainMeasurementFitResult fFit = set.FitToFunction(ff, new double[] { 0, 0, 0, 0 });

            // dimension
            Assert.IsTrue(pFit.Parameters.Count == fFit.Parameters.Count);
            // chi squared
            Assert.IsTrue(TestUtilities.IsNearlyEqual(pFit.GoodnessOfFit.Statistic, fFit.GoodnessOfFit.Statistic, Math.Sqrt(TestUtilities.TargetPrecision)));
            // don't demand super-high precision agreement of parameters and covariance matrix
            // parameters
            Assert.IsTrue(TestUtilities.IsNearlyEqual(pFit.Parameters.ValuesVector, fFit.Parameters.ValuesVector, Math.Pow(TestUtilities.TargetPrecision, 0.3)));
            // covariance
            Assert.IsTrue(TestUtilities.IsNearlyEqual(pFit.Parameters.CovarianceMatrix, fFit.Parameters.CovarianceMatrix, Math.Pow(TestUtilities.TargetPrecision, 0.3)));
        }
コード例 #4
0
        public void FitDataToLinearFunctionTest()
        {
            // create a data set from a linear combination of sine and cosine
            Interval r = Interval.FromEndpoints(-4.0, 6.0);

            double[] c = new double[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 };
            Func <double, double> fv = delegate(double x) {
                return(2.0 * Math.Cos(x) + 1.0 * Math.Sin(x));
            };
            Func <double, double> fu = delegate(double x) {
                return(0.1 + 0.1 * Math.Abs(x));
            };
            UncertainMeasurementSample set = CreateDataSet(r, fv, fu, 20, 2);

            // fit the data set to a linear combination of sine and cosine
            Func <double, double>[] fs = new Func <double, double>[]
            { delegate(double x) { return(Math.Cos(x)); }, delegate(double x) { return(Math.Sin(x)); } };
            FitResult result = set.FitToLinearFunction(fs);

            // the fit should be right right dimension
            Assert.IsTrue(result.Dimension == 2);

            // the coefficients should match
            Console.WriteLine(result.Parameter(0));
            Console.WriteLine(result.Parameter(1));
            Assert.IsTrue(result.Parameter(0).ConfidenceInterval(0.95).ClosedContains(2.0));
            Assert.IsTrue(result.Parameter(1).ConfidenceInterval(0.95).ClosedContains(1.0));

            // diagonal covarainces should match errors
            Assert.IsTrue(TestUtilities.IsNearlyEqual(Math.Sqrt(result.Covariance(0, 0)), result.Parameter(0).Uncertainty));
            Assert.IsTrue(TestUtilities.IsNearlyEqual(Math.Sqrt(result.Covariance(1, 1)), result.Parameter(1).Uncertainty));
        }
コード例 #5
0
        public void TestBivariateRegression()
        {
            // Do a bunch of linear regressions. r^2 should be distributed as expected.

            double a0 = 1.0;
            double b0 = 0.0;

            Random rng = new Random(1001110000);
            ContinuousDistribution xDistribution = new UniformDistribution(Interval.FromEndpoints(-2.0, 4.0));
            ContinuousDistribution eDistribution = new NormalDistribution();

            List <double> r2Sample = new List <double>();

            for (int i = 0; i < 500; i++)
            {
                BivariateSample xySample = new BivariateSample();
                for (int k = 0; k < 10; k++)
                {
                    double x = xDistribution.GetRandomValue(rng);
                    double y = a0 + b0 * x + eDistribution.GetRandomValue(rng);
                    xySample.Add(x, y);
                }
                LinearRegressionResult fit = xySample.LinearRegression();
                double a = fit.Intercept.Value;
                double b = fit.Slope.Value;

                r2Sample.Add(fit.RSquared);
            }

            ContinuousDistribution r2Distribution = new BetaDistribution((2 - 1) / 2.0, (10 - 2) / 2.0);
            TestResult             ks             = r2Sample.KolmogorovSmirnovTest(r2Distribution);

            Assert.IsTrue(ks.Probability > 0.05);
        }
コード例 #6
0
        public void EllipticKCatalanIntegral()
        {
            System.Diagnostics.Stopwatch timer = System.Diagnostics.Stopwatch.StartNew();


            Interval i = Interval.FromEndpoints(0.0, 1.0);

            // http://mathworld.wolfram.com/CatalansConstant.html equation 37

            Func <double, double> f1 = delegate(double k) {
                return(AdvancedMath.EllipticK(k));
            };

            Assert.IsTrue(TestUtilities.IsNearlyEqual(
                              FunctionMath.Integrate(f1, i), 2.0 * AdvancedMath.Catalan
                              ));

            Func <double, double> f2 = delegate(double k) {
                return(AdvancedMath.EllipticK(k) * k);
            };

            Assert.IsTrue(TestUtilities.IsNearlyEqual(
                              FunctionMath.Integrate(f2, i), 1.0
                              ));

            timer.Stop();
            Console.WriteLine(timer.ElapsedTicks);
        }
コード例 #7
0
        public void RootOfEi()
        {
            double x = FunctionMath.FindZero(AdvancedMath.IntegralEi, Interval.FromEndpoints(0.1, 1.0));

            Assert.IsTrue(TestUtilities.IsNearlyEqual(x, 0.37250741078136663446));
            Assert.IsTrue(Math.Abs(AdvancedMath.IntegralEi(x)) < TestUtilities.TargetPrecision);
        }
コード例 #8
0
        public void ThompsonGlobal()
        {
            for (int n = 2; n < 8; n++)
            {
                Console.WriteLine("n={0}", n);

                Func <IList <double>, double> f = GetThompsonFunction(n);

                Interval[] box = new Interval[2 * (n - 1)];
                for (int i = 0; i < (n - 1); i++)
                {
                    box[2 * i]     = Interval.FromEndpoints(-Math.PI, Math.PI);
                    box[2 * i + 1] = Interval.FromEndpoints(-Math.PI / 2.0, Math.PI / 2.0);
                }

                MultiExtremum minimum = MultiFunctionMath.FindGlobalMinimum(f, box);

                Console.WriteLine(minimum.EvaluationCount);
                Console.WriteLine("{0} ({1})", minimum.Value, minimum.Precision);
                for (int i = 0; i < (n - 1); i++)
                {
                    Console.WriteLine("{0} {1}", minimum.Location[2 * i], minimum.Location[2 * i + 1]);
                }
                Console.WriteLine("0.0 0.0");

                Assert.IsTrue(minimum.Dimension == 2 * (n - 1));
                Assert.IsTrue(TestUtilities.IsNearlyEqual(minimum.Value, thompsonSolutions[n], new EvaluationSettings()
                {
                    AbsolutePrecision = 2.0 * minimum.Precision
                }));
            }
        }
コード例 #9
0
        public void FindExtremaNegativeGamma()
        {
            // https://en.wikipedia.org/wiki/Particular_values_of_the_Gamma_function#Other_constants

            Tuple <double, double>[] extrema = new Tuple <double, double>[] {
                Tuple.Create(-0.5040830082644554092582693045, -3.5446436111550050891219639933),
                Tuple.Create(-1.5734984731623904587782860437, 2.3024072583396801358235820396),
                Tuple.Create(-2.6107208684441446500015377157, -0.8881363584012419200955280294),
                Tuple.Create(-3.6352933664369010978391815669, 0.2451275398343662504382300889)
            };

            foreach (Tuple <double, double> extremum in extrema)
            {
                // We use brackets since we know the extremum must lie between the singularities.
                // We should be able to use the actual singularities at endpoints, but this doesn't work; look into it.

                Interval bracket = Interval.FromEndpoints(Math.Floor(extremum.Item1) + 0.01, Math.Ceiling(extremum.Item1) - 0.01);
                Extremum result;
                if (extremum.Item2 < 0.0)
                {
                    result = FunctionMath.FindMaximum(AdvancedMath.Gamma, bracket);
                }
                else
                {
                    result = FunctionMath.FindMinimum(AdvancedMath.Gamma, bracket);
                }
                Assert.IsTrue(result.Bracket.OpenContains(extremum.Item1));
                Assert.IsTrue(result.Bracket.OpenContains(result.Location));
                Assert.IsTrue(TestUtilities.IsNearlyEqual(result.Value, extremum.Item2));
            }
        }
コード例 #10
0
        public void WatsonIntegrals()
        {
            // Watson defined and analytically integrated three complicated triple integrals related to random walks in three dimension
            // See http://mathworld.wolfram.com/WatsonsTripleIntegrals.html

            Interval watsonWidth = Interval.FromEndpoints(0.0, Math.PI);

            Interval[] watsonBox = new Interval[] { watsonWidth, watsonWidth, watsonWidth };

            Assert.IsTrue(
                MultiFunctionMath.Integrate(
                    (IReadOnlyList <double> x) => 1.0 / (1.0 - Math.Cos(x[0]) * Math.Cos(x[1]) * Math.Cos(x[2])), watsonBox
                    ).Estimate.ConfidenceInterval(0.99).ClosedContains(
                    MoreMath.Pow(AdvancedMath.Gamma(1.0 / 4.0), 4) / 4.0
                    )
                );

            Assert.IsTrue(
                MultiFunctionMath.Integrate(
                    (IReadOnlyList <double> x) => 1.0 / (3.0 - Math.Cos(x[0]) * Math.Cos(x[1]) - Math.Cos(x[1]) * Math.Cos(x[2]) - Math.Cos(x[0]) * Math.Cos(x[2])), watsonBox
                    ).Estimate.ConfidenceInterval(0.99).ClosedContains(
                    3.0 * MoreMath.Pow(AdvancedMath.Gamma(1.0 / 3.0), 6) / Math.Pow(2.0, 14.0 / 3.0) / Math.PI
                    )
                );

            Assert.IsTrue(
                MultiFunctionMath.Integrate(
                    (IReadOnlyList <double> x) => 1.0 / (3.0 - Math.Cos(x[0]) - Math.Cos(x[1]) - Math.Cos(x[2])), watsonBox
                    ).Estimate.ConfidenceInterval(0.99).ClosedContains(
                    Math.Sqrt(6.0) / 96.0 * AdvancedMath.Gamma(1.0 / 24.0) * AdvancedMath.Gamma(5.0 / 24.0) * AdvancedMath.Gamma(7.0 / 24.0) * AdvancedMath.Gamma(11.0 / 24.0)
                    )
                );
        }
コード例 #11
0
        public void JacobiIntegrals()
        {
            foreach (double k in TestUtilities.GenerateRealValues(1.0E-1, 1.0, 4))
            {
                // DLMF 22.14.18 says
                //   \int_{0}^{K(k)} \ln(\dn(t, k)) \, dt = \frac{1}{2} K(k) \ln k'

                double K = AdvancedMath.EllipticK(k);

                double k1 = Math.Sqrt(1.0 - k * k);

                double I1 = FunctionMath.Integrate(
                    x => Math.Log(AdvancedMath.JacobiDn(x, k)),
                    Interval.FromEndpoints(0.0, K)
                    );

                Assert.IsTrue(TestUtilities.IsNearlyEqual(I1, K / 2.0 * Math.Log(k1)));

                // If k is small, log(k1) is subject to cancellation error, so don't pick k too small.

                // It also gives values for the analogous integrals with \sn and \cn,
                // but their values involve K'(k), for which we don't have a method.

                // Mathworld (http://mathworld.wolfram.com/CompleteEllipticIntegraloftheSecondKind.html) says
                //   \int_{0}^{K(k)} \dn^2(t, k) = E(k)

                double I2 = FunctionMath.Integrate(
                    u => MoreMath.Sqr(AdvancedMath.JacobiDn(u, k)),
                    Interval.FromEndpoints(0.0, K)
                    );

                Assert.IsTrue(TestUtilities.IsNearlyEqual(I2, AdvancedMath.EllipticE(k)));
            }
        }
コード例 #12
0
        public void HypergeometricIntegral()
        {
            // A&S 15.3.1

            // F(a, b, c, x) = \frac{\Gamma(c)}{\Gamma(b) \Gamma(c - b)}
            // \int_{0}^{1} \! dt \, t^{b-1} (1 - t)^{c - b - 1} (1 - x t)^{-a}

            // Choose limits on a, b, c so that singularities of integrand are numerically integrable.

            foreach (double a in TestUtilities.GenerateRealValues(1.0, 10.0, 2))
            {
                foreach (double b in TestUtilities.GenerateRealValues(0.6, 10.0, 2))
                {
                    foreach (double c in TestUtilities.GenerateRealValues(b + 0.6, 10.0, 2))
                    {
                        foreach (double x in xs)
                        {
                            double I = FunctionMath.Integrate(
                                t => Math.Pow(t, b - 1.0) * Math.Pow(1.0 - t, c - b - 1.0) * Math.Pow(1.0 - x * t, -a),
                                Interval.FromEndpoints(0.0, 1.0)
                                );

                            double B = AdvancedMath.Beta(b, c - b);

                            Assert.IsTrue(TestUtilities.IsNearlyEqual(AdvancedMath.Hypergeometric2F1(a, b, c, x), I / B));
                        }
                    }
                }
            }
        }
コード例 #13
0
ファイル: IntervalTest.cs プロジェクト: zyzhu/metanumerics
        public void IntervalMidpoint()
        {
            Interval ab = Interval.FromEndpoints(a, b);

            Assert.IsTrue(ab.Midpoint == (a + b) / 2.0);
            Assert.IsTrue(ab.Contains(ab.Midpoint));
        }
コード例 #14
0
        public void FitDataToProportionalityTest()
        {
            Interval r = Interval.FromEndpoints(0.0, 0.1);
            Func <double, double> fv = delegate(double x) {
                return(0.5 * x);
            };
            Func <double, double> fu = delegate(double x) {
                return(0.02);
            };
            UncertainMeasurementSample set = CreateDataSet(r, fv, fu, 20);

            // fit to proportionality
            FitResult prop = set.FitToProportionality();

            Assert.IsTrue(prop.Dimension == 1);
            Assert.IsTrue(prop.Parameter(0).ConfidenceInterval(0.95).ClosedContains(0.5));
            Assert.IsTrue(prop.GoodnessOfFit.LeftProbability < 0.95);

            // fit to line
            FitResult line = set.FitToLine();

            Assert.IsTrue(line.Dimension == 2);

            // line's intercept should be compatible with zero and slope with proportionality constant
            Assert.IsTrue(line.Parameter(0).ConfidenceInterval(0.95).ClosedContains(0.0));
            Assert.IsTrue(line.Parameter(1).ConfidenceInterval(0.95).ClosedContains(prop.Parameter(0).Value));

            // the fit should be better, but not too much better
            Assert.IsTrue(line.GoodnessOfFit.Statistic < prop.GoodnessOfFit.Statistic);
        }
コード例 #15
0
        public void MultivariateLinearRegressionAgreement2()
        {
            // A multivariate linear regression with just one x-column should be the same as a bivariate linear regression.

            double intercept = 1.0;
            double slope     = -2.0;
            ContinuousDistribution yErrDist = new NormalDistribution(0.0, 3.0);
            UniformDistribution    xDist    = new UniformDistribution(Interval.FromEndpoints(-2.0, 3.0));
            Random rng = new Random(1111111);

            MultivariateSample multi = new MultivariateSample("x", "y");

            for (int i = 0; i < 10; i++)
            {
                double x = xDist.GetRandomValue(rng);
                double y = intercept + slope * x + yErrDist.GetRandomValue(rng);
                multi.Add(x, y);
            }

            // Old multi linear regression code.
            MultiLinearRegressionResult result1 = multi.LinearRegression(1);

            // Simple linear regression code.
            LinearRegressionResult result2 = multi.TwoColumns(0, 1).LinearRegression();

            Assert.IsTrue(TestUtilities.IsNearlyEqual(result1.Parameters["Intercept"].Estimate, result2.Parameters["Intercept"].Estimate));

            // New multi linear regression code.
            MultiLinearRegressionResult result3 = multi.Column(1).ToList().MultiLinearRegression(multi.Column(0).ToList());

            Assert.IsTrue(TestUtilities.IsNearlyEqual(result1.Parameters["Intercept"].Estimate, result3.Parameters["Intercept"].Estimate));
        }
コード例 #16
0
        public void AssociatedLegendreOrthonormalityL()
        {
            // don't let l and m get too big, or numerical integration will fail due to heavily oscilatory behavior

            int[] ells = TestUtilities.GenerateIntegerValues(1, 10, 4);
            for (int ki = 0; ki < ells.Length; ki++)
            {
                int k = ells[ki];
                for (int li = 0; li <= ki; li++)
                {
                    int l = ells[li];
                    foreach (int m in TestUtilities.GenerateUniformIntegerValues(0, Math.Min(k, l), 4))
                    {
                        Func <double, double> f = delegate(double x) {
                            return(OrthogonalPolynomials.LegendreP(k, m, x) * OrthogonalPolynomials.LegendreP(l, m, x));
                        };
                        double I = FunctionMath.Integrate(f, Interval.FromEndpoints(-1.0, 1.0));

                        Console.WriteLine("k={0} l={1} m={2} I={3}", k, l, m, I);

                        if (k == l)
                        {
                            Assert.IsTrue(TestUtilities.IsNearlyEqual(
                                              I, 2.0 / (2 * k + 1) * Math.Exp(AdvancedIntegerMath.LogFactorial(l + m) - AdvancedIntegerMath.LogFactorial(l - m))
                                              ));
                        }
                        else
                        {
                            Assert.IsTrue(Math.Abs(I) < TestUtilities.TargetPrecision);
                        }
                    }
                }
            }
        }
コード例 #17
0
        public void FitDataToPolynomialChiSquaredTest()
        {
            // we want to make sure that the chi^2 values we are producing from polynomial fits are distributed as expected

            // create a sample to hold chi^2 values
            Sample chis = new Sample();

            // define a model
            Interval r = Interval.FromEndpoints(-5.0, 15.0);
            Func <double, double> fv = delegate(double x) {
                return(1.0 * x - 2.0 * x * x);
            };
            Func <double, double> fu = delegate(double x) {
                return(1.0 + 0.5 * Math.Sin(x));
            };

            // draw 50 data sets from the model and fit year
            // store the resulting chi^2 value in the chi^2 set
            for (int i = 0; i < 50; i++)
            {
                UncertainMeasurementSample xs = CreateDataSet(r, fv, fu, 10, i);
                FitResult fit = xs.FitToPolynomial(2);
                double    chi = fit.GoodnessOfFit.Statistic;
                chis.Add(chi);
            }

            // sanity check the sample
            Assert.IsTrue(chis.Count == 50);

            // test whether the chi^2 values are distributed as expected
            ContinuousDistribution chiDistribution = new ChiSquaredDistribution(7);
            TestResult             ks = chis.KolmogorovSmirnovTest(chiDistribution);

            Assert.IsTrue(ks.LeftProbability < 0.95);
        }
コード例 #18
0
        public void IntervalEndpoints()
        {
            Interval ab = Interval.FromEndpoints(a, b);

            Assert.IsTrue(ab.LeftEndpoint == a);
            Assert.IsTrue(ab.RightEndpoint == b);
        }
コード例 #19
0
        public void IntervalZeroWidth()
        {
            Interval aa = Interval.FromEndpoints(a, a);

            Assert.IsTrue(aa.Width == 0.0);
            Assert.IsTrue(aa.LeftEndpoint == aa.RightEndpoint);
            Assert.IsTrue(aa.Midpoint == a);
        }
コード例 #20
0
        public void RootOfJ0()
        {
            Func <double, double> f = delegate(double x) {
                return(AdvancedMath.BesselJ(0, x));
            };
            double y = FunctionMath.FindZero(f, Interval.FromEndpoints(2.0, 4.0));

            Assert.IsTrue(TestUtilities.IsNearlyEqual(y, 2.40482555769577276862));
        }
コード例 #21
0
        // Returns a d-dimensional symmetric unit cube [-1,+1]^d

        public Interval[] SymmetricUnitCube(int d)
        {
            Interval[] box = new Interval[d];
            for (int j = 0; j < d; j++)
            {
                box[j] = Interval.FromEndpoints(-1.0, 1.0);
            }
            return(box);
        }
コード例 #22
0
        public void BesselWeberIntegral()
        {
            Func <double, double> f = delegate(double t) {
                return(Math.Exp(-t * t) * AdvancedMath.BesselJ(0, t) * t);
            };
            Interval r = Interval.FromEndpoints(0.0, Double.PositiveInfinity);

            Assert.IsTrue(TestUtilities.IsNearlyEqual(FunctionMath.Integrate(f, r).Estimate.Value, Math.Exp(-1.0 / 4.0) / 2.0));
        }
コード例 #23
0
        public void IntervalClosedContains()
        {
            Interval ac = Interval.FromEndpoints(a, c);

            Assert.IsTrue(ac.ClosedContains(a));
            Assert.IsTrue(ac.ClosedContains(c));
            Assert.IsTrue(ac.ClosedContains(b));
            Assert.IsFalse(ac.ClosedContains(d));
        }
コード例 #24
0
        public void MultivariateLinearRegressionSimple()
        {
            // define model y = a + b0 * x0 + b1 * x1 + noise
            double a  = 1.0;
            double b0 = -2.0;
            double b1 = 3.0;
            ContinuousDistribution x0distribution = new CauchyDistribution(10.0, 5.0);
            ContinuousDistribution x1distribution = new UniformDistribution(Interval.FromEndpoints(-10.0, 20.0));
            ContinuousDistribution noise          = new NormalDistribution(0.0, 10.0);

            // draw a sample from the model
            Random             rng    = new Random(1);
            MultivariateSample sample = new MultivariateSample("x0", "x1", "y");
            FrameTable         table  = new FrameTable();

            table.AddColumns <double>("x0", "x1", "y");

            for (int i = 0; i < 100; i++)
            {
                double x0  = x0distribution.GetRandomValue(rng);
                double x1  = x1distribution.GetRandomValue(rng);
                double eps = noise.GetRandomValue(rng);
                double y   = a + b0 * x0 + b1 * x1 + eps;
                sample.Add(x0, x1, y);
                table.AddRow(x0, x1, y);
            }

            // do a linear regression fit on the model
            ParameterCollection         oldResult = sample.LinearRegression(2).Parameters;
            MultiLinearRegressionResult newResult = table["y"].As <double>().MultiLinearRegression(
                table["x0"].As <double>(), table["x1"].As <double>()
                );

            // the result should have the appropriate dimension
            Assert.IsTrue(oldResult.Count == 3);
            Assert.IsTrue(newResult.Parameters.Count == 3);

            // The parameters should match the model
            Assert.IsTrue(oldResult[0].Estimate.ConfidenceInterval(0.90).ClosedContains(b0));
            Assert.IsTrue(oldResult[1].Estimate.ConfidenceInterval(0.90).ClosedContains(b1));
            Assert.IsTrue(oldResult[2].Estimate.ConfidenceInterval(0.90).ClosedContains(a));

            Assert.IsTrue(newResult.CoefficientOf(0).ConfidenceInterval(0.99).ClosedContains(b0));
            Assert.IsTrue(newResult.CoefficientOf("x1").ConfidenceInterval(0.99).ClosedContains(b1));
            Assert.IsTrue(newResult.Intercept.ConfidenceInterval(0.99).ClosedContains(a));

            // The residuals should be compatible with the model predictions
            for (int i = 0; i < table.Rows.Count; i++)
            {
                FrameRow row = table.Rows[i];
                double   x0  = (double)row["x0"];
                double   x1  = (double)row["x1"];
                double   yp  = newResult.Predict(x0, x1).Value;
                double   y   = (double)row["y"];
                Assert.IsTrue(TestUtilities.IsNearlyEqual(newResult.Residuals[i], y - yp));
            }
        }
コード例 #25
0
ファイル: IntervalTest.cs プロジェクト: zyzhu/metanumerics
        public void IntervalEquality()
        {
            Interval ab = Interval.FromEndpoints(a, b);
            Interval ac = Interval.FromEndpoints(a, c);

            Assert.IsTrue(ab.Equals(ab));
            Assert.IsTrue(ab.Equals((object)ab));
            Assert.IsFalse(ab.Equals(ac));
            Assert.IsFalse(ab.Equals((object)ac));
        }
コード例 #26
0
        public void IntegralSiDefinition()
        {
            Func <double, double> f = t => Math.Sin(t) / t;

            foreach (double x in TestUtilities.GenerateRealValues(1.0E-2, 1.0E2, 8))
            {
                Interval r = Interval.FromEndpoints(0.0, x);
                Assert.IsTrue(TestUtilities.IsNearlyEqual(AdvancedMath.IntegralSi(x), FunctionMath.Integrate(f, r)));
            }
        }
コード例 #27
0
        public void BesselLipshitzIntegral()
        {
            // \int_{0}^{\infty} e^{-x} J_0(x) \, dx = \frac{1}{\sqrt{2}}
            Func <double, double> f = delegate(double t) {
                return(Math.Exp(-t) * AdvancedMath.BesselJ(0, t));
            };
            Interval r = Interval.FromEndpoints(0.0, Double.PositiveInfinity);

            Assert.IsTrue(TestUtilities.IsNearlyEqual(FunctionMath.Integrate(f, r).Estimate.Value, 1.0 / Math.Sqrt(2.0)));
        }
コード例 #28
0
 public void GammaIntegral()
 {
     foreach (double x in TestUtilities.GenerateRealValues(1.0, 1.0E2, 4))
     {
         Func <double, double> f = delegate(double t) {
             return(Math.Pow(t, x - 1.0) * Math.Exp(-t));
         };
         Interval r = Interval.FromEndpoints(0.0, Double.PositiveInfinity);
         Assert.IsTrue(TestUtilities.IsNearlyEqual(AdvancedMath.Gamma(x), FunctionMath.Integrate(f, r)));
     }
 }
コード例 #29
0
        public void AiryAiIntegral()
        {
            // DLMF 9.10.11
            Func <double, double> f = delegate(double t) {
                return(AdvancedMath.AiryAi(t));
            };
            Interval r = Interval.FromEndpoints(0.0, Double.PositiveInfinity);
            double   I = FunctionMath.Integrate(f, r);

            Assert.IsTrue(TestUtilities.IsNearlyEqual(I, 1.0 / 3.0));
        }
コード例 #30
0
ファイル: IntervalTest.cs プロジェクト: zyzhu/metanumerics
        public void IntervalContainsEndpoints()
        {
            Interval ab = Interval.FromEndpoints(a, b);

            Assert.IsTrue(ab.Contains(a, IntervalType.Closed));
            Assert.IsFalse(ab.Contains(a, IntervalType.Open));
            Assert.IsTrue(ab.Contains(a, IntervalType.Closed, IntervalType.Open));
            Assert.IsTrue(ab.Contains(b, IntervalType.Closed));
            Assert.IsFalse(ab.Contains(b, IntervalType.Open));
            Assert.IsFalse(ab.Contains(b, IntervalType.Closed, IntervalType.Open));
        }