Skip to content

Instantly share code, notes, and snippets.

@MarkusPrim
Last active July 27, 2017 13:00
Show Gist options
  • Save MarkusPrim/206bd81f388aa9f224016b48f99dec50 to your computer and use it in GitHub Desktop.
Save MarkusPrim/206bd81f388aa9f224016b48f99dec50 to your computer and use it in GitHub Desktop.
Cramer-von-Mises distance for two weighted samples
import numpy
def cvm_2samp_weigted(data1, data2, wei1, wei2):
"""Calculate Cramer-vonMises test statistics with weighted samples."""
# Indices for a sorted array
ix1 = numpy.argsort(data1)
ix2 = numpy.argsort(data2)
# Sorted data
data1 = data1[ix1]
data2 = data2[ix2]
# Sorted weights
wei1 = wei1[ix1]
wei2 = wei2[ix2]
data = numpy.concatenate([data1, data2])
cwei1 = numpy.hstack([0, numpy.cumsum(wei1)/sum(wei1)])
cwei2 = numpy.hstack([0, numpy.cumsum(wei2)/sum(wei2)])
cdf1we = cwei1[[numpy.searchsorted(data1, data, side='right')]]
cdf2we = cwei2[[numpy.searchsorted(data2, data, side='right')]]
return sum((f1-f2)**2 for f1, f2 in zip(cdf1we, cdf2we))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment