Example #1
0
        public void FitDataToLineUncertaintyTest()
        {
            double[] xs = TestUtilities.GenerateUniformRealValues(0.0, 10.0, 10);
            Func <double, double> fv = delegate(double x) {
                return(2.0 * x - 1.0);
            };
            Func <double, double> fu = delegate(double x) {
                return(1.0 + x);
            };

            MultivariateSample sample     = new MultivariateSample(2);
            SymmetricMatrix    covariance = new SymmetricMatrix(2);

            // create a bunch of small data sets
            for (int i = 0; i < 100; i++)
            {
                UncertainMeasurementSample data = CreateDataSet(xs, fv, fu, i);
                FitResult fit = data.FitToLine();

                sample.Add(fit.Parameters);
                covariance = fit.CovarianceMatrix;
                // because it depends only on the x's and sigmas, the covariance is always the same

                Console.WriteLine("cov_00 = {0}", covariance[0, 0]);
            }

            // the measured covariances should agree with the claimed covariances
            //Assert.IsTrue(sample.PopulationCovariance(0,0).ConfidenceInterval(0.95).ClosedContains(covariance[0,0]));
            //Assert.IsTrue(sample.PopulationCovariance(0,1).ConfidenceInterval(0.95).ClosedContains(covariance[0,1]));
            //Assert.IsTrue(sample.PopulationCovariance(1,0).ConfidenceInterval(0.95).ClosedContains(covariance[1,0]));
            //Assert.IsTrue(sample.PopulationCovariance(1,1).ConfidenceInterval(0.95).ClosedContains(covariance[1,1]));
        }
Example #2
0
        public void FitDataToLineChiSquaredTest()
        {
            // we want to make sure that the chi^2 values we are producing from line 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, 5.0);
            Func <double, double> fv = delegate(double x) {
                return(1.0 - 2.0 * 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.FitToLine();
                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(8);
            TestResult             ks = chis.KolmogorovSmirnovTest(chiDistribution);

            Assert.IsTrue(ks.LeftProbability < 0.95);
        }
Example #3
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);
        }
Example #4
0
        public void FitToFunctionLinearCompatibilityTest()
        {
            // specify a cubic function
            Interval r = Interval.FromEndpoints(-5.0, 5.0);
            Func <double, double> fv = delegate(double x) {
                return(1.0 + 2.0 * 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.Sin(x));
            };

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

            // fit it to a cubic polynomial
            UncertainMeasurementFitResult pFit = set.FitToLine();
            //FitResult 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);
                //return (p[0] + p[1] * x + p[2] * x * x + p[3] * x * x * x);
            };
            UncertainMeasurementFitResult fFit = set.FitToFunction(ff, new double[] { 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)));
            // parameters
            Assert.IsTrue(TestUtilities.IsNearlyEqual(pFit.Parameters.ValuesVector, fFit.Parameters.ValuesVector, Math.Sqrt(TestUtilities.TargetPrecision)));
            // covariance
            Assert.IsTrue(TestUtilities.IsNearlyEqual(pFit.Parameters.CovarianceMatrix, fFit.Parameters.CovarianceMatrix, Math.Sqrt(TestUtilities.TargetPrecision)));
        }
Example #5
0
        public void FitDataToLineTest()
        {
            Interval r = Interval.FromEndpoints(0.0, 10.0);
            Func <double, double> fv = delegate(double x) {
                return(2.0 * x - 1.0);
            };
            Func <double, double> fu = delegate(double x) {
                return(1.0 + x);
            };
            UncertainMeasurementSample data = CreateDataSet(r, fv, fu, 20);


            // sanity check the data set
            Assert.IsTrue(data.Count == 20);

            // fit to a line
            FitResult line = data.FitToLine();

            Assert.IsTrue(line.Dimension == 2);
            Assert.IsTrue(line.Parameter(0).ConfidenceInterval(0.95).ClosedContains(-1.0));
            Assert.IsTrue(line.Parameter(1).ConfidenceInterval(0.95).ClosedContains(2.0));
            Assert.IsTrue(line.GoodnessOfFit.LeftProbability < 0.95);

            // correlation coefficient should be related to covariance as expected
            Assert.IsTrue(TestUtilities.IsNearlyEqual(line.CorrelationCoefficient(0, 1), line.Covariance(0, 1) / line.Parameter(0).Uncertainty / line.Parameter(1).Uncertainty));

            // fit to a 1st order polynomial and make sure it agrees
            FitResult poly = data.FitToPolynomial(1);

            Assert.IsTrue(poly.Dimension == 2);
            Assert.IsTrue(TestUtilities.IsNearlyEqual(poly.Parameters, line.Parameters));
            Assert.IsTrue(TestUtilities.IsNearlyEqual(poly.CovarianceMatrix, line.CovarianceMatrix));
            Assert.IsTrue(TestUtilities.IsNearlyEqual(poly.GoodnessOfFit.Statistic, line.GoodnessOfFit.Statistic));
            Assert.IsTrue(TestUtilities.IsNearlyEqual(poly.GoodnessOfFit.LeftProbability, line.GoodnessOfFit.LeftProbability));
            Assert.IsTrue(TestUtilities.IsNearlyEqual(poly.GoodnessOfFit.RightProbability, line.GoodnessOfFit.RightProbability));

            // fit to a constant; the result should be poor
            FitResult constant = data.FitToConstant();

            Assert.IsTrue(constant.GoodnessOfFit.LeftProbability > 0.95);
        }