Skip to content

Instantly share code, notes, and snippets.

@conradlee
Created May 24, 2012 08:24
Show Gist options
  • Save conradlee/2780203 to your computer and use it in GitHub Desktop.
Save conradlee/2780203 to your computer and use it in GitHub Desktop.
Leader follower hypothesis testing
# First get distributions of lagged dot products in both directions
c1_leading_c2_dps = get_lag_dot_products(c1, c2, lagged_timesteps)
c2_leading_c1_dps = get_lag_dot_products(c2, c1, lagged_timesteps)
# Check whether distributions are the same using paired t-test
t_score, pval = scipy.stats.ttest_rel(c1_leading_c2_dps, c2_leading_c1_dps)
if pval < PVAL_THRESH:
# Choose direction with larger mean
if numpy.mean(c1_leading_c2_dps) > numpy.mean(c2_leading_c1_dps):
leader, follower, dot_products = c1, c2, c1_leading_c2_dps
else:
leader, follower, dot_products = c2, c1, c2_leading_c1_dps
# Make sure mean of dot products (our measure of correlation) differs from zero
tscore, nonzero_pval = stats.ttest_1samp(dot_products, 0.) # get two-sided pval score
nonzero_pval = nonzero_pval / 2. # convert to 1-sided score because we know apriori that we'll accept only a positive correlation
if (nonzero_pval < pval_thresh):
correlation = numpy.mean(dot_products)
if correlation > 0.:
record_relationship(c1, c2, correlation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment