]> git.sesse.net Git - pkanalytics/blobdiff - ultimate.js
Two decimals is a bit too hardcore for possession times.
[pkanalytics] / ultimate.js
index 200303c32fdbada7d3df8738ee240d3903be4d06..6c066bcfac8149330c26a8413b4a7237240a90e2 100644 (file)
@@ -116,9 +116,14 @@ function add_3cell_ci(tr, ci) {
        let text;
        if (ci.format === 'percentage') {
                text = (100 * ci.val).toFixed(0) + '%';
+       } else if (ci.decimals !== undefined) {
+               text = ci.val.toFixed(ci.decimals);
        } else {
                text = ci.val.toFixed(2);
        }
+       if (ci.unit !== undefined) {
+               text += ' '+ ci.unit;
+       }
        let element = add_3cell(tr, text);
        let to_x = (val) => { return ci_width * (val - ci.min) / (ci.max - ci.min); };
 
@@ -239,6 +244,13 @@ function calc_stats(json, filters) {
                'turnovers_lost': 0,
                'their_clean_holds': 0,
                'their_clean_breaks': 0,
+
+               'touches_for_turnover': 0,
+               'touches_for_goal': 0,
+               'time_for_turnover': [],
+               'time_for_goal': [],
+               'their_time_for_turnover': [],
+               'their_time_for_goal': [],
        };
        let globals = players['globals'];
 
@@ -266,6 +278,9 @@ function calc_stats(json, filters) {
 
                let we_lost_disc = false;
                let they_lost_disc = false;
+               let touches_this_possession = 0;
+               let possession_started = null;
+               let extra_ms_this_possession = 0;  // Used at stoppages.
 
                // The last used formations of the given kind, if any; they may be reused
                // when the point starts, if nothing else is set.
@@ -366,7 +381,9 @@ function calc_stats(json, filters) {
                        // Liveness management
                        if (type === 'pull' || type === 'their_pull' || type === 'restart') {
                                live_since = t;
-                               between_points = false;
+                               if (type !== 'restart') {
+                                       between_points = false;
+                               }
                        } else if (type === 'catch' && last_pull_was_ours === null) {
                                // Someone forgot to add the pull, so we'll need to wing it.
                                console.log(match['description'] + ' ' + format_time(t) + ': Missing pull on ' + our_score + '\u2013' + their_score + '; pretending to have one.');
@@ -374,6 +391,10 @@ function calc_stats(json, filters) {
                                last_pull_was_ours = !offense;
                                between_points = false;
                        } 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) {
                                                continue;
@@ -496,18 +517,33 @@ 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;
+                                       if (possession_started != null) globals.time_for_turnover.push((t - possession_started) + extra_ms_this_possession);
+                               } else if (keep && type === 'goal') {
+                                       globals.touches_for_goal += touches_this_possession;
+                                       if (possession_started != null) globals.time_for_goal.push((t - possession_started) + extra_ms_this_possession);
                                }
+                               touches_this_possession = 0;
+                               possession_started = null;
+                               extra_ms_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;
+                                       if (possession_started != null) globals.their_time_for_turnover.push((t - possession_started) + extra_ms_this_possession);
+                               } else if (keep && type === 'their_goal') {
+                                       if (possession_started != null) globals.their_time_for_goal.push((t - possession_started) + extra_ms_this_possession);
                                }
+                               touches_this_possession = 0;
+                               possession_started = null;
+                               extra_ms_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,
@@ -528,6 +564,19 @@ function calc_stats(json, filters) {
                                }
                                live_since = t;
                        }
+                       // The rules for possessions are slightly different; we want to start at pulls,
+                       // not previous goals.
+                       if ((last_offense !== offense && type !== 'goal' && type !== 'their_goal' && live_since !== null) ||
+                           type === 'pull' || type === 'their_pull') {
+                               possession_started = t;
+                       } else if (type === 'stoppage') {
+                               if (possession_started !== null) {
+                                       extra_ms_this_possession = t - possession_started;
+                                       possession_started = null;
+                               }
+                       } else if (type === 'restart' && live_since !== null && !between_points) {
+                               possession_started = t;
+                       }
 
                        if (type === 'pull') {
                                last_pull_was_ours = true;
@@ -616,6 +665,7 @@ function calc_stats(json, filters) {
                                }
 
                                if (keep) ++p.touches;
+                               ++touches_this_possession;
                                if (type === 'goal') {
                                        if (keep) {
                                                if (prev_handler !== null) {
@@ -655,6 +705,7 @@ function calc_stats(json, filters) {
                                        ++p.catches;
                                        ++p.defenses;
                                        ++p.touches;
+                                       ++touches_this_possession;
                                }
                                prev_handler = null;
                                handler = e['player'];
@@ -861,6 +912,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;
@@ -882,6 +935,75 @@ 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)
+{
+       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': false,
+       };
+}
+
+// We don't really know what to expect for possessions; it appears roughly
+// lognormal, but my head doesn't work well enough to extract the CI for
+// the estimated sample mean, and now my head spins around whether we do
+// this correctly even for Poisson :-) But we do a simple (non-BCa) bootstrap
+// here, which should give us what we want.
+//
+// FIXME: z is ignored.
+function make_ci_duration(vals, z)
+{
+       // Bootstrap.
+       let sample_means = [];
+       for (let i = 0; i < 300; ++i) {
+               let sum = 0.0;
+               for (let j = 0; j < vals.length; ++j) {
+                       sum += vals[Math.floor(Math.random() * vals.length)];
+               }
+               sample_means.push(sum / vals.length);
+       }
+       sample_means.sort();
+
+       // Interpolating here would probably be better, but OK.
+       let low  = sample_means[Math.floor(0.025 * sample_means.length)];
+       let high = sample_means[Math.floor(0.975 * sample_means.length)];
+
+       // Find the point estimate of the mean.
+       let sum = 0.0;
+       for (const v of vals) {
+               sum += v;
+       }
+       let avg = sum / vals.length;
+
+       return {
+               'val': avg / 1000.0,
+               'lower_ci': low / 1000.0,
+               'upper_ci': high / 1000.0,
+               'min': 0.0,
+               'max': 120.0,
+               'desired': 0.0,
+               'inverted': false,
+               'unit': 'sec',
+               'decimals': 1,
+       };
+}
+
 function make_table_general(players) {
        let rows = [];
        {
@@ -944,10 +1066,10 @@ function make_table_team_wide(players) {
        {
                let row = document.createElement('tr');
                let name = add_3cell(row, 'Turnovers generated', 'name');
-               add_3cell(row, '');
                add_3cell(row, globals.turnovers_won);
                add_3cell(row, '');
                add_3cell(row, globals.turnovers_lost);
+               add_3cell(row, '');
                rows.push(row);
        }
 
@@ -985,6 +1107,69 @@ 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);
+       }
+
+       // Time. Here we have (roughly) for both.
+       {
+               let row = document.createElement('tr');
+               let name = add_3cell(row, 'Time per possession (all)', 'name');
+               let time = globals.time_for_turnover.concat(globals.time_for_goal);
+               let their_time = globals.their_time_for_turnover.concat(globals.their_time_for_goal);
+               add_3cell(row, '');
+               add_3cell_ci(row, make_ci_duration(time, z));
+               add_3cell(row, '');
+               add_3cell_ci(row, make_ci_duration(their_time, z));
+               rows.push(row);
+
+               row = document.createElement('tr');
+               add_3cell(row, 'Time per possession (goals)', 'name');
+               add_3cell(row, '');
+               add_3cell_ci(row, make_ci_duration(globals.time_for_goal, z));
+               add_3cell(row, '');
+               add_3cell_ci(row, make_ci_duration(globals.their_time_for_goal, z));
+               rows.push(row);
+
+               row = document.createElement('tr');
+               add_3cell(row, 'Time per possession (turnovers)', 'name');
+               add_3cell(row, '');
+               add_3cell_ci(row, make_ci_duration(globals.time_for_turnover, z));
+               add_3cell(row, '');
+               add_3cell_ci(row, make_ci_duration(globals.their_time_for_turnover, z));
+               rows.push(row);
+       }
+
        return rows;
 }