Exemplo n.º 1
0
        public void TimeSeriesFitAR1()
        {
            double alpha = 0.3;
            double mu    = 0.2;
            double sigma = 0.4;
            int    n     = 24;

            // For our fit to AR(1), we have incorporated bias correction (at least
            // for the most important parameter alpha), so we can do a small-n test.

            FrameTable data = new FrameTable();

            data.AddColumn <UncertainValue>("mu");
            data.AddColumn <UncertainValue>("alpha");
            data.AddColumn <UncertainValue>("sigma");
            data.AddColumn <SymmetricMatrix>("covariance");
            data.AddColumn <double>("p");

            for (int i = 0; i < 128; i++)
            {
                TimeSeries series = GenerateAR1TimeSeries(alpha, mu, sigma, n, n * i + 271828);

                AR1FitResult result = series.FitToAR1();

                data.AddRow(
                    result.Mu, result.Alpha, result.Sigma,
                    result.Parameters.CovarianceMatrix, result.GoodnessOfFit.Probability
                    );
            }

            data.AddComputedColumn("alphaValue", r => ((UncertainValue)r["alpha"]).Value);
            data.AddComputedColumn("muValue", r => ((UncertainValue)r["mu"]).Value);

            // Check that fit parameters agree with inputs
            Assert.IsTrue(data["mu"].As((UncertainValue v) => v.Value).PopulationMean().ConfidenceInterval(0.99).ClosedContains(mu));
            Assert.IsTrue(data["alpha"].As((UncertainValue v) => v.Value).PopulationMean().ConfidenceInterval(0.99).ClosedContains(alpha));
            Assert.IsTrue(data["sigma"].As((UncertainValue v) => v.Value).PopulationMean().ConfidenceInterval(0.99).ClosedContains(sigma));

            // Check that reported variances agree with actual variances
            Assert.IsTrue(data["mu"].As((UncertainValue v) => v.Value).PopulationStandardDeviation().ConfidenceInterval(0.99).ClosedContains(data["mu"].As((UncertainValue v) => v.Uncertainty).Median()));
            Assert.IsTrue(data["alpha"].As((UncertainValue v) => v.Value).PopulationStandardDeviation().ConfidenceInterval(0.99).ClosedContains(data["alpha"].As((UncertainValue v) => v.Uncertainty).Median()));
            Assert.IsTrue(data["sigma"].As((UncertainValue v) => v.Value).PopulationStandardDeviation().ConfidenceInterval(0.99).ClosedContains(data["sigma"].As((UncertainValue v) => v.Uncertainty).Median()));

            // Check that reported co-variances agree with actual co-variances
            Assert.IsTrue(data["mu"].As((UncertainValue v) => v.Value).PopulationCovariance(data["alpha"].As((UncertainValue v) => v.Value)).ConfidenceInterval(0.99).ClosedContains(data["covariance"].As((SymmetricMatrix c) => c[0, 1]).Median()));

            // For small n, the fitted alpha can vary considerably, and the formula for var(m) varies
            // quite strongly with alpha, so the computed var(m) have a very long tail. This pushes the
            // mean computed var(m) quite a bit higher than a typical value, so we use medians instead
            // of means for our best guess for the predicted variance.

            TestResult ks = data["p"].As <double>().KolmogorovSmirnovTest(new UniformDistribution());

            Assert.IsTrue(ks.Probability > 0.05);

            // This is an onerous way to store values, but it does let us test how the data-frame machinery deals with
            // non-trivial storage types.
        }
Exemplo n.º 2
0
        public void TimeSeriesBadFit()
        {
            // Fit AR1 to MA1; the fit should be bad

            TimeSeries series = GenerateMA1TimeSeries(0.4, 0.3, 0.2, 1000);

            AR1FitResult result = series.FitToAR1();

            Assert.IsTrue(result.GoodnessOfFit.Probability < 0.01);
        }