| 1 |
|
| #include "histogram_agg.h" |
| 2 |
|
| #include "histogram.h" |
| 3 |
|
| |
| 4 |
|
| double |
| 5 |
10 |
| histo_mean(pTHX_ simple_histo_1d* self) |
| 6 |
|
| { |
| 7 |
|
| double x; |
| 8 |
|
| double* data; |
| 9 |
|
| unsigned int i, n; |
| 10 |
|
| double retval = 0.; |
| 11 |
|
| |
| 12 |
10 |
| data = self->data; |
| 13 |
10 |
| n = self->nbins; |
| 14 |
10 |
▶ | if (self->bins == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->bins == NULL) { |
1 |
9 |
10 |
|
| 15 |
9 |
| const double binsize = self->binsize; |
| 16 |
9 |
| x = self->min + 0.5*binsize; |
| 17 |
900009 |
▶ | for (i = 0; i < n; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; ++i) { |
900000 |
9 |
900009 |
|
| 18 |
900000 |
| retval += data[i] * x; |
| 19 |
900000 |
| x += binsize; |
| 20 |
|
| } |
| 21 |
|
| } |
| 22 |
|
| else { /* non-constant binsize */ |
| 23 |
|
| const double* bins = self->bins; |
| 24 |
11 |
▶ | for (i = 0; i < n; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; ++i) { |
10 |
1 |
11 |
|
| 25 |
10 |
| x = 0.5*(bins[i] + bins[i+1]); |
| 26 |
10 |
| retval += data[i] * x; |
| 27 |
|
| } |
| 28 |
|
| } |
| 29 |
10 |
| retval /= self->total; |
| 30 |
|
| |
| 31 |
10 |
| return retval; |
| 32 |
|
| } |
| 33 |
|
| |
| 34 |
|
| double |
| 35 |
17 |
| histo_median(pTHX_ simple_histo_1d* self) |
| 36 |
|
| { |
| 37 |
|
| simple_histo_1d* cum_hist; |
| 38 |
|
| double* data; |
| 39 |
|
| unsigned int i, n, median_bin; |
| 40 |
|
| double sum_below, sum_above, x; |
| 41 |
|
| |
| 42 |
17 |
▶ | HS_ASSERT_CUMULATIVE(self); |
Branch: 1 branch
| Branch | True | False | Total |
| HS_ASSERT_CUMULATIVE(self); |
10 |
7 |
17 |
|
| 43 |
17 |
| cum_hist = self->cumulative_hist; |
| 44 |
17 |
| data = self->data; |
| 45 |
17 |
| n = self->nbins; |
| 46 |
|
| /* The bin which is >= 0.5, thus the +1 */ |
| 47 |
17 |
▶ | if (cum_hist->data[0] >= 0.5) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cum_hist->data[0] >= 0.5) |
14 |
3 |
17 |
|
| 48 |
|
| median_bin = 0; |
| 49 |
|
| else |
| 50 |
14 |
| median_bin = 1+find_bin_nonconstant(0.5, cum_hist->nbins, cum_hist->data); |
| 51 |
|
| |
| 52 |
|
| sum_below = 0.; |
| 53 |
380014 |
▶ | for (i = 0; i < median_bin; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < median_bin; ++i) |
379997 |
17 |
380014 |
|
| 54 |
379997 |
| sum_below += data[i]; |
| 55 |
|
| sum_above = 0.; |
| 56 |
1320003 |
▶ | for (i = median_bin+1; i < n; ++i) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = median_bin+1; i < n; ++i) |
1319986 |
17 |
1320003 |
|
| 57 |
1319986 |
| sum_above += data[i]; |
| 58 |
|
| /* The fraction of the median bin that is below the estimated median */ |
| 59 |
17 |
| x = 0.5 * ( (sum_above-sum_below)/data[median_bin] + 1 ); |
| 60 |
|
| |
| 61 |
|
| /* median estimate = lower boundary of median bin + x * median bin size */ |
| 62 |
17 |
▶ | if (self->bins == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->bins == 0) |
17 |
0 |
17 |
|
| 63 |
17 |
| return self->min + ( (double)median_bin + x ) * self->binsize; |
| 64 |
|
| else /* variable bin sizes */ |
| 65 |
0 |
| return self->bins[median_bin] + (self->bins[median_bin+1] - self->bins[median_bin]) * x; |
| 66 |
|
| } |
| 67 |
|
| |
| 68 |
|
| double |
| 69 |
4 |
| histo_standard_deviation(pTHX_ simple_histo_1d* self) |
| 70 |
|
| { |
| 71 |
4 |
| const double mean = histo_mean(aTHX_ self); |
| 72 |
4 |
| return histo_standard_deviation_with_mean(aTHX_ self, mean); |
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
|
| double |
| 76 |
4 |
| histo_standard_deviation_with_mean(pTHX_ simple_histo_1d* self, double mean) |
| 77 |
|
| { |
| 78 |
|
| /* sqrt( (1/n) * sum( x_i - x_mean )^2 ) */ |
| 79 |
|
| |
| 80 |
|
| double x; |
| 81 |
|
| double* data; |
| 82 |
|
| unsigned int i, n; |
| 83 |
|
| double retval = 0.; |
| 84 |
|
| |
| 85 |
4 |
| data = self->data; |
| 86 |
4 |
| n = self->nbins; |
| 87 |
4 |
▶ | if (self->bins == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (self->bins == NULL) { |
0 |
4 |
4 |
|
| 88 |
4 |
| const double binsize = self->binsize; |
| 89 |
4 |
| x = self->min + 0.5*binsize; |
| 90 |
400004 |
▶ | for (i = 0; i < n; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; ++i) { |
400000 |
4 |
400004 |
|
| 91 |
400000 |
| retval += data[i] * (x - mean) * (x - mean); |
| 92 |
400000 |
| x += binsize; |
| 93 |
|
| } |
| 94 |
|
| } |
| 95 |
|
| else { /* non-constant binsize */ |
| 96 |
|
| const double* bins = self->bins; |
| 97 |
0 |
▶ | for (i = 0; i < n; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; ++i) { |
0 |
0 |
0 |
|
| 98 |
0 |
| x = 0.5*(bins[i] + bins[i+1]); |
| 99 |
0 |
| retval += data[i] * (x - mean) * (x - mean); |
| 100 |
|
| } |
| 101 |
|
| } |
| 102 |
|
| |
| 103 |
4 |
| return sqrt(retval/(double)self->total); |
| 104 |
|
| } |