【问题标题】:GSL histogram issueGSL直方图问题
【发布时间】:2013-06-28 12:35:33
【问题描述】:

我正在尝试计算一组值的累积分布函数。

我使用 gsl 计算了直方图,并尝试从此处计算 CDF,但这些值似乎移动了一个位置。

这是我正在使用的代码:

gHist =  gsl_histogram_alloc((maxRange - minRange) / 5);
gsl_histogram_set_ranges_uniform(gHist, minRange, maxRange);

for (int j = 0; j < ValidDataCount; j++)
gsl_histogram_increment (gHist, ValAdd[j]);

gsl_histogram_pdf * p = gsl_histogram_pdf_alloc(gsl_histogram_bins(gHist));
gsl_histogram_pdf_init (p,  gHist);

for (int j = 0; j < gsl_histogram_bins(gHist) + 1 ; j++)
printf ("%f ", p->sum[j]);

直方图是这样的: 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 .... 就这样继续下去。一共有20个值

而 cdf 是: 0.00 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.1 0.1 ...

为什么第一个位置有一个 0?不应该从0.05开始吗?

谢谢。

【问题讨论】:

  • 答案来晚了:希望它仍然有用。

标签: c++ histogram gsl


【解决方案1】:

GSL alloc sum 为一个大小为 n+1 的数组,其中 n 是 bin 的数量。但是,计算 pdf 只需要 n 个条目。一个元素的这种额外分配发生是因为 gsl 定义 sum[0] = 0。

在 GSL 源代码“pdf.c”中你可以看到

 gsl_histogram_pdf *gsl_histogram_pdf_alloc (const size_t n)
 {
   (...)
   p->sum = (double *) malloc ((n + 1) * sizeof (double));
 }


 int  gsl_histogram_pdf_init (gsl_histogram_pdf * p, const gsl_histogram * h)
 {
   (...)
    p->sum[0] = 0;
    for (i = 0; i < n; i++)
    {
     sum += (h->bin[i] / mean) / n;
     p->sum[i + 1] = sum;
    }
 }

【讨论】:

    猜你喜欢
    • 2016-07-26
    • 2021-02-07
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    相关资源
    最近更新 更多