Skip to content

Instantly share code, notes, and snippets.

@cplaisier
Last active November 7, 2017 20:35
Show Gist options
  • Select an option

  • Save cplaisier/084f4914bdb0dda801e74b31da0cdd53 to your computer and use it in GitHub Desktop.

Select an option

Save cplaisier/084f4914bdb0dda801e74b31da0cdd53 to your computer and use it in GitHub Desktop.
Code to compute coefficient of determination
from itertools import imap
import operator
import logicFuncs as lf
def mean_squared_error(r1, p1):
#assert len(r1) == len(p1)
return sum([(float(p1[i])-float(r1[i]))**2 for i in range(len(r1))])/float(len(r1))
def hammingDist(str1, str2):
assert len(str1) == len(str2)
return sum(imap(operator.ne, str1, str2))
def calculateCoD(y, fx):
"""
Calculate Coefficient of Determination (CoD)
for X as predictor of Y
NOTE -> X and Y are required to be discretized
Steps:
1. eopt = 0.5
2. Get mean squared error (eopt) between X and Y
3. Calculate CoD as:
CoD = (e0 - eopt)/e0
"""
return (0.5 - mean_squared_error(y, fx))/0.5
def main():
# Response variables to comapre
import itertools
sixers = list(itertools.product([0, 1], repeat=6))
# Tests
y = [1,0,1,0,1,0]
print y
for x in sixers:
tmp1 = calculateCoD(y,x)
tmp3 = calculateCoD(y,lf.notFunc(x))
print x, tmp1, tmp3, hammingDist(y,x), hammingDist(y,lf.notFunc(x))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment