public static bool OneTailNormalHypoTest(double[] data, double mu, double sigma, int n, double signLevel = 0.05, AlternateHypo tail = AlternateHypo.GreaterThan) { try { double?mean = Mean(data); if (mean != null) { return(OneTailNormalHypoTest((double)mean, mu, sigma, n, signLevel, tail)); } else { throw new InvalidOperationException(); } } catch (InvalidOperationException) { return(true); } catch { throw; } }
/// <summary> /// This will perform a binomial hypothesis test. /// </summary> /// <param name="sampleSuccess">How many were successful</param> /// <param name="n"></param> /// <param name="p"></param> /// <param name="tail">Perform test on upper or lower tail</param> /// <param name="signLevel">Significance level.</param> /// <returns>True means that there is enough evidence to reject the null hypothesis.</returns> public static bool HypoTest(int sampleSuccess, int n, double p, AlternateHypo tail, double signLevel) { try { int[] x = Enumerable.Range(0, n + 1).ToArray(); BinomialDistribution distribution = new BinomialDistribution(n, p); switch (tail) { case AlternateHypo.LessThan: { int criticalRegion = x.AsParallel() .Where(num => distribution.DistributionFunction(num) < signLevel) .OrderBy(num => num) .First(); if (sampleSuccess <= criticalRegion) { return(true); } else { return(false); } } default: { int criticalRegion = x.AsParallel() .Where(num => (1 - distribution.DistributionFunction(num)) < signLevel) .OrderBy(num => num) .First(); if (sampleSuccess >= criticalRegion) { return(true); } else { return(false); } } } } catch (InvalidOperationException) { return(false); //Because there is no evidence at all. } catch { throw; } }
public static bool OneTailNormalHypoTest(double meanOfSample, double mu, double sigma, int n, double signLevel = 0.05, AlternateHypo tail = AlternateHypo.GreaterThan) { try { switch (tail) { case AlternateHypo.GreaterThan: double upperTail = signLevel; double upperCritRegion = new NormalDistribution(mu, sigma / Math.Sqrt(n)).InverseDistributionFunction(upperTail); if (meanOfSample > upperCritRegion) { return(true); } else { return(false); } default: double lowerTail = signLevel; double lowerCritRegion = new NormalDistribution(mu, sigma / Math.Sqrt(n)).InverseDistributionFunction(lowerTail); if (meanOfSample < lowerCritRegion) { return(true); } else { return(false); } } } catch (InvalidOperationException) { return(true); } catch { throw; } }