Ejemplo 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.
        }
Ejemplo n.º 2
0
        private FrameTable GetTestFrame()
        {
            FrameTable frame = new FrameTable();

            frame.AddColumn <string>("name");
            frame.AddColumn <double>("height");
            frame.AddColumn <double>("weight");
            frame.AddColumn <bool?>("male");

            frame.AddRow("a", 7.0, 10.0, false);
            frame.AddRow(null, 6.5, 11.0, true);
            frame.AddRow("c", 6.0, 12.0, false);
            frame.AddRow("d", 5.5, 11.0, true);
            frame.AddRow("e", 5.0, 12.0, null);
            frame.AddRow("f", 4.5, 13.0, true);
            frame.AddRow(null, 4.0, 12.0, false);

            frame.AddComputedColumn("bmi", r => ((double)r["weight"]) / MoreMath.Sqr((double)r["height"]));

            return(frame);
        }
Ejemplo n.º 3
0
        public void FrameTableManipulation()
        {
            FrameTable table = new FrameTable();

            table.AddColumn <int>("Id");
            table.AddColumn <DateTime?>("Birthdate");
            table.AddColumns <string>("FirstName", "LastName");
            Assert.IsTrue(table.Columns.Count == 4);

            // Index lookup should work
            Assert.IsTrue(table.GetColumnIndex("Birthdate") >= 0);
            Assert.IsTrue(table.GetColumnIndex("None") < 0);

            // Add rows
            Assert.IsTrue(table.Rows.Count == 0);
            table.AddRow(1, DateTime.Parse("1990-01-01"), "a", "p");
            table.AddRow(2, DateTime.Parse("2000-02-02"), null, null);
            table.AddRow(new Dictionary <string, object>()
            {
                { "Id", 3 }, { "Birthdate", null }, { "FirstName", "c" }, { "LastName", "r" }
            });
            Assert.IsTrue(table.Rows.Count == 3);

            // Adding rows with the wrong types and/or entries should fail
            // Careful, some of these will leave the table in a bad state
            //try {
            //    table.AddRow(4, DateTime.Parse("2010-04-04"), 1.0, "s");
            //    Assert.Fail();
            //} catch (Exception) { }
            try {
                table.AddRow(4, DateTime.Parse("2010-04-04"));
                Assert.Fail();
            } catch (Exception) { }
            //try {
            //    table.AddRow(new Dictionary<string, object>() {
            //        {"Id", 4}, { "FirstName", "d" }, { "LastName", "r" }
            //    });
            //    Assert.Fail();
            //} catch (Exception) { }
            //try {
            //    table.AddRow(new Dictionary<string, object>() {
            //        {"Id", 4}, { "Birthdate", null }, { "FirstName", "d" }, { "LastName", "r" }, { "MiddleName", "u" }
            //    });
            //    Assert.Fail();
            //} catch (Exception) { }

            // Adding a new column with the wrong length should fail
            try {
                table.AddColumn <double>("Score");
                Assert.Fail();
            } catch (Exception) { }
            Assert.IsTrue(table.GetColumnIndex("Score") < 0);

            // Adding a new column with the right length should work
            List <double> scores = new List <double>()
            {
                1.1, 1.2, 1.3
            };

            table.AddColumn("Score", scores);
            Assert.IsTrue(table.GetColumnIndex("Score") >= 0);

            // Adding a new computed column should work
            table.AddComputedColumn <TimeSpan?>("Age", r => {
                DateTime?b = (DateTime?)r["Birthdate"];
                if (b.HasValue)
                {
                    return(DateTime.Now - b.Value);
                }
                else
                {
                    return(null);
                }
            });
            Assert.IsTrue(table.GetColumnIndex("Age") >= 0);

            // Changing a value should change the result of the computed column that depends on it
            int      birthdateIndex = table.GetColumnIndex("Birthdate");
            int      ageIndex       = table.GetColumnIndex("Age");
            TimeSpan age1           = (TimeSpan)table[0, ageIndex];

            table[0, birthdateIndex] = DateTime.Parse("2010-01-01");
            TimeSpan age2 = (TimeSpan)table[0, ageIndex];

            Assert.IsTrue(age2 != age1);

            // Clearing a table should work
            table.Clear();
            Assert.IsTrue(table.Columns.Count > 0);
            Assert.IsTrue(table.Rows.Count == 0);
        }