public WelchResult IsGreater(double[] x, double[] y, Threshold threshold = null) { int n1 = x.Length, n2 = y.Length; if (n1 < 2) { throw new ArgumentException("x should contains at least 2 elements", nameof(x)); } if (n2 < 2) { throw new ArgumentException("y should contains at least 2 elements", nameof(y)); } Moments xm = Moments.Create(x), ym = Moments.Create(y); double v1 = xm.Variance, v2 = ym.Variance, m1 = xm.Mean, m2 = ym.Mean; threshold = threshold ?? RelativeThreshold.Default; double thresholdValue = threshold.GetValue(x); double se = Math.Sqrt(v1 / n1 + v2 / n2); double t = ((m1 - m2) - thresholdValue) / se; double df = (v1 / n1 + v2 / n2).Sqr() / ((v1 / n1).Sqr() / (n1 - 1) + (v2 / n2).Sqr() / (n2 - 1)); double pValue = 1 - StudentDistribution.StudentOneTail(t, df); return(new WelchResult(t, df, pValue, threshold)); }
public void StudentTest2() { // make sure Student t is consistent with its definition // we are going to take a sample that we expect to be t-distributed Sample tSample = new Sample(); // begin with an underlying normal distribution ContinuousDistribution xDistribution = new NormalDistribution(); // compute a bunch of t satistics from the distribution for (int i = 0; i < 100000; i++) { // take a small sample from the underlying distribution // (as the sample gets large, the t distribution becomes normal) Random rng = new Random(314159 + i); double p = xDistribution.InverseLeftProbability(rng.NextDouble()); double q = 0.0; for (int j = 0; j < 5; j++) { double x = xDistribution.InverseLeftProbability(rng.NextDouble()); q += x * x; } q = q / 5; double t = p / Math.Sqrt(q); tSample.Add(t); } ContinuousDistribution tDistribution = new StudentDistribution(5); TestResult result = tSample.KolmogorovSmirnovTest(tDistribution); Console.WriteLine(result.LeftProbability); }
/// <summary> /// Calculates Z value (z-star) for confidence interval /// </summary> /// <param name="level">ConfidenceLevel for a confidence interval</param> /// <param name="n">Sample size (n >= 3)</param> public static double GetZValue(this ConfidenceLevel level, int n) { if (n <= 1) { throw new ArgumentOutOfRangeException(nameof(n), "n should be >= 2"); } return(StudentDistribution.InverseTwoTailStudent(1 - level.ToPercent(), n - 1)); }
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 Cdf() { var x = new[] { -2, -1, -0.5, 0, 0.5, 1, 2 }; var expectedCdf = new[] { 0.0696629842794216, 0.195501109477885, 0.325723982424076, 0.5, 0.674276017575924, 0.804498890522115, 0.930337015720578 }; for (int i = 0; i < x.Length; i++) { double actualCdf = new StudentDistribution(3).Cdf(x[i]); Assert.Equal(expectedCdf[i], actualCdf, 5); } }
public void CauchyStudentAgreement() { StudentDistribution S = new StudentDistribution(1); CauchyDistribution C = new CauchyDistribution(); // don't compare moments directly, because NaN != NaN foreach (double P in probabilities) { double xS = S.InverseLeftProbability(P); double xC = C.InverseLeftProbability(P); Console.WriteLine("{0} {1} {2}", P, xS, xC); Assert.IsTrue(TestUtilities.IsNearlyEqual(xS, xC)); Assert.IsTrue(TestUtilities.IsNearlyEqual(S.ProbabilityDensity(xS), C.ProbabilityDensity(xC))); } }
public void Quantile() { var x = new[] { 0.1, 0.25, 0.5, 0.75, 0.9 }; var expected = new[] { -1.63774435369621, -0.764892328404345, 0, 0.764892328404345, 1.63774435369621 }; for (int i = 0; i < x.Length; i++) { double actual = new StudentDistribution(3).Quantile(x[i]); Assert.Equal(expected[i], actual, 5); } }
/// <summary> /// Determines whether the sample mean is different from a known mean /// </summary> /// <remarks>Should be consistent with t.test(x, mu = mu, alternative = "greater") from R </remarks> public OneSidedTestResult IsGreater(double[] sample, double value, Threshold threshold = null) { var moments = Moments.Create(sample); double mean = moments.Mean; double stdDev = moments.StandardDeviation; double n = sample.Length; double df = n - 1; threshold = threshold ?? RelativeThreshold.Default; double t = (mean - value) / (stdDev / Math.Sqrt(n)); double pValue = 1 - StudentDistribution.StudentOneTail(t, df); return(new OneSidedTestResult(pValue, threshold)); }
public void StudentTest() { // make sure Student t is consistent with its definition // we are going to take a sample that we expect to be t-distributed Sample tSample = new Sample(); // begin with an underlying normal distribution ContinuousDistribution xDistribution = new NormalDistribution(1.0, 2.0); // compute a bunch of t satistics from the distribution for (int i = 0; i < 200000; i++) { // take a small sample from the underlying distribution // (as the sample gets large, the t distribution becomes normal) Random rng = new Random(i); Sample xSample = new Sample(); for (int j = 0; j < 5; j++) { double x = xDistribution.InverseLeftProbability(rng.NextDouble()); xSample.Add(x); } // compute t for the sample double t = (xSample.Mean - xDistribution.Mean) / (xSample.PopulationStandardDeviation.Value / Math.Sqrt(xSample.Count)); tSample.Add(t); //Console.WriteLine(t); } // t's should be t-distrubuted; use a KS test to check this ContinuousDistribution tDistribution = new StudentDistribution(4); TestResult result = tSample.KolmogorovSmirnovTest(tDistribution); Console.WriteLine(result.LeftProbability); //Assert.IsTrue(result.LeftProbability < 0.95); // t's should be demonstrably not normally distributed Console.WriteLine(tSample.KolmogorovSmirnovTest(new NormalDistribution()).LeftProbability); //Assert.IsTrue(tSample.KolmogorovSmirnovTest(new NormalDistribution()).LeftProbability > 0.95); }
public void StudentFromNormal() { // make sure Student t is consistent with its definition // we are going to take a sample that we expect to be t-distributed Sample tSample = new Sample(); // begin with an underlying normal distribution ContinuousDistribution xDistribution = new NormalDistribution(); // compute a bunch of t statistics from the distribution Random rng = new Random(314159); for (int i = 0; i < 10000; i++) { double p = xDistribution.GetRandomValue(rng); double q = 0.0; for (int j = 0; j < 5; j++) { double x = xDistribution.GetRandomValue(rng); q += x * x; } q = q / 5; double t = p / Math.Sqrt(q); tSample.Add(t); } ContinuousDistribution tDistribution = new StudentDistribution(5); TestResult tResult = tSample.KolmogorovSmirnovTest(tDistribution); Assert.IsTrue(tResult.Probability > 0.05); // Distribution should be demonstrably non-normal ContinuousDistribution zDistribution = new NormalDistribution(tDistribution.Mean, tDistribution.StandardDeviation); TestResult zResult = tSample.KolmogorovSmirnovTest(zDistribution); Assert.IsTrue(zResult.Probability < 0.05); }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(StudentDistribution obj) { return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr; }
public void StudentOneTailTest(double t, double n, double expected) { double actual = StudentDistribution.StudentOneTail(t, n); Assert.Equal(expected, actual, 4); }
private double GetZLevel(double confidenceLevel) => StudentDistribution.InverseTwoTailStudent(1 - confidenceLevel, DegreeOfFreedom);
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(StudentDistribution obj) { return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr); }