defmodule TradeMachine.GMGradeV2.Pillars.AssetManagement do @moduledoc """ Aggregates AVC's four Asset Management metrics. An uncalibrated player-involving Asset Exchange Value remains visible but is excluded under D-10. The other three weights are then renormalized from 35/20/20 to exact proportions of their 75% subtotal. """ alias TradeMachine.GMGradeV2.Pillars.AssetManagement.AssetExchangeValue alias TradeMachine.GMGradeV2.ModelVersion alias TradeMachine.GMGradeV2.Result.MetricResult alias TradeMachine.GMGradeV2.Result.PillarResult alias TradeMachine.GMGradeV2.Weighting @full_weights %{ draft_capital_value: 0.35, pick_quality: 0.20, asset_optionality: 0.20, asset_exchange_value: 0.25 } @inventory_total 0.75 @excluded_metric :asset_exchange_value @inventory_metrics [:draft_capital_value, :pick_quality, :asset_optionality] @provisional_weights %{ draft_capital_value: 0.35 / @inventory_total, pick_quality: 0.20 / @inventory_total, asset_optionality: 0.20 / @inventory_total, asset_exchange_value: 0.0 } if abs(Enum.sum(Map.values(@full_weights)) - 1.0) > 1.0e-9 or abs(Enum.sum(Map.values(@provisional_weights)) - 1.0) > 1.0e-9 do raise "Asset Management weights must sum to 1.0" end @doc """ The published full metric weights, before D-10's provisional exclusion. Public so the parameter registry and the model-document seed can read the authored weights instead of restating them (plan §5.1). These are the four the model document stores; the provisional set is derived from them by renormalizing against the #{@inventory_total} inventory subtotal. """ @spec full_weights() :: %{atom() => float()} def full_weights, do: @full_weights @doc """ The provisional metric weights D-10's exclusion produces. The three inventory metrics renormalized against their #{@inventory_total} subtotal, with Asset Exchange Value pinned to `0.0` — the weight set `score/1` uses when AEV is provisional. Public so the model-document validator can read *which* metric is excluded rather than restating it (plan §3.4 item 8): the document stores `full_weights/0` and derives this set, so the validator has to know the exclusion to check the renormalization. """ @spec provisional_weights() :: %{atom() => float()} def provisional_weights, do: @provisional_weights @doc """ Renormalizes an arbitrary full weight set the way D-10's exclusion does. The published set is `provisional_weights/0`; this is the same derivation over the four weights a resolved grading model supplies. The divisor is the retained metrics' own subtotal, so the result sums to 1.0 whatever the document rounded to — and for the published 35/20/20/25 it is bit-identical to `provisional_weights/0`, since `0.35 + 0.20 + 0.20` is exactly `#{@inventory_total}` in binary floating point. Lives here, and not on `ResolvedModel`, because the exclusion is this pillar's rule: the document stores the full four, and a second implementation of the renormalization is the thing brief invariant 2 exists to prevent. """ @spec provisional_weights(Weighting.weights()) :: %{atom() => float()} def provisional_weights(full_weights) when is_map(full_weights) do subtotal = @inventory_metrics |> Enum.map(&Map.fetch!(full_weights, &1)) |> Enum.sum() @inventory_metrics |> Map.new(&{&1, Map.fetch!(full_weights, &1) / subtotal}) |> Map.put(@excluded_metric, 0.0) end @doc """ Aggregates the four canonical metric results in published order. `full_weights` defaults to `full_weights/0`; a resolved grading model supplies its own four, from which the provisional set is derived by `provisional_weights/1` (plan §5.1). This pillar receives metric results rather than `scenario_state`, which is the other half of why the weights are an explicit argument. """ @spec score([MetricResult.t()], Weighting.weights()) :: PillarResult.t() def score( [ %MetricResult{key: :draft_capital_value}, %MetricResult{key: :pick_quality}, %MetricResult{key: :asset_optionality}, %MetricResult{key: :asset_exchange_value} = exchange ] = metric_results, full_weights \\ @full_weights ) do provisional? = AssetExchangeValue.status(exchange) == :provisional weights = if(provisional?, do: provisional_weights(full_weights), else: full_weights) scored_results = if(provisional?, do: Enum.take(metric_results, 3), else: metric_results) {strongest, weakest} = extrema(scored_results) %PillarResult{ key: :asset_management, version: ModelVersion.model_version(), score: Weighting.combine(scored_results, weights), metric_results: metric_results, metric_weights: weights, provisional?: false, strongest: strongest.key, weakest: weakest.key, explanation: explanation(provisional?, strongest, weakest, full_weights) } end @doc """ Projects Asset Management for a state-only comparison. The four canonical metric slots and the **full** weights are preserved, but Asset Exchange Value is fixed at neutral `50`. This deliberately ignores whether the canonical AEV result was provisional: transaction-path evidence must affect neither side of a Comparable State Score, and that is also why the weight set is never renormalized here — the AEV slot is scored at neutral, not excluded. `full_weights` defaults to `full_weights/0`. It is the **seventh** call site for this pillar's weights and the one off the `score/2` path: `Progression` reaches it through `ComparableState.project/2`, which resolves the same model the canonical grade used, so a Comparable State Score and the grade it is compared against cannot be weighted differently. """ @spec comparable_score([MetricResult.t()], Weighting.weights()) :: float() def comparable_score( [ %MetricResult{key: :draft_capital_value}, %MetricResult{key: :pick_quality}, %MetricResult{key: :asset_optionality}, %MetricResult{key: :asset_exchange_value} ] = metric_results, full_weights \\ @full_weights ) do metric_results |> Enum.map(fn %MetricResult{key: :asset_exchange_value} = result -> %{result | score: 50.0} result -> result end) |> Weighting.combine(full_weights) end defp extrema(results), do: {Enum.max_by(results, & &1.score), Enum.min_by(results, & &1.score)} defp explanation( true, %MetricResult{key: strongest, score: strongest_score}, %MetricResult{key: weakest, score: weakest_score}, full_weights ) do "Asset Exchange Value is displayed but excluded until PTV is calibrated; " <> "the #{rendered_weights(full_weights, @inventory_metrics)} inventory weights are " <> "renormalized to " <> "#{rendered_weights(provisional_weights(full_weights), @inventory_metrics)}. " <> "#{label(strongest)} is strongest at #{strongest_score}, while " <> "#{label(weakest)} is weakest at #{weakest_score}." end defp explanation( false, %MetricResult{key: strongest, score: strongest_score}, %MetricResult{key: weakest, score: weakest_score}, full_weights ) do "Asset Management applies the full " <> "#{rendered_weights(full_weights, @inventory_metrics ++ [@excluded_metric])} " <> "composition; #{label(strongest)} is strongest at #{strongest_score}, while " <> "#{label(weakest)} is weakest at #{weakest_score}." end # Read out of the weight set rather than written into the prose, because a # document may carry weights that are not 35/20/20/25 and a sentence naming # the published numbers while scoring with different ones is worse than no # sentence. Trailing `.0` is trimmed so the published set still renders # exactly `35/20/20/25` and `46.67/26.67/26.67`, as every existing grade's # explanation does. defp rendered_weights(weights, keys) do Enum.map_join(keys, "/", fn key -> weights |> Map.fetch!(key) |> Kernel.*(100) |> Float.round(2) |> :erlang.float_to_binary([:compact, decimals: 2]) |> String.replace_suffix(".0", "") end) end defp label(:draft_capital_value), do: "Draft Capital Value" defp label(:pick_quality), do: "Pick Quality" defp label(:asset_optionality), do: "Asset Optionality" defp label(:asset_exchange_value), do: "Asset Exchange Value" end