CRAMERVONMISES_2SAMP
Overview
The CRAMERVONMISES_2SAMP
function performs the two-sample Cramér-von Mises test for goodness of fit between two independent samples. This test evaluates whether two samples come from the same continuous distribution, providing a statistic and p-value. It is more sensitive to differences in the tails of distributions than the Kolmogorov-Smirnov test. The test statistic is computed as:
where and are the empirical distribution functions of the two samples, and , are their sizes. For more details, see the scipy.stats.cramervonmises_2samp documentation .
This wrapper exposes only the most commonly used parameters: the two samples and the method for p-value calculation. Parameters related to array axis, NaN handling, and broadcasting are omitted for Excel compatibility. This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=CRAMERVONMISES_2SAMP(x, y, [method])
x
(2D list, required): First sample data. Must be a table with at least two rows, each containing one or more numeric values.y
(2D list, required): Second sample data. Must be a table with at least two rows, each containing one or more numeric values.method
(string, optional, default='auto'
): Method to compute p-value. Allowed values:'auto'
,'asymptotic'
,'exact'
.
The function returns a single-row 2D array: [statistic, pvalue]
, or an error message (string) if the input is invalid. The statistic quantifies the difference between distributions; the p-value indicates the probability of observing such a difference under the null hypothesis.
Examples
Example 1: Basic Case (auto method)
Inputs:
x | y |
---|---|
1.0 | 1.5 |
2.0 | 2.5 |
3.0 | 3.5 |
4.0 | 4.5 |
Excel formula:
=CRAMERVONMISES_2SAMP({1.0;2.0;3.0;4.0}, {1.5;2.5;3.5;4.5})
Expected output:
statistic | pvalue |
---|---|
0.063 | 1.000 |
Example 2: Asymptotic Method
Inputs:
x | y | method |
---|---|---|
1.0 | 1.5 | asymptotic |
2.0 | 2.5 | |
3.0 | 3.5 | |
4.0 | 4.5 |
Excel formula:
=CRAMERVONMISES_2SAMP({1.0;2.0;3.0;4.0}, {1.5;2.5;3.5;4.5}, "asymptotic")
Expected output:
statistic | pvalue |
---|---|
0.063 | 0.974 |
Example 3: Exact Method
Inputs:
x | y | method |
---|---|---|
1.0 | 1.5 | exact |
2.0 | 2.5 | |
3.0 | 3.5 | |
4.0 | 4.5 |
Excel formula:
=CRAMERVONMISES_2SAMP({1.0;2.0;3.0;4.0}, {1.5;2.5;3.5;4.5}, "exact")
Expected output:
statistic | pvalue |
---|---|
0.063 | 1.000 |
Example 4: Different Samples
Inputs:
x | y |
---|---|
1.0 | 10.0 |
2.0 | 20.0 |
3.0 | 30.0 |
4.0 | 40.0 |
Excel formula:
=CRAMERVONMISES_2SAMP({1.0;2.0;3.0;4.0}, {10.0;20.0;30.0;40.0})
Expected output:
statistic | pvalue |
---|---|
0.688 | 0.029 |
Python Code
from scipy.stats import cramervonmises_2samp as scipy_cramervonmises_2samp
from typing import List, Union
def cramervonmises_2samp(x: List[List[float]], y: List[List[float]], method: str = 'auto') -> Union[List[List[float]], str]:
"""
Performs the two-sample Cramér-von Mises test for goodness of fit between two independent samples.
Args:
x: 2D list of float values. First sample data.
y: 2D list of float values. Second sample data.
method: Method to compute p-value ('auto', 'asymptotic', 'exact'). Default is 'auto'.
Returns:
2D list with a single row: [statistic, pvalue], or an error message (str) if input is invalid.
This example function is provided as-is without any representation of accuracy.
"""
# Validate x and y are 2D lists with at least two rows
if not (isinstance(x, list) and all(isinstance(row, list) for row in x) and len(x) >= 2):
return "Invalid input: x must be a 2D list with at least two rows."
if not (isinstance(y, list) and all(isinstance(row, list) for row in y) and len(y) >= 2):
return "Invalid input: y must be a 2D list with at least two rows."
# Flatten x and y
try:
x_flat = [float(item) for row in x for item in row]
y_flat = [float(item) for row in y for item in row]
except Exception:
return "Invalid input: x and y must contain only numeric values."
if len(x_flat) < 2 or len(y_flat) < 2:
return "Invalid input: each sample must contain at least two values."
# Validate method
if method not in ['auto', 'asymptotic', 'exact']:
return "Invalid input: method must be 'auto', 'asymptotic', or 'exact'."
# Call scipy.stats.cramervonmises_2samp
try:
result = scipy_cramervonmises_2samp(x_flat, y_flat, method=method)
stat = float(result.statistic)
pvalue = float(result.pvalue)
except Exception as e:
return f"scipy.stats.cramervonmises_2samp error: {e}"
# Check for nan/inf
if any([isinstance(val, float) and (val != val or val in [float('inf'), float('-inf')]) for val in [stat, pvalue]]):
return "Invalid result: statistic or pvalue is nan or inf."
return [[stat, pvalue]]