>= for vectors of integers

I am trying to create a function that calculates the probability of winning a tennis game in vectorized form.

However, no matter how i slice it (as int arrays or as vectors) i am getting an error when trying to do vector wise greater than.

Any ideas how to do this? TIA

Ill-typed arguments supplied to infix operator >=. Available signatures:
(int, int) => int
(int, real) => int
(real, int) => int
(real, real) => int
Instead supplied arguments of incompatible type: vector, vector.

here is my function call

functions {
  vector probability_win_game(array[] int server_score, 
                              array[] int receiver_score, 
                              vector p_win_point, 
                              array[] int case6, 
                              array[] int server_points_to_win, 
                              array[] int total_points,
                              array[] int set_break) {
    int N = size(server_score);
    vector[N] result = rep_vector(0.0, N);
    vector[N] p_win_from_deuce = square(p_win_point) ./ (square(p_win_point) + square(1 - p_win_point));
    
    // Case 1: Server won (regular game or tiebreaker)
    vector[N] condition_game_won = to_vector(server_score >= 7) .* to_vector(set_break) + 
                                   to_vector(server_score >= 4) .* (1 - to_vector(set_break));
    vector[N] condition_score_diff = to_vector(server_score >= (receiver_score + 2));
    result = result + condition_game_won .* condition_score_diff;
    
    // Case 3: Deuce
    vector[N] condition_deuce = to_vector(server_score == receiver_score) .* to_vector(server_score >= 3);
    result = result + condition_deuce .* p_win_from_deuce;
    
    // Case 4: Advantage server
    vector[N] condition_adv_server = to_vector(server_score == (receiver_score + 1)) .* to_vector(server_score >= 4);
    result = result + condition_adv_server .* (p_win_point + (1 - p_win_point) .* p_win_from_deuce);
    
    // Case 5: Advantage receiver
    vector[N] condition_adv_receiver = to_vector(receiver_score == (server_score + 1)) .* to_vector(receiver_score >= 4);
    result = result + condition_adv_receiver .* ((1 - p_win_point) .* p_win_from_deuce);
    
    // Case 6: Other scenarios
    result = result + to_vector(case6) .* 
             (1 - to_vector(binomial_cdf(server_points_to_win - 1, 
                                         total_points, 
                                         p_win_point)));
    
    return result;
  }
}```

I do not think Stan supports logical tests with datatypes other than real and/or integers. Vectorization in this sense is hence not possible and you’ll have to loop over each element in the vector.

Thanks garren. Is that also the case with something like max(vector1-vector2,0)?

What is desired behavior here? To return a vector with the difference between vector_1 and vector_2 if it is greater than zero, and zero otherwise? In this case you’ll probably have to use a loop again.

The fmax function supports the signature you want here

1 Like