Author: bugman Date: Fri Apr 19 19:52:10 2013 New Revision: 19494 URL: http://svn.gna.org/viewcvs/relax?rev=19494&view=rev Log: Shifted the standard deviation code from the Monte Carlo error_analysis() function to the lib package. The new function lib.stats.std() is now used to simplify the error_analysis() function and allow the code to be reused. This will be useful for expanding the pipe_control.monte_carlo.error_analysis() function to handle parameters which are dictionaries, for example as in the relax_disp branch. Added: trunk/lib/stats.py Modified: trunk/lib/__init__.py trunk/pipe_control/monte_carlo.py Modified: trunk/lib/__init__.py URL: http://svn.gna.org/viewcvs/relax/trunk/lib/__init__.py?rev=19494&r1=19493&r2=19494&view=diff ============================================================================== --- trunk/lib/__init__.py (original) +++ trunk/lib/__init__.py Fri Apr 19 19:52:10 2013 @@ -44,6 +44,7 @@ 'regex', 'selection', 'spectral_densities', + 'stats', 'structure', 'text', 'warnings' Added: trunk/lib/stats.py URL: http://svn.gna.org/viewcvs/relax/trunk/lib/stats.py?rev=19494&view=auto ============================================================================== --- trunk/lib/stats.py (added) +++ trunk/lib/stats.py Fri Apr 19 19:52:10 2013 @@ -1,0 +1,85 @@ +############################################################################### +# # +# Copyright (C) 2013 Edward d'Auvergne # +# # +# This file is part of the program relax (http://www.nmr-relax.com). # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <http://www.gnu.org/licenses/>. # +# # +############################################################################### + +# Module docstring. +"""Module for calculating simple statistics.""" + +# Python module imports. +from math import sqrt + + +def std(values=None, skip=None, dof=1): + """Calculate the standard deviation of the given values, skipping values if asked. + + @keyword values: The list of values to calculate the standard deviation of. + @type values: list of float + @keyword skip: An optional list of booleans specifying if a value should be skipped. The length of this list must match the values. An element of True will cause the corresponding value to not be included in the calculation. + @type skip: list of bool or None. + @keyword dof: The degrees of freedom, whereby the standard deviation is multipled by 1/(N - dof). + @type dof: int + @return: The standard deviation. + @rtype: float + """ + + # The total number of points. + n = 0 + for i in range(len(values)): + # Skip deselected values. + if skip != None and not skip[i]: + continue + + # Increment n. + n = n + 1 + + # Calculate the sum of the values for all points. + Xsum = 0.0 + for i in range(len(values)): + # Skip deselected values. + if skip != None and not skip[i]: + continue + + # Sum. + Xsum = Xsum + values[i] + + # Calculate the mean value for all points. + if n == 0: + Xav = 0.0 + else: + Xav = Xsum / float(n) + + # Calculate the sum part of the standard deviation. + sd = 0.0 + for i in range(len(values)): + # Skip deselected values. + if skip != None and not skip[i]: + continue + + # Sum. + sd = sd + (values[i] - Xav)**2 + + # Calculate the standard deviation. + if n <= 1: + sd = 0.0 + else: + sd = sqrt(sd / (float(n) - float(dof))) + + # Return the SD. + return sd Modified: trunk/pipe_control/monte_carlo.py URL: http://svn.gna.org/viewcvs/relax/trunk/pipe_control/monte_carlo.py?rev=19494&r1=19493&r2=19494&view=diff ============================================================================== --- trunk/pipe_control/monte_carlo.py (original) +++ trunk/pipe_control/monte_carlo.py Fri Apr 19 19:52:10 2013 @@ -24,14 +24,14 @@ # Python module imports. from copy import deepcopy -from math import sqrt from numpy import ndarray from random import gauss # relax module imports. +from lib.errors import RelaxError, RelaxNoSequenceError +from lib import stats from pipe_control.mol_res_spin import exists_mol_res_spin_data from pipe_control import pipes -from lib.errors import RelaxError, RelaxNoSequenceError from specific_analyses.setup import get_specific_fn @@ -156,49 +156,9 @@ if param_array == None: break - # Simulation parameters with values (ie not None). + # SD of simulation parameters with values (ie not None). if param_array[0] != None: - # The total number of simulations. - n = 0 - for i in range(len(param_array)): - # Skip deselected simulations. - if not select_sim[i]: - continue - - # Increment n. - n = n + 1 - - # Calculate the sum of the parameter value for all simulations. - Xsum = 0.0 - for i in range(len(param_array)): - # Skip deselected simulations. - if not select_sim[i]: - continue - - # Sum. - Xsum = Xsum + param_array[i] - - # Calculate the mean parameter value for all simulations. - if n == 0: - Xav = 0.0 - else: - Xav = Xsum / float(n) - - # Calculate the sum part of the standard deviation. - sd = 0.0 - for i in range(len(param_array)): - # Skip deselected simulations. - if not select_sim[i]: - continue - - # Sum. - sd = sd + (param_array[i] - Xav)**2 - - # Calculate the standard deviation. - if n <= 1: - sd = 0.0 - else: - sd = sqrt(sd / (float(n) - 1.0)) + sd = stats.std(values=param_array, skip=select_sim) # Simulation parameters with the value None. else: