diff --git a/grain_size_tools/GrainSizeTools_script.py b/grain_size_tools/GrainSizeTools_script.py index 24ae86b..ac76536 100644 --- a/grain_size_tools/GrainSizeTools_script.py +++ b/grain_size_tools/GrainSizeTools_script.py @@ -21,13 +21,6 @@ # For details see: http://marcoalopez.github.io/GrainSizeTools/ # # download at https://github.com/marcoalopez/GrainSizeTools/releases # # # -# Requirements: # -# Python v3.6 or higher # -# Numpy v1.11 or higher # -# Matplotlib v2.0 or higher # -# Scipy v1.0 or higher # -# Pandas v0.16 or higher # -# # # ============================================================================ # # import grain_size_tools modules @@ -87,6 +80,54 @@ def conf_interval(data, confidence=0.95): return amean, err, (low, high) +def weighted_mean_and_se(means, standard_errors): + """ + Calculate the weighted mean and standard error of averages + using the Mantel-Haenszel method. + + Parameters + ---------- + means : numpy.ndarray + 1-D array containing the averages. + standard_errors : numpy.ndarray + 1-D array containing the standard errors associated + with each average. + + Returns + ------- + float + The weighted mean of averages. + float + The standard error of the weighted mean. + + Raises + ------ + ValueError + If input arrays have different shapes. + + Notes + ----- + The function uses the Mantel-Haenszel method to calculate + the weighted mean, where each average is weighted by the + inverse of its squared standard error. The standard error + of the weighted mean is also calculated. + """ + # Ensure the input arrays have the same shape + if means.shape != standard_errors.shape: + raise ValueError("Input arrays must have the same shape") + + # Calculate the weights based on the inverse of squared standard errors + weights = 1 / standard_errors**2 + + # Calculate the weighted mean + weighted_mean = np.sum(means * weights) / np.sum(weights) + + # Calculate the standard error of the weighted mean + se_weighted_mean = 1 / np.sqrt(np.sum(1 / standard_errors**2)) + + return weighted_mean, se_weighted_mean + + def summarize(data, avg=('amean', 'gmean', 'median', 'mode'), ci_level=0.95, bandwidth='silverman', precision=0.1): """ Estimate different grain size statistics. This includes different means,