数据直方图 - 优化的宽带优化

时间:2013-01-31 21:00:06

标签: c# statistics

我希望从给定的数据集中生成数据直方图。我已经阅读了构建直方图的不同选项,我对基于

工作的方法最感兴趣
  

Shimazaki,H。; Shinomoto,S。(2007)。 “选择垃圾箱的方法   时间直方图的大小“

上述方法使用估算来确定最佳箱宽和分布,这在我的情况下是必需的,因为样本数据的分布会有所不同,并且难以提前确定箱数和宽度。

有人可以推荐一个好的来源或起点,用于在c#中编写这样的函数,或者有足够的c#直方图代码。

非常感谢。

4 个答案:

答案 0 :(得分:7)

以下是我从here编写的此算法的Python版本的端口。我知道API可以做一些工作,但这应该足以让你开始。此代码的结果与Python代码为相同输入数据生成的结果相同。

public class HistSample
{
    public static void CalculateOptimalBinWidth(double[] x)
    {
        double xMax = x.Max(), xMin = x.Min();
        int minBins = 4, maxBins = 50;
        double[] N = Enumerable.Range(minBins, maxBins - minBins)
            .Select(v => (double)v).ToArray();
        double[] D = N.Select(v => (xMax - xMin) / v).ToArray();
        double[] C = new double[D.Length];

        for (int i = 0; i < N.Length; i++)
        {
            double[] binIntervals = LinearSpace(xMin, xMax, (int)N[i] + 1);
            double[] ki = Histogram(x, binIntervals);
            ki = ki.Skip(1).Take(ki.Length - 2).ToArray();

            double mean = ki.Average();
            double variance = ki.Select(v => Math.Pow(v - mean, 2)).Sum() / N[i];

            C[i] = (2 * mean - variance) / (Math.Pow(D[i], 2));
        }

        double minC = C.Min();
        int index = C.Select((c, ix) => new { Value = c, Index = ix })
            .Where(c => c.Value == minC).First().Index;
        double optimalBinWidth = D[index];
    }

    public static double[] Histogram(double[] data, double[] binEdges)
    {
        double[] counts = new double[binEdges.Length - 1];

        for (int i = 0; i < binEdges.Length - 1; i++)
        {
            double lower = binEdges[i], upper = binEdges[i + 1];

            for (int j = 0; j < data.Length; j++)
            {
                if (data[j] >= lower && data[j] <= upper)
                {
                    counts[i]++;
                }
            }
        }

        return counts;
    }

    public static double[] LinearSpace(double a, double b, int count)
    {
        double[] output = new double[count];

        for (int i = 0; i < count; i++)
        {
            output[i] = a + ((i * (b - a)) / (count - 1));
        }

        return output;
    }
}

像这样运行:

double[] x =
{
    4.37, 3.87, 4.00, 4.03, 3.50, 4.08, 2.25, 4.70, 1.73,
    4.93, 1.73, 4.62, 3.43, 4.25, 1.68, 3.92, 3.68, 3.10,
    4.03, 1.77, 4.08, 1.75, 3.20, 1.85, 4.62, 1.97, 4.50,
    3.92, 4.35, 2.33, 3.83, 1.88, 4.60, 1.80, 4.73, 1.77,
    4.57, 1.85, 3.52, 4.00, 3.70, 3.72, 4.25, 3.58, 3.80,
    3.77, 3.75, 2.50, 4.50, 4.10, 3.70, 3.80, 3.43, 4.00,
    2.27, 4.40, 4.05, 4.25, 3.33, 2.00, 4.33, 2.93, 4.58,
    1.90, 3.58, 3.73, 3.73, 1.82, 4.63, 3.50, 4.00, 3.67,
    1.67, 4.60, 1.67, 4.00, 1.80, 4.42, 1.90, 4.63, 2.93,
    3.50, 1.97, 4.28, 1.83, 4.13, 1.83, 4.65, 4.20, 3.93,
    4.33, 1.83, 4.53, 2.03, 4.18, 4.43, 4.07, 4.13, 3.95,
    4.10, 2.27, 4.58, 1.90, 4.50, 1.95, 4.83, 4.12
};

HistSample.CalculateOptimalBinWidth(x);

答案 1 :(得分:1)

检查直方图功能。如果任何数据元素不幸等于bin边界(除了第一个或最后一个bin),它们将在两个连续的bin中计数。 代码需要检查(降低&lt; = data [j]&amp;&amp; data [j]&lt; upper)并处理所有等于xMax的元素进入最后一个bin的情况。

答案 2 :(得分:0)

我建议使用二进制搜索来加快类间隔的分配。

public void Add(double element)
{
  if (element < Bins.First().LeftBound || element > Bins.Last().RightBound)
    return;

  var min = 0;
  var max = Bins.Length - 1;
  var index = 0;

  while (min <= max)
  {
    index = min + ((max - min) / 2);

    if (element >= Bins[index].LeftBound && element < Bins[index].RightBound)
      break;

    if (element < Bins[index].LeftBound)
      max = index - 1;
    else
      min = index + 1;
  }

  Bins[index].Count++;
}

&#34;箱和#34;是&#34; HistogramItem&#34;的类型项目列表。它定义了像&#34; Leftbound&#34;,&#34; RightBound&#34;等属性。和&#34;伯爵&#34;。

答案 3 :(得分:0)

对nick_w回答的一个小更新。

如果你真的需要垃圾箱。另外优化了直方图函数中的双循环,并且除去了linspace函数。

     /// <summary>
     /// Calculate the optimal bins for the given data
     /// </summary>
     /// <param name="x">The data you have</param>
     /// <param name="xMin">The minimum element</param>
     /// <param name="optimalBinWidth">The width between each bin</param>
     /// <returns>The bins</returns>
     public static int[] CalculateOptimalBinWidth(List<double> x, out double xMin, out double optimalBinWidth)
     {
        var xMax = x.Max();
        xMin = x.Min();
        optimalBinWidth = 0;
        const int MIN_BINS = 1;
        const int MAX_BINS = 20;

        int[] minKi = null;
        var minOffset = double.MaxValue;

        foreach (var n in Enumerable.Range(MIN_BINS, MAX_BINS - MIN_BINS).Select(v => v*5))
        {
           var d = (xMax - xMin)/n;
           var ki = Histogram(x, n, xMin, d);
           var ki2 = ki.Skip(1).Take(ki.Length - 2).ToArray();

           var mean = ki2.Average();
           var variance = ki2.Select(v => Math.Pow(v - mean, 2)).Sum()/n;

           var offset = (2*mean - variance)/Math.Pow(d, 2);

           if (offset < minOffset)
           {
              minKi = ki;
              minOffset = offset;
              optimalBinWidth = d;
           }
        }
        return minKi;
     }

     private static int[] Histogram(List<double> data, int count, double xMin, double d)
     {
        var histogram = new int[count];
        foreach (var t in data)
        {
           var bucket = (int) Math.Truncate((t - xMin)/d);
           if (count == bucket) //fix xMax
              bucket --;
           histogram[bucket]++;
        }
        return histogram;
     }