Skip to content

Commit

Permalink
lib: stats-dist - Add variance
Browse files Browse the repository at this point in the history
  • Loading branch information
cmouse committed Aug 7, 2018
1 parent 75c248e commit 4720956
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/lib/stats-dist.c
Expand Up @@ -121,6 +121,24 @@ uint64_t stats_dist_get_median(const struct stats_dist *stats)
return (stats->samples[idx1] + stats->samples[idx2]) / 2;
}

double stats_dist_get_variance(const struct stats_dist *stats)
{
double sum = 0;
if (stats->count == 0)
return 0;

double avg = (double)(int64_t)stats_dist_get_avg(stats);
double count = (stats->count < stats->sample_count)
? stats->count
: stats->sample_count;

for(unsigned int i = 0; i < count; i++) {
sum += (stats->samples[i] - avg)*(stats->samples[i] - avg);
}

return sum / count;
}

/* This is independent of the stats framework, useful for any selection task */
static unsigned int stats_dist_get_index(unsigned int range, double fraction)
{
Expand Down
2 changes: 2 additions & 0 deletions src/lib/stats-dist.h
Expand Up @@ -24,6 +24,8 @@ uint64_t stats_dist_get_max(const struct stats_dist *stats);
uint64_t stats_dist_get_avg(const struct stats_dist *stats);
/* Returns events' approximate (through random subsampling) median. */
uint64_t stats_dist_get_median(const struct stats_dist *stats);
/* Returns events' variance */
double stats_dist_get_variance(const struct stats_dist *stats);
/* Returns events' approximate (through random subsampling) percentile.
fraction parameter is in the range (0., 1.], so 95th %-ile is 0.95. */
uint64_t stats_dist_get_percentile(const struct stats_dist *stats, double fraction);
Expand Down

0 comments on commit 4720956

Please sign in to comment.