A perfect ORDER BY rating

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

A perfect ORDER BY rating

Post by kaisellgren »

Hello,

I'm currently developing a showcase where I want this option order by rating. However, I'm not happy with the way most sites work. For example, think about having a 5 stars rating system, if I rate something 5 stars and I am the only one who has rated it, then it's already in the top 1! No that's not good, we need something to add more weight to items that have more votes.

Currently I'm doing a thumbs up and down system. And I am trying to figure out a good algorithm for this. I started playing with PHP, try running this:

Code: Select all

$values = array(array(5,3),array(5,0),array(10,5),array(15,13));
function get($valuesarray)
 {
  global $method;
  $up = $valuesarray[0];
  $down = $valuesarray[1];
  return (($up-$down)*log($up+$down,3));;
 }
foreach ($values as $array)
 {
  echo 'Up: '.$array[0].'<br />Down: '.$array[1].'<br />Order: '.number_format(get($array),2,'.',' ').'<br /><br />';
 }
You get something like:
Up: 5
Down: 3
Order: 3.79

Up: 5
Down: 0
Order: 7.32

Up: 10
Down: 5
Order: 12.32

Up: 15
Down: 13
Order: 6.07
Up 5, Down 0 has a great rate, but only 5 rates so it loses against 10/5.

Are there any algorithms for these kind of situation where we are trying to make a realistic "order by rating" feature?

In 5-star rating system, It's obvious that something that has rate of 4.95 and 1000 votes should rank higher than rate of 4.96 with e.g. 600 votes.
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: A perfect ORDER BY rating

Post by it2051229 »

riiggghhhhtttttt. :dubious: :P
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: A perfect ORDER BY rating

Post by alex.barylski »

Up 5, Down 0 has a great rate, but only 5 rates so it loses against 10/5.
Depends on how you define 'loses' I suppose. Statistically, the former is perfect at least hitherto, whereas the latter might be more worn and rated lower.

I think you need to separate 'rating' from 'weight' and you should find the answer you seek. :)

p.s-I would buy a product rated 5/5 5 times over a product rated 1/5 500 times any day. ;)

Cheers,
Alex
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: A perfect ORDER BY rating

Post by allspiritseve »

You may want to check out the bottom of this page: http://www.imdb.com/chart/top
IMDB has a weighted system for rating movies.

Also, I know there are plenty of statistical formulas that could give you p values, t values, etc., that would allow you a standard weighting system rather than something homegrown. I can't think of any specifically off the top of my head, it's been a while since I've taken a stats class.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: A perfect ORDER BY rating

Post by kaisellgren »

allspiritseve wrote:You may want to check out the bottom of this page: http://www.imdb.com/chart/top
IMDB has a weighted system for rating movies.

Also, I know there are plenty of statistical formulas that could give you p values, t values, etc., that would allow you a standard weighting system rather than something homegrown. I can't think of any specifically off the top of my head, it's been a while since I've taken a stats class.
Thanks for the link, so IMDB uses this algorithm:
The formula for calculating the Top Rated 250 Titles gives a true Bayesian estimate:

weighted rating (WR) = (v ÷ (v+m)) × R + (m ÷ (v+m)) × C

where:
R = average for the movie (mean) = (Rating)
v = number of votes for the movie = (votes)
m = minimum votes required to be listed in the Top 250 (currently 1300)
C = the mean vote across the whole report (currently 6.7)
EDIT: Ok that's hard to port to a thumbs up/down system :p
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: A perfect ORDER BY rating

Post by allspiritseve »

kaisellgren wrote:EDIT: Ok that's hard to port to a thumbs up/down system :p
Probably... your first example was a 5 star system though.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: A perfect ORDER BY rating

Post by VladSun »

Take a look how ELO (chess players rating) is calculated
http://en.wikipedia.org/wiki/Elo_rating_system

This way a low rated user's vote will increase a high rated user's rating with a small amount. And the opposite - if a high rated user votes for a low rated one, the rating will increase with much more.
It's interesting that in the second case, the rating of the voting user should be decreased. ;)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: A perfect ORDER BY rating

Post by kaisellgren »

VladSun wrote:Take a look how ELO (chess players rating) is calculated
http://en.wikipedia.org/wiki/Elo_rating_system

This way a low rated user's vote will increase a high rated user's rating with a small amount. And the opposite - if a high rated user votes for a low rated one, the rating will increase with much more.
It's interesting that in the second case, the rating of the voting user should be decreased. ;)
Oh yes ELO! I have a one of 1981 :) (Hint -- I'm a chess player :D)

I'm already working on a second version of my "perfect sorting" :)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: A perfect ORDER BY rating

Post by VladSun »

//offtopic
kaisellgren wrote:Oh yes ELO! I have a one of 1981 :) (Hint -- I'm a chess player :D)
Oh! You look younger than you are ;) At least you are elder than me :)

Wanna play? ;)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: A perfect ORDER BY rating

Post by kaisellgren »

VladSun wrote://offtopic
kaisellgren wrote:Oh yes ELO! I have a one of 1981 :) (Hint -- I'm a chess player :D)
Oh! You look younger than you are ;) At least you are elder than me :)

Wanna play? ;)
Let's see maybe we could. Do you know good online web sites to play chess free? I've used http://www.aapeli.com a couple of times, but it's in Finnish, however, we only need to recognize the pawns to play the chess :)

I just turned out 19.

EDIT: If we play, there's one rule: Infinite time. I don't play Fischers or other. I think an average game I play lasts 2-3 hours. I'm sure I am able to give you an opponent ;) I started chess at age of 3, but at that time I was only able to move pawns based on rules - not able to make rational moves.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: A perfect ORDER BY rating

Post by VladSun »

I have a one of 1981
I've read it as "I have ELO since 1981" :) My mistake. You are far younger than me :)
And I don't wanna play with you anymore :P

My ELO used to be between 1600 and 1700, but for the last couple of years I successfully made it 1450 :)

And I typically play 5 minutes games with 3 seconds added time per move. I don't like playing long games - it makes me nervous and I make a lot of mistakes. I play at Yahoo! Chess.
Last edited by VladSun on Thu Jan 22, 2009 5:19 pm, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VirtuosiMedia
Forum Contributor
Posts: 133
Joined: Thu Jun 12, 2008 6:16 pm

Re: A perfect ORDER BY rating

Post by VirtuosiMedia »

Not perfect, but maybe a start to play around with...

Code: Select all

<form method="post">
    <label>Votes</label>
    <input type="text" name="numVotes" />
    <label>Points</label>
    <input type="text" name="numPoints" />
    <input type="hidden" name="submitted" value="TRUE" />
    <input type="submit" value="Submit" /> 
</form>
 
<?php
if ($_POST['submitted']){
    $numVotes = $_POST['numVotes'];
    $numPoints = $_POST['numPoints'];
    $rating = ($numPoints / $numVotes);
    
 
    $rank = ($numVotes + $numPoints) * $rating;
    echo $rank;
}
?>
Post Reply