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)); }
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); }
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)); } }
public void Add_SampleValuesShouldCalculate() { // Arrange int expected = 5; // Act int actual = sample.Add(3, 2); // Assert Assert.Equal(expected, actual); }
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); }
private string UporediUzorke(List <int> trenutnaGeneracija, List <int> prosleGeneracije) { Sample sample1 = new Sample(); Sample sample2 = new Sample(); string poruka = ""; bool sporiji; foreach (int i in trenutnaGeneracija) { sample1.Add(i); } foreach (int i in prosleGeneracije) { sample2.Add(i); } if (trenutnaGeneracija.Count != 0 && prosleGeneracije.Count != 0) { TestResult tTest = Sample.StudentTTest(sample1, sample2); double mi1 = sample1.Mean; double mi2 = sample2.Mean; double mi = 0; //koristice se kao predlog za novu vrednost vremenskog slota,tj koliko put atreba smanjiti ili povacati slot if (tTest.Probability < 0.05) { if (tTest.Statistic.Value > 0) { sporiji = true; mi = mi1 / mi2; poruka += $"Trenutna generacija je sporija od proslih {mi:N2} puta, u pogledu na srednju vrednost zadrzavanja."; } else { sporiji = false; mi = mi2 / mi1; poruka += $"Trenutna generacija je brza od proslih {mi:N2} puta, u pogledu na srednju vrednost zadrzavanja."; } } if (poruka == "") { poruka += "Generacija se ponasa ocekivano."; } } else { poruka = "Nema podataka za trazenu statistiku!"; } return(poruka); }
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); }
private string PredloziSlot(List <int> prosleGeneracije) { Sample sample = new Sample(); string poruka = ""; foreach (int i in prosleGeneracije) { sample.Add(i); } double mi = sample.Mean; double odVredost = mi / 8; double doVrednost = mi / 4; poruka += $"Preporucuje se vremenski slot u vrednosti od {odVredost:N0} do {doVrednost:N0} minuta."; return(poruka); }
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)); } }
public override async Task Update() { await Task.Run(() => { lock (_pc) { var cpu = _pc.Hardware.FirstOrDefault(x => x.HardwareType == HardwareType.CPU); cpu?.Update(); var load = cpu?.Sensors.FirstOrDefault(x => x.SensorType == SensorType.Load && x.Name == CPULoadKey)?.Value; var temperature = cpu?.Sensors.FirstOrDefault(x => x.SensorType == SensorType.Temperature && x.Name == CPUTemperatureKey)?.Value; if (load.HasValue) { _load.Add(load.Value); } if (temperature.HasValue) { _temperature.Add(temperature.Value); } } }); }
public override async Task Update() { await Task.Run(() => { lock (_pc) { var gpu = _pc.Hardware.FirstOrDefault(x => x.HardwareType == HardwareType.GpuNvidia || x.HardwareType == HardwareType.GpuAti); gpu?.Update(); var load = gpu?.Sensors.FirstOrDefault(x => x.SensorType == SensorType.Load && x.Name == GPUCoreKey)?.Value; var temperature = gpu?.Sensors.FirstOrDefault(x => x.SensorType == SensorType.Temperature && x.Name == GPUCoreKey)?.Value; if (load.HasValue) { _load.Add(load.Value); } if (temperature.HasValue) { _temperature.Add(temperature.Value); } } }); }
public void SampleCopy() { // test independency of copy Sample sample1 = new Sample(); sample1.Add(1.0, 2.0); Sample sample2 = sample1.Copy(); sample2.Add(3.0, 4.0); Assert.IsTrue(sample1.Count == 2); Assert.IsTrue(sample2.Count == 4); }
public void LowSampleMoments() { // this is designed to test that means and standard deviations change properly when values // are added and removed Sample s = new Sample(); s.Add(2.0); s.Add(4.0); s.Add(6.0); // set is 2, 4, 6: M = 4, V = (2^2 + 2^2) / 3 Assert.IsTrue(s.Count == 3); Assert.IsTrue(TestUtilities.IsNearlyEqual(s.Mean, 4.0)); Assert.IsTrue(TestUtilities.IsNearlyEqual(s.Variance, 8.0 / 3.0)); // set is 4, 6: M = 5, V = (1^2 + 1^2) / 2 s.Remove(2.0); Assert.IsTrue(s.Count == 2); Assert.IsTrue(TestUtilities.IsNearlyEqual(s.Mean, 5.0)); Assert.IsTrue(TestUtilities.IsNearlyEqual(s.Variance, 1.0)); }
public void BivariateLinearRegression() { // 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 logistic parameters double a0 = 2.0; double b0 = -1.0; // keep track of sample of returned a and b fit parameters BivariateSample ps = new BivariateSample(); // also keep track of returned covariance estimates // since these vary slightly from fit to fit, we will average them double caa = 0.0; double cbb = 0.0; double cab = 0.0; // also keep track of test statistics Sample fs = new Sample(); // do 100 fits for (int k = 0; k < 100; k++) { // we should be able to draw x's from any distribution; noise should be drawn from a normal distribution Distribution xd = new LogisticDistribution(); Distribution nd = new NormalDistribution(0.0, 2.0); // generate a synthetic data set BivariateSample s = new BivariateSample(); for (int i = 0; i < 25; i++) { double x = xd.GetRandomValue(rng); double y = a0 + b0 * x + nd.GetRandomValue(rng); s.Add(x, y); } // do the regression FitResult r = s.LinearRegression(); // record best fit parameters double a = r.Parameter(0).Value; double b = r.Parameter(1).Value; ps.Add(a, b); // record estimated covariances caa += r.Covariance(0, 0); cbb += r.Covariance(1, 1); cab += r.Covariance(0, 1); // record the fit statistic fs.Add(r.GoodnessOfFit.Statistic); Console.WriteLine("F={0}", r.GoodnessOfFit.Statistic); } caa /= ps.Count; cbb /= ps.Count; cab /= ps.Count; // check that mean parameter estimates are what they should be: the underlying population parameters Assert.IsTrue(ps.X.PopulationMean.ConfidenceInterval(0.95).ClosedContains(a0)); Assert.IsTrue(ps.Y.PopulationMean.ConfidenceInterval(0.95).ClosedContains(b0)); Console.WriteLine("{0} {1}", caa, ps.X.PopulationVariance); Console.WriteLine("{0} {1}", cbb, ps.Y.PopulationVariance); // check that parameter covarainces are what they should be: the reported covariance estimates Assert.IsTrue(ps.X.PopulationVariance.ConfidenceInterval(0.95).ClosedContains(caa)); Assert.IsTrue(ps.Y.PopulationVariance.ConfidenceInterval(0.95).ClosedContains(cbb)); Assert.IsTrue(ps.PopulationCovariance.ConfidenceInterval(0.95).ClosedContains(cab)); // check that F is distributed as it should be Console.WriteLine(fs.KolmogorovSmirnovTest(new FisherDistribution(2, 48)).LeftProbability); }
static TransferDataSong ReadAndProcess(AudioFileReader reader, string SongPath) { FourierTransformer ft = new FourierTransformer(_FrameSize); Complex[] temp = new Complex[_FrameSize]; double[] Amp = new double[_FrameSize]; double[] F = new double[_FrameSize]; double[] sBass = LinSpace(16, 60, _NumberOfFreqIntervals).ToArray(); double[] Bass = LinSpace(60, 250, _NumberOfFreqIntervals).ToArray(); double[] Mid = LinSpace(250, 2000, _NumberOfFreqIntervals).ToArray(); double[] HighMid = LinSpace(2000, 6000, _NumberOfFreqIntervals).ToArray(); double[] High = LinSpace(6000, 15000, _NumberOfFreqIntervals).ToArray(); double[] MainFreqs = sBass.Concat(Bass).Concat(Mid).Concat(HighMid).Concat(High).Distinct().ToArray(); double[] MeanFreqs = new double[MainFreqs.Length - 1]; double[] MedianFreqs = new double[MainFreqs.Length - 1]; double[] StdFreqs = new double[MainFreqs.Length - 1]; Sample FreqSample = new Sample(); double sum; for (int i = 0; i < _FrameSize; i++) { F[i] = (double)i / (_FrameSize / 2 + 1) * reader.WaveFormat.SampleRate / 2; } int[] FreqIndRange = new int[MainFreqs.Length]; for (int i = 0; i < MainFreqs.Length; i++) { int j = 0; while (F[j] < MainFreqs[i]) { j++; } FreqIndRange[i] = j; } // Create mediumsize array for reading from stream int sampleCount = Math.Min(Convert.ToInt32(reader.Length / sizeof(float) / _NumberOfFrames), Convert.ToInt32(reader.WaveFormat.SampleRate * _TimeToProcess * 2 / _NumberOfFrames)); if (sampleCount < _FrameSize * 2) { sampleCount = _FrameSize * 2; } float[] buffer = new float[sampleCount]; for (int m = 0; m < _NumberOfFrames; m++) { reader.Read(buffer, 0, sampleCount); for (int k = 0; k < temp.Length; k++) { temp[k] = buffer[2 * k]; } Complex[] FFTresults = ft.Transform(temp); for (int i = 0; i < _FrameSize; i++) { Amp[i] = Meta.Numerics.ComplexMath.Abs(FFTresults[i]); } sum = Amp.Sum(); Amp = Amp.Select(x => x / sum).ToArray(); for (int s = 0; s < MainFreqs.Length - 1; s++) { FreqSample.Clear(); double[] PartAmp = new double[FreqIndRange[s + 1] - FreqIndRange[s]]; Array.Copy(Amp, FreqIndRange[s], PartAmp, 0, PartAmp.Length); FreqSample.Add(PartAmp); MeanFreqs[s] += FreqSample.Mean / _NumberOfFrames; MedianFreqs[s] += FreqSample.Median / _NumberOfFrames; StdFreqs[s] += FreqSample.StandardDeviation / _NumberOfFrames; } } for (int i = 0; i < MeanFreqs.Length; i++) { if (Double.IsNaN(MeanFreqs[i])) { throw new System.Exception("Results of FFT is NaN"); } } MemoryStream DataStream = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(DataStream, new TransferDataFFT(MeanFreqs, MedianFreqs, StdFreqs)); //DataStream.Position = 0; //TransferDataFFT TransferStruct2 = (TransferDataFFT)formatter.Deserialize(DataStream); TransferDataSong tds = new TransferDataSong(); tds.data = DataStream.ToArray(); tds.path = SongPath; TimeSpan t = TimeSpan.FromSeconds(reader.TotalTime.TotalSeconds); tds.duration = string.Format("{0:D2}:{1:D2}", t.Minutes, t.Seconds); TagLib.ByteVector.UseBrokenLatin1Behavior = true; using (TagLib.File tagFile = TagLib.File.Create(SongPath)) { tds.title = tagFile.Tag.Title; tds.artist = tagFile.Tag.FirstPerformer; } DataStream.Close(); return(tds); }
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); }
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 Distribution 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); } Distribution tDistribution = new StudentDistribution(5); TestResult result = tSample.KolmogorovSmirnovTest(tDistribution); Console.WriteLine(result.LeftProbability); }
public void InverseGaussianSummation() { // X_i ~ IG(\mu,\lambda) \rightarrow \sum_{i=0}^{n} X_i ~ IG(n \mu, n^2 \lambda) Random rng = new Random(0); WaldDistribution d0 = new WaldDistribution(1.0, 2.0); Sample s = new Sample(); for (int i = 0; i < 64; i++) { s.Add(d0.GetRandomValue(rng) + d0.GetRandomValue(rng) + d0.GetRandomValue(rng)); } WaldDistribution d1 = new WaldDistribution(3.0 * 1.0, 9.0 * 2.0); TestResult r = s.KolmogorovSmirnovTest(d1); Console.WriteLine(r.LeftProbability); }
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); }
public void Bug7213() { Sample s = new Sample(); s.Add(0.00590056, 0.00654598, 0.0066506, 0.00679065, 0.008826); FitResult r = WeibullDistribution.FitToSample(s); }
public void Load(Origin origin = Origin.MainThread) { Samples = new List <Sample>(); if (origin == Origin.MainThread) { Group.UpdateDescriptionMask(Description); if (Description.Mask != null) { FrameList frameList = Group.GetFocusThread(Description.Mask.Value); if (frameList != null) { List <FrameData> frames = frameList.Events; for (int i = 0; i < frames.Count; ++i) { Sample sample = new Sample() { Name = String.Format("Frame {0:000}", i), Index = i }; long start = frames[i].Start; long finish = i == frames.Count - 1 ? frames[i].Finish : frames[i + 1].Start; foreach (ThreadData thread in Group.Threads) { Utils.ForEachInsideIntervalStrict(thread.Events, start, finish, (frame) => { List <Entry> shortEntries = null; if (frame.ShortBoard.TryGetValue(Description, out shortEntries)) { foreach (Entry e in shortEntries) { sample.Add(e); } } }); } Samples.Add(sample); } } else { // Fallback to Individual Calls Load(Origin.IndividualCalls); } } } if (origin == Origin.IndividualCalls) { foreach (ThreadData thread in Group.Threads) { foreach (EventFrame frame in thread.Events) { List <Entry> shortEntries = null; if (frame.ShortBoard.TryGetValue(Description, out shortEntries)) { foreach (Entry e in shortEntries) { Samples.Add(new Sample(e) { Index = Samples.Count, Name = Description.Name }); } } } } Samples.Sort((a, b) => (a.Entries[0].CompareTo(b.Entries[0]))); } }
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); } } }
public void SampleKuiperTest() { // this test has a whiff of meta-statistics about it // we want to make sure that the Kuiper test statistic V is distributed according to the Kuiper // distribution; to do this, we create a sample of V statistics and do KS/Kuiper tests // comparing it to the claimed Kuiper distribution // start with any 'ol underlying distribution Distribution distribution = new ExponentialDistribution(2.0); // generate some samples from it, and for each one get a V statistic from a KS test Sample VSample = new Sample(); Distribution VDistribution = null; for (int i = 0; i < 25; i++) { // the sample size must be large enough that the asymptotic assumptions are satistifed // at the moment this test fails if we make the sample size much smaller; we should // be able shrink this number when we expose the finite-sample distributions Sample sample = CreateSample(distribution, 250, i); TestResult kuiper = sample.KuiperTest(distribution); double V = kuiper.Statistic; Console.WriteLine("V = {0}", V); VSample.Add(V); VDistribution = kuiper.Distribution; } // check on the mean Console.WriteLine("m = {0} vs. {1}", VSample.PopulationMean, VDistribution.Mean); Assert.IsTrue(VSample.PopulationMean.ConfidenceInterval(0.95).ClosedContains(VDistribution.Mean)); // check on the standard deviation Console.WriteLine("s = {0} vs. {1}", VSample.PopulationStandardDeviation, VDistribution.StandardDeviation); Assert.IsTrue(VSample.PopulationStandardDeviation.ConfidenceInterval(0.95).ClosedContains(VDistribution.StandardDeviation)); // do a KS test comparing the sample to the expected distribution TestResult kst = VSample.KolmogorovSmirnovTest(VDistribution); Console.WriteLine("D = {0}, P = {1}", kst.Statistic, kst.LeftProbability); Assert.IsTrue(kst.LeftProbability < 0.95); // do a Kuiper test comparing the sample to the expected distribution TestResult kut = VSample.KuiperTest(VDistribution); Console.WriteLine("V = {0}, P = {1}", kut.Statistic, kut.LeftProbability); Assert.IsTrue(kut.LeftProbability < 0.95); }
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); }
public void GammaFitTest() { double k0 = 0.05; double t0 = 2.0; GammaDistribution G = new GammaDistribution(k0, t0); Random rng = new Random(1); Sample S = new Sample(); for (int i = 0; i < 1000; i++) { S.Add(G.GetRandomValue(rng)); } Console.WriteLine("mean sample={0} distribution={1}", S.Mean, G.Mean); Console.WriteLine("variance sample={0} distribution={1}", S.Variance, G.Variance); Console.WriteLine("moment estimate k={0} t={1}", S.Mean * S.Mean / S.Variance, S.Variance / S.Mean); double q = 0; foreach (double x in S) { q += Math.Log(x / S.Mean); } q /= S.Count; q = -q; Console.WriteLine("q = {0}, Log(k)-Psi(k)={1}", q, Math.Log(k0) - AdvancedMath.Psi(k0)); double ke0 = S.Mean * S.Mean / S.Variance; Console.WriteLine("ke0={0} Log(ke0)-Psi(ke0)={1}", ke0, Math.Log(ke0) - AdvancedMath.Psi(ke0)); double ke1 = 1.0 / (2.0 * q - 2.0 / 3.0 * q * q + 4.0 / 9.0 * q * q * q - 14.0 / 135.0 * q * q * q * q); Console.WriteLine("ke1={0} Log(ke1)-Psi(ke1)={1}", ke1, Math.Log(ke1) - AdvancedMath.Psi(ke1)); double ke2 = 1.0 / (q - AdvancedMath.EulerGamma); Console.WriteLine("ke2={0} Log(ke2)-Psi(ke2)={1}", ke2, Math.Log(ke2) - AdvancedMath.Psi(ke2)); }
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); }
public void BivariatePolynomialRegression() { // do a set of polynomial regression fits // make sure not only that the fit parameters are what they should be, but that their variances/covariances are as claimed Random rng = new Random(271828); // define logistic parameters double[] a = new double[] { 0.0, -1.0, 2.0, -3.0 }; // keep track of sample of returned a and b fit parameters MultivariateSample A = new MultivariateSample(a.Length); // also keep track of returned covariance estimates // since these vary slightly from fit to fit, we will average them SymmetricMatrix C = new SymmetricMatrix(a.Length); // also keep track of test statistics Sample F = new Sample(); // do 100 fits for (int k = 0; k < 100; k++) { // we should be able to draw x's from any distribution; noise should be drawn from a normal distribution Distribution xd = new CauchyDistribution(); Distribution nd = new NormalDistribution(0.0, 4.0); // generate a synthetic data set BivariateSample s = new BivariateSample(); for (int j = 0; j < 20; j++) { double x = xd.GetRandomValue(rng); double y = nd.GetRandomValue(rng); for (int i = 0; i < a.Length; i++) { y += a[i] * MoreMath.Pow(x, i); } s.Add(x, y); } // do the regression FitResult r = s.PolynomialRegression(a.Length - 1); ColumnVector ps = r.Parameters; //Console.WriteLine("{0} {1} {2}", ps[0], ps[1], ps[2]); // record best fit parameters A.Add(ps); // record estimated covariances C += r.CovarianceMatrix; // record the fit statistic F.Add(r.GoodnessOfFit.Statistic); //Console.WriteLine("F={0}", r.GoodnessOfFit.Statistic); } C = (1.0 / A.Count) * C; // allow matrix division by real numbers // check that mean parameter estimates are what they should be: the underlying population parameters for (int i = 0; i < A.Dimension; i++) { Console.WriteLine("{0} {1}", A.Column(i).PopulationMean, a[i]); Assert.IsTrue(A.Column(i).PopulationMean.ConfidenceInterval(0.95).ClosedContains(a[i])); } // check that parameter covarainces are what they should be: the reported covariance estimates for (int i = 0; i < A.Dimension; i++) { for (int j = i; j < A.Dimension; j++) { Console.WriteLine("{0} {1} {2} {3}", i, j, C[i, j], A.TwoColumns(i, j).PopulationCovariance); Assert.IsTrue(A.TwoColumns(i, j).PopulationCovariance.ConfidenceInterval(0.95).ClosedContains(C[i, j])); } } // check that F is distributed as it should be //Console.WriteLine(fs.KolmogorovSmirnovTest(new FisherDistribution(2, 48)).LeftProbability); }
public void GammaFromExponential() { // test that x_1 + x_2 + ... + x_n ~ Gamma(n) when z ~ Exponential() Random rng = new Random(1); ExponentialDistribution eDistribution = new ExponentialDistribution(); // pick some low values of n so distribution is not simply normal foreach (int n in new int[] { 2, 3, 4, 5 }) { Sample gSample = new Sample(); for (int i = 0; i < 100; i++) { double sum = 0.0; for (int j = 0; j < n; j++) { sum += eDistribution.GetRandomValue(rng); } gSample.Add(sum); } GammaDistribution gDistribution = new GammaDistribution(n); TestResult result = gSample.KolmogorovSmirnovTest(gDistribution); Assert.IsTrue(result.LeftProbability < 0.95); } }
public void SignTestDistribution() { // start with a non-normally distributed population Distribution xDistribution = new ExponentialDistribution(); Random rng = new Random(1); // draw 100 samples from it and compute the t statistic for each Sample wSample = new Sample(); for (int i = 0; i < 100; i++) { // each sample has 8 observations Sample xSample = new Sample(); for (int j = 0; j < 8; j++) { xSample.Add(xDistribution.GetRandomValue(rng)); } //Sample xSample = CreateSample(xDistribution, 8, i); TestResult wResult = xSample.SignTest(xDistribution.Median); double W = wResult.Statistic; //Console.WriteLine("W = {0}", W); wSample.Add(W); } // sanity check our sample of t's Assert.IsTrue(wSample.Count == 100); // check that the t statistics are distributed as expected DiscreteDistribution wDistribution = new BinomialDistribution(0.5, 8); // check on the mean Console.WriteLine("m = {0} vs. {1}", wSample.PopulationMean, wDistribution.Mean); Assert.IsTrue(wSample.PopulationMean.ConfidenceInterval(0.95).ClosedContains(wDistribution.Mean)); // check on the standard deviation Console.WriteLine("s = {0} vs. {1}", wSample.PopulationStandardDeviation, wDistribution.StandardDeviation); Assert.IsTrue(wSample.PopulationStandardDeviation.ConfidenceInterval(0.95).ClosedContains(wDistribution.StandardDeviation)); // check on the skew Console.WriteLine("t = {0} vs. {1}", wSample.PopulationMomentAboutMean(3), wDistribution.MomentAboutMean(3)); Assert.IsTrue(wSample.PopulationMomentAboutMean(3).ConfidenceInterval(0.95).ClosedContains(wDistribution.MomentAboutMean(3))); // check on the kuritosis Console.WriteLine("u = {0} vs. {1}", wSample.PopulationMomentAboutMean(4), wDistribution.MomentAboutMean(4)); Assert.IsTrue(wSample.PopulationMomentAboutMean(4).ConfidenceInterval(0.95).ClosedContains(wDistribution.MomentAboutMean(4))); // KS tests are only for continuous distributions }
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); }
public void TTestDistribution() { // start with a normally distributed population Distribution xDistribution = new NormalDistribution(2.0, 3.0); Random rng = new Random(1); // draw 100 samples from it and compute the t statistic for each Sample tSample = new Sample(); for (int i = 0; i < 100; i++) { // each sample has 9 values Sample xSample = new Sample(); for (int j = 0; j < 9; j++) { xSample.Add(xDistribution.GetRandomValue(rng)); } //Sample xSample = CreateSample(xDistribution, 10, i); TestResult tResult = xSample.StudentTTest(2.0); double t = tResult.Statistic; Console.WriteLine("t = {0}", t); tSample.Add(t); } // sanity check our sample of t's Assert.IsTrue(tSample.Count == 100); // check that the t statistics are distributed as expected Distribution tDistribution = new StudentDistribution(9); // check on the mean Console.WriteLine("m = {0} vs. {1}", tSample.PopulationMean, tDistribution.Mean); Assert.IsTrue(tSample.PopulationMean.ConfidenceInterval(0.95).ClosedContains(tDistribution.Mean), String.Format("{0} vs. {1}", tSample.PopulationMean, tDistribution.Mean)); // check on the standard deviation Console.WriteLine("s = {0} vs. {1}", tSample.PopulationStandardDeviation, tDistribution.StandardDeviation); Assert.IsTrue(tSample.PopulationStandardDeviation.ConfidenceInterval(0.95).ClosedContains(tDistribution.StandardDeviation)); // do a KS test TestResult ksResult = tSample.KolmogorovSmirnovTest(tDistribution); Assert.IsTrue(ksResult.LeftProbability < 0.95); Console.WriteLine("D = {0}", ksResult.Statistic); // check that we can distinguish the t distribution from a normal distribution? }
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); }
public void WaldFitUncertainties() { WaldDistribution wald = new WaldDistribution(3.5, 2.5); Random rng = new Random(314159); BivariateSample P = new BivariateSample(); double cmm = 0.0; double css = 0.0; double cms = 0.0; for (int i = 0; i < 50; i++) { Sample s = new Sample(); for (int j = 0; j < 50; j++) { s.Add(wald.GetRandomValue(rng)); } FitResult r = WaldDistribution.FitToSample(s); P.Add(r.Parameter(0).Value, r.Parameter(1).Value); cmm += r.Covariance(0, 0); css += r.Covariance(1, 1); cms += r.Covariance(0, 1); } cmm /= P.Count; css /= P.Count; cms /= P.Count; Console.WriteLine("{0} {1}", P.X.PopulationMean, P.Y.PopulationMean); Assert.IsTrue(P.X.PopulationMean.ConfidenceInterval(0.95).ClosedContains(wald.Mean)); Assert.IsTrue(P.Y.PopulationMean.ConfidenceInterval(0.95).ClosedContains(wald.ShapeParameter)); // the ML shape parameter estimate appears to be asymptoticly unbiased, as it must be according to ML fit theory, // but detectably upward biased for small n. we now correct for this. Console.WriteLine("{0} {1} {2}", P.X.PopulationVariance, P.Y.PopulationVariance, P.PopulationCovariance); Console.WriteLine("{0} {1} {2}", cmm, css, cms); Assert.IsTrue(P.X.PopulationVariance.ConfidenceInterval(0.95).ClosedContains(cmm)); Assert.IsTrue(P.Y.PopulationVariance.ConfidenceInterval(0.95).ClosedContains(css)); Assert.IsTrue(P.PopulationCovariance.ConfidenceInterval(0.95).ClosedContains(cms)); }
public void BivariateLinearRegressionGoodnessOfFitDistribution() { // create uncorrelated x and y values // the distribution of F-test statistics returned by linear fits should follow the expected F-distribution Random rng = new Random(987654321); NormalDistribution xd = new NormalDistribution(1.0, 2.0); NormalDistribution yd = new NormalDistribution(-3.0, 4.0); Sample fs = new Sample(); for (int i = 0; i < 127; i++) { BivariateSample xys = new BivariateSample(); for (int j = 0; j < 7; j++) { xys.Add(xd.GetRandomValue(rng), yd.GetRandomValue(rng)); } double f = xys.LinearRegression().GoodnessOfFit.Statistic; fs.Add(f); } Distribution fd = new FisherDistribution(1, 5); Console.WriteLine("{0} v. {1}", fs.PopulationMean, fd.Mean); TestResult t = fs.KolmogorovSmirnovTest(fd); Console.WriteLine(t.LeftProbability); Assert.IsTrue(t.LeftProbability < 0.95); }
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)); }
public void PearsonRDistribution() { Random rng = new Random(1); // pick some underlying distributions for the sample variables, which must be normal but can have any parameters NormalDistribution xDistribution = new NormalDistribution(1, 2); NormalDistribution yDistribution = new NormalDistribution(3, 4); // try this for several sample sizes, all low so that we see the difference from the normal distribution // n = 3 maxima at ends; n = 4 uniform; n = 5 semi-circular "mound"; n = 6 parabolic "mound" foreach (int n in new int[] { 3, 4, 5, 6, 8 }) { Console.WriteLine("n={0}", n); // find r values Sample rSample = new Sample(); for (int i = 0; i < 100; i++) { // to get each r value, construct a bivariate sample of the given size with no cross-correlation BivariateSample xySample = new BivariateSample(); for (int j = 0; j < n; j++) { xySample.Add(xDistribution.GetRandomValue(rng), yDistribution.GetRandomValue(rng)); } double r = xySample.PearsonRTest().Statistic; rSample.Add(r); } // check whether r is distributed as expected TestResult result = rSample.KolmogorovSmirnovTest(new PearsonRDistribution(n)); Console.WriteLine("P={0}", result.LeftProbability); Assert.IsTrue(result.LeftProbability < 0.95); } }
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)); }
public void AddTest(int number1, int number2, int expected) { Assert.Equal(expected, Sample.Add(number1, number2)); }
//Return t-test values between individual personality traits and the rest of the data //persTrait = [neutral o- o+ c- c+ e- e+ a- a+ n- n+] //metricType = [0, 1, 2, 3] for posture, weight, space, time, torso //submetricType : individual elements in metric.txt double EvaluateMetric(string persTrait, int metricType, int subMetricType) { Sample traitValues = new Sample(); Sample otherTraitValues = new Sample(); string s; if (metricType == (int)MetricType.Posture) { StreamReader sr = new StreamReader("METRICS\\posture.txt"); sr.ReadLine(); //the title line while ((s = sr.ReadLine()) != null) { string [] line = s.Split('\t'); if (s.Contains(persTrait)) { traitValues.Add(float.Parse(line[1 + subMetricType])); } else { otherTraitValues.Add(float.Parse(line[1 + subMetricType])); // 1 for animation name } } sr.Close(); } else if (metricType == (int)MetricType.Weight) { /*StreamReader sr = new StreamReader("METRICS\\weightStrongCnt.txt"); * while((s = sr.ReadLine()) != null) { * string [] line = s.Split('\t'); * if(s.Contains(persTrait)) * traitValues.Add(float.Parse(line[1 + subMetricType])); * else * otherTraitValues.Add(float.Parse(line[1 + subMetricType])); // 1 for animation name * } * sr.Close(); * */ StreamReader sr = new StreamReader("METRICS\\weightStrong.txt"); while ((s = sr.ReadLine()) != null) { string [] line = s.Split('\t'); if (s.Contains(persTrait)) { traitValues.Add(float.Parse(line[1 + subMetricType])); } else { otherTraitValues.Add(float.Parse(line[1 + subMetricType])); // 1 for animation name } } sr.Close(); } TestResult result = Sample.StudentTTest(traitValues, otherTraitValues); return(result.RightProbability); // OneWayAnovaResult result = Sample.OneWayAnovaTest(traitValues, otherTraitValues); //return(result.Result.RightProbability); }