]> git.sesse.net Git - pkanalytics/commitdiff
Add some team-wide touches stats.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 27 Jul 2023 11:13:13 +0000 (13:13 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 27 Jul 2023 11:13:13 +0000 (13:13 +0200)
ultimate.js

index d9196967c8502fe9127a2d032a65ad80f381a980..7e54560141821432b3a65f154dca8168a3b08fdd 100644 (file)
@@ -239,6 +239,9 @@ function calc_stats(json, filters) {
                'turnovers_lost': 0,
                'their_clean_holds': 0,
                'their_clean_breaks': 0,
+
+               'touches_for_turnover': 0,
+               'touches_for_goal': 0,
        };
        let globals = players['globals'];
 
@@ -266,6 +269,7 @@ function calc_stats(json, filters) {
 
                let we_lost_disc = false;
                let they_lost_disc = false;
+               let touches_this_possession = 0;
 
                // The last used formations of the given kind, if any; they may be reused
                // when the point starts, if nothing else is set.
@@ -376,6 +380,7 @@ function calc_stats(json, filters) {
                        } else if (type === 'goal' || type === 'their_goal' || type === 'stoppage') {
                                if (type === 'goal') {
                                        if (keep) ++p.touches;
+                                       ++touches_this_possession;
                                }
                                for (const [q,p] of Object.entries(players)) {
                                        if (p.on_field_since === null) {
@@ -499,18 +504,24 @@ function calc_stats(json, filters) {
                                we_lost_disc = true;
                                if (keep && type !== 'goal' && !(type === 'set_defense' && last_pull_was_ours === null)) {
                                        ++globals.turnovers_lost;
+                                       globals.touches_for_turnover += touches_this_possession;
+                               } else if (keep && type === 'goal') {
+                                       globals.touches_for_goal += touches_this_possession;
                                }
+                               touches_this_possession = 0;
                        } else if (type === 'set_offense' || type === 'their_goal' || type === 'their_throwaway' || type === 'defense' || type === 'interception') {
                                offense = true;
                                they_lost_disc = true;
                                if (keep && type !== 'their_goal' && !(type === 'set_offense' && last_pull_was_ours === null)) {
                                        ++globals.turnovers_won;
                                }
+                               touches_this_possession = 0;
                        }
                        if (type === 'goal' || type === 'their_goal') {
                                between_points = true;
                                we_lost_disc = false;
                                they_lost_disc = false;
+                               touches_this_possession = 0;
                        }
                        if (last_offense !== offense && live_since !== null) {
                                // Switched offense/defense status, so attribute this drive as needed,
@@ -619,6 +630,7 @@ function calc_stats(json, filters) {
                                }
 
                                if (keep) ++p.touches;
+                               ++touches_this_possession;
                                if (type === 'goal') {
                                        if (keep) {
                                                if (prev_handler !== null) {
@@ -658,6 +670,7 @@ function calc_stats(json, filters) {
                                        ++p.catches;
                                        ++p.defenses;
                                        ++p.touches;
+                                       ++touches_this_possession;
                                }
                                prev_handler = null;
                                handler = e['player'];
@@ -864,6 +877,8 @@ function make_efficiency_ci(points_won, points_completed, z)
 //
 // Modified Wald (recommended by http://www.ine.pt/revstat/pdf/rs120203.pdf
 // since our rates are definitely below 2 per point).
+//
+// FIXME: z is ignored.
 function make_poisson_ci(val, num, z, inverted)
 {
        let low  = (val == 0) ? 0.0 : ((val - 0.5) - Math.sqrt(val - 0.5)) / num;
@@ -885,6 +900,31 @@ function make_poisson_ci(val, num, z, inverted)
        };
 }
 
+// Wilson and Hilferty, again recommended for general use in the PDF above
+// (anything from their group “G1” works).
+// https://en.wikipedia.org/wiki/Poisson_distribution#Confidence_interval
+function make_poisson_ci_large(val, num, z, inverted)
+{
+       let low  = val * Math.pow(1.0 - 1.0 / (9.0 * val) - z / (3.0 * Math.sqrt(val)), 3.0) / num;
+       let high = (val + 1.0) * Math.pow(1.0 - 1.0 / (9.0 * (val + 1.0)) + z / (3.0 * Math.sqrt(val + 1.0)), 3.0) / num;
+
+       // Fix the signs so that we don't get -0.00.
+       low = Math.max(low, 0.0);
+
+       // The display range of 0 to 25.0 is fairly arbitrary. We have no desire here
+       // (this is used for number of touches).
+       let avg = val / num;
+       return {
+               'val': avg,
+               'lower_ci': low,
+               'upper_ci': high,
+               'min': 0.0,
+               'max': 25.0,
+               'desired': 0.0,
+               'inverted': inverted,
+       };
+}
+
 function make_table_general(players) {
        let rows = [];
        {
@@ -988,6 +1028,41 @@ function make_table_team_wide(players) {
                rows.push(row);
        }
 
+       // Touches. We only have information for our team here.
+       {
+               let goals = 0;
+               for (const [q,p] of get_sorted_players(players)) {
+                       if (q === 'globals') continue;
+                       goals += p.goals;
+               }
+
+               let row = document.createElement('tr');
+               let name = add_3cell(row, 'Touches per possession (all)', 'name');
+               let touches = globals.touches_for_turnover + globals.touches_for_goal;
+               let possessions = goals + globals.turnovers_lost;
+               add_3cell(row, '');
+               add_3cell_ci(row, make_poisson_ci_large(touches, possessions, z));
+               add_3cell(row, '');
+               add_3cell(row, '');
+               rows.push(row);
+
+               row = document.createElement('tr');
+               add_3cell(row, 'Touches per possession (goals)', 'name');
+               add_3cell(row, '');
+               add_3cell_ci(row, make_poisson_ci_large(globals.touches_for_goal, goals, z));
+               add_3cell(row, '');
+               add_3cell(row, '');
+               rows.push(row);
+
+               row = document.createElement('tr');
+               add_3cell(row, 'Touches per possession (turnovers)', 'name');
+               add_3cell(row, '');
+               add_3cell_ci(row, make_poisson_ci_large(globals.touches_for_turnover, globals.turnovers_lost, z));
+               add_3cell(row, '');
+               add_3cell(row, '');
+               rows.push(row);
+       }
+
        return rows;
 }