本文整理汇总了C#中Sample.Add方法的典型用法代码示例。如果您正苦于以下问题:C# Sample.Add方法的具体用法?C# Sample.Add怎么用?C# Sample.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Sample
的用法示例。
在下文中一共展示了Sample.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CookieJar_Example1
public void CookieJar_Example1(int total, int choc1, int choc2, int expectedNumerator, int expectedDenominator)
{
var sample1 = new Sample<Cookie>("Jar1");
var sample2 = new Sample<Cookie>("Jar2");
var hypos = new HypoSet<Cookie>("All");
hypos.Add(sample1, sample2);
sample1.Add(total - choc1, n => new Cookie() { F = 'V' });
sample1.Add(choc1, n => new Cookie() { F = 'C' });
sample2.Add(total - choc2, n => new Cookie() { F = 'V' });
sample2.Add(choc2, n => new Cookie() { F = 'C' });
var choc = It.IsAny<Cookie>(c => c.F == 'C');
var vani = It.IsAny<Cookie>(c => c.F == 'V');
sample1.ProbabilityOfEvent(choc);
sample2.ProbabilityOfEvent(choc);
var postProb = hypos.PosterierProbability(sample1, vani);
Assert.That(postProb.Numerator, Is.EqualTo(expectedNumerator));
Assert.That(postProb.Denominator, Is.EqualTo(expectedDenominator));
}
示例2: KendallNullDistributionTest
public void KendallNullDistributionTest()
{
// pick independent distributions for x and y, which needn't be normal and needn't be related
Distribution xDistrubtion = new LogisticDistribution();
Distribution yDistribution = new ExponentialDistribution();
Random rng = new Random(314159265);
// generate bivariate samples of various sizes
//int n = 64; {
foreach (int n in TestUtilities.GenerateIntegerValues(4, 64, 8)) {
Sample testStatistics = new Sample();
Distribution testDistribution = null;
for (int i = 0; i < 128; i++) {
BivariateSample sample = new BivariateSample();
for (int j = 0; j < n; j++) {
sample.Add(xDistrubtion.GetRandomValue(rng), yDistribution.GetRandomValue(rng));
}
TestResult result = sample.KendallTauTest();
testStatistics.Add(result.Statistic);
testDistribution = result.Distribution;
}
//TestResult r2 = testStatistics.KolmogorovSmirnovTest(testDistribution);
//Console.WriteLine("n={0} P={1}", n, r2.LeftProbability);
//Assert.IsTrue(r2.RightProbability > 0.05);
Console.WriteLine("{0} {1}", testStatistics.PopulationVariance, testDistribution.Variance);
Assert.IsTrue(testStatistics.PopulationMean.ConfidenceInterval(0.95).ClosedContains(testDistribution.Mean));
Assert.IsTrue(testStatistics.PopulationVariance.ConfidenceInterval(0.95).ClosedContains(testDistribution.Variance));
}
}
示例3: AnovaDistribution
public void AnovaDistribution()
{
Distribution sDistribution = new NormalDistribution();
Random rng = new Random(1);
Sample fSample = new Sample();
// do 100 ANOVAs
for (int t = 0; t < 100; t++) {
// each ANOVA has 4 groups
List<Sample> groups = new List<Sample>();
for (int g = 0; g < 4; g++) {
// each group has 3 data points
Sample group = new Sample();
for (int i = 0; i < 3; i++) {
group.Add(sDistribution.GetRandomValue(rng));
}
groups.Add(group);
}
OneWayAnovaResult result = Sample.OneWayAnovaTest(groups);
fSample.Add(result.Factor.Result.Statistic);
}
// compare the distribution of F statistics to the expected distribution
Distribution fDistribution = new FisherDistribution(3, 8);
Console.WriteLine("m={0} s={1}", fSample.PopulationMean, fSample.PopulationStandardDeviation);
TestResult kResult = fSample.KolmogorovSmirnovTest(fDistribution);
Console.WriteLine(kResult.LeftProbability);
Assert.IsTrue(kResult.LeftProbability < 0.95);
}
示例4: CreateSample
public static Sample CreateSample(Distribution distribution, int count, int seed)
{
Sample sample = new Sample();
Random rng = new Random(seed);
for (int i = 0; i < count; i++) {
double x = distribution.GetRandomValue(rng);
sample.Add(x);
}
return (sample);
}
示例5: BivariateNullAssociation
public void BivariateNullAssociation()
{
Random rng = new Random(314159265);
// Create sample sets for our three test statisics
Sample PS = new Sample();
Sample SS = new Sample();
Sample KS = new Sample();
// variables to hold the claimed distribution of teach test statistic
Distribution PD = null;
Distribution SD = null;
Distribution KD = null;
// generate a large number of bivariate samples and conduct our three tests on each
for (int j = 0; j < 100; j++) {
BivariateSample S = new BivariateSample();
// sample size should be large so that asymptotic assumptions are justified
for (int i = 0; i < 100; i++) {
double x = rng.NextDouble();
double y = rng.NextDouble();
S.Add(x, y);
}
TestResult PR = S.PearsonRTest();
PS.Add(PR.Statistic);
PD = PR.Distribution;
TestResult SR = S.SpearmanRhoTest();
SS.Add(SR.Statistic);
SD = SR.Distribution;
TestResult KR = S.KendallTauTest();
KS.Add(KR.Statistic);
KD = KR.Distribution;
}
// do KS to test whether the samples follow the claimed distributions
//Console.WriteLine(PS.KolmogorovSmirnovTest(PD).LeftProbability);
//Console.WriteLine(SS.KolmogorovSmirnovTest(SD).LeftProbability);
//Console.WriteLine(KS.KolmogorovSmirnovTest(KD).LeftProbability);
Assert.IsTrue(PS.KolmogorovSmirnovTest(PD).LeftProbability < 0.95);
Assert.IsTrue(SS.KolmogorovSmirnovTest(SD).LeftProbability < 0.95);
Assert.IsTrue(KS.KolmogorovSmirnovTest(KD).LeftProbability < 0.95);
}
示例6: KolmogorovNullDistributionTest
public void KolmogorovNullDistributionTest()
{
// The distribution is irrelevent; pick one at random
Distribution sampleDistribution = new LognormalDistribution();
// Loop over various sample sizes
foreach (int n in TestUtilities.GenerateIntegerValues(2, 128, 16)) {
// Create a sample to hold the KS statistics
Sample testStatistics = new Sample();
// and a variable to hold the claimed null distribution, which should be the same for each test
Distribution nullDistribution = null;
// Create a bunch of samples, each with n data points
for (int i = 0; i < 256; i++) {
// Just use n+i as a seed in order to get different points each time
Sample sample = TestUtilities.CreateSample(sampleDistribution, n, 512 * n + i + 1);
// Do a KS test of the sample against the distribution each time
TestResult r1 = sample.KolmogorovSmirnovTest(sampleDistribution);
// Record the test statistic value and the claimed null distribution
testStatistics.Add(r1.Statistic);
nullDistribution = r1.Distribution;
}
// Do a Kuiper test of our sample of KS statistics against the claimed null distribution
// We could use a KS test here instead, which would be way cool and meta, but we picked Kuiper instead for variety
TestResult r2 = testStatistics.KuiperTest(nullDistribution);
Console.WriteLine("{0} {1} {2}", n, r2.Statistic, r2.LeftProbability);
Assert.IsTrue(r2.RightProbability > 0.05);
// Test moment matches, too
Console.WriteLine(" {0} {1}", testStatistics.PopulationMean, nullDistribution.Mean);
Console.WriteLine(" {0} {1}", testStatistics.PopulationVariance, nullDistribution.Variance);
Assert.IsTrue(testStatistics.PopulationMean.ConfidenceInterval(0.99).ClosedContains(nullDistribution.Mean));
Assert.IsTrue(testStatistics.PopulationVariance.ConfidenceInterval(0.99).ClosedContains(nullDistribution.Variance));
}
}
示例7: FisherTest
public void FisherTest()
{
// we will need a RNG
Random rng = new Random(314159);
int n1 = 1;
int n2 = 2;
// define chi squared distributions
Distribution d1 = new ChiSquaredDistribution(n1);
Distribution d2 = new ChiSquaredDistribution(n2);
// create a sample of chi-squared variates
Sample s = new Sample();
for (int i = 0; i < 250; i++) {
double x1 = d1.InverseLeftProbability(rng.NextDouble());
double x2 = d2.InverseLeftProbability(rng.NextDouble());
double x = (x1/n1) / (x2/n2);
s.Add(x);
}
// it should match a Fisher distribution with the appropriate parameters
Distribution f0 = new FisherDistribution(n1, n2);
TestResult t0 = s.KuiperTest(f0);
Console.WriteLine(t0.LeftProbability);
Assert.IsTrue(t0.LeftProbability < 0.95);
// it should be distinguished from a Fisher distribution with different parameters
Distribution f1 = new FisherDistribution(n1 + 1, n2);
TestResult t1 = s.KuiperTest(f1);
Console.WriteLine(t1.LeftProbability);
Assert.IsTrue(t1.LeftProbability > 0.95);
}
示例8: SampleMedian
public void SampleMedian()
{
Sample sample = new Sample();
sample.Add(2.0, 1.0);
Assert.IsTrue(sample.Minimum == 1.0);
Assert.IsTrue(sample.Median == 1.5);
Assert.IsTrue(sample.Maximum == 2.0);
sample.Add(3.0);
Assert.IsTrue(sample.Minimum == 1.0);
Assert.IsTrue(sample.Median == 2.0);
Assert.IsTrue(sample.Maximum == 3.0);
}
示例9: AnovaTest
public void AnovaTest()
{
Sample A = new Sample();
A.Add(new double[] { 25, 30, 20, 32 });
Sample B = new Sample();
B.Add(new double[] { 30, 33, 29, 40, 36 });
Sample C = new Sample();
C.Add(new double[] { 32, 39, 35, 41, 44 });
OneWayAnovaResult result = Sample.OneWayAnovaTest(A, B, C);
Assert.IsTrue(TestUtilities.IsNearlyEqual(result.Factor.SumOfSquares + result.Residual.SumOfSquares, result.Total.SumOfSquares));
Assert.IsTrue(result.Factor.DegreesOfFreedom + result.Residual.DegreesOfFreedom == result.Total.DegreesOfFreedom);
Assert.IsTrue(result.Total.DegreesOfFreedom == A.Count + B.Count + C.Count - 1);
Assert.IsTrue(result.Result.Statistic == result.Factor.Result.Statistic);
}
示例10: SampleManipulations
public void SampleManipulations()
{
// create a sample
double[] data = new double[] { -1.1, 2.2, -3.3, 4.4 };
Sample sample = new Sample(data);
// check the length
Assert.IsTrue(sample.Count == data.Length);
// add a datum and check the length
sample.Add(5.5);
Assert.IsTrue(sample.Count == data.Length + 1);
// check wether an elements exists, remove it, check the length, check that it doesn't exist
Assert.IsTrue(sample.Contains(2.2));
Assert.IsTrue(sample.Remove(2.2));
Assert.IsTrue(sample.Count == data.Length);
Assert.IsFalse(sample.Contains(2.2));
Assert.IsFalse(sample.Remove(2.2));
// clear the sample and check the length
sample.Clear();
Assert.IsTrue(sample.Count == 0);
}
示例11: TwoSampleKolmogorovNullDistributionTest
public void TwoSampleKolmogorovNullDistributionTest()
{
Distribution population = new ExponentialDistribution();
int[] sizes = new int[] { 23, 30, 175 };
foreach (int na in sizes) {
foreach (int nb in sizes) {
Console.WriteLine("{0} {1}", na, nb);
Sample d = new Sample();
Distribution nullDistribution = null;
for (int i = 0; i < 128; i++) {
Sample a = TestUtilities.CreateSample(population, na, 31415 + na + i);
Sample b = TestUtilities.CreateSample(population, nb, 27182 + nb + i);
TestResult r = Sample.KolmogorovSmirnovTest(a, b);
d.Add(r.Statistic);
nullDistribution = r.Distribution;
}
// Only do full KS test if the number of bins is larger than the sample size, otherwise we are going to fail
// because the KS test detects the granularity of the distribution
TestResult mr = d.KolmogorovSmirnovTest(nullDistribution);
Console.WriteLine(mr.LeftProbability);
if (AdvancedIntegerMath.LCM(na, nb) > d.Count) Assert.IsTrue(mr.LeftProbability < 0.99);
// But always test that mean and standard deviation are as expected
Console.WriteLine("{0} {1}", nullDistribution.Mean, d.PopulationMean.ConfidenceInterval(0.99));
Assert.IsTrue(d.PopulationMean.ConfidenceInterval(0.99).ClosedContains(nullDistribution.Mean));
Console.WriteLine("{0} {1}", nullDistribution.StandardDeviation, d.PopulationStandardDeviation.ConfidenceInterval(0.99));
Assert.IsTrue(d.PopulationStandardDeviation.ConfidenceInterval(0.99).ClosedContains(nullDistribution.StandardDeviation));
Console.WriteLine("{0} {1}", nullDistribution.MomentAboutMean(3), d.PopulationMomentAboutMean(3).ConfidenceInterval(0.99));
//Assert.IsTrue(d.PopulationMomentAboutMean(3).ConfidenceInterval(0.99).ClosedContains(nullDistribution.MomentAboutMean(3)));
//Console.WriteLine("m {0} {1}", nullDistribution.Mean, d.PopulationMean);
}
}
}
示例12: StudentTest
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
Distribution 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
Distribution 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);
}
示例13: UniformOrderStatistics
public void UniformOrderStatistics()
{
// Check that the order statistics of the uniform distribution are distributed as expected.
Random rng = new Random(1);
UniformDistribution u = new UniformDistribution();
Sample maxima = new Sample();
Sample minima = new Sample();
for (int i = 0; i < 100; i++) {
double maximum = 0.0;
double minimum = 1.0;
for (int j = 0; j < 4; j++) {
double value = u.GetRandomValue(rng);
if (value > maximum) maximum = value;
if (value < minimum) minimum = value;
}
maxima.Add(maximum);
minima.Add(minimum);
}
// maxima should be distributed according to Beta(n,1)
TestResult maxTest = maxima.KolmogorovSmirnovTest(new BetaDistribution(4, 1));
Assert.IsTrue(maxTest.LeftProbability < 0.95);
// minima should be distributed according to Beta(1,n)
TestResult minTest = minima.KolmogorovSmirnovTest(new BetaDistribution(1, 4));
Assert.IsTrue(minTest.LeftProbability < 0.95);
}
示例14: ExponentialFitUncertainty
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
Distribution 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 < 50; i++) {
Sample sample = CreateSample(distribution, 10, i);
FitResult fit = ExponentialDistribution.FitToSample(sample);
UncertainValue lambda = fit.Parameter(0);
values.Add(lambda.Value);
uncertainties.Add(lambda.Uncertainty);
}
Console.WriteLine(uncertainties.Mean);
Console.WriteLine(values.PopulationStandardDeviation);
// the reported errors should agree with the standard deviation of the reported parameters
Assert.IsTrue(values.PopulationStandardDeviation.ConfidenceInterval(0.95).ClosedContains(uncertainties.Mean));
}
示例15: ZTestDistribution
public void ZTestDistribution()
{
Random rng = new Random(1);
// define the sampling population (which must be normal for a z-test)
Distribution population = new NormalDistribution(2.0, 3.0);
// collect 100 samples
Sample zSample = new Sample();
for (int i = 0; i < 100; i++) {
// each z-statistic is formed by making a 4-count sample from a normal distribution
Sample sample = new Sample();
for (int j = 0; j < 4; j++) {
sample.Add(population.GetRandomValue(rng));
}
// for each sample, do a z-test against the population
TestResult zResult = sample.ZTest(population.Mean, population.StandardDeviation);
zSample.Add(zResult.Statistic);
}
// the z's should be distrubuted normally
TestResult result = zSample.KolmogorovSmirnovTest(new NormalDistribution());
Console.WriteLine("{0} {1}", result.Statistic, result.LeftProbability);
Assert.IsTrue((result.LeftProbability > 0.05) && (result.LeftProbability < 0.95));
}