Calculating Bayesian average for custom post type

This is the (updated) code I’m using, which gives results like I’d expect:

    $args = array(
        'post_type' => 'entry',
        'orderby' => 'bayesian_average',
        'order' => 'DESC',
        'post_status' => 'publish'
    );
    $loop = new WP_Query($args);
    $number_of_entrants = $loop->post_count;
    $total_ratings = $total_num_votes = 0;
    foreach ($loop->posts as $query_post) {
        $count = $query_post->ratings_count;
        $average = $query_post->ratings_average;
        $total_num_votes += $count;
        $total_ratings += $average;
    }
    $average_rating = $total_ratings / $number_of_entrants;
    $avg_num_votes = $total_num_votes / $number_of_entrants;
    if ($loop>have_posts()):
        ?>
        <table class="wp-list-table widefat fixed striped pages">
        <thead>
        <tr>
            <th scope="col" id="entry"><?php _e('Entry', 'textdomain'); ?></th>
            <th scope="col" id="rating-average"><?php _e('Rating average', 'textdomain'); ?></th>
            <th scope="col" id="rating-count"><?php _e('Rating count', 'textdomain'); ?></th>
            <th scope="col" id="beyesian-rating"><?php _e('Bayesian Rating', 'textdomain'); ?></th>
        </tr>
        </thead>
        <tbody><?php
        global $post;
        while ($loop->have_posts()) :
            $loop->the_post();
            $title = get_the_title();
            $this_num_votes = $post->ratings_count;
            $this_avg_rating = $post->ratings_average;
            $bayesian_average =  (($avg_num_votes * $average_rating) + ($this_num_votes * $this_avg_rating)) / ($avg_num_votes + $this_num_votes);
            update_post_meta(get_the_ID(), 'bayesian_average', $bayesian_average); ?>
            <tr>
            <td><a href="https://wordpress.stackexchange.com/questions/259601/<?php echo get_permalink(); ?>" target="_blank"
                   title="<?php echo $title; ?>"><?php echo $title; ?>
                </a></td>
            <td>
                <?php echo $post->ratings_average; ?>
            </td>
            <td>
                <?php echo $post->ratings_count; ?>
            </td>
            <td>
                <?php echo round($bayesian_average, 3); ?>
            </td>
            </tr><?php
        endwhile; ?>
        </tbody>
        </table><?php
    else:
        _e('No Entries', 'textdomain');
    endif;