Skip to content

Instantly share code, notes, and snippets.

@jan-krueger
Last active December 8, 2017 15:46
Show Gist options
  • Save jan-krueger/e28478a1bdc46d3272d5de0c909bdd44 to your computer and use it in GitHub Desktop.
Save jan-krueger/e28478a1bdc46d3272d5de0c909bdd44 to your computer and use it in GitHub Desktop.
Reddit's Score
import java.time.Instant;
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class Score {
private final static Date epoch = Date.from(Instant.EPOCH);
private final Z z = Z.V_50;
private final int upvotes, downvotes;
private final long timeDiff;
private final int score;
public Score(int upvotes, int downvotes, Date now) {
this.upvotes = upvotes;
this.downvotes = downvotes;
this.timeDiff = TimeUnit.MILLISECONDS.toSeconds(now.getTime() - epoch.getTime());
this.score = (upvotes - downvotes);
}
public double hot() {
double order = Math.log10(Math.max(Math.abs(this.score), 1));
double sign = 0;
if(this.score > 0) {
sign = 1;
} else if(score < 0) {
sign = -1;
}
return (sign * order + this.timeDiff / 45000);
}
public double controversy() {
if(this.downvotes <= 0 || this.upvotes <= 0) {
return 0;
}
int magnitude = this.upvotes + downvotes;
double balance = (upvotes > downvotes ? Double.valueOf(downvotes) / upvotes : Double.valueOf(upvotes) / downvotes);
return Math.pow(magnitude, balance);
}
public double confidence() {
double n = upvotes + downvotes;
if(n == 0) {
return 0;
}
double z = this.z.value();
double p = Double.valueOf(upvotes) / n;
double left = p + 1 / (2 * n) * Math.pow(z, 2);
double right = z * Math.sqrt(p * (1 - p) / n + z * z / (4 * n * n));
double under = 1 +1 / n * z * z;
return (left - right) / under;
}
public enum Z {
V_50(0.67449D),
V_75(1.15035D),
V_80(1.282D),
V_90(1.64485D),
V_95(1.95996D),
V_97(2.17009D),
V_99(2.57583D),
V_999(3.29053D);
private final double z;
Z(double z) {
this.z = z;
}
public double value() {
return this.z;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment