Skip to content

Instantly share code, notes, and snippets.

@JohnDeJesus22
Created August 5, 2022 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohnDeJesus22/0ef1ec00e7a4bb93df0d287c0e36d8a9 to your computer and use it in GitHub Desktop.
Save JohnDeJesus22/0ef1ec00e7a4bb93df0d287c0e36d8a9 to your computer and use it in GitHub Desktop.
jb_scratch.py
def jb_test_statistic(column):
"""
Function for creating JB test statistic equation from pandas dataframe column
"""
# have all nulls removed, get the data length, and convert column values to an array
column = column.dropna()
n = column.shape[0]
column_values = column.values
# compute mean and mean diffs
mean = column_values.mean()
mean_diff = column_values - mean
# compute sample skewness
skew_numerator = (1/n) * sum(mean_diff**3)
skew_denominator = ((1/n) * sum(mean_diff**2))**(3/2)
s = skew_numerator/skew_denominator
# compute sample kurtosis
kurtosis_numerator = (1/n) * sum(mean_diff**4)
kurtosis_denominator = ((1/n) * sum(mean_diff**2))**2
k = kurtosis_numerator/kurtosis_denominator
# compute JB test statistic
jb = (n/6) * (s**2 + (1/4)*(k - 3)**2)
return jb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment