Skip to content

Instantly share code, notes, and snippets.

@ogregoire
Created November 26, 2015 14:02
Show Gist options
  • Save ogregoire/3e6de7702868b90a50e9 to your computer and use it in GitHub Desktop.
Save ogregoire/3e6de7702868b90a50e9 to your computer and use it in GitHub Desktop.
Confidence score
public class Confidence {
private static double erf(double x) {
double a1 = 0.254829592;
double a2 = -0.284496736;
double a3 = 1.421413741;
double a4 = -1.453152027;
double a5 = 1.061405429;
double p = 0.3275911;
x = Math.abs(x);
double t = 1 / (1 + p * x);
return 1 - ((((((a5 * t + a4) * t) + a3) * t + a2) * t) + a1) * t * Math.exp(-1 * x * x);
}
static double snt(double z) {
return 0.5 * (1.0 + Math.signum(z) * erf(Math.abs(z) / Math.sqrt(2)));
}
private final double z;
public Confidence(double confidence) {
z = snt(confidence);
}
public double confidence(int ups, int downs) {
int n = ups + downs;
if (n == 0) {
return 0;
}
double phat = (double) ups / n;
return Math.sqrt(phat + z * z / (2 * n) - z * ((phat * (1 - phat) + z * z / (4 * n)) / n)) / (1 + z * z / n);
}
public class ConfidenceTest {
@Test
public void testConfidence() {
Confidence confidence = new Confidence(0.80);
class Score {
int ups, downs;
Score(int ups, int downs) {
this.ups = ups;
this.downs = downs;
}
double confidence() {
return confidence.confidence(ups, downs);
}
}
Score s1 = new Score(1, 0);
Score s2 = new Score(10, 1);
Score s3 = new Score(40, 20);
Score s4 = new Score(100, 40);
List<Score> scores = asList(s1, s2, s3, s4);
scores.sort(Comparator.comparing(Score::confidence).reversed());
assertThat(scores, is(equalTo(asList(s2, s4, s3, s1))));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment