2013-06-28 46 views
0

我正在嘗試計算一組值的累積分佈函數。GSL直方圖問題

我使用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開頭嗎?

謝謝。

+0

答案出來得比較晚:希望它仍然是有用。 –

回答

0

GSL alloc sum是一個大小爲n + 1的數組,其中n是垃圾箱的數量。但是,只需要n個條目來計算pdf。一個元素的這種額外的分配情況是因爲GSL定義總和[0] = 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; 
    } 
}