JARQUE_BERA
Overview
The JARQUE_BERA
function performs the Jarque-Bera goodness of fit test for normality. This test evaluates whether the sample data has skewness and kurtosis matching a normal distribution. It is commonly used in statistics and econometrics to assess the normality of residuals or data samples. The test statistic is calculated as:
where is the sample size, is the sample skewness, and is the sample kurtosis. The p-value is computed based on the chi-squared distribution with 2 degrees of freedom. For more details, see the scipy.stats.jarque_bera documentation .
This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=JARQUE_BERA(x)
x
(2D list, required): Sample data. Must contain at least two elements. Ideally, use more than 2000 elements for the test to be valid.
The function returns a 1-row, 2-column array: the test statistic and the p-value. If the input is invalid, an error message is returned as a string.
Examples
Example 1: Normal Data
Inputs:
x |
---|
0.1 |
-0.2 |
0.3 |
0.0 |
0.2 |
-0.1 |
Excel formula:
=JARQUE_BERA({0.1;-0.2;0.3;0.0;0.2;-0.1})
Expected output:
Statistic | P-value |
---|---|
0.40231836734693943 | 0.8177822436106005 |
Example 2: Skewed Data
Inputs:
x |
---|
1.0 |
2.0 |
3.0 |
4.0 |
5.0 |
6.0 |
Excel formula:
=JARQUE_BERA({1;2;3;4;5;6})
Expected output:
Statistic | P-value |
---|---|
0.40231836734693854 | 0.8177822436106008 |
Example 3: Small Sample
Inputs:
x |
---|
2.5 |
2.7 |
2.9 |
3.1 |
3.3 |
3.5 |
Excel formula:
=JARQUE_BERA({2.5;2.7;2.9;3.1;3.3;3.5})
Expected output:
Statistic | P-value |
---|---|
0.40231836734693843 | 0.8177822436106009 |
Example 4: Data with Outlier
Inputs:
x |
---|
0.0 |
0.1 |
0.2 |
0.3 |
0.4 |
5.0 |
Excel formula:
=JARQUE_BERA({0;0.1;0.2;0.3;0.4;5})
Expected output:
Statistic | P-value |
---|---|
3.463994184620268 | 0.17693070994138124 |
This means the function returns the test statistic and p-value for the Jarque-Bera test for each input sample.
Python Code
from scipy.stats import jarque_bera as scipy_jarque_bera
def jarque_bera(x):
"""
Perform the Jarque-Bera goodness of fit test for normality.
Args:
x: 2D list. Sample data (ideally >2000 elements).
Returns:
2D list. Test statistic and p-value, or an error message (str) if input is invalid.
This example function is provided as-is without any representation of accuracy.
"""
# Validate input
if not isinstance(x, list) or len(x) < 2:
return "Invalid input: x must be a 2D list with at least two elements."
# Flatten 2D list to 1D
try:
flat = []
for row in x:
if isinstance(row, list):
flat.extend(row)
else:
flat.append(row)
data = [float(val) for val in flat]
except Exception:
return "Invalid input: x must contain only numeric values."
if len(data) < 2:
return "Invalid input: x must contain at least two numeric values."
try:
res = scipy_jarque_bera(data)
# Try to access statistic and pvalue attributes if present
if hasattr(res, 'statistic') and hasattr(res, 'pvalue'):
stat = float(res.statistic)
pval = float(res.pvalue)
elif isinstance(res, (tuple, list)) and len(res) == 2:
stat, pval = float(res[0]), float(res[1])
else:
return f"scipy.stats.jarque_bera unexpected result: {res}"
# Disallow nan/inf
if any([s is None or isinstance(s, str) or s != s or s == float('inf') or s == float('-inf') for s in [stat, pval]]):
return [["error", "error"]]
return [[stat, pval]]
except Exception as e:
return f"scipy.stats.jarque_bera error: {e}"