defmodule TradeMachine.GMGradeV2.Pillars.RosterConstruction.FunctionalDepth do @moduledoc """ Scores AVC's Functional Depth metric over the entire final roster. Position Depth credits a player's listed position at their shared contribution factor and adjacent positions at 35% of that factor. Skill Redundancy counts contribution-factor units only for reliability-adjusted skill indexes of at least 60. Skill Concentration measures the best player's share of each skill's top-three adjusted indexes. Per D-24, concentration is neutral (50) for a skill with fewer than three measured indexes or a zero top-three sum, and every such use is recorded as a fallback. The result's `inputs.detail` records who makes up each total, so a breakdown can show them without working anything out again: %{ experience_minutes: 800, position_depth: %{"PG" => [%{player_id: id, name: name, listed_at: "PG", amount: 1.0, season_minutes: 2100, two_way: false}, ...], ...all five}, skill_redundancy: %{shooting: [%{player_id: id, name: name, figure: 85.1, amount: 1.0, season_minutes: 2100, two_way: false}, ...], ...all five}, skill_concentration: %{shooting: %{players: [%{player_id: id, name: name, figure: 85.1}, ...], stand_in: nil}, ...all five} } * `position_depth`: every player who adds to a position, at his own listed position (`listed_at` is that position) or a neighbouring one (his contribution times the neighbour credit), largest amount first. A position's amounts sum to `inputs.position_depth.units[position]`. * `skill_redundancy`: every qualified provider of the skill, largest amount first, then highest figure. A skill's amounts sum to `inputs.skill_redundancy.units[skill]`. * `skill_concentration`: the players whose figures were used, largest first, and `stand_in`: `nil` where the skill was scored normally (the first figure over the sum of them is `inputs.skill_concentration.top_player_shares[skill]`), otherwise which reason the neutral score was used for, `"fewer_than_three"` (with the players there were) or `"zero_total"`. `season_minutes` and `two_way` are what a player's contribution was worked out from (`SkillIndexes.contribution_basis/1`), and `experience_minutes` is the number of minutes that counts as full experience under the same model override. Nothing reads `detail` to compute a score. """ alias TradeMachine.GMGradeV2.Benchmark alias TradeMachine.GMGradeV2.ChallengeSnapshot alias TradeMachine.GMGradeV2.ModelVersion alias TradeMachine.GMGradeV2.Pillars.RosterConstruction.Inputs alias TradeMachine.GMGradeV2.Pillars.RosterConstruction.PositionCoverage alias TradeMachine.GMGradeV2.Result.MetricResult alias TradeMachine.GMGradeV2.SkillIndexes @adjacent %{ "PG" => ["SG"], "SG" => ["PG", "SF"], "SF" => ["SG", "PF"], "PF" => ["SF", "C"], "C" => ["PF"] } @adjacent_credit 0.35 @qualified_index 60.0 @average_weight 0.70 @weakest_weight 0.30 @weights %{position_depth: 0.35, skill_redundancy: 0.40, skill_concentration: 0.25} @neutral_concentration 50.0 @doc "Returns a full-precision Functional Depth metric result." @spec score(map(), ChallengeSnapshot.t()) :: MetricResult.t() def score( %{roster: roster} = scenario_state, %ChallengeSnapshot{benchmark: %Benchmark{} = benchmark} = snapshot ) when is_list(roster) do tables = benchmark |> Benchmark.section(:skills) |> Map.fetch!("player_percentiles") override = Inputs.model_override(scenario_state, snapshot) profiles = profiles(roster, tables, override) {position_depth, position_inputs, position_fallbacks, position_entries} = position_depth(profiles) {skill_redundancy, redundancy_inputs} = skill_redundancy(profiles) {skill_concentration, concentration_inputs, concentration_fallbacks, concentration_entries} = skill_concentration(profiles) supporting = %{ position_depth: position_depth, skill_redundancy: skill_redundancy, skill_concentration: skill_concentration } %MetricResult{ key: :functional_depth, version: benchmark.version, score: position_depth * @weights.position_depth + skill_redundancy * @weights.skill_redundancy + skill_concentration * @weights.skill_concentration, inputs: %{ position_depth: position_inputs, skill_redundancy: redundancy_inputs, skill_concentration: concentration_inputs, detail: %{ experience_minutes: ModelVersion.experience_minutes(override), position_depth: position_entries, skill_redundancy: redundancy_entries(profiles), skill_concentration: concentration_entries } }, supporting: supporting, weights: @weights, fallbacks: position_fallbacks ++ concentration_fallbacks, explanation: "Functional Depth combines position depth (35%), skill redundancy (40%), " <> "and skill concentration (25%) across the entire roster." } end @doc "Converts non-negative position-depth units through AVC's piecewise curve." @spec position_depth_score(number()) :: float() def position_depth_score(units) when is_number(units) and units >= 0 do cond do units <= 2.0 -> units * 50.0 units <= 3.0 -> 100.0 units <= 4.0 -> interpolate(units, 3.0, 100.0, 4.0, 90.0) units <= 5.0 -> interpolate(units, 4.0, 90.0, 5.0, 75.0) true -> 75.0 end end @doc "Converts non-negative qualified-provider units through AVC's redundancy curve." @spec redundancy_score(number()) :: float() def redundancy_score(units) when is_number(units) and units >= 0 do cond do units <= 1.0 -> interpolate(units, 0.0, 0.0, 1.0, 45.0) units <= 2.0 -> interpolate(units, 1.0, 45.0, 2.0, 75.0) units <= 3.0 -> interpolate(units, 2.0, 75.0, 3.0, 90.0) units <= 4.0 -> interpolate(units, 3.0, 90.0, 4.0, 100.0) true -> 100.0 end end @doc "Converts a best-player top-three share through AVC's concentration curve." @spec concentration_score(number()) :: float() def concentration_score(share) when is_number(share) and share >= 0 do cond do share <= 0.40 -> 100.0 share <= 0.45 -> interpolate(share, 0.40, 100.0, 0.45, 85.0) share <= 0.50 -> interpolate(share, 0.45, 85.0, 0.50, 70.0) share <= 0.55 -> interpolate(share, 0.50, 70.0, 0.55, 50.0) share <= 0.60 -> interpolate(share, 0.55, 50.0, 0.60, 30.0) share <= 0.65 -> interpolate(share, 0.60, 30.0, 0.65, 10.0) true -> 10.0 end end defp profiles(roster, tables, override) do Enum.map(roster, fn player -> %{ player: player, position: PositionCoverage.normalize_position!(fetch_any!(player, :position)), contribution_factor: SkillIndexes.contribution_factor(player, override), indexes: SkillIndexes.player_indexes(player, tables, %{override: override}).skills } end) end defp position_depth(profiles) do initial = Map.new(PositionCoverage.positions(), &{&1, 0.0}) no_entries = Map.new(PositionCoverage.positions(), &{&1, []}) {units, fallbacks, entries} = Enum.reduce(profiles, {initial, [], no_entries}, fn profile, {units, fallbacks, entries} -> case profile.contribution_factor do factor when is_number(factor) -> units = units |> Map.update!(profile.position, &(&1 + factor)) |> add_adjacent_units(profile.position, factor) {units, fallbacks, add_position_entries(entries, profile, factor)} nil -> fallback = %{ input: player_identifier(profile.player), method: :missing_contribution_factor, note: "#{player_label(profile.player)} has no season-minutes sample, so no " <> "position-depth or qualified-provider credit was invented." } {units, [fallback | fallbacks], entries} end end) scores = Map.new(units, fn {position, value} -> {position, position_depth_score(value)} end) {score, average, weakest} = weakest_blend(Map.values(scores)) inputs = %{ units: units, scores: scores, average_score: average, weakest_score: weakest } entries = Map.new(entries, fn {position, players} -> {position, players |> Enum.reverse() |> Enum.sort_by(& &1.amount, :desc)} end) {score, inputs, Enum.reverse(fallbacks), entries} end # The entries `position_depth/1` records beside its units: the player at his # own position, at his contribution, and at each neighbouring position, at # the amount `add_adjacent_units/3` adds there. Prepended; reversed once at # the end so each position keeps roster order before sorting. defp add_position_entries(entries, profile, factor) do own = player_entry(profile, %{listed_at: profile.position, amount: factor}) entries = Map.update!(entries, profile.position, &[own | &1]) Enum.reduce(Map.fetch!(@adjacent, profile.position), entries, fn adjacent, acc -> entry = player_entry(profile, %{ listed_at: profile.position, amount: factor * @adjacent_credit }) Map.update!(acc, adjacent, &[entry | &1]) end) end defp skill_redundancy(profiles) do units = Map.new(SkillIndexes.skills(), fn skill -> value = Enum.reduce(profiles, 0.0, fn profile, total -> if qualified_provider?(profile, skill) do total + profile.contribution_factor else total end end) {skill, value} end) scores = Map.new(units, fn {skill, value} -> {skill, redundancy_score(value)} end) {score, average, weakest} = weakest_blend(Map.values(scores)) {score, %{ qualified_index_threshold: @qualified_index, units: units, scores: scores, average_score: average, weakest_score: weakest }} end # The one test of who counts as a provider of a skill, used by the units # above and by the entries recorded beside them. defp qualified_provider?(profile, skill) do index = get_in(profile, [:indexes, skill, :index]) is_number(index) and index >= @qualified_index and is_number(profile.contribution_factor) end defp redundancy_entries(profiles) do Map.new(SkillIndexes.skills(), fn skill -> providers = profiles |> Enum.filter(&qualified_provider?(&1, skill)) |> Enum.map(fn profile -> player_entry(profile, %{ figure: get_in(profile, [:indexes, skill, :index]), amount: profile.contribution_factor }) end) |> Enum.sort_by(&{&1.amount, &1.figure}, :desc) {skill, providers} end) end defp skill_concentration(profiles) do initial = {%{}, %{}, [], %{}} {shares, scores, fallbacks, detail} = Enum.reduce(SkillIndexes.skills(), initial, fn skill, {shares, scores, fallbacks, detail} -> top_three = profiles |> Enum.map(&get_in(&1, [:indexes, skill, :index])) |> Enum.filter(&is_number/1) |> Enum.sort(:desc) |> Enum.take(3) players = top_three_players(profiles, skill) total = Enum.sum(top_three) if length(top_three) < 3 or total == 0 do reason = if length(top_three) < 3, do: :fewer_than_three_indexes, else: :zero_top_three_sum fallback = %{ input: Atom.to_string(skill), method: :neutral_skill_concentration, note: "D-24 applies the neutral concentration score of 50 for #{skill}: " <> concentration_reason(reason, length(top_three)) } { Map.put(shares, skill, nil), Map.put(scores, skill, @neutral_concentration), [fallback | fallbacks], Map.put(detail, skill, %{players: players, stand_in: stand_in(reason)}) } else share = hd(top_three) / total { Map.put(shares, skill, share), Map.put(scores, skill, concentration_score(share)), fallbacks, Map.put(detail, skill, %{players: players, stand_in: nil}) } end end) average = scores |> Map.values() |> then(&(Enum.sum(&1) / length(SkillIndexes.skills()))) {average, %{top_player_shares: shares, scores: scores}, Enum.reverse(fallbacks), detail} end # The players whose figures `top_three` holds, in the same order: each # profile beside its own index, the unmeasured left out, highest first. defp top_three_players(profiles, skill) do profiles |> Enum.map(&{&1, get_in(&1, [:indexes, skill, :index])}) |> Enum.filter(fn {_profile, figure} -> is_number(figure) end) |> Enum.sort_by(fn {_profile, figure} -> figure end, :desc) |> Enum.take(3) |> Enum.map(fn {profile, figure} -> %{ player_id: player_id(profile.player), name: fetch_any(profile.player, :name), figure: figure } end) end defp stand_in(:fewer_than_three_indexes), do: "fewer_than_three" defp stand_in(:zero_top_three_sum), do: "zero_total" # One player's entry in a position-depth or skill-redundancy list: who he # is, what his contribution was worked out from, and the given facts. defp player_entry(profile, facts) do basis = SkillIndexes.contribution_basis(profile.player) Map.merge( %{ player_id: player_id(profile.player), name: fetch_any(profile.player, :name), season_minutes: basis.season_minutes, two_way: basis.two_way }, facts ) end defp player_id(player), do: fetch_any(player, :id) || fetch_any(player, :player_id) defp add_adjacent_units(units, position, factor) do Enum.reduce(Map.fetch!(@adjacent, position), units, fn adjacent, acc -> Map.update!(acc, adjacent, &(&1 + factor * @adjacent_credit)) end) end defp weakest_blend(scores) do average = Enum.sum(scores) / length(scores) weakest = Enum.min(scores) {average * @average_weight + weakest * @weakest_weight, average, weakest} end defp interpolate(value, low_x, low_y, high_x, high_y) do low_y + (value - low_x) / (high_x - low_x) * (high_y - low_y) end defp fetch_any!(map, key) do case fetch_any(map, key) do nil -> raise KeyError, key: key, term: map value -> value end end defp fetch_any(map, key), do: Map.get(map, key, Map.get(map, Atom.to_string(key))) defp player_identifier(player) do fetch_any(player, :id) || fetch_any(player, :player_id) || player_label(player) end defp player_label(player) do fetch_any(player, :name) || "Player #{inspect(fetch_any(player, :id) || fetch_any(player, :player_id))}" end defp concentration_reason(:fewer_than_three_indexes, count), do: "only #{count} measured player indexes were available." defp concentration_reason(:zero_top_three_sum, _count), do: "the top-three index sum was zero." end