Commit d3380e24 authored by Morten Welinder's avatar Morten Welinder

COVARIANCE.S

One of several we're going to need to match Excel 2010's silliness.
parent bce3b046
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
......
......@@ -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
......
......@@ -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 },
......
......@@ -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"/>
......
......@@ -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);
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment