public void FrameViewRowInterfaces() { FrameView original = GetTestFrame(); FrameRow row = original.Rows[0]; IReadOnlyDictionary <string, object> rowAsDictionary = (IReadOnlyDictionary <string, object>)row; Assert.IsTrue(rowAsDictionary.Count == original.Columns.Count); Assert.IsTrue(rowAsDictionary.ContainsKey(original.Columns[0].Name)); Assert.IsFalse(rowAsDictionary.ContainsKey("NoName")); object value = null; Assert.IsTrue(rowAsDictionary.TryGetValue(original.Columns[0].Name, out value)); Assert.IsTrue(original.Columns[0][0].Equals(value)); Assert.IsFalse(rowAsDictionary.TryGetValue("NoName", out value)); Assert.IsTrue(rowAsDictionary.Keys.Count() == original.Columns.Count); Assert.IsTrue(rowAsDictionary.Values.Count() == original.Columns.Count); IReadOnlyList <object> rowAsList = (IReadOnlyList <object>)row; Assert.IsTrue(rowAsList.Count == original.Columns.Count); Assert.IsTrue(rowAsList[0].Equals(original.Columns[0][0])); }
public void MultivariateLinearLogisticRegressionSimple() { // define model y = a + b0 * x0 + b1 * x1 + noise double a = 1.0; double b0 = -1.0 / 2.0; double b1 = 1.0 / 3.0; ContinuousDistribution x0distribution = new LaplaceDistribution(); ContinuousDistribution x1distribution = new NormalDistribution(); // draw a sample from the model Random rng = new Random(1); MultivariateSample old = new MultivariateSample("y", "x0", "x1"); FrameTable table = new FrameTable(); table.AddColumn <double>("x0"); table.AddColumn <double>("x1"); table.AddColumn <bool>("y"); for (int i = 0; i < 100; i++) { double x0 = x0distribution.GetRandomValue(rng); double x1 = x1distribution.GetRandomValue(rng); double t = a + b0 * x0 + b1 * x1; double p = 1.0 / (1.0 + Math.Exp(-t)); bool y = (rng.NextDouble() < p); old.Add(y ? 1.0 : 0.0, x0, x1); table.AddRow(x0, x1, y); } // do a linear regression fit on the model MultiLinearLogisticRegressionResult oldResult = old.LogisticLinearRegression(0); MultiLinearLogisticRegressionResult newResult = table["y"].As <bool>().MultiLinearLogisticRegression( table["x0"].As <double>(), table["x1"].As <double>() ); // the result should have the appropriate dimension Assert.IsTrue(newResult.Parameters.Count == 3); // The parameters should match the model 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)); // Our predictions should be better than chance. int correct = 0; 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 p = newResult.Predict(x0, x1).Value; bool y = (bool)row["y"]; if ((y && p > 0.5) || (!y & p < 0.5)) { correct++; } } Assert.IsTrue(correct > 0.5 * table.Rows.Count); }
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)); } }