Skip to content

Instantly share code, notes, and snippets.

@heri
Last active August 29, 2015 14:25
Show Gist options
  • Save heri/7536b15ed08dd4388f65 to your computer and use it in GitHub Desktop.
Save heri/7536b15ed08dd4388f65 to your computer and use it in GitHub Desktop.
Community ads for redditors
# community ads ranking for subreddit
# see https://medium.com/product-dot-io/reddit-empowering-the-community-5c0ce2c266e2
def user_ad_ranking(ad, ups, downs)
# difference between upvotes and downvotes for user-submitted ad
# coeff is a business constant to be determined
s = ups + coeff * Math.log10(ad.budget * 10) - downs
order = Math.log10([s.abs, 1].max)
sign = if s > 0
1
elsif s < 0
-1
else
0
end
# Time.now.to_i converts ruby timestamp to seconds in Epoch
seconds = Time.now.to_i - 1134028003
# rating function, very similar to stories rating
return Math.round(sign * order + seconds / 45000, 7)
end
# returns current ad to show for subreddit. Only one ad is shown per subreddit
def current_subreddit_ad(user_ads)
return user_ads.map {|a| user_ad_ranking(a, a.ups, a.down )}.max
end
# returns pricing for user
def ad_price(subreddit, ups, downs)
s = downs + subreddit.current_ads.size - ups
# ad_coeff is a business constant to be determined
return ad_coeff * Math.exp(s)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment