how to calculate top 7 results from 12 rounds - php

earlier I asked how to get the best 3 results (which OTARIKI and Raging Bull answered)
for a static system that works great.
ok my question.
how can I get the top 7 rounds out of 12 rounds (code below is static best 3 rounds out of 4)
SELECT RacerID , round1 + round2 + round3 + round4 - LEAST(round1, round2, round3, round4) AS Top3Rounds
FROM tablename
any ideas?
many thanks again :)

Related

1 raiting subtraction from total weighted rating

This is a 10 star rating.
I have a weighted rating of 8.1 made from 25 users ratings.
I want to delete 1 user rating (7.5) that is invalid, how i do direct calculation on this? I don't want to recalculate all 24 ratings that are left.
I tough of this (8.1+7.5)/2 but it doesn't give me the right answer
Any others ideas of subtraction from a weighted rating?
When you calculate an average, the formula is :
(rate1 + rate2 + ... + rateN) / number of rates.
So, we have this equation where x is the addition of all rates exept the one you want to substract :
(x + 7.5) / 25 = 8.1
x + 7.5 = 8.1 * 25
x = 8.1 * 25 - 7.5
x = 195
So the answer is 195 / 24 = 8.125
Based on previous, the formula is :
(average * total votes - rate to remove) / total votes - 1

Generate a single elimination tournament

first , sorry for my english I'll do my best to explain my problem !
So , i'm trying to generate a single elimination tournament with an unlimited number of players.
for now i'm just thinking about it , i have nothing on paper , i think i will not have problem for tournament with power of two ( 2 4 8 16 32 players..) , my brain hangs on players going directly to round 2 , i don't know how to determine this number and where to place them.
eg (with 59 players)
I think there is a formula but I can't find it, I have some ideas but i think too specifically on a case, without knowing if it would work for another .
Thank you if you can help me !
For a given number N, find the difference between it and the smallest power of 2 at least as large as N. For 59, that'll be 5 (64 - 59). Those 5 players will be added to the tournament schedule at the second round.
This algorithm allows for all the players to be part of the game when the second round begins - i.e., as early as possible. Its explanation is very simple: imagine that originally there were 2**N players - but some just didn't come to their games, so their opponents went further without a fight. )
As a sidenote, your formula should take into account that it's strongest players that should enter the game from the second round, not the weakest ones. )
The first step apparently is calculating the number of players that will participate in the first round. Now, let's continue that 'missing players' metaphor - let's say there were 64 players originally, so the first round should have 32 games played. But 5 players (64 - 59) didn't come for those games - so the number of real games is 27 ( 64/2 - 5 ), and the number of real participants of the first round is 54 (27 * 2).
After the first round, there'll be 27 people left in the tournament - those people will be joined by those other 5 guys, so the total number of the 2nd round players is 32. The rest is trivial, I suppose. )
Actually, this is easy to commonize. Let's say we have N players, and the smallest power of 2 at least as large as N is P. Now...
The first round should have (N - (P - N)) (or just (2*N - P)) players.
The total number of the games in the first round is (N - P/2).
Apparently, the same is the number of players going into the 2nd round.
These will be joined by (P - N) players left without a play in the 1st round,
so the total number of players in the 2nd round will be...
N - P/2 + P - N => P - P/2 => P/2
... and from now you just go with the direct schedule of 2^N players (as P/2, as well as P, is the power of 2).
ooh thank you #raina77ow , you blew my mind , So here are my calculations:
64/2 = 32
59/2 = 29 ( rounded to the lower ) => nb of total player at left ( round 1 & 2)
32-29 = 3 => nb players at left going to round 2
29-3 = 26 => nb players at left going to round 1
59-29 = 30 => nb total players at right ( round 1 & 2 )
5-3 = 2 => nb players going to round 2 at right
30-2 = 28 = nb players round 1
`
I think I can make an algorithm now if that's right for each case.

Convert rating percent (50%) to the equivelant 5-star rating in PHP

I'm sure this is possible but my math isn't that fantastic.
I'm showing latest movies on my page and my application uses a 5-star rating system, however, the data I receive from a Web Service arrives as a percentage e.g. 50%.
Is there any way I can convert this percentage to a star rating equivalent, which in this case would be 50% = 2.5, allowing me to show 2.5 stars?
It seems fairly simple when I have 50% but if I get 94%, it confuses my poor little pea for a brain! Please help.
If you want to convert the 0..100 scale to a 0..5 scale, just divide by 20.
If you want it on a half-star boundary, then divide it by 10 instead and that's the number of half-stars you need.
Keep in mind I'm talking about integer division here, where the value is truncated (rounded down).
You may also want to consider rounding it more intelligently during the division, rather than truncating, so that something like 99% is 5 stars (not 4.5). This can be done by simply adding half the amount you're dividing by before the division, something like (in C):
int percent = 94;
int halfstars = (percent + 5) / 10;
This would give the following results for input values between 0 and 100 inclusive:
percent halfstars
------- ---------
0- 4 0
5- 14 1
15- 24 2
25- 34 3
35- 44 4
45- 54 5
55- 64 6
65- 74 7
75- 84 8
85- 94 9
95-100 10
The formula for finding the percentage of a number is fairly simple:
$percentInDecimalForm * $number
For example, a 94% rating would be:
.94 * 5 = 4.7
You just need to solve the following:
100% ---------- 5
94% ---------- x = (94 * 5) / 100 (=) x = 4.7
Now it's necessary to know the granularity of your star scale (how many times you can divide the star).
Since you mentioned 0.5 stars, I'm gonna assume your star granularity is 1 / 0.5 = 2, so just solve:
round(4.7 * 2) / 2 (=) 9 / 2 (=) 4.5

Done: for Z companies, calculate average value of A over X days. How do I now: return value of B for day X for company with highest average of A?

(I'll first explain my problem. The below table (any example query) is available at http://sqlfiddle.com/#!2/8ec17/4 though)
I have a table with stock information, as follows:
sp100_id _date bullishness returnpct
----------------------------------------------
1 2011-03-16 1.01 -0.33
1 2011-03-17 0.85 -1.28
1 2011-03-18 0.89 1.25
1 2011-03-21 1.46 1.21
1 2011-03-22 0.39 -2.53
2 2011-03-16 3.07 1.27
2 2011-03-17 2.09 -0.80
2 2011-03-18 0.91 -0.12
2 2011-03-21 1.50 0.00
2 2011-03-22 2.62 1.10
3 2011-03-16 0.73 -1.13
3 2011-03-17 1.13 1.21
3 2011-03-18 1.12 0.45
3 2011-03-21 1.00 1.01
3 2011-03-22 1.00 -0.53
4 2011-03-16 0.40 1.10
4 2011-03-17 2.40 0.03
4 2011-03-18 3.16 -0.10
4 2011-03-21 0.86 0.50
4 2011-03-22 1.00 0.10
What I need is:
for each 3 consecutive days, calculate the average bullishness for each company (sp100_id)
take the 3rd day returnpct of the company with the highest average bullishness and store it
at the end, calculate the total returnpct by adding up the stored returnpcts
There are 5 _dates in this example, so there folling pairs of consecutive days have to be made:
2011-03-16, 2011-03-17, 2011-03-18
2011-03-17, 2011-03-18, 2011-03-21 (please note, 2011-03-19 isn't in the table)
2011-03-18, 2011-03-21, 2011-03-22
To get back to what I need:
For the first three days, the "winner" is company 2 with an average bullsihness of (3.07 + 2.09 + 0.91) / 3 = 2.0233 and 3rd day returnpct of -0.12. For the other 2 "dateranges" the winners are company 4 (with average bullsihness 2.14 and returnpct 0.50) and company 2 again (bullsihness 1.67, returnpct 1.10)
The values -0.12, 0.50, 1.10 should be stored
The total returnpct would then be -0.12 + 0.50 + 1.10 = 1.48, which should be returned from the query (or script)
Problem 1: Concering the above example, my query returns the 1st day returnpct (1.27) instead of the 3rd day returnpct (-0.12). How can I change this?
Problem 2: In the sqlfiddle, I hardcoded the first group of 3 consecutive days. How can I automate this (perhaps using php) so that I don't have to type all queries manually? Please note there are dates missing in the table. As in the example, the script should just take the next available date in the table (so after 2011-03-18 comes 2011-03-21, not 2011-03-19 since it is not in the table)
Problem 3: In the example I use 3 consecutive days, but ideally a script can be easily changed to take any other number of consecutive days (2, 4, or 8, for instance). Consequently, the returnpct of respectively the 2nd, 4th or 8th day should be stored.
Who can help me out with some of the issues I'm experiencing here? Any help is greatly appreciated :-)
The following query goes a long way toward what you want to do. It calculates the 3-day average, then orders within each date by the highest average:
SELECT s.sp100_id, s._date,
(s.bullishness+splus1.bullishness+splus2.bullishness)/3 as avgb,
splus2.returnpct
FROM (select s3.*,
(select min(_date)
from stocks s4
where s4.sp100_id = s3.sp100_id and
s4._date > s3.dateplus1
) as dateplus2
from (select s.*,
(select min(_date)
from stocks s2
where s2.sp100_id = s.sp100_id and
s2._date > s._date
) as dateplus1
from stocks s
) s3
) s left outer join
stocks splus1
on s.sp100_id = splus1.sp100_id and
s.dateplus1 = splus1._date left outer join
stocks splus2
on s.sp100_id = splus2.sp100_id and
s.dateplus2 = splus2._date
order by 2, 3 desc
At this point, using mysql gets tedious. This would be much easier in a database that supported analytic/windows functions and the "with" statement (which is just about every database apart from mysql: Oracle, Postgres, DB2, SQL Server, for example).
You can do (3) in MySQL with a single query, but it is a pain. You might want to do that in the application layer.
By the way, thank you for setting up a SQL Fiddle. For than reason alone, I'll upvote the question.

standard deviation and mode

I have a system that monitors the performance of students. It tabulates the number of students who gained a score of 1,1.25,1.5,....5 (this is our grading system). For example:
grading system number of students
1 12
1.25 10
1.5 15
1.75 15
2 20
2.25 1
2.5 5
2.75 6
3 8
5 0
From this example, I need my system to determine which is the mode and then print it. I also need to get the standard deviation.
I need this in PHP. Can anyone help me with this?
Your ideas, comments, and suggestions are appreciated.
Update:
Here's what I've done so far:
Finished the standard deviation...but there are still discrepancies i can't resolve...when i calculate the standard deviation manually..the answer is different from the output of my system.. >.<
While for the mode I used an array..this is my code:
$sample = array($one[$ctr],$two[$ctr],$three[$ctr],$four[$ctr],$five[$ctr],$six[$ctr],$seven[$ctr],$eight[$ctr],$nine[$ctr],$ten[$ctr],$fda[$ctr]);
rsort($sample);
$holder = $sample[0];
//$holder = $mode;
The sorting is successful and I can the highest number but I need to print the value of $holder to a table using fpdf.
Any ideas, why the value is not visible in the output?
Well, the mode is easy. Just find the grade (2) which has the highest number of students (20) and there you are.
If there's more than one, then it's multi-modal and you should probably allow for that.
For the standard deviation, the method can be found here. It's basically working out the mean of all those numbers (let's simplify this by using 1, 1, 2 and 7):
1 + 1 + 2 + 7 10
------------- = -- = 2.5
4 4
then calculating the square root of the variance of all those samples from that mean:
_____________________________________________
/ (1-2.5)^2 + (1-2.5)^2 + (2-2.5)^2 + (7-2.5)^2
/ ---------------------------------------------
\/ 4
__________________________
/ 2.25 + 2.25 + 0.25 + 20.25
= / --------------------------
\/ 4
= 2.5
If you're asking a beginner-level question like how best to do this in a specific language like PHP, you should investigate the use of arrays and loops.

Categories