Skip to contentSkip to conversation

stats: Be more precise and talk about mean, not average

PATCH In review Damien Lespiau 2 participants 6 messages Jun 25, 2015 – Jun 26, 2015 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 stats: Be more precise and talk about mean, not average

There are several types of averages eg. mean, median and mode.

---
 lib/igt_stats.c           |  2 +-
 lib/igt_stats.h           |  2 +-
 lib/tests/igt_stats.c     | 10 +++++-----
 tools/skl_compute_wrpll.c |  3 ++-
 4 files changed, 9 insertions(+), 8 deletions(-)
lib/igt_stats.c +1 -1
index fcf7abe..e6babde 100644
@@ -44,7 +44,7 @@ void igt_stats_push(igt_stats_t *stats, uint64_t value)
 	stats->values[stats->n_values++] = value;
 }
 
-double igt_stats_get_average(igt_stats_t *stats)
+double igt_stats_get_mean(igt_stats_t *stats)
 {
 	unsigned int i;
 	double a = 0.0;
lib/igt_stats.h +1 -1
index 99dc1c9..a46b399 100644
@@ -33,4 +33,4 @@ typedef struct {
 void igt_stats_init(igt_stats_t *stats, unsigned int capacity);
 void igt_stats_fini(igt_stats_t *stats);
 void igt_stats_push(igt_stats_t *stats, uint64_t value);
-double igt_stats_get_average(igt_stats_t *stats);
+double igt_stats_get_mean(igt_stats_t *stats);
lib/tests/igt_stats.c +5 -5
index e75a203..3f45314 100644
@@ -25,10 +25,10 @@
 #include "igt_core.h"
 #include "igt_stats.h"
 
-static void test_average(void)
+static void test_mean(void)
 {
 	igt_stats_t stats;
-	double average;
+	double mean;
 
 	igt_stats_init(&stats, 5);
 
@@ -38,12 +38,12 @@ static void test_average(void)
 	igt_stats_push(&stats, 8);
 	igt_stats_push(&stats, 10);
 
-	average = igt_stats_get_average(&stats);
+	mean = igt_stats_get_mean(&stats);
 
-	igt_assert(average == (2 + 4 + 6 + 8 + 10) / 5.);
+	igt_assert(mean == (2 + 4 + 6 + 8 + 10) / 5.);
 }
 
 igt_simple_main
 {
-	test_average();
+	test_mean();
 }
tools/skl_compute_wrpll.c +2 -1
index 9245724..cf808c7 100644
@@ -917,7 +917,8 @@ static void test_run(struct test_ops *test)
 	}
 
 	printf("even/odd dividers: %d/%d\n", p_odd_even[0], p_odd_even[1]);
-	printf("average deviation: %.2lf\n", igt_stats_get_average(&stats));
+	printf("mean central freq deviation: %.2lf\n",
+	       igt_stats_get_mean(&stats));
 
 	igt_stats_fini(&stats);
 }
Signature · 6 lines
2.1.0

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#1 lore raw

PATCH 2/5 tests/igt_stats: Call igt_stats_fini() to not leak the array

Sure, it'll be freed at exit(), but might as well be a bit pedantic.

---
 lib/tests/igt_stats.c | 2 ++
 1 file changed, 2 insertions(+)
lib/tests/igt_stats.c +2 -0
index 3f45314..c20b453 100644
@@ -41,6 +41,8 @@ static void test_mean(void)
 	mean = igt_stats_get_mean(&stats);
 
 	igt_assert(mean == (2 + 4 + 6 + 8 + 10) / 5.);
+
+	igt_stats_fini(&stats);
 }
 
 igt_simple_main
Signature · 6 lines
2.1.0

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#2 lore raw

Re: [PATCH i-g-t 2/5] tests/igt_stats: Call igt_stats_fini() to not leak the array

Quoted 1 line · Damien Lespiau

Sure, it'll be freed at exit(), but might as well be a bit pedantic.

s/pedantic/pedagological/

Tests also serve as examples and will undoubtably be copy-pasted at some point.
-Chris

Signature · 5 lines
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#3 lore raw

PATCH 3/5 stats: Use an algorithm popularised by Knuth to compute mean and variance

In reply to Damien Lespiau
---
 lib/igt_stats.c | 42 +++++++++++++++++++++++++++++++++++++-----
 lib/igt_stats.h |  3 +++
 2 files changed, 40 insertions(+), 5 deletions(-)
lib/igt_stats.c +37 -5
index e6babde..20db806 100644
@@ -42,15 +42,47 @@ void igt_stats_push(igt_stats_t *stats, uint64_t value)
 {
 	igt_assert(stats->n_values < stats->capacity);
 	stats->values[stats->n_values++] = value;
+	stats->mean_variance_valid = false;
 }
 
-double igt_stats_get_mean(igt_stats_t *stats)
+/*
+ * Algorithm popularised by Knuth in:
+ *
+ * The Art of Computer Programming, volume 2: Seminumerical Algorithms,
+ * 3rd edn., p. 232. Boston: Addison-Wesley
+ *
+ * Source: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
+ */
+static void igt_stats_knuth_mean_variance(igt_stats_t *stats)
 {
+	double mean = 0., m2 = 0.;
 	unsigned int i;
-	double a = 0.0;
 
-	for (i = 0; i < stats->n_values; i++)
-		a += (double)stats->values[i] / stats->n_values;
+	if (stats->mean_variance_valid)
+		return;
+
+	for (i = 0; i < stats->n_values; i++) {
+		double delta = stats->values[i] - mean;
+
+		mean += delta / (i + 1);
+		m2 += delta * (stats->values[i] - mean);
+	}
+
+	stats->mean = mean;
+	stats->variance = m2 / stats->n_values;
+	stats->mean_variance_valid = true;
+}
+
+double igt_stats_get_mean(igt_stats_t *stats)
+{
+	igt_stats_knuth_mean_variance(stats);
+
+	return stats->mean;
+}
+
+double igt_stats_get_variance(igt_stats_t *stats)
+{
+	igt_stats_knuth_mean_variance(stats);
 
-	return a;
+	return stats->variance;
 }
lib/igt_stats.h +3 -0
index a46b399..acb86a6 100644
@@ -28,9 +28,12 @@ typedef struct {
 	uint64_t *values;
 	unsigned int capacity;
 	unsigned int n_values;
+	unsigned int mean_variance_valid : 1;
+	double mean, variance;
 } igt_stats_t;
 
 void igt_stats_init(igt_stats_t *stats, unsigned int capacity);
 void igt_stats_fini(igt_stats_t *stats);
 void igt_stats_push(igt_stats_t *stats, uint64_t value);
 double igt_stats_get_mean(igt_stats_t *stats);
+double igt_stats_get_variance(igt_stats_t *stats);
Signature · 6 lines
2.1.0

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#4 lore raw

PATCH 4/5 stats: Add a way to retrieve the standard deviation

In reply to Damien Lespiau
---
 lib/igt_stats.c              |  9 +++++++++
 lib/igt_stats.h              |  1 +
 lib/tests/Makefile.am        |  2 +-
 lib/tests/igt_stats.c        | 33 +++++++++++++++++++++++++++++++++
 tools/Makefile.am            |  2 +-
 tools/quick_dump/Makefile.am |  1 +
 6 files changed, 46 insertions(+), 2 deletions(-)
lib/igt_stats.c +9 -0
index 20db806..65c0ac6 100644
@@ -22,6 +22,8 @@
  *
  */
 
+#include <math.h>
+
 #include "igt_core.h"
 #include "igt_stats.h"
 
@@ -86,3 +88,10 @@ double igt_stats_get_variance(igt_stats_t *stats)
 
 	return stats->variance;
 }
+
+double igt_stats_get_std_deviation(igt_stats_t *stats)
+{
+	igt_stats_knuth_mean_variance(stats);
+
+	return sqrt(stats->variance);
+}
lib/igt_stats.h +1 -0
index acb86a6..371def3 100644
@@ -37,3 +37,4 @@ void igt_stats_fini(igt_stats_t *stats);
 void igt_stats_push(igt_stats_t *stats, uint64_t value);
 double igt_stats_get_mean(igt_stats_t *stats);
 double igt_stats_get_variance(igt_stats_t *stats);
+double igt_stats_get_std_deviation(igt_stats_t *stats);
lib/tests/Makefile.am +1 -1
index e59065b..938d2ab 100644
@@ -15,5 +15,5 @@ AM_CFLAGS = $(DRM_CFLAGS) $(CWARNFLAGS) \
 
 LDADD = ../libintel_tools.la $(PCIACCESS_LIBS) $(DRM_LIBS) $(LIBUNWIND_LIBS)
 
-LDADD += $(CAIRO_LIBS) $(LIBUDEV_LIBS) $(GLIB_LIBS)
+LDADD += $(CAIRO_LIBS) $(LIBUDEV_LIBS) $(GLIB_LIBS) -lm
 AM_CFLAGS += $(CAIRO_CFLAGS) $(LIBUDEV_CFLAGS) $(GLIB_CFLAGS)
lib/tests/igt_stats.c +33 -0
index c20b453..172e4b3 100644
@@ -45,7 +45,40 @@ static void test_mean(void)
 	igt_stats_fini(&stats);
 }
 
+/*
+ * Taken from the "Basic examples" section of:
+ * https://en.wikipedia.org/wiki/Standard_deviation
+ */
+static void test_std_deviation(void)
+{
+	igt_stats_t stats;
+	double mean, variance, std_deviation;
+
+	igt_stats_init(&stats, 8);
+
+	igt_stats_push(&stats, 2);
+	igt_stats_push(&stats, 4);
+	igt_stats_push(&stats, 4);
+	igt_stats_push(&stats, 4);
+	igt_stats_push(&stats, 5);
+	igt_stats_push(&stats, 5);
+	igt_stats_push(&stats, 7);
+	igt_stats_push(&stats, 9);
+
+	mean = igt_stats_get_mean(&stats);
+	igt_assert(mean == (2 + 3 * 4 + 2 * 5 + 7 + 9) / 8.);
+
+	variance = igt_stats_get_variance(&stats);
+	igt_assert(variance == 4);
+
+	std_deviation = igt_stats_get_std_deviation(&stats);
+	igt_assert(std_deviation == 2);
+
+	igt_stats_fini(&stats);
+}
+
 igt_simple_main
 {
 	test_mean();
+	test_std_deviation();
 }
tools/Makefile.am +1 -1
index 04bfd12..8288248 100644
@@ -8,5 +8,5 @@ endif
 
 AM_CPPFLAGS = -I$(top_srcdir) -I$(top_srcdir)/lib
 AM_CFLAGS = $(DRM_CFLAGS) $(PCIACCESS_CFLAGS) $(CWARNFLAGS) $(CAIRO_CFLAGS) $(LIBUNWIND_CFLAGS)
-LDADD = $(top_builddir)/lib/libintel_tools.la $(DRM_LIBS) $(PCIACCESS_LIBS) $(CAIRO_LIBS) $(LIBUDEV_LIBS) $(LIBUNWIND_LIBS)
+LDADD = $(top_builddir)/lib/libintel_tools.la $(DRM_LIBS) $(PCIACCESS_LIBS) $(CAIRO_LIBS) $(LIBUDEV_LIBS) $(LIBUNWIND_LIBS) -lm
 
tools/quick_dump/Makefile.am +1 -0
index 3d0bd23..0643a81 100644
@@ -15,6 +15,7 @@ I915ChipsetPython_la_LIBADD =			\
 	$(CAIRO_LIBS)				\
 	$(LIBUNWIND_LIBS)			\
 	-lrt					\
+	-lm					\
 	$(NULL)
 
 chipset.py: chipset_wrap_python.c
Signature · 6 lines
2.1.0

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#5 lore raw

PATCH 5/5 tests/stats: Make sure we properly invalidate the cached mean

In reply to Damien Lespiau

Sure, that's an implementation details, but make sure we do recompute the mean when we add a new value.

---
 lib/tests/igt_stats.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
lib/tests/igt_stats.c +25 -0
index 172e4b3..f9081c7 100644
@@ -45,6 +45,30 @@ static void test_mean(void)
 	igt_stats_fini(&stats);
 }
 
+static void test_invalidate_mean(void)
+{
+	igt_stats_t stats;
+	double mean1, mean2;
+
+	igt_stats_init(&stats, 6);
+
+	igt_stats_push(&stats, 2);
+	igt_stats_push(&stats, 4);
+	igt_stats_push(&stats, 6);
+	igt_stats_push(&stats, 8);
+	igt_stats_push(&stats, 10);
+
+	mean1 = igt_stats_get_mean(&stats);
+	igt_assert(mean1 == (2 + 4 + 6 + 8 + 10) / 5.);
+
+	igt_stats_push(&stats, 100);
+
+	mean2 = igt_stats_get_mean(&stats);
+	igt_assert(mean1 != mean2);
+
+	igt_stats_fini(&stats);
+}
+
 /*
  * Taken from the "Basic examples" section of:
  * https://en.wikipedia.org/wiki/Standard_deviation
@@ -80,5 +104,6 @@ static void test_std_deviation(void)
 igt_simple_main
 {
 	test_mean();
+	test_invalidate_mean();
 	test_std_deviation();
 }
Signature · 6 lines
2.1.0

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx