Skip to content

Instantly share code, notes, and snippets.

@xwlee
Last active August 2, 2019 08:13
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save xwlee/9777480 to your computer and use it in GitHub Desktop.
Save xwlee/9777480 to your computer and use it in GitHub Desktop.
<?php
//////////////////////////////////
// Reddit "hot" story algorithm //
//////////////////////////////////
function hot($ups, $downs, $date)
{
if (is_string($date)) $date = strtotime($date);
$s = $ups - $downs;
$order = log10(max(abs($s), 1));
if ($s > 0)
$sign = 1;
elseif ($s < 0)
$sign = -1;
else
$sign = 0;
$seconds = $date - 1134028003;
return round($sign * $order + $seconds / 45000, 7);
}
/////////////////////////////////////
// Reddit "Best" comment algorithm //
/////////////////////////////////////
function confidence($ups, $downs)
{
$n = $ups + $downs;
if ($n == 0) return 0;
$z = 1.281551565545; // 80% confidence
$phat = $ups / $n;
$left = $phat + 1/(2*$n)*$z*$z;
$right = $z*sqrt($phat*(1-$phat)/$n + $z*$z/(4*$n*$n));
$under = 1+1/$n*$z*$z;
return ($left - $right) / $under;
}
//////////////////////////////////////////////
// Hacker News front page ranking algorithm //
//////////////////////////////////////////////
function calculateScore($votes, $hourAge, $gravity = 1.8)
{
return ($votes-1) / pow(($hourAge+2), $gravity);
}
//////////////////////////////////////
// Zite "Popular on Zite" algorithm //
//////////////////////////////////////
function popular($ups, $downs, $hourAge, $clicks)
{
$averageRating = confidence($ups, $downs);
$interest = log10((1+$clicks) / (2*$hourAge/6.0));
return $averageRating * $interest;
}
// @see http://stackoverflow.com/questions/11653545/hot-content-algorithm-score-with-time-decay
function decay($score, $weekNo)
{
$score = 30;
$baseScore = log10(max($score,1));
if ($weekNo > 1) {
$x = $weekNo - 1;
$baseScore = $baseScore * exp(-8*$x*$x);
}
return $baseScore;
}
@rockyraccoon
Copy link

This is great. One question regarding Reddit's "hot" algo: when I employ this ranking I see new posts with a single up-vote at the very top. How do I protest against this volatility?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment