internal Face(ServerModel.Face face) { if (face != null) { Id = face.face_id; if (face.attribute != null) { Age = new Age(face.attribute.age); Gender = new UncertainValue(face.attribute.gender); HasGlasses = new UncertainValue(face.attribute.glass); Race = new UncertainValue(face.attribute.race); Smiling = new UncertainValue(face.attribute.smiling); } if (face.position != null) { Position = new Position(face.position.center); Size = new Size { Width = face.position.width, Height = face.position.height }; EyeLeft = new Position(face.position.eye_left); EyeRight = new Position(face.position.eye_right); MouthLeft = new Position(face.position.mouth_left); MouthRight = new Position(face.position.mouth_right); Nose = new Position(face.position.nose); } } }
public void ExponentialFitUncertainty() { // check that the uncertainty in reported fit parameters is actually meaningful // it should be the standard deviation of fit parameter values in a sample of many fits // define a population distribution ExponentialDistribution distribution = new ExponentialDistribution(4.0); // draw a lot of samples from it; fit each sample and // record the reported parameter value and error of each Sample values = new Sample(); Sample uncertainties = new Sample(); for (int i = 0; i < 128; i++) { Sample sample = SampleTest.CreateSample(distribution, 8, i); ExponentialFitResult fit = ExponentialDistribution.FitToSample(sample); UncertainValue lambda = fit.Parameters[0].Estimate; values.Add(lambda.Value); uncertainties.Add(lambda.Uncertainty); } // the reported values should agree with the source distribution Assert.IsTrue(values.PopulationMean.ConfidenceInterval(0.95).ClosedContains(distribution.Mean)); // the reported errors should agree with the standard deviation of the reported parameters Assert.IsTrue(values.PopulationStandardDeviation.ConfidenceInterval(0.95).ClosedContains(uncertainties.Mean)); }
public void UncertainMathLogExp() { UncertainValue la = UncertainMath.Log(a); Assert.IsTrue(la.Uncertainty == a.RelativeUncertainty); Assert.IsTrue(UncertainMath.Exp(la) == a); }
public void BinaryContingencyTest() { // Create a table with significant association and test for it. ContingencyTable e1 = CreateExperiment(0.50, 0.50, 0.75, 128); Assert.IsTrue(e1.RowTotal(0) + e1.RowTotal(1) == e1.Total); Assert.IsTrue(e1.ColumnTotal(0) + e1.ColumnTotal(1) == e1.Total); UncertainValue lnr = e1.Binary.LogOddsRatio; Assert.IsTrue(!lnr.ConfidenceInterval(0.95).ClosedContains(0.0)); UncertainValue r = e1.Binary.OddsRatio; Assert.IsTrue(!r.ConfidenceInterval(0.95).ClosedContains(1.0)); TestResult p = e1.PearsonChiSquaredTest(); Assert.IsTrue(p.Probability < 0.05); TestResult f = e1.Binary.FisherExactTest(); Assert.IsTrue(f.Probability < 0.05); }
public void BinaryContingencyNullTest() { // Create a table with no significant association and test for it. ContingencyTable e0 = CreateExperiment(0.25, 0.33, 0.33, 128); Assert.IsTrue(e0.Total == 128); Assert.IsTrue(e0.RowTotal(0) + e0.RowTotal(1) == e0.Total); Assert.IsTrue(e0.ColumnTotal(0) + e0.ColumnTotal(1) == e0.Total); UncertainValue lnr = e0.Binary.LogOddsRatio; Assert.IsTrue(lnr.ConfidenceInterval(0.95).ClosedContains(0.0)); UncertainValue r = e0.Binary.OddsRatio; Assert.IsTrue(r.ConfidenceInterval(0.95).ClosedContains(1.0)); TestResult p = e0.PearsonChiSquaredTest(); Assert.IsTrue(p.Probability > 0.05); TestResult f = e0.Binary.FisherExactTest(); Assert.IsTrue(f.Probability > 0.05); }
public void DifferentiationTest() { int i = 0; foreach (TestDerivative test in derivatives) { i++; foreach (double x in TestUtilities.GenerateRealValues(0.1, 100.0, 5)) { UncertainValue nd = FunctionMath.Differentiate(test.Function, x); double ed = test.Derivative(x); Console.WriteLine("{0} f'({1}) = {2} = {3}", i, x, nd, ed); Console.WriteLine( //Assert.IsTrue( nd.ConfidenceInterval(0.999).ClosedContains(ed) ); /* * TestUtilities.IsNearlyEqual( * FunctionMath.Differentiate(test.Function, x), * test.Derivative(x), * Math.Pow(2, -42) */ } } }
public void MultivariateLinearRegressionVariances() { // define model y = a + b0 * x0 + b1 * x1 + noise double a = -3.0; double b0 = 2.0; double b1 = -1.0; ContinuousDistribution x0distribution = new LaplaceDistribution(); ContinuousDistribution x1distribution = new CauchyDistribution(); ContinuousDistribution eDistribution = new NormalDistribution(0.0, 4.0); FrameTable data = new FrameTable(); data.AddColumns <double>("a", "da", "b0", "db0", "b1", "db1", "ab1Cov", "p", "dp"); // draw a sample from the model Random rng = new Random(4); for (int j = 0; j < 64; j++) { List <double> x0s = new List <double>(); List <double> x1s = new List <double>(); List <double> ys = new List <double>(); for (int i = 0; i < 16; i++) { double x0 = x0distribution.GetRandomValue(rng); double x1 = x1distribution.GetRandomValue(rng); double e = eDistribution.GetRandomValue(rng); double y = a + b0 * x0 + b1 * x1 + e; x0s.Add(x0); x1s.Add(x1); ys.Add(y); } // do a linear regression fit on the model MultiLinearRegressionResult result = ys.MultiLinearRegression( new Dictionary <string, IReadOnlyList <double> > { { "x0", x0s }, { "x1", x1s } } ); UncertainValue pp = result.Predict(-5.0, 6.0); data.AddRow( result.Intercept.Value, result.Intercept.Uncertainty, result.CoefficientOf("x0").Value, result.CoefficientOf("x0").Uncertainty, result.CoefficientOf("x1").Value, result.CoefficientOf("x1").Uncertainty, result.Parameters.CovarianceOf("Intercept", "x1"), pp.Value, pp.Uncertainty ); } // The estimated parameters should agree with the model that generated the data. // The variances of the estimates should agree with the claimed variances Assert.IsTrue(data["a"].As <double>().PopulationStandardDeviation().ConfidenceInterval(0.99).ClosedContains(data["da"].As <double>().Mean())); Assert.IsTrue(data["b0"].As <double>().PopulationStandardDeviation().ConfidenceInterval(0.99).ClosedContains(data["db0"].As <double>().Mean())); Assert.IsTrue(data["b1"].As <double>().PopulationStandardDeviation().ConfidenceInterval(0.99).ClosedContains(data["db1"].As <double>().Mean())); Assert.IsTrue(data["a"].As <double>().PopulationCovariance(data["b1"].As <double>()).ConfidenceInterval(0.99).ClosedContains(data["ab1Cov"].As <double>().Mean())); Assert.IsTrue(data["p"].As <double>().PopulationStandardDeviation().ConfidenceInterval(0.99).ClosedContains(data["dp"].As <double>().Median())); }
public void ContingencyTableProbabilitiesAndUncertainties() { // start with an underlying population double[,] pp = new double[, ] { { 1.0 / 45.0, 2.0 / 45.0, 3.0 / 45.0 }, { 4.0 / 45.0, 5.0 / 45.0, 6.0 / 45.0 }, { 7.0 / 45.0, 8.0 / 45.0, 9.0 / 45.0 } }; // form 50 contingency tables, each with N = 50 Random rng = new Random(314159); BivariateSample p22s = new BivariateSample(); BivariateSample pr0s = new BivariateSample(); BivariateSample pc1s = new BivariateSample(); BivariateSample pr2c0s = new BivariateSample(); BivariateSample pc1r2s = new BivariateSample(); for (int i = 0; i < 50; i++) { ContingencyTable T = new ContingencyTable(3, 3); for (int j = 0; j < 50; j++) { int r, c; ChooseRandomCell(pp, rng.NextDouble(), out r, out c); T.Increment(r, c); } Assert.IsTrue(T.Total == 50); // for each contingency table, compute estimates of various population quantities UncertainValue p22 = T.ProbabilityOf(2, 2); UncertainValue pr0 = T.ProbabilityOfRow(0); UncertainValue pc1 = T.ProbabilityOfColumn(1); UncertainValue pr2c0 = T.ProbabilityOfRowConditionalOnColumn(2, 0); UncertainValue pc1r2 = T.ProbabilityOfColumnConditionalOnRow(1, 2); p22s.Add(p22.Value, p22.Uncertainty); pr0s.Add(pr0.Value, pr0.Uncertainty); pc1s.Add(pc1.Value, pc1.Uncertainty); pr2c0s.Add(pr2c0.Value, pr2c0.Uncertainty); pc1r2s.Add(pc1r2.Value, pc1r2.Uncertainty); } // the estimated population mean of each probability should include the correct probability in the underlyting distribution Assert.IsTrue(p22s.X.PopulationMean.ConfidenceInterval(0.95).ClosedContains(9.0 / 45.0)); Assert.IsTrue(pr0s.X.PopulationMean.ConfidenceInterval(0.95).ClosedContains(6.0 / 45.0)); Assert.IsTrue(pc1s.X.PopulationMean.ConfidenceInterval(0.95).ClosedContains(15.0 / 45.0)); Assert.IsTrue(pr2c0s.X.PopulationMean.ConfidenceInterval(0.95).ClosedContains(7.0 / 12.0)); Assert.IsTrue(pc1r2s.X.PopulationMean.ConfidenceInterval(0.95).ClosedContains(8.0 / 24.0)); // the estimated uncertainty for each population parameter should be the standard deviation across independent measurements // since the reported uncertainly changes each time, we use the mean value for comparison Assert.IsTrue(p22s.X.PopulationStandardDeviation.ConfidenceInterval(0.95).ClosedContains(p22s.Y.Mean)); Assert.IsTrue(pr0s.X.PopulationStandardDeviation.ConfidenceInterval(0.95).ClosedContains(pr0s.Y.Mean)); Assert.IsTrue(pc1s.X.PopulationStandardDeviation.ConfidenceInterval(0.95).ClosedContains(pc1s.Y.Mean)); Assert.IsTrue(pr2c0s.X.PopulationStandardDeviation.ConfidenceInterval(0.95).ClosedContains(pr2c0s.Y.Mean)); Assert.IsTrue(pc1r2s.X.PopulationStandardDeviation.ConfidenceInterval(0.95).ClosedContains(pc1r2s.Y.Mean)); }
internal MA1FitResult(UncertainValue mu, UncertainValue beta, UncertainValue sigma, IReadOnlyList <double> residuals) : base() { this.mu = mu; this.beta = beta; this.sigma = sigma; this.residuals = residuals; goodnessOfFit = new Lazy <TestResult>(() => residuals.LjungBoxTest()); }
public void UncertainMathSqrtSquare() { UncertainValue sa = UncertainMath.Sqrt(a); Assert.IsTrue(TestUtilities.IsNearlyEqual(sa.RelativeUncertainty, a.RelativeUncertainty / 2.0)); Assert.IsTrue(TestUtilities.IsNearlyEqual(UncertainMath.Pow(sa, 2.0).Value, a.Value)); Assert.IsTrue(TestUtilities.IsNearlyEqual(UncertainMath.Pow(sa, 2.0).Uncertainty, a.Uncertainty)); }
public void UncertainMathSinASin() { UncertainValue y = new UncertainValue(0.5, 0.1); UncertainValue x = UncertainMath.Asin(y); UncertainValue sx = UncertainMath.Sin(x); Assert.IsTrue(TestUtilities.IsNearlyEqual(sx.Value, y.Value)); Assert.IsTrue(TestUtilities.IsNearlyEqual(sx.Uncertainty, y.Uncertainty)); }
public void UncertainMathCosACos() { UncertainValue y = new UncertainValue(0.5, 0.1); UncertainValue x = UncertainMath.Acos(y); UncertainValue cx = UncertainMath.Cos(x); Assert.IsTrue(TestUtilities.IsNearlyEqual(cx.Value, y.Value)); Assert.IsTrue(TestUtilities.IsNearlyEqual(cx.Uncertainty, y.Uncertainty)); }
private static UncertainValue Differentiate(Function<double, double> f, double x, double h, double s) { // choose an initial step size //double h = Math.Abs(x) / 16.0 + Math.Pow(2.0, -16); //double t = x + h; //h = t - x; // keep track of the best value UncertainValue best = new UncertainValue(0.0, Double.MaxValue); // create a tableau int max = 12; double[][] D = new double[max][]; // fill out the tableau for (int j = 0; j < max; j++) { // create new row in the tableau D[j] = new double[j + 1]; //Console.WriteLine(j); // add our next evaluation double fp = f(x + h); double fm = f(x - h); D[j][0] = (fp - fm) / (2.0 * h); //Console.WriteLine(D[j][0]); // fill out the row by extrapolation double e = 1.0; for (int k = 1; k <= j; k++) { e = e / (s*s); D[j][k] = (D[j][k - 1] - e * D[j - 1][k - 1]) / (1 - e); } //Console.WriteLine(D[j][j]); if (j > 0) { double u = Math.Max( Math.Abs(D[j][j] - D[j - 1][j - 1]), Math.Abs(D[j][j] - D[j][j-1]) ); // check for if (u < best.Uncertainty) { best = new UncertainValue(D[j][j], u); } else if (u > 2.0 * best.Uncertainty) { return (best); } } // reduce the step-size for the next iteration h = h / s; } return (best); //throw new NonconvergenceException(); }
public void LinearRegressionVariances() { // do a set of logistic regression fits // make sure not only that the fit parameters are what they should be, but that their variances/covariances are as returned Random rng = new Random(314159); // define line parameters double a0 = 2.0; double b0 = -1.0; // do a lot of fits, recording results of each FrameTable data = new FrameTable(); data.AddColumns <double>("a", "va", "b", "vb", "abCov", "p", "dp"); for (int k = 0; k < 128; k++) { // we should be able to draw x's from any distribution; noise should be drawn from a normal distribution ContinuousDistribution xd = new LogisticDistribution(); ContinuousDistribution nd = new NormalDistribution(0.0, 2.0); // generate a synthetic data set BivariateSample sample = new BivariateSample(); for (int i = 0; i < 12; i++) { double x = xd.GetRandomValue(rng); double y = a0 + b0 * x + nd.GetRandomValue(rng); sample.Add(x, y); } // do the regression LinearRegressionResult result = sample.LinearRegression(); // record result UncertainValue p = result.Predict(12.0); data.AddRow(new Dictionary <string, object>() { { "a", result.Intercept.Value }, { "va", result.Parameters.VarianceOf("Intercept") }, { "b", result.Slope.Value }, { "vb", result.Parameters.VarianceOf("Slope") }, { "abCov", result.Parameters.CovarianceOf("Slope", "Intercept") }, { "p", p.Value }, { "dp", p.Uncertainty } }); } // variances of parameters should agree with predictions Assert.IsTrue(data["a"].As <double>().PopulationVariance().ConfidenceInterval(0.99).ClosedContains(data["va"].As <double>().Median())); Assert.IsTrue(data["b"].As <double>().PopulationVariance().ConfidenceInterval(0.99).ClosedContains(data["vb"].As <double>().Median())); Assert.IsTrue(data["a"].As <double>().PopulationCovariance(data["b"].As <double>()).ConfidenceInterval(0.99).ClosedContains(data["abCov"].As <double>().Median())); // variance of prediction should agree with claim Assert.IsTrue(data["p"].As <double>().PopulationStandardDeviation().ConfidenceInterval(0.99).ClosedContains(data["dp"].As <double>().Median())); }
public static void ContingencyTable() { ContingencyTable <string, bool> contingency = new ContingencyTable <string, bool>( new string[] { "P", "N" }, new bool[] { true, false } ); contingency["P", true] = 35; contingency["P", false] = 65; contingency["N", true] = 4; contingency["N", false] = 896; IReadOnlyList <string> x = new string[] { "N", "P", "N", "N", "P", "N", "N", "N", "P" }; IReadOnlyList <bool> y = new bool[] { false, false, false, true, true, false, false, false, true }; ContingencyTable <string, bool> contingencyFromLists = Bivariate.Crosstabs(x, y); foreach (string row in contingency.Rows) { Console.WriteLine($"Total count of {row}: {contingency.RowTotal(row)}"); } foreach (bool column in contingency.Columns) { Console.WriteLine($"Total count of {column}: {contingency.ColumnTotal(column)}"); } Console.WriteLine($"Total counts: {contingency.Total}"); foreach (string row in contingency.Rows) { UncertainValue probability = contingency.ProbabilityOfRow(row); Console.WriteLine($"Estimated probability of {row}: {probability}"); } foreach (bool column in contingency.Columns) { UncertainValue probability = contingency.ProbabilityOfColumn(column); Console.WriteLine($"Estimated probablity of {column}: {probability}"); } UncertainValue sensitivity = contingency.ProbabilityOfRowConditionalOnColumn("P", true); Console.WriteLine($"Chance of P result given true condition: {sensitivity}"); UncertainValue precision = contingency.ProbabilityOfColumnConditionalOnRow(true, "P"); Console.WriteLine($"Chance of true condition given P result: {precision}"); UncertainValue logOddsRatio = contingency.Binary.LogOddsRatio; Console.WriteLine($"log(r) = {logOddsRatio}"); TestResult pearson = contingency.PearsonChiSquaredTest(); Console.WriteLine($"Pearson χ² = {pearson.Statistic.Value} has P = {pearson.Probability}."); TestResult fisher = contingency.Binary.FisherExactTest(); Console.WriteLine($"Fisher exact test has P = {fisher.Probability}."); }
/// <summary> /// Estimates the derivative of the given function at the given point. /// </summary> /// <param name="f">The function to differentiate.</param> /// <param name="x">The point at which to evaluate the derivative.</param> /// <returns>The estimate, with uncertainty, of the value of the derivative.</returns> public static UncertainValue Differentiate(Function <double, double> f, double x) { if (f == null) { throw new ArgumentNullException("f"); } double step = Math.Pow(2, -4) * Math.Abs(x) + Math.Pow(2, -8); double factor = Math.Pow(2.0, 1.0 / 2.0); UncertainValue v = Differentiate(f, x, step, factor); return(v); }
public void BinaryContingencyTest() { // Create a table with significant association and test for it. ContingencyTable e1 = CreateExperiment(0.50, 0.50, 0.75, 128); Assert.IsTrue(e1.RowTotal(0) + e1.RowTotal(1) == e1.Total); Assert.IsTrue(e1.ColumnTotal(0) + e1.ColumnTotal(1) == e1.Total); UncertainValue lnr = e1.Binary.LogOddsRatio; Assert.IsTrue(!lnr.ConfidenceInterval(0.95).ClosedContains(0.0)); UncertainValue r = e1.Binary.OddsRatio; Assert.IsTrue(!r.ConfidenceInterval(0.95).ClosedContains(1.0)); // Chi square should detect association TestResult p = e1.PearsonChiSquaredTest(); Assert.IsTrue(p.Probability < 0.05); // Fisher exact should detect association TestResult f = e1.Binary.FisherExactTest(); Assert.IsTrue(f.Probability < 0.05); // Phi should be the same as Pearson correlation coefficient List <double> x = new List <double>(); List <double> y = new List <double>(); for (int i = 0; i < e1[0, 0]; i++) { x.Add(0); y.Add(0); } for (int i = 0; i < e1[0, 1]; i++) { x.Add(0); y.Add(1); } for (int i = 0; i < e1[1, 0]; i++) { x.Add(1); y.Add(0); } for (int i = 0; i < e1[1, 1]; i++) { x.Add(1); y.Add(1); } double s = x.CorrelationCoefficient(y); Assert.IsTrue(TestUtilities.IsNearlyEqual(s, e1.Binary.Phi)); }
public void LinearLogisticRegressionVariances() { // define model y = a + b0 * x0 + b1 * x1 + noise double a = -2.0; double b = 1.0; ContinuousDistribution xDistribution = new StudentDistribution(2.0); FrameTable data = new FrameTable(); data.AddColumns <double>("a", "da", "b", "db", "abcov", "p", "dp"); // draw a sample from the model Random rng = new Random(3); for (int j = 0; j < 32; j++) { List <double> xs = new List <double>(); List <bool> ys = new List <bool>(); for (int i = 0; i < 32; i++) { double x = xDistribution.GetRandomValue(rng); double t = a + b * x; double p = 1.0 / (1.0 + Math.Exp(-t)); bool y = (rng.NextDouble() < p); xs.Add(x); ys.Add(y); } // do a linear regression fit on the model LinearLogisticRegressionResult result = ys.LinearLogisticRegression(xs); UncertainValue pp = result.Predict(1.0); data.AddRow( result.Intercept.Value, result.Intercept.Uncertainty, result.Slope.Value, result.Slope.Uncertainty, result.Parameters.CovarianceMatrix[0, 1], pp.Value, pp.Uncertainty ); } // The estimated parameters should agree with the model that generated the data. // The variances of the estimates should agree with the claimed variances Assert.IsTrue(data["a"].As <double>().PopulationStandardDeviation().ConfidenceInterval(0.99).ClosedContains(data["da"].As <double>().Mean())); Assert.IsTrue(data["b"].As <double>().PopulationStandardDeviation().ConfidenceInterval(0.99).ClosedContains(data["db"].As <double>().Mean())); Assert.IsTrue(data["a"].As <double>().PopulationCovariance(data["b"].As <double>()).ConfidenceInterval(0.99).ClosedContains(data["abcov"].As <double>().Mean())); Assert.IsTrue(data["p"].As <double>().PopulationStandardDeviation().ConfidenceInterval(0.99).ClosedContains(data["dp"].As <double>().Mean())); }
public void UncertainValueEquality() { UncertainValue aCopy = a; Assert.IsTrue(a == aCopy); Assert.IsTrue(a.Equals(a)); Assert.IsTrue(a.Equals((object)a)); Assert.IsTrue(a != b); Assert.IsTrue(!a.Equals(b)); Assert.IsTrue(!a.Equals((object)b)); Assert.IsTrue(!a.Equals(null)); Assert.IsTrue(a.GetHashCode() != b.GetHashCode()); }
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 UncertainValueZeroUncertaintiesTest() { UncertainValue p = new UncertainValue(3.141592653, 0.0); UncertainValue q = new UncertainValue(2.718281828, 0.0); UncertainValue sum = p + q; Assert.IsTrue(sum.Value == p.Value + q.Value); Assert.IsTrue(sum.Uncertainty == 0.0); UncertainValue difference = p - q; Assert.IsTrue(difference.Value == p.Value - q.Value); Assert.IsTrue(difference.Uncertainty == 0.0); UncertainValue product = p * q; Assert.IsTrue(product.Value == p.Value * q.Value); Assert.IsTrue(product.Uncertainty == 0.0); UncertainValue quotient = p / q; Assert.IsTrue(quotient.Value == p.Value / q.Value); Assert.IsTrue(quotient.Uncertainty == 0.0); UncertainValue exp = UncertainMath.Exp(p); Assert.IsTrue(exp.Value == Math.Exp(p.Value)); Assert.IsTrue(exp.Uncertainty == 0.0); UncertainValue log = UncertainMath.Log(q); Assert.IsTrue(log.Value == Math.Log(q.Value)); Assert.IsTrue(log.Uncertainty == 0.0); UncertainValue tan = UncertainMath.Tan(q); Assert.IsTrue(TestUtilities.IsNearlyEqual(tan.Value, Math.Tan(q.Value))); Assert.IsTrue(tan.Uncertainty == 0.0); UncertainValue atan = UncertainMath.Atan(q); Assert.IsTrue(TestUtilities.IsNearlyEqual(atan.Value, Math.Atan(q.Value))); Assert.IsTrue(atan.Uncertainty == 0.0); }
public void BinaryContingencyNullTest() { BinaryContingencyTable e0 = CreateExperiment(0.25, 0.33, 0.33, 200); Assert.IsTrue(e0.Total == 200); Assert.IsTrue(e0.RowTotal(0) + e0.RowTotal(1) == e0.Total); Assert.IsTrue(e0.ColumnTotal(0) + e0.ColumnTotal(1) == e0.Total); UncertainValue lnr = e0.LogOddsRatio; Assert.IsTrue(lnr.ConfidenceInterval(0.95).ClosedContains(0.0)); UncertainValue r = e0.OddsRatio; Assert.IsTrue(r.ConfidenceInterval(0.95).ClosedContains(1.0)); TestResult f = e0.FisherExactTest(); Assert.IsTrue(f.RightProbability < 0.95, f.RightProbability.ToString()); }
public static void LinearRegression() { List <double> x = new List <double>() { -1.1, 2.2, 1.4, 0.5, 3.7, 2.8 }; List <double> y = new List <double>() { -2.9, 3.4, 0.9, 0.1, 6.8, 5.7 }; LinearRegressionResult result = y.LinearRegression(x); Console.WriteLine($"y = ({result.Intercept}) + ({result.Slope}) x"); Console.WriteLine($"Fit explains {result.RSquared * 100.0}% of the variance"); Console.WriteLine($"Probability of no dependence {result.R.Probability}."); OneWayAnovaResult anova = result.Anova; Console.WriteLine("Fit dof = {0} SS = {1}", anova.Factor.DegreesOfFreedom, anova.Factor.SumOfSquares); Console.WriteLine("Residual dof = {0} SS = {1}", anova.Residual.DegreesOfFreedom, anova.Residual.SumOfSquares); Console.WriteLine("Total dof = {0} SS = {1}", anova.Total.DegreesOfFreedom, anova.Total.SumOfSquares); Console.WriteLine($"Probability of no dependence {anova.Result.Probability}."); // Print a 95% confidence interval on the slope Console.WriteLine($"slope is in {result.Slope.ConfidenceInterval(0.95)} with 95% confidence"); IReadOnlyList <double> residuals = result.Residuals; ColumnVector parameters = result.Parameters.ValuesVector; SymmetricMatrix covariance = result.Parameters.CovarianceMatrix; result.Parameters.CovarianceOf("Intercept", "Slope"); double x1 = 3.0; UncertainValue y1 = result.Predict(x1); Console.WriteLine($"Predicted y({x1}) = {y1}."); }
public void UncertainValueMixedArithmeticTest() { double b = -2.0; UncertainValue c = a * b; Assert.IsTrue(c.Value == a.Value * b); Assert.IsTrue(c.Uncertainty == a.Uncertainty * Math.Abs(b)); c = a / b; Assert.IsTrue(c.Value == a.Value / b); Assert.IsTrue(c.Uncertainty == a.Uncertainty / Math.Abs(b)); c = a + b; Assert.IsTrue(c.Value == a.Value + b); Assert.IsTrue(c.Uncertainty == a.Uncertainty); c = a - b; Assert.IsTrue(c.Value == a.Value - b); Assert.IsTrue(c.Uncertainty == a.Uncertainty); }
private void ComputeExtrapolation(ref double[] y, out double yNorm, out double yError, ref double[] yp, out double ypNorm, out double ypError) { yNorm = 0.0; yError = 0.0; ypNorm = 0.0; ypError = 0.0; for (int i = 0; i < Dimension; i++) { UncertainValue yEstimate = yExtrapolators[i].Estimate; y[i] = yEstimate.Value; yNorm += MoreMath.Sqr(yEstimate.Value); yError += MoreMath.Sqr(yEstimate.Uncertainty); UncertainValue ypEstimate = ypExtrapolators[i].Estimate; yp[i] = ypEstimate.Value; ypNorm += MoreMath.Sqr(ypEstimate.Value); ypError += MoreMath.Sqr(ypEstimate.Uncertainty); } yNorm = Math.Sqrt(yNorm); yError += Math.Sqrt(yError); ypNorm = Math.Sqrt(ypNorm); ypError += Math.Sqrt(ypError); }
private void Compute() { // we will use these repeatedly double mx = Range.Midpoint; double w2 = Range.Width / 2.0; // evaluate at the points double s1 = 0.0; double s2 = 0.0; for (int i = 0; i < x.Length; i++) { double xx = mx + w2 * x[i]; double fx = Evaluate(xx); s1 += fx * c1[i]; s2 += fx * c2[i]; } double I1 = w2 * s1; double I2 = w2 * s2; estimate = new UncertainValue(I2, Math.Abs(I2 - I1)); }
public void BinaryContingencyTest() { BinaryContingencyTable e1 = CreateExperiment(0.50, 0.50, 0.75, 200); Assert.IsTrue(e1.RowTotal(0) + e1.RowTotal(1) == e1.Total); Assert.IsTrue(e1.ColumnTotal(0) + e1.ColumnTotal(1) == e1.Total); UncertainValue lnr = e1.LogOddsRatio; Assert.IsFalse(lnr.ConfidenceInterval(0.95).ClosedContains(0.0)); UncertainValue r = e1.OddsRatio; Assert.IsFalse(r.ConfidenceInterval(0.95).ClosedContains(1.0)); TestResult p = e1.PearsonChiSquaredTest(); Assert.IsTrue(p.LeftProbability > 0.95, p.RightProbability.ToString()); TestResult f = e1.FisherExactTest(); Assert.IsTrue(f.RightProbability > 0.95); }
private void PerformExtrapolation(out double[] value, out double norm, out double error) { value = new double[extrapolators.Length]; norm = 0.0; error = 0.0; for (int i = 0; i < extrapolators.Length; i++) { UncertainValue estimate = extrapolators[i].Estimate; value[i] = estimate.Value; //norm += MoreMath.Sqr(estimate.Value); //error += MoreMath.Sqr(estimate.Uncertainty); double aValue = Math.Abs(estimate.Value); if (aValue > norm) { norm = aValue; } if (estimate.Uncertainty > error) { error = estimate.Uncertainty; } } //norm = Math.Sqrt(norm); //error = Math.Sqrt(error); }
/// <summary> /// Estimates a multi-dimensional integral using the given evaluation settings. /// </summary> /// <param name="function">The function to be integrated.</param> /// <param name="volume">The volume over which to integrate.</param> /// <param name="settings">The integration settings.</param> /// <returns>A numerical estimate of the multi-dimensional integral.</returns> /// <remarks> /// <para>Note that the integration function must not attempt to modify the argument passed to it.</para> /// <para>Note that the integration volume must be a hyper-rectangle. You can integrate over regions with more complex boundaries by specifying the integration /// volume as a bounding hyper-rectangle that encloses your desired integration region, and returing the value 0 for the integrand outside of the desired integration /// region. For example, to find the volume of a unit d-sphere, you can integrate a function that is 1 inside the unit d-sphere and 0 outside it over the volume /// [-1,1]<sup>d</sup>. You can integrate over infinite volumes by specifing volume endpoints of <see cref="Double.PositiveInfinity"/> and/or /// <see cref="Double.NegativeInfinity"/>. Volumes with dimension greater than 15 are not currently supported.</para> /// <para>Integrals with hard boundaries (like our hyper-sphere volume problem) typically require more evaluations than integrals of smooth functions to achieve the same accuracy. /// Integrals with canceling positive and negative contributions also typically require more evaluations than integtrals of purely positive functions.</para> /// <para>Numerical multi-dimensional integration is computationally expensive. To make problems more tractable, keep in mind some rules of thumb:</para> /// <ul> /// <li>Reduce the required accuracy to the minimum required. Alternatively, if you are willing to wait longer, increase the evaluation budget.</li> /// <li>Exploit symmetries of the problem to reduce the integration volume. For example, to compute the volume of the unit d-sphere, it is better to /// integrate over [0,1]<sup>d</sup> and multiply the result by 2<sup>d</sup> than to simply integrate over [-1,1]<sup>d</sup>.</li> /// <li>Apply analytic techniques to reduce the dimension of the integral. For example, when computing the volume of the unit d-sphere, it is better /// to do a (d-1)-dimesional integral over the function that is the height of the sphere in the dth dimension than to do a d-dimensional integral over the /// indicator function that is 1 inside the sphere and 0 outside it.</li> /// </ul> /// </remarks> /// <exception cref="ArgumentException"><paramref name="function"/>, <paramref name="volume"/>, or <paramref name="settings"/> are null, or /// the dimension of <paramref name="volume"/> is larger than 15.</exception> /// <exception cref="NonconvergenceException">The prescribed accuracy could not be achieved with the given evaluation budget.</exception> public static IntegrationResult Integrate(Func <IList <double>, double> function, IList <Interval> volume, IntegrationSettings settings) { if (function == null) { throw new ArgumentNullException(nameof(function)); } if (volume == null) { throw new ArgumentNullException(nameof(volume)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } // Get the dimension from the box int d = volume.Count; settings = SetMultiIntegrationDefaults(settings, d); // Translate the integration volume, which may be infinite, into a bounding box plus a coordinate transform. Interval[] box = new Interval[d]; CoordinateTransform[] map = new CoordinateTransform[d]; for (int i = 0; i < d; i++) { Interval limits = volume[i]; if (Double.IsInfinity(limits.RightEndpoint)) { if (Double.IsInfinity(limits.LeftEndpoint)) { // -\infinity to +\infinity box[i] = Interval.FromEndpoints(-1.0, +1.0); map[i] = new TangentCoordinateTransform(0.0); } else { // a to +\infinity box[i] = Interval.FromEndpoints(0.0, 1.0); map[i] = new TangentCoordinateTransform(limits.LeftEndpoint); } } else { if (Double.IsInfinity(limits.LeftEndpoint)) { // -\infinity to b box[i] = Interval.FromEndpoints(-1.0, 0.0); map[i] = new TangentCoordinateTransform(limits.RightEndpoint); } else { // a to b box[i] = limits; map[i] = new IdentityCoordinateTransform(); } } } // Use adaptive cubature for small dimensions, Monte-Carlo for large dimensions. MultiFunctor f = new MultiFunctor(function); f.IgnoreInfinity = true; f.IgnoreNaN = true; UncertainValue estimate; if (d < 1) { throw new ArgumentException("The dimension of the integration volume must be at least 1.", "volume"); } else if (d < 4) { IntegrationRegion r = new IntegrationRegion(box); estimate = Integrate_Adaptive(f, map, r, settings); } else if (d < 16) { estimate = Integrate_MonteCarlo(f, map, box, settings); } else { throw new ArgumentException("The dimension of the integrtion volume must be less than 16.", "volume"); } // Sometimes the estimated uncertainty drops precipitiously. We will not report an uncertainty less than 3/4 of that demanded. double minUncertainty = Math.Max(0.75 * settings.AbsolutePrecision, Math.Abs(estimate.Value) * 0.75 * settings.RelativePrecision); if (estimate.Uncertainty < minUncertainty) { estimate = new UncertainValue(estimate.Value, minUncertainty); } return(new IntegrationResult(estimate, f.EvaluationCount, settings)); }
internal IntegrationResult(UncertainValue estimate, int evaluationCount, EvaluationSettings settings) : base(evaluationCount, settings) { this.estimate = estimate; }
internal IntegrationResult(UncertainValue estimate, int evaluationCount, IntegrationSettings settings) : base(evaluationCount) { Debug.Assert(settings != null); this.estimate = estimate; this.settings = settings; }
internal ExponentialFitResult(UncertainValue mu, TestResult goodnessOfFit) : base(new ExponentialDistribution(mu.Value), goodnessOfFit) { this.mu = mu; }
// the drivers private static IntegrationResult Integrate_Adaptive(IAdaptiveIntegrator integrator, EvaluationSettings s) { LinkedList<IAdaptiveIntegrator> list = new LinkedList<IAdaptiveIntegrator>(); list.AddFirst(integrator); int n = integrator.EvaluationCount; while (true) { // go through the intervals, adding estimates (and errors) // and noting which contributes the most error // keep track of the total value and uncertainty UncertainValue vTotal = new UncertainValue(); //double v = 0.0; //double u = 0.0; // keep track of which node contributes the most error LinkedListNode<IAdaptiveIntegrator> maxNode = null; double maxError = 0.0; LinkedListNode<IAdaptiveIntegrator> node = list.First; while (node != null) { IAdaptiveIntegrator i = node.Value; UncertainValue v = i.Estimate; vTotal += v; //UncertainValue uv = i.Estimate; //v += uv.Value; //u += uv.Uncertainty; if (v.Uncertainty > maxError) { maxNode = node; maxError = v.Uncertainty; } node = node.Next; } // if our error is small enough, return if ((vTotal.Uncertainty <= Math.Abs(vTotal.Value) * s.RelativePrecision) || (vTotal.Uncertainty <= s.AbsolutePrecision)) { return (new IntegrationResult(vTotal, n, s)); } //if ((vTotal.Uncertainty <= Math.Abs(vTotal.Value) * s.RelativePrecision) || (vTotal.Uncertainty <= s.AbsolutePrecision)) { // return (new IntegrationResult(vTotal.Value, n)); //} // if our evaluation count is too big, throw if (n > s.EvaluationBudget) throw new NonconvergenceException(); // subdivide the interval with the largest error IEnumerable<IAdaptiveIntegrator> divisions = maxNode.Value.Divide(); foreach (IAdaptiveIntegrator division in divisions) { list.AddBefore(maxNode, division); n += division.EvaluationCount; //v2 += division.Estimate; } list.Remove(maxNode); } }