private static void WriteHistogramToSheet(Worksheet sheet, IEnumerable<double> data) { // Throw delivery time into the histogram Histogram hist = new Histogram(data, 60); sheet[0, 0].Value = "Count"; sheet[0, 1].Value = "Width"; sheet[0, 2].Value = "LowerBound"; sheet[0, 3].Value = "UpperBound"; // Write the histogram data out to Excel for (int i = 1; i < hist.BucketCount; i++) { sheet[i, 0].Value = hist[i].Count; sheet[i, 1].Value = hist[i].Width; sheet[i, 2].Value = hist[i].LowerBound; sheet[i, 3].Value = hist[i].UpperBound; } // Plot the histogram data Charts charts = sheet.Charts; Anchor anchor = sheet.CreateAnchor(4, 6, 0, 0); Chart chart = charts.CreateChart(ChartType.Column.Clustered, anchor); SeriesCollection collection = chart.SeriesCollection; collection.CategoryData = String.Format("{0}!{1}:{2}", sheet.Name, sheet.Cells[1, 2].Name, sheet.Cells[50, 2].Name); Series series = collection.CreateSeries(String.Format("{0}!{1}:{2}", sheet.Name, sheet.Cells[1, 0].Name, sheet.Cells[50, 0].Name)); }
public static MathNetHistogram AddBuckets(this MathNetHistogram histogram, IEnumerable <Bucket> buckets) { foreach (var bucket in buckets) { histogram.AddBucket(bucket); } return(histogram); }
private static IEnumerable<Bucket> Enumerate(Histogram histogram) { for (var i = 0; i < histogram.BucketCount; i++) { var bucket = histogram[i]; yield return bucket; } }
public void CanCreateCategoricalFromHistogram() { double[] smallDataset = { 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5 }; Histogram hist = new Histogram(smallDataset, 10, 0.0, 10.0); var m = new Categorical(hist); for (int i = 0; i <= m.Maximum; i++) { AssertEx.AreEqual<double>(1.0/10.0, m.P[i]); } }
public void HistogramDataContractSerializationTest() { var expected = new Histogram(new[] { 1.0, 2.0, 3.0, 4.0 }, 2); var serializer = new DataContractSerializer(typeof(Histogram)); var stream = new MemoryStream(); serializer.WriteObject(stream, expected); stream.Position = 0; var actual = (Histogram)serializer.ReadObject(stream); Assert.That(actual.BucketCount, Is.EqualTo(expected.BucketCount)); Assert.That(actual.DataCount, Is.EqualTo(expected.DataCount)); Assert.That(actual.LowerBound, Is.EqualTo(expected.LowerBound)); Assert.That(actual[0].Width, Is.EqualTo(expected[0].Width)); Assert.That(actual[0].UpperBound, Is.EqualTo(expected[0].UpperBound)); }
public Form1() { InitializeComponent(); // clear chart1.Series.Clear(); chart1.ChartAreas.Clear(); chart1.Titles.Clear(); Title title1 = new Title("Histgram"); // series Series seriesColumn = new Series(); seriesColumn.LegendText = "Histgram"; seriesColumn.ChartType = SeriesChartType.Column; // 正規乱数を作る mRand = new Random(DateTime.Now.Millisecond); int nData = 10000; double[] data = new double[nData]; for (int i = 0; i < nData; i++) { // Box-Muller法で一様乱数から正規乱数を作る data[i] = boxmuller(mRand, 0 /* average */ , 10 /* sigma */); } // ヒストグラムを作る int nBuckets = 10; Histogram hist = new Histogram(data, nBuckets, -50 /* lower */, 50 /* upper */); for (int i = 0; i < nBuckets; i++) { double mid = Math.Round((hist[i].UpperBound+hist[i].LowerBound)/2, 1); seriesColumn.Points.Add(new DataPoint(mid, hist[i].Count)); } // chartarea ChartArea area1 = new ChartArea(); area1.AxisX.Title = "Value"; area1.AxisY.Title = "Frequency"; chart1.Titles.Add(title1); chart1.ChartAreas.Add(area1); chart1.Series.Add(seriesColumn); }
/// <summary> /// Initializes a new instance of the Categorical class from a histogram <paramref name="h"/>. The distribution /// will not be automatically updated when the histogram changes. The categorical distribution will have /// one value for each bucket and a probability for that value proportional to the bucket count. /// </summary> /// <param name="histogram">The histogram from which to create the categorical variable.</param> public Categorical(Histogram histogram) { if (histogram == null) { throw new ArgumentNullException("Cannot create a categorical variable from a null histogram."); } // The probability distribution vector. double[] p = new double[histogram.BucketCount]; // Fill in the distribution vector. for (int i = 0; i < histogram.BucketCount; i++) { p[i] = histogram[i].Count; } SetParameters(p); RandomSource = new System.Random(); }
public object NormalDistSample(int mean, int stddev, int size, int nbuckets) { var normal = new Normal(mean, stddev); var data = new double[size]; for (var i = 0; i < data.Length; i++) { data[i] = normal.Sample(); } var histogram = new Histogram(data, nbuckets); var buckets = new List<Bucket>(); for (var i = 0; i < histogram.BucketCount; i++) { buckets.Add(histogram[i]); } return buckets.Select(x=> new {lowerBound = x.LowerBound, upperBound = x.UpperBound, count = x.Count}).ToList(); }
public void CanCreateEqualSpacedHistogram() { var h = new Histogram(new double[] { 1.0, 5.0, 10.0 }, 2); }
/// <summary> /// Vapnik Chervonenkis test. /// </summary> /// <param name="epsilon">The error we are willing to tolerate.</param> /// <param name="delta">The error probability we are willing to tolerate.</param> /// <param name="s">The samples to use for testing.</param> /// <param name="dist">The distribution we are testing.</param> public static void VapnikChervonenkisTest(double epsilon, double delta, IEnumerable<double> s, IUnivariateDistribution dist) { // Using VC-dimension, we can bound the probability of making an error when estimating empirical probability // distributions. We are using Theorem 2.41 in "All Of Nonparametric Statistics". // http://books.google.com/books?id=MRFlzQfRg7UC&lpg=PP1&dq=all%20of%20nonparametric%20statistics&pg=PA22#v=onepage&q=%22shatter%20coe%EF%AC%83cients%20do%20not%22&f=false .</para> // For intervals on the real line the VC-dimension is 2. double n = s.Count(); Assert.Greater(n, Math.Ceiling(32.0 * Math.Log(16.0 / delta) / epsilon / epsilon)); var histogram = new Histogram(s, NumberOfBuckets); for (var i = 0; i < NumberOfBuckets; i++) { var p = dist.CumulativeDistribution(histogram[i].UpperBound) - dist.CumulativeDistribution(histogram[i].LowerBound); var pe = histogram[i].Count / n; Assert.Less(Math.Abs(p - pe), epsilon, dist.ToString()); } }
public void SmallValuesManyBucketsHistogramWithBounds() { var hist = new Histogram(_smallValueDataset, 100, 0.0, 10e-22); Assert.AreEqual(100, hist.BucketCount); Assert.AreEqual(0.0, hist.LowerBound); Assert.AreEqual(10.0e-22, hist.UpperBound); }
public void SmallValuesHistogramWithBounds() { var hist = new Histogram(_smallValueDataset, 10, 0.0, 10e-22); Assert.AreEqual(10, hist.BucketCount); for (var i = 0; i < 10; i++) { Assert.AreEqual(1.0, hist[i].Count); } Assert.AreEqual(0.0, hist.LowerBound); Assert.AreEqual(10.0e-22, hist.UpperBound); }
/// <summary> /// Returns the optimal squared freedom histogram. /// </summary> public static Histogram OptimalSquaredFreedom(int histSize, ICollection <double> distribution) { if (distribution.Count < Math.Max(histSize, 2)) { throw new ArgumentException(Properties.LocalStrings.InvalidOperationHistogramNotEnoughPoints); } // "values" contains the sorted distribution. double[] values = new double[distribution.Count]; distribution.CopyTo(values, 0); Array.Sort(values); // 'optimalCost[i,k]' contains the optimal costs for an // histogram with the 'i+1' first values and 'k' buckets. double[,] optimalCost = new double[values.Length, histSize]; // 'lastBucketIndex[i,k]' contains the index of the first // value of the last bucket for optimal histogram comprising // the 'i+1' first values and 'k' buckets. int[,] lastBucketIndex = new int[values.Length, histSize]; // "One bucket" histograms initialization for (int i = 0; i < values.Length; i++) { optimalCost[i, 0] = (values[i] - values[0]) * (values[i] - values[0]) * (i + 1); } // "One value per bucket" histograms initialization for (int k = 0; k < histSize; k++) { // optimalCost[k, k] = 0; lastBucketIndex[k, k] = k; } // ----- Dynamic programming part ----- // Loop on the number of buckets // (note that there are 'k+1' buckets) for (int k = 1; k < histSize; k++) { // Loop on the number of considered values // (note that there are 'i+1' considered values) for (int i = k; i < values.Length; i++) { optimalCost[i, k] = double.PositiveInfinity; // Loop for finding the optimal boundary of the last bucket // ('j+1' is the index of the first value in the last bucket) for (int j = (k - 1); j < i; j++) { double currentCost = optimalCost[j, k - 1] + ((values[i] - values[j + 1]) * (values[i] - values[j + 1]) * (i - j)); if (currentCost < optimalCost[i, k]) { optimalCost[i, k] = currentCost; lastBucketIndex[i, k] = j + 1; } } } } // ----- Reconstitution of the histogram ----- Histogram histogram = new Histogram(); int index = values.Length - 1; for (int k = (histSize - 1); k >= 0; k--) { histogram.Add(new Bucket( values[lastBucketIndex[index, k]], values[index], index - lastBucketIndex[index, k] + 1)); index = lastBucketIndex[index, k] - 1; } return(histogram); }
/// <summary> /// Returns the optimal dispersion histogram. /// </summary> public static Histogram OptimalDispersion(int bucketCount, ICollection <double> distribution) { if (distribution.Count < Math.Max(bucketCount, 2)) { throw new ArgumentException(Properties.LocalStrings.InvalidOperationHistogramNotEnoughPoints); } // "values" contains the sorted distribution. double[] values = new double[distribution.Count]; distribution.CopyTo(values, 0); Array.Sort(values); // 'optimalCost[i,k]' contains the optimal costs for an // histogram with the 'i+1' first values and 'k' buckets. double[,] optimalCost = new double[values.Length, bucketCount]; // 'lastBucketIndex[i,k]' contains the index of the first // value of the last bucket for optimal histogram comprising // the 'i+1' first values and 'k' buckets. int[,] lastBucketIndex = new int[values.Length, bucketCount]; // 'prefixSum[i]' contains the sum of the 'i-1' first values. double[] prefixSum = new double[values.Length + 1]; // Initialization of the prefix sums for (int i = 0; i < values.Length; i++) { prefixSum[i + 1] = prefixSum[i] + values[i]; } // "One bucket" histograms initialization for (int i = 0, avg = 0; i < values.Length; i++) { while ((avg + 1) < values.Length && values[avg + 1] < prefixSum[i + 1] / (i + 1)) { avg++; } optimalCost[i, 0] = prefixSum[i + 1] - (2 * prefixSum[avg + 1]) + (((2 * avg) - i + 1) * (prefixSum[i + 1] / (i + 1))); } // "One value per bucket" histograms initialization for (int k = 0; k < bucketCount; k++) { // optimalCost[k, k] = 0; lastBucketIndex[k, k] = k; } // ----- Dynamic programming part ----- // Loop on the number of buckets // (note that there are 'k+1' buckets) for (int k = 1; k < bucketCount; k++) { // Loop on the number of considered values // (note that there are 'i+1' considered values) for (int i = k; i < values.Length; i++) { optimalCost[i, k] = double.PositiveInfinity; // Loop for finding the optimal boundary of the last bucket // ('j+1' is the index of the first value in the last bucket) for (int j = (k - 1), avg = (k - 1); j < i; j++) { while ((avg + 1) < values.Length && values[avg + 1] < (prefixSum[i + 1] - prefixSum[j + 1]) / (i - j)) { avg++; } double currentCost = optimalCost[j, k - 1] + prefixSum[i + 1] + prefixSum[j + 1] - (2 * prefixSum[avg + 1]) + (((2 * avg) - i - j) * (prefixSum[i + 1] - prefixSum[j + 1]) / (i - j)); if (currentCost < optimalCost[i, k]) { optimalCost[i, k] = currentCost; lastBucketIndex[i, k] = j + 1; } } } } // ----- Reconstitution of the histogram ----- Histogram histogram = new Histogram(); int index = values.Length - 1; for (int k = (bucketCount - 1); k >= 0; k--) { histogram.Add(new Bucket( values[lastBucketIndex[index, k]], values[index], index - lastBucketIndex[index, k] + 1)); index = lastBucketIndex[index, k] - 1; } return(histogram); }
private void FillHistogram() { _histogram = new MathNetHistogram(); _histogram.AddBuckets(For(Numbers)).AddData(Numbers); }
public void CanCreateEqualSpacedHistogramWithGivenLowerAndUpperBound() { var h = new Histogram(new double[] { 1.0, 5.0, 10.0 }, 2, 0.0, 20.0); }
/// <summary> /// Initializes a new instance of the Categorical class from a <paramref name="histogram"/>. The distribution /// will not be automatically updated when the histogram changes. The categorical distribution will have /// one value for each bucket and a probability for that value proportional to the bucket count. /// </summary> /// <param name="histogram">The histogram from which to create the categorical variable.</param> public Categorical(Histogram histogram) { if (histogram == null) { throw new ArgumentNullException("histogram"); } // The probability distribution vector. var p = new double[histogram.BucketCount]; // Fill in the distribution vector. for (var i = 0; i < histogram.BucketCount; i++) { p[i] = histogram[i].Count; } _random = SystemRandomSource.Default; if (Control.CheckDistributionParameters && !IsValidProbabilityMass(p)) { throw new ArgumentException(Resources.InvalidDistributionParameters); } // Extract unnormalized cumulative distribution _cdfUnnormalized = new double[p.Length]; _cdfUnnormalized[0] = p[0]; for (int i1 = 1; i1 < p.Length; i1++) { _cdfUnnormalized[i1] = _cdfUnnormalized[i1 - 1] + p[i1]; } // Extract normalized probability mass var sum = _cdfUnnormalized[_cdfUnnormalized.Length - 1]; _pmfNormalized = new double[p.Length]; for (int i2 = 0; i2 < p.Length; i2++) { _pmfNormalized[i2] = p[i2]/sum; } }
public void CanCreateEmptyHistogram() { var h = new Histogram(); }
/// <summary> /// Initializes a new instance of the Multinomial class from histogram <paramref name="h"/>. The distribution will /// not be automatically updated when the histogram changes. /// </summary> /// <param name="h">Histogram instance</param> /// <param name="n">The number of trials.</param> /// <exception cref="ArgumentOutOfRangeException">If any of the probabilities are negative or do not sum to one.</exception> /// <exception cref="ArgumentOutOfRangeException">If <paramref name="n"/> is negative.</exception> public Multinomial(Histogram h, int n) { if (h == null) { throw new ArgumentNullException("h"); } // The probability distribution vector. var p = new double[h.BucketCount]; // Fill in the distribution vector. for (var i = 0; i < h.BucketCount; i++) { p[i] = h[i].Count; } SetParameters(p, n); RandomSource = MersenneTwister.Default; }
public void CanAddDataList() { var h = new Histogram(new double[] { 1.0, 5.0, 10.0 }, 2); h.AddData(new double[] { 7.0, 8.0} ); Assert.AreEqual(3, h[1].Count); }
public void CanAddDataSingle() { var h = new Histogram(new double[] { 1.0, 5.0, 10.0 }, 2); h.AddData(7.0); Assert.AreEqual(2, h[1].Count); }
public void AddDataIncreasesUpperBound() { var h = new Histogram(new double[] { 1.0, 5.0, 10.0 }, 2); h.AddData(20.0); Assert.AreEqual(2, h[1].Count); }
public void CanAddBucket() { var h = new Histogram(); h.AddBucket(new Bucket(0.0, 1.0)); }
public void ValidateItem() { var h = new Histogram(); var b = new Bucket(0.0, 1.0); var c = new Bucket(3.0, 20.0); h.AddBucket(b); h.AddBucket(c); h.AddBucket(new Bucket(1.0, 2.0)); h.AddBucket(new Bucket(2.0, 3.0)); h.AddBucket(new Bucket(20.0, Double.PositiveInfinity)); Assert.AreEqual(b, h[0]); Assert.AreEqual(c, h[3]); }
public void CanGetBucketIndexOf(double x, double i) { var h = new Histogram(); h.AddBucket(new Bucket(0.0, 1.0)); h.AddBucket(new Bucket(1.0, 2.0)); h.AddBucket(new Bucket(2.0, 3.0)); h.AddBucket(new Bucket(3.0, 20.0)); h.AddBucket(new Bucket(20.0, Double.PositiveInfinity)); Assert.AreEqual(i, h.GetBucketIndexOf(x)); }
public void SmallDatasetHistogramWithBounds() { Histogram hist = new Histogram(smallDataset, 10, 0.0, 10.0); Assert.AreEqual(10, hist.BucketCount); for (int i = 0; i < 10; i++) { Assert.AreEqual(1.0, hist[i].Count); } Assert.AreEqual(0.0, hist.LowerBound); Assert.AreEqual(10.0, hist.UpperBound); }
public void SmallDatasetHistogramWithoutBounds() { Histogram hist = new Histogram(smallDataset, 9); Assert.AreEqual(9, hist.BucketCount); Console.WriteLine("{0}", hist); for (int i = 0; i < 8; i++) { Console.WriteLine("{0} : {1}", i, hist[i].Count); Assert.AreEqual(1.0, hist[i].Count); } Assert.AreEqual(2.0, hist[8].Count); Assert.AreEqual(0.5, hist.LowerBound); Assert.AreEqual(9.5.Increment(), hist.UpperBound); }
public void FailCreateEqualSpacedHistogramWithNoData() { var h = new Histogram(new List<double>(), 10); }
public void CanGetTotalCount() { var h = new Histogram(); h.AddBucket(new Bucket(0.0, 1.0, 1)); h.AddBucket(new Bucket(1.0, 2.0, 1)); h.AddBucket(new Bucket(2.0, 3.0, 1)); h.AddBucket(new Bucket(3.0, 20.0, 1)); h.AddBucket(new Bucket(20.0, Double.PositiveInfinity, 1)); Assert.AreEqual(5, h.DataCount); }
public void CanGetBucketOf() { var h = new Histogram(); var b = new Bucket(0.0, 1.0); h.AddBucket(b); h.AddBucket(new Bucket(1.0, 2.0)); h.AddBucket(new Bucket(2.0, 3.0)); h.AddBucket(new Bucket(3.0, 20.0)); h.AddBucket(new Bucket(20.0, Double.PositiveInfinity)); Assert.AreEqual(b, h.GetBucketOf(0.1)); }
/// <summary> /// Initializes a new instance of the Categorical class from a <paramref name="histogram"/>. The distribution /// will not be automatically updated when the histogram changes. The categorical distribution will have /// one value for each bucket and a probability for that value proportional to the bucket count. /// </summary> /// <param name="histogram">The histogram from which to create the categorical variable.</param> public Categorical(Histogram histogram) { if (histogram == null) { throw new ArgumentNullException("histogram"); } // The probability distribution vector. var p = new double[histogram.BucketCount]; // Fill in the distribution vector. for (var i = 0; i < histogram.BucketCount; i++) { p[i] = histogram[i].Count; } _random = new System.Random(); SetParameters(p); }
public void AddDataDecreasesLowerBound() { var h = new Histogram(new double[] { 1.0, 5.0, 10.0 }, 2); h.AddData(0.0); Assert.AreEqual(3, h[0].Count); }
public void CanGetBucketCountInHistogram() { var h = new Histogram(); h.AddBucket(new Bucket(0.0, 1.0)); h.AddBucket(new Bucket(1.0, 2.0)); h.AddBucket(new Bucket(2.0, 3.0)); h.AddBucket(new Bucket(3.0, 20.0)); h.AddBucket(new Bucket(20.0, Double.PositiveInfinity)); Assert.AreEqual(5, h.BucketCount); }
private void FillHistogram() { _histogram = new Hist(); _histogram .AddBuckets(For(Numbers)) .AddData(Numbers); }