Skip to content

Instantly share code, notes, and snippets.

@vpekar
Last active July 7, 2023 13:13
Show Gist options
  • Save vpekar/df58ac8f07ec9d4ef24bcf1c176812b0 to your computer and use it in GitHub Desktop.
Save vpekar/df58ac8f07ec9d4ef24bcf1c176812b0 to your computer and use it in GitHub Desktop.
A Python implementation of the Directional Accuracy Score and Pesaran-Timmermann statistic.
import numpy as np
import scipy.stats as stats
def pttest(y, yhat):
"""Given NumPy arrays with predictions and with true values,
return Directional Accuracy Score, Pesaran-Timmermann statistic and its p-value
"""
size = y.shape[0]
pyz = np.sum(np.sign(y) == np.sign(yhat))/size
py = np.sum(y > 0)/size
qy = py*(1 - py)/size
pz = np.sum(yhat > 0)/size
qz = pz*(1 - pz)/size
p = py*pz + (1 - py)*(1 - pz)
v = p*(1 - p)/size
w = ((2*py - 1)**2) * qz + ((2*pz - 1)**2) * qy + 4*qy*qz
pt = (pyz - p) / (np.sqrt(v - w))
pval = 1 - stats.norm.cdf(pt, 0, 1)
return pyz, pt, pval
if __name__ == "__main__":
a = np.array([23, -2, 56, 51, 4, -45, -12, -24, -51, 78, -6, -7, -39, 31, 35])
b = np.array([14, 3, 45, 23, -5, -56, 4, -11, -34, 29, 3, -11, -12, 24, 3])
dac, pt, pval = pttest(a, b)
print(f"Directional Accuracy: {dac}, PT stat: {pt}, p-value: {pval}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment