Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 1st, 2012  |  syntax: None  |  size: 1.60 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can I improve this equation so items with more negative votes than positive return a more useful Wilson Score
  2. def ci_lower_bound(pos, n, confidence):
  3.     if n==0: return 0
  4.  
  5.     z = 1.96
  6.     phat = 1.0*pos/n
  7.  
  8.     score = (phat + z*z/(2*n) - z*math.sqrt((phat*(1-phat)+z*z/(4*n))/n))/(1+z*z/n)
  9.     return score
  10.        
  11. import math
  12.  
  13. def ci_lower_bound(pos, n, neg=0):
  14.  
  15.     if n == 0:
  16.         return 0
  17.  
  18.     # Cannot calculate the square-root of a negative number
  19.     if pos == 0:
  20.         votes, use_neg = neg, True
  21.     else:
  22.         votes, use_neg = pos, False
  23.  
  24.  
  25.     # Confidence
  26.     z = 1.96
  27.  
  28.     phat = 1.0 * votes / n
  29.  
  30.     # Calculate how confident we are that this is bad or good.
  31.     score = (phat + z*z/(2*n) - z * math.sqrt((phat*(1-phat)+z*z/(4*n))/n))/(1+z*z/n)
  32.  
  33.     # This relationship is defined above.
  34.     # Multiply by -1 to return a negative confidence.
  35.     if use_neg:
  36.         return -1 * score
  37.  
  38.     return score
  39.        
  40. import math
  41.  
  42. def ci_lower_bound(pos, n, neg=0):
  43.  
  44.     if n == 0:
  45.         return 0
  46.  
  47.     # Cannot calculate the square-root of a negative number
  48.     if pos == 0:
  49.         votes, use_neg = neg, True
  50.     else:
  51.         votes, use_neg = pos, False
  52.  
  53.  
  54.     # Confidence
  55.     z = 1.96
  56.  
  57.     phat = 1.0 * votes / n
  58.  
  59.     # Calculate how confident we are that this is bad or good.
  60.     score = (phat + z*z/(2*n) - z * math.sqrt((phat*(1-phat)+z*z/(4*n))/n))/(1+z*z/n)
  61.  
  62.     # This relationship is defined above.
  63.     # Multiply by -1 to return a negative confidence.
  64.     if use_neg:
  65.         return -1 * score
  66.  
  67.     return score
  68.        
  69. >>> sorted([(0,-4000),(1,-4000),(0,-1),(1,-1)])
  70. [(0, -4000), (0, -1), (1, -4000), (1, -1)]