我正在尝试计算一组值的累积分布函数。
我使用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 1 0 ...就像这样。总共有20个值
cdf为: 0.00 0.05 0.05 0.05 0.1 ...
为什么第一个位置会有一个0?它不应该从0.05开始吗?
谢谢。
发布于 2013-08-29 00:09:04
GSL alloc sum是一个大小为n+1的数组,其中n是仓位的数量。然而,计算pdf只需要n个条目。这种额外分配一个元素的原因是gsl定义sum = 0。
在GSL源代码coode "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;
}
}https://stackoverflow.com/questions/17364871
复制相似问题