英文:
Calculate the swiss tennis ranking classification
问题
The Swiss tennis ranking is calculated using the classification value C
, which is the sum of the match value W
and the risk value R
. You want to calculate C
with a function. Here's the function in R:
calculate_c = function(tibble, w0) {
tibble_out <- tibble %>%
mutate(e_pos_wi = case_when(result == "win" ~ exp(match_value_opponent),
TRUE ~ 1),
e_neg_wj = case_when(result == "loss" ~ exp(-match_value_opponent),
TRUE ~ 1)) %>%
summarise(sum_e_pos_wi = sum(e_pos_wi, na.rm = TRUE),
sum_e_neg_wj = sum(e_neg_wj, na.rm = TRUE)) %>%
mutate(e_pos_w0 = exp(w0),
e_neg_w0 = exp(-w0),
W = (1/2) * (log(sum_e_pos_wi + e_pos_w0) - log(sum_e_neg_wj + e_neg_w0)),
R = (1/6) * (log(sum_e_pos_wi + e_pos_w0) - log(sum_e_neg_wj + e_neg_w0)),
C = W + R)
return(tibble_out[, c("W", "R", "C")])
}
# Example usage:
results <- tibble(match = c(1, 2, 3), match_value_opponent = c(2.4, 4.5, 3.4), result = c("win", "loss", "win"))
w0 <- 2
calculate_c(tibble = results, w0 = w0)
Please note that this code includes some adjustments to handle cases where result
is neither "win" nor "loss," as the formulas you provided seem to expect.
英文:
The Swiss tennis ranking is calculated using the classification value C
C
is the sum of the match value W
and the risk value R
I would like to calculate C
with a function.
W
= your match valueW0
= your previous match valueWi
= the match value of the player you defeatedWj
= the match value of the player you lost tos
= sum of the matches you wonN
= sum of the matches you lost
Now I would like to write a function that calculates C
with a tibble input of the matches played and the match value of the opponents.
(I've already learned how to estimate a desired W
)
results <- tibble(match=c(1,2,3),match_value_opponent=c(2.4,4.5,3.4), result=c("win","loss","win"))
The previous match value is a constant:
w0 <- 2
I think the function would look like this, but I am not sure how to implement the two formulas in R
calculate_w= function(results)(...)
Update1:
With the function from the kind answer from theN below I do not get the expected results, especially not expected W
.
Reprex with the data from this accepted answer:
library(tidyverse)
calculate_c= function(tibble,w0){
tibble_out <- tibble %>%
mutate(e_pos_wi = case_when(result == "win" ~ exp(match_value_opponent)),
e_neg_wj = case_when(result == "loss" ~ exp(-match_value_opponent))) %>%
summarise(sum_e_pos_wi = sum(e_pos_wi, na.rm = T),
sum_e_neg_wj = sum(e_neg_wj, na.rm = T)) %>%
mutate(e_pos_w0 = exp(w0),
e_neg_w0 = exp(w0),
W = (1/2) * (log(sum_e_pos_wi + e_pos_w0) - log(sum_e_neg_wj + e_neg_w0)),
R = (1/6) * (log(sum_e_pos_wi + e_pos_w0) - log(sum_e_neg_wj + e_neg_w0)),
C = W + R)
return(tibble_out[,c("W","R","C")])
}
# make a tibble with 15 wins vs a player with a W of 3400 (same input as described in the question/answer on Mathematica linked above)
results <- tibble(match=paste(1:15),match_value_opponent=c(rep(3.400,15)), result=c(rep("win",15)))
# calculate W with the same W0 as in the Mathematica question/answer
calculate_c(tibble = results, w0=2.354)
#> # A tibble: 1 × 3
#> W R C
#> <dbl> <dbl> <dbl>
#> 1 1.89 0.630 2.52
#expected output
# W at least 3.2
<sup>Created on 2023-04-22 with reprex v2.0.2</sup>
Update2:
These are the formulas directly copied from the link above:
Update3:
Because of the comments (the given formulas might not be unequivocal), I contacted Swiss Tennis for clarification. They sent me updated formulas (sorry for the quality, these are the originals):
Where WA
= W0
Unfortunately, they did not provide an example of the formula in use.
答案1
得分: 1
This should work, feel free to modify the function to return intermediate parts of the calculation if needed.
calculate_c = function(tibble){
tibble_out <- tibble %>%
mutate(e_pos_wi = case_when(result == "win" ~ exp(match_value_opponent)),
e_neg_wj = case_when(result == "loss" ~ exp(-match_value_opponent))) %>%
summarise(sum_e_pos_wi = sum(e_pos_wi, na.rm = T),
sum_e_neg_wj = sum(e_neg_wj, na.rm = T)) %>%
mutate(e_pos_w0 = exp(2),
e_neg_w0 = exp(-2),
W = (1/2) * (log(sum_e_pos_wi + e_pos_w0) - log(sum_e_neg_wj + e_neg_w0)),
R = (1/6) * (log(sum_e_pos_wi + e_pos_w0) - log(sum_e_neg_wj + e_neg_w0)),
C = W + R)
return(tibble_out[, "C"])
}
calculate_c(tibble = results)
英文:
This should work, feel free to modify the function to return intermediate parts of the calculation if needed.
calculate_c= function(tibble){
tibble_out <- tibble %>%
mutate(e_pos_wi = case_when(result == "win" ~ exp(match_value_opponent)),
e_neg_wj = case_when(result == "loss" ~ exp(-match_value_opponent))) %>%
summarise(sum_e_pos_wi = sum(e_pos_wi, na.rm = T),
sum_e_neg_wj = sum(e_neg_wj, na.rm = T)) %>%
mutate(e_pos_w0 = exp(2),
e_neg_w0 = exp(-2),
W = (1/2) * (log(sum_e_pos_wi + e_pos_w0) - log(sum_e_neg_wj + e_neg_w0)),
R = (1/6) * (log(sum_e_pos_wi + e_pos_w0) - log(sum_e_neg_wj + e_neg_w0)),
C = W + R)
return(tibble_out[, "C"])
}
calculate_c(tibble = results)
答案2
得分: 1
Here's the translated code portion:
W = function(res, w0) {
# 胜利时的分数
wi = res$match_value_opponent[res$result == "win"]
# W的前半部分
w1 = log(sum(exp(wi)) + exp(w0))
# 失败时的分数
wj = res$match_value_opponent[res$result == "loss"]
# W的后半部分
w2 = log(sum(exp(-wj)) + exp(-w0))
return(0.5*(w1 - w2))
}
R = function(res, w0) {
# 胜利时的分数
wi = res$match_value_opponent[res$result == "win"]
# R的前半部分
w1 = log(sum(exp(wi)) + exp(w0))
# 失败时的分数
wj = res$match_value_opponent[res$result == "loss"]
# R的后半部分
w2 = log(sum(exp(-wj)) + exp(-w0))
return((1/6)*(w1 + w2))
}
C = function(res, w0) {
w = W(res, w0)
print("W")
print(w)
r = R(res, w0)
print("R")
print(r)
return(w + r)
}
Please note that I've translated the comments as well for clarity.
英文:
W = function(res, w0) {
# scores when you win
wi = res$match_value_opponent[res$result == "win"]
# first half of W
w1 = log(sum(exp(wi)) + exp(w0))
# scores when you lose
wj = res$match_value_opponent[res$result == "loss"]
# second half of W
w2 = log(sum(exp(-wj)) + exp(-w0))
return(0.5*(w1 - w2))
}
R = function(res, w0) {
# scores when you win
wi = res$match_value_opponent[res$result == "win"]
# first half of W
w1 = log(sum(exp(wi)) + exp(w0))
# scores when you lose
wj = res$match_value_opponent[res$result == "loss"]
# second half of W
w2 = log(sum(exp(-wj)) + exp(-w0))
return((1/6)*(w1 + w2))
}
C = function(res, w0) {
w = W(res, w0)
print("W")
print(w)
r = R(res, w0)
print("R")
print(r)
return(w + r)
}
I haven't written it in the most efficient way, but you have separate functions for calculating W and R if you need them.
If I run
results2 <- tibble(match=paste(1:15),match_value_opponent=c(rep(3.400,15)), result=c(rep("win",15)))
C(results2, 2)
I get
[1] "W"
[1] 4.062178
[1] "R"
[1] 0.6873927
[1] 4.749571
I think this makes more sense - your W score should be greater than 2 after winning 15 matches against players with match values of 3.4.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论