public void FitDataToPolynomialTest()
        {
            Interval r = Interval.FromEndpoints(-10.0, 10.0);

            double[] c = new double[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 };
            Func <double, double> fv = delegate(double x) {
                double f = 0.0;
                for (int i = c.Length - 1; i >= 0; i--)
                {
                    f = f * x + c[i];
                }
                return(f);
            };
            Func <double, double> fu = delegate(double x) {
                return(1.0 + 0.5 * Math.Cos(x));
            };

            UncertainMeasurementSample set = CreateDataSet(r, fv, fu, 50);

            Assert.IsTrue(set.Count == 50);

            // fit to an appropriate polynomial
            FitResult poly = set.FitToPolynomial(5);

            // the coefficients should match
            for (int i = 0; i < poly.Dimension; i++)
            {
                Assert.IsTrue(poly.Parameter(i).ConfidenceInterval(0.95).ClosedContains(c[i]));
            }

            // the fit should be good
            Console.WriteLine(poly.GoodnessOfFit.LeftProbability);
            Assert.IsTrue(poly.GoodnessOfFit.LeftProbability < 0.95);

            // fit to a lower order polynomial
            FitResult low = set.FitToPolynomial(4);

            // the fit should be bad
            Assert.IsTrue(low.GoodnessOfFit.Statistic > poly.GoodnessOfFit.Statistic);
            Assert.IsTrue(low.GoodnessOfFit.LeftProbability > 0.95);

            // fit to a higher order polynomial
            FitResult high = set.FitToPolynomial(6);

            // the higher order coefficients should be compatible with zero
            Assert.IsTrue(high.Parameter(6).ConfidenceInterval(0.95).ClosedContains(0.0));

            // the fit should be better, but not too much better
            Assert.IsTrue(high.GoodnessOfFit.Statistic < poly.GoodnessOfFit.Statistic);
        }
        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);
        }
Beispiel #3
0
        public void Bug6162()
        {
            // When UncertianMeasurementSample.FitToPolynomial used Cholesky inversion of (A^T A), this inversion
            // would fail when roundoff errors would made the matrix non-positive-definite. We have now changed
            // to QR decomposition, which is more robust.

            //real data
            double[] X_axis = new double[] { 40270.65625, 40270.6569444444, 40270.6576388888, 40270.6583333332, 40270.6590277776,
                                             40270.659722222, 40270.6604166669, 40270.6611111113, 40270.6618055557, 40270.6625000001 };

            double[] Y_axis = new double[] { 246.824996948242, 246.850006103516, 245.875, 246.225006103516, 246.975006103516,
                                             247.024993896484, 246.949996948242, 246.875, 247.5, 247.100006103516 };

            UncertainMeasurementSample DataSet = new UncertainMeasurementSample();

            for (int i = 0; i < 10; i++)
            {
                DataSet.Add(X_axis[i], Y_axis[i], 1);
            }

            UncertainMeasurementFitResult DataFit = DataSet.FitToPolynomial(3);

            BivariateSample bs = new BivariateSample();

            for (int i = 0; i < 10; i++)
            {
                bs.Add(X_axis[i], Y_axis[i]);
            }
            PolynomialRegressionResult bsFit = bs.PolynomialRegression(3);

            foreach (Parameter p in bsFit.Parameters)
            {
                Console.WriteLine(p);
            }
        }
Beispiel #4
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)));
        }
Beispiel #5
0
        public void FitDataToPolynomialTest()
        {
            Interval              r  = Interval.FromEndpoints(-10.0, 10.0);
            Polynomial            p  = Polynomial.FromCoefficients(1.0, -2.0, 3.0, -4.0, 5.0, -6.0);
            Func <double, double> fu = delegate(double x) {
                return(1.0 + 0.5 * Math.Cos(x));
            };

            UncertainMeasurementSample set = CreateDataSet(r, p.Evaluate, fu, 50);

            Assert.IsTrue(set.Count == 50);

            // fit to an appropriate polynomial
            UncertainMeasurementFitResult poly = set.FitToPolynomial(5);

            // the coefficients should match
            for (int i = 0; i < poly.Parameters.Count; i++)
            {
                Assert.IsTrue(poly.Parameters[i].Estimate.ConfidenceInterval(0.95).ClosedContains(p.Coefficient(i)));
            }

            // the fit should be good
            Assert.IsTrue(poly.GoodnessOfFit.Probability > 0.05);

            // fit to a lower order polynomial
            UncertainMeasurementFitResult low = set.FitToPolynomial(4);

            // the fit should be bad
            Assert.IsTrue(low.GoodnessOfFit.Statistic > poly.GoodnessOfFit.Statistic);
            Assert.IsTrue(low.GoodnessOfFit.Probability < 0.05);

            // fit to a higher order polynomial
            UncertainMeasurementFitResult high = set.FitToPolynomial(6);

            // the higher order coefficients should be compatible with zero
            Assert.IsTrue(high.Parameters[6].Estimate.ConfidenceInterval(0.95).ClosedContains(0.0));

            // the fit should be better, but not too much better
            Assert.IsTrue(high.GoodnessOfFit.Statistic < poly.GoodnessOfFit.Statistic);
        }
Beispiel #6
0
        public void FitDataToPolynomialUncertaintiesTest()
        {
            // make sure the reported uncertainties it fit parameters really represent their standard deviation,
            // and that the reported off-diagonal elements really represent their correlations

            double[] xs = TestUtilities.GenerateUniformRealValues(-1.0, 2.0, 10);
            Func <double, double> fv = delegate(double x) {
                return(0.0 + 1.0 * x + 2.0 * x * x);
            };
            Func <double, double> fu = delegate(double x) {
                return(0.5);
            };

            // keep track of best-fit parameters and claimed parameter covariances
            MultivariateSample sample = new MultivariateSample(3);

            // generate 50 small data sets and fit each
            UncertainMeasurementFitResult[] fits = new UncertainMeasurementFitResult[50];
            for (int i = 0; i < fits.Length; i++)
            {
                UncertainMeasurementSample set = CreateDataSet(xs, fv, fu, 314159 + i);
                fits[i] = set.FitToPolynomial(2);
                sample.Add(fits[i].Parameters.ValuesVector);
            }

            // check that parameters agree
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine(sample.Column(i).PopulationMean);
            }

            // for each parameter, verify that the standard deviation of the reported values agrees with the (average) reported uncertainty
            double[] pMeans = new double[3];
            for (int i = 0; i <= 2; i++)
            {
                Sample values        = new Sample();
                Sample uncertainties = new Sample();
                for (int j = 0; j < fits.Length; j++)
                {
                    UncertainValue p = fits[j].Parameters[i].Estimate;
                    values.Add(p.Value);
                    uncertainties.Add(p.Uncertainty);
                }
                pMeans[i] = values.Mean;
                Assert.IsTrue(values.PopulationStandardDeviation.ConfidenceInterval(0.95).ClosedContains(uncertainties.Mean));
            }
        }
        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);
        }
        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
            FitResult pFit = set.FitToPolynomial(3);

            // fit it to a cubic polynomaial
            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);
            };
            FitResult fFit = set.FitToFunction(ff, new double[] { 0, 0, 0, 0 });

            // the fits should agree
            Console.WriteLine("{0} ?= {1}", pFit.GoodnessOfFit.Statistic, fFit.GoodnessOfFit.Statistic);
            for (int i = 0; i < pFit.Dimension; i++)
            {
                Console.WriteLine("{0} ?= {1}", pFit.Parameter(i), fFit.Parameter(i));
                Assert.IsTrue(pFit.Parameter(i).ConfidenceInterval(0.01).ClosedContains(fFit.Parameter(i).Value));
            }
            // dimension
            Assert.IsTrue(pFit.Dimension == fFit.Dimension);
            // 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, fFit.Parameters, Math.Pow(TestUtilities.TargetPrecision, 0.3)));
            // covariance
            Assert.IsTrue(TestUtilities.IsNearlyEqual(pFit.CovarianceMatrix, fFit.CovarianceMatrix, Math.Pow(TestUtilities.TargetPrecision, 0.3)));
        }
        public void FitDataToPolynomialUncertaintiesTest()
        {
            // make sure the reported uncertainties it fit parameters really represent their standard deviation,
            // and that the reported off-diagonal elements really represent their correlations

            double[] xs = TestUtilities.GenerateUniformRealValues(-1.0, 2.0, 10);
            Func <double, double> fv = delegate(double x) {
                return(0.0 + 1.0 * x + 2.0 * x * x);
            };
            Func <double, double> fu = delegate(double x) {
                return(0.5);
            };

            // keep track of best-fit parameters and claimed parameter covariances
            MultivariateSample sample = new MultivariateSample(3);

            // generate 50 small data sets and fit each
            FitResult[] fits = new FitResult[50];
            for (int i = 0; i < fits.Length; i++)
            {
                //DataSet set = CreateDataSet(Interval.FromEndpoints(-1.0,2.0), fv, fu, 10, i);
                UncertainMeasurementSample set = CreateDataSet(xs, fv, fu, 314159 + i);

                /*
                 * foreach (DataPoint point in set) {
                 *  Console.WriteLine("  i={0} x={1} y={2}", i, point.X, point.Y);
                 * }
                 */
                fits[i] = set.FitToPolynomial(2);

                /*
                 * for (int j = 0; j < fits[i].Dimension; j++) {
                 *  Console.WriteLine("p[{0}] = {1}", j, fits[i].Parameter(j));
                 * }
                 */

                for (int j = 0; j < fits[i].Dimension; j++)
                {
                    sample.Add(fits[i].Parameters);
                }
            }

            // check that parameters agree
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine(sample.Column(i).PopulationMean);
            }
            //Assert.IsTrue(sample.PopulationMean(0).ConfidenceInterval(0.95).ClosedContains(0.0));
            //Assert.IsTrue(sample.PopulationMean(1).ConfidenceInterval(0.95).ClosedContains(1.0));
            //Assert.IsTrue(sample.PopulationMean(2).ConfidenceInterval(0.95).ClosedContains(2.0));

            /*
             * // check that the claimed covariances agree with the measured covariances
             * for (int i = 0; i < 3; i++) {
             *  for (int j = 0; j < 3; j++) {
             *
             *      Console.WriteLine("{0},{1} {2} {3}", i, j, sample.PopulationCovariance(i, j), C[i, j]);
             *
             *      Assert.IsTrue(sample.PopulationCovariance(i, j).ConfidenceInterval(0.95).ClosedContains(C[i, j]));
             *  }
             * }
             */

            // for each parameter, verify that the standard devition of the reported values agrees with the (average) reported uncertainty
            double[] pMeans = new double[3];
            for (int i = 0; i <= 2; i++)
            {
                Console.WriteLine("paramater {0}", i);
                Sample values        = new Sample();
                Sample uncertainties = new Sample();
                for (int j = 0; j < fits.Length; j++)
                {
                    UncertainValue p = fits[j].Parameter(i);
                    values.Add(p.Value);
                    uncertainties.Add(p.Uncertainty);
                }
                pMeans[i] = values.Mean;
                Console.WriteLine("reported values mean={0}, standard deviation={1}", values.Mean, values.StandardDeviation);
                Console.WriteLine("reported uncertainties mean={0}", uncertainties.Mean);
                Assert.IsTrue(values.PopulationStandardDeviation.ConfidenceInterval(0.95).ClosedContains(uncertainties.Mean));
            }

            // for each parameter pair, verify that the covariances of the reported values agrees with the (average) reported covarance

            /*
             * for (int i = 0; i <= 2; i++) {
             *  for (int j = 0; j <= 2; j++) {
             *      // compute cov(i,j)
             *      double cov = 0.0;
             *      for (int k = 0; k < fits.Length; k++) {
             *          cov += (fits[k].Parameter(i).Value - pMeans[i]) * (fits[k].Parameter(j).Value - pMeans[j]);
             *      }
             *      cov = cov / fits.Length;
             *      // collect the reported covarainces
             *      Sample covs = new Sample();
             *      for (int k = 0; k < fits.Length; k++) {
             *          covs.Add(fits[k].Covariance(i, j));
             *      }
             *      Console.WriteLine("cov({0},{1}) computed={2} reported={3}", i, j, cov, covs.PopulationMean);
             *      // the computed covariance should agree with the (average) reported covariance
             *      // problem: we need to estimate the uncertainty in our covariance, but that isn't ever computed
             *      // note: covs just depend on x's, so the reported cov is the same for all fits
             *      // solution: for now, just assume it scales with 1/Sqrt(N); long term, do a multivariate fit
             *      //Assert.IsTrue((new UncertainValue(cov, Math.Abs(cov)* Math.Sqrt(2.0/fits.Length))).ConfidenceInterval(0.95).ClosedContains(covs.Mean));
             *  }
             * }
             */
        }
Beispiel #10
0
        public void Bug6162()
        {
            // When UncertianMeasurementSample.FitToPolynomial used Cholesky inversion of (A^T A), this inversion
            // would fail when roundoff errors would made the matrix non-positive-definite. We have now changed
            // to QR decomposition, which is more robust.

            //real data
            double[] X_axis = new double[] { 40270.65625, 40270.6569444444, 40270.6576388888, 40270.6583333332, 40270.6590277776,
                40270.659722222, 40270.6604166669, 40270.6611111113, 40270.6618055557, 40270.6625000001 };

            double[] Y_axis = new double[] { 246.824996948242, 246.850006103516, 245.875, 246.225006103516, 246.975006103516,
                247.024993896484, 246.949996948242, 246.875, 247.5, 247.100006103516 };

            UncertainMeasurementSample DataSet = new UncertainMeasurementSample();

            for (int i = 0; i < 10; i++) DataSet.Add(X_axis[i], Y_axis[i], 1);
            //for (int i = 0; i < 10; i++) DataSet.Add(X_axis[i] - 40270.0, Y_axis[i] - 247.0, 1);

            FitResult DataFit = DataSet.FitToPolynomial(3);

            for (int i = 0; i < DataFit.Dimension; i++)
                Console.WriteLine("a" + i.ToString() + " = " + DataFit.Parameter(i).Value);

            BivariateSample bs = new BivariateSample();
            for (int i = 0; i < 10; i++) bs.Add(X_axis[i], Y_axis[i]);
            FitResult bsFit = bs.PolynomialRegression(3);
            for (int i = 0; i < bsFit.Dimension; i++) Console.WriteLine(bsFit.Parameter(i));
        }