Author: bugman
Date: Fri Jun 26 09:56:16 2015
New Revision: 27861
URL: http://svn.gna.org/viewcvs/relax?rev=27861&view=rev
Log:
Added functions for calculating the geometric mean and standard deviation to
the relax library.
These are the geometric_mean() and geometric_std() functions of the
lib.statistics module. The
implementation is designed to be fast, using numpy array arithmetic rather than
Python loops.
Modified:
trunk/lib/statistics.py
Modified: trunk/lib/statistics.py
URL:
http://svn.gna.org/viewcvs/relax/trunk/lib/statistics.py?rev=27861&r1=27860&r2=27861&view=diff
==============================================================================
--- trunk/lib/statistics.py (original)
+++ trunk/lib/statistics.py Fri Jun 26 09:56:16 2015
@@ -1,6 +1,6 @@
###############################################################################
# #
-# Copyright (C) 2013 Edward d'Auvergne #
+# Copyright (C) 2013-2015 Edward d'Auvergne #
# Copyright (C) 2014 Troels E. Linnet #
# #
# This file is part of the program relax (http://www.nmr-relax.com). #
@@ -24,7 +24,8 @@
"""Module for calculating simple statistics."""
# Python module imports.
-from numpy import absolute, diag, dot, eye, multiply, transpose
+from math import exp
+from numpy import absolute, array, diag, dot, eye, float64, log, multiply,
transpose
from numpy.linalg import inv, qr
# Python module imports.
@@ -101,6 +102,67 @@
return exp(-(x-mu)**2 / (2.0*sigma**2)) / (sigma * sqrt(2.0 * pi))
+def geometric_mean(values=None):
+ """Calculate the geometric mean for the given values.
+
+ This uses the real mean to normalise all values to be centred at 1, so
that truncation artifacts in the large multiplication are avoided.
+
+
+ @keyword values: The list of values to calculate the geometric mean of.
+ @type values: list of float
+ @return: The geometric mean.
+ @rtype: float
+ """
+
+ # Init.
+ n = len(values)
+ values = array(values, float64)
+
+ # The log of all values.
+ log_vals = log(values)
+
+ # Calculate the sum of the log values for all points.
+ Xsum = log_vals.sum()
+
+ # Average.
+ Xav = Xsum / float(n)
+
+ # Return the geometric mean.
+ return exp(Xav)
+
+
+def geometric_std(values=None, mean=None):
+ """Calculate the geometric standard deviation for the given values.
+
+ This uses the real mean to normalise all values to be centred at 1, so
that truncation artifacts in the large multiplication are avoided.
+
+
+ @keyword values: The list of values to calculate the geometric mean of.
+ @type values: list of float
+ @keyword mean: The pre-calculated geometric mean. If not supplied,
the value will be calculated.
+ @type mean: float
+ @return: The geometric mean.
+ @rtype: float
+ """
+
+ # Init.
+ n = len(values)
+ values = array(values, float64)
+
+ # The geometric mean.
+ if mean == None:
+ mean = geometric_mean(values=values)
+
+ # Divide the values by the geometric mean, take the log, and square
everything.
+ factor = (log(values / mean))**2
+
+ # The square root of the sum divided by n.
+ factor2 = sqrt(factor.sum() / n)
+
+ # Return the geometric std.
+ return exp(factor2)
+
+
def std(values=None, skip=None, dof=1):
"""Calculate the standard deviation of the given values, skipping values
if asked.
_______________________________________________
relax (http://www.nmr-relax.com)
This is the relax-commits mailing list
[email protected]
To unsubscribe from this list, get a password
reminder, or change your subscription options,
visit the list information page at
https://mail.gna.org/listinfo/relax-commits