[gnumeric] COVARIANCE.S



commit d3380e24ef7259917ccd686416de94ae25609984
Author: Morten Welinder <terra gnome org>
Date:   Sun Mar 25 15:52:14 2012 -0400

    COVARIANCE.S
    
    One of several we're going to need to match Excel 2010's silliness.

 ChangeLog                     |    6 ++++++
 NEWS                          |    1 +
 plugins/fn-stat/functions.c   |   37 +++++++++++++++++++++++++++++++++++--
 plugins/fn-stat/plugin.xml.in |    1 +
 src/rangefunc.c               |   22 +++++++++++++++++++---
 src/rangefunc.h               |    3 ++-
 6 files changed, 64 insertions(+), 6 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index de2f78b..5143eae 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2012-03-25  Morten Welinder  <terra gnome org>
+
+	* src/rangefunc.c (gnm_range_covar_pop): Rename from
+	gnm_range_covar.
+	(gnm_range_covar_est): New function.
+
 2012-03-23  Morten Welinder  <terra gnome org>
 
 	* src/sheet-object-image.c (content_end): Simplify.  Decode image
diff --git a/NEWS b/NEWS
index 93fbf57..ab169b7 100644
--- a/NEWS
+++ b/NEWS
@@ -47,6 +47,7 @@ Morten:
 	* Fix pattern matching for VLOOKUP/HLOOKUP/MATCH.
 	* Improve test suite for ods.
 	* Fix scrollbar/spinbutton load-from-.gnumeric.  [Part of #672716]
+	* New function COVARIANCE.S.
 
 --------------------------------------------------------------------------
 Gnumeric 1.11.2
diff --git a/plugins/fn-stat/functions.c b/plugins/fn-stat/functions.c
index 7622ac2..a998f5b 100644
--- a/plugins/fn-stat/functions.c
+++ b/plugins/fn-stat/functions.c
@@ -337,7 +337,37 @@ gnumeric_covar (GnmFuncEvalInfo *ei, GnmValue const * const *argv)
 {
 	return float_range_function2 (argv[0], argv[1],
 				      ei,
-				      gnm_range_covar,
+				      gnm_range_covar_pop,
+				      COLLECT_IGNORE_BLANKS |
+				      COLLECT_IGNORE_STRINGS |
+				      COLLECT_IGNORE_BOOLS,
+				      GNM_ERROR_VALUE);
+}
+
+/***************************************************************************/
+
+static GnmFuncHelp const help_covariance_s[] = {
+	{ GNM_FUNC_HELP_NAME, F_("COVARIANCE.S:sample covariance of two data sets")},
+	{ GNM_FUNC_HELP_ARG, F_("array1:first data set")},
+	{ GNM_FUNC_HELP_ARG, F_("array2:set data set")},
+	{ GNM_FUNC_HELP_DESCRIPTION, F_("Strings and empty cells are simply ignored.") },
+	{ GNM_FUNC_HELP_EXCEL, F_("This function is Excel compatible.") },
+	{ GNM_FUNC_HELP_EXAMPLES, F_("Let us assume that the cells A1, A2, ..., A5 contain numbers "
+	   "11.4, 17.3, 21.3, 25.9, and 40.1, and the cells B1, B2, ... "
+	   "B5 23.2, 25.8, 29.9, 33.5, and 42.7.") },
+	{ GNM_FUNC_HELP_EXAMPLES, F_("Then COVAR(A1:A5,B1:B5) equals 65.858.") },
+	{ GNM_FUNC_HELP_SEEALSO, "COVAR,CORREL"},
+	{ GNM_FUNC_HELP_EXTREF, F_("wiki:en:Covariance") },
+	{ GNM_FUNC_HELP_EXTREF, F_("wolfram:Covariance.html") },
+	{ GNM_FUNC_HELP_END }
+};
+
+static GnmValue *
+gnumeric_covariance_s (GnmFuncEvalInfo *ei, GnmValue const * const *argv)
+{
+	return float_range_function2 (argv[0], argv[1],
+				      ei,
+				      gnm_range_covar_est,
 				      COLLECT_IGNORE_BLANKS |
 				      COLLECT_IGNORE_STRINGS |
 				      COLLECT_IGNORE_BOOLS,
@@ -4271,7 +4301,7 @@ gnumeric_cronbach (GnmFuncEvalInfo *ei, int argc, GnmExprConstPtr const *argv)
 			GnmValue *fl_val;
 			fl_val = float_range_function2 (values[i], values[j],
 							ei,
-							gnm_range_covar, 0,
+							gnm_range_covar_pop, 0,
 							GNM_ERROR_VALUE);
 			if (!VALUE_IS_NUMBER (fl_val)) {
 				free_values (values, argc);
@@ -4963,6 +4993,9 @@ GnmFuncDescriptor const stat_functions[] = {
         { "covar",        "AA",
 	  help_covar, gnumeric_covar, NULL, NULL, NULL, NULL,
 	  GNM_FUNC_SIMPLE, GNM_FUNC_IMPL_STATUS_COMPLETE, GNM_FUNC_TEST_STATUS_BASIC },
+        { "covariance.s", "AA",
+	  help_covariance_s, gnumeric_covariance_s, NULL, NULL, NULL, NULL,
+	  GNM_FUNC_SIMPLE, GNM_FUNC_IMPL_STATUS_COMPLETE, GNM_FUNC_TEST_STATUS_BASIC },
 	{ "critbinom",    "fff",
 	  help_critbinom, gnumeric_critbinom, NULL, NULL, NULL, NULL,
 	  GNM_FUNC_SIMPLE, GNM_FUNC_IMPL_STATUS_COMPLETE, GNM_FUNC_TEST_STATUS_BASIC },
diff --git a/plugins/fn-stat/plugin.xml.in b/plugins/fn-stat/plugin.xml.in
index 5d6cd85..020db5b 100644
--- a/plugins/fn-stat/plugin.xml.in
+++ b/plugins/fn-stat/plugin.xml.in
@@ -31,6 +31,7 @@
 				<function name="cronbach"/>
 				<function name="correl"/>
 				<function name="covar"/>
+				<function name="covariance.s"/>
 				<function name="cvmtest"/>
 				<function name="devsq"/>
 				<function name="exppowdist"/>
diff --git a/src/rangefunc.c b/src/rangefunc.c
index 06e10fc..9ee2c5b 100644
--- a/src/rangefunc.c
+++ b/src/rangefunc.c
@@ -340,9 +340,9 @@ gnm_range_multinomial (gnm_float const *xs, int n, gnm_float *res)
 	return 0;
 }
 
-/* Co-variance.  */
+/* Population co-variance.  */
 int
-gnm_range_covar (gnm_float const *xs, const gnm_float *ys, int n, gnm_float *res)
+gnm_range_covar_pop (gnm_float const *xs, const gnm_float *ys, int n, gnm_float *res)
 {
 	gnm_float ux, uy, s = 0;
 	int i;
@@ -356,6 +356,22 @@ gnm_range_covar (gnm_float const *xs, const gnm_float *ys, int n, gnm_float *res
 	return 0;
 }
 
+/* Estimation co-variance.  */
+int
+gnm_range_covar_est (gnm_float const *xs, const gnm_float *ys, int n, gnm_float *res)
+{
+	gnm_float ux, uy, s = 0;
+	int i;
+
+	if (n <= 1 || gnm_range_average (xs, n, &ux) || gnm_range_average (ys, n, &uy))
+		return 1;
+
+	for (i = 0; i < n; i++)
+		s += (xs[i] - ux) * (ys[i] - uy);
+	*res = s / (n - 1);
+	return 0;
+}
+
 /* Population correlation coefficient.  */
 int
 gnm_range_correl_pop (gnm_float const *xs, const gnm_float *ys, int n, gnm_float *res)
@@ -364,7 +380,7 @@ gnm_range_correl_pop (gnm_float const *xs, const gnm_float *ys, int n, gnm_float
 
 	if (gnm_range_stddev_pop (xs, n, &sx) || sx == 0 ||
 	    gnm_range_stddev_pop (ys, n, &sy) || sy == 0 ||
-	    gnm_range_covar (xs, ys, n, &vxy))
+	    gnm_range_covar_pop (xs, ys, n, &vxy))
 		return 1;
 
 	*res = vxy / (sx * sy);
diff --git a/src/rangefunc.h b/src/rangefunc.h
index bfbfb01..772a1bf 100644
--- a/src/rangefunc.h
+++ b/src/rangefunc.h
@@ -53,7 +53,8 @@ int gnm_range_skew_est	(gnm_float const *xs, int n, gnm_float *res);
 int gnm_range_kurtosis_m3_pop (gnm_float const *xs, int n, gnm_float *res);
 int gnm_range_kurtosis_m3_est (gnm_float const *xs, int n, gnm_float *res);
 
-int gnm_range_covar		(gnm_float const *xs, const gnm_float *ys, int n, gnm_float *res);
+int gnm_range_covar_pop		(gnm_float const *xs, const gnm_float *ys, int n, gnm_float *res);
+int gnm_range_covar_est		(gnm_float const *xs, const gnm_float *ys, int n, gnm_float *res);
 int gnm_range_correl_pop	(gnm_float const *xs, const gnm_float *ys, int n, gnm_float *res);
 int gnm_range_rsq_pop	(gnm_float const *xs, const gnm_float *ys, int n, gnm_float *res);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]