Skip to content

Commit

Permalink
Add new function
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoalopez committed Jan 30, 2024
1 parent 9217498 commit 4b5aabd
Showing 1 changed file with 48 additions and 7 deletions.
55 changes: 48 additions & 7 deletions grain_size_tools/GrainSizeTools_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 4b5aabd

Please sign in to comment.