计算瑞士网球排名分类。

huangapple go评论54阅读模式
英文:

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.

计算瑞士网球排名分类。

计算瑞士网球排名分类。

  1. W = your match value
  2. W0= your previous match value
  3. Wi= the match value of the player you defeated
  4. Wj= the match value of the player you lost to
  5. s= sum of the matches you won
  6. N= 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 &lt;- tibble(match=c(1,2,3),match_value_opponent=c(2.4,4.5,3.4), result=c(&quot;win&quot;,&quot;loss&quot;,&quot;win&quot;))

The previous match value is a constant:
w0 &lt;- 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 &lt;- tibble %&gt;%
    mutate(e_pos_wi = case_when(result == &quot;win&quot;  ~ exp(match_value_opponent)),
           e_neg_wj = case_when(result == &quot;loss&quot; ~ exp(-match_value_opponent))) %&gt;%
    summarise(sum_e_pos_wi = sum(e_pos_wi, na.rm = T),
              sum_e_neg_wj = sum(e_neg_wj, na.rm = T)) %&gt;%
    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(&quot;W&quot;,&quot;R&quot;,&quot;C&quot;)])
  
}


# 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 &lt;- tibble(match=paste(1:15),match_value_opponent=c(rep(3.400,15)), result=c(rep(&quot;win&quot;,15)))

# calculate W with the same W0 as in the Mathematica question/answer
calculate_c(tibble = results, w0=2.354)
#&gt; # A tibble: 1 &#215; 3
#&gt;       W     R     C
#&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
#&gt; 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 &lt;- tibble %&gt;%
    mutate(e_pos_wi = case_when(result == &quot;win&quot;  ~ exp(match_value_opponent)),
           e_neg_wj = case_when(result == &quot;loss&quot; ~ exp(-match_value_opponent))) %&gt;%
    summarise(sum_e_pos_wi = sum(e_pos_wi, na.rm = T),
              sum_e_neg_wj = sum(e_neg_wj, na.rm = T)) %&gt;%
    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[, &quot;C&quot;])
  
}

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 == &quot;win&quot;]
    # first half of W
    w1 = log(sum(exp(wi)) + exp(w0))
    
    # scores when you lose
    wj = res$match_value_opponent[res$result == &quot;loss&quot;]
    # 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 == &quot;win&quot;]
    # first half of W
    w1 = log(sum(exp(wi)) + exp(w0))
    
    # scores when you lose
    wj = res$match_value_opponent[res$result == &quot;loss&quot;]
    # 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(&quot;W&quot;)
    print(w)
    r = R(res, w0)
    print(&quot;R&quot;)
    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 &lt;- tibble(match=paste(1:15),match_value_opponent=c(rep(3.400,15)), result=c(rep(&quot;win&quot;,15)))
C(results2, 2)

I get

[1] &quot;W&quot;
[1] 4.062178
[1] &quot;R&quot;
[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.

huangapple
  • 本文由 发表于 2023年4月19日 15:27:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定