namespace AIStudio.Assistants.ModelComparison;
///
/// How often each model came out ahead across several runs: by the votes of the user, or by the verdicts of the judge.
///
///
/// Counted in models, never in columns: the column a model stood in was drawn fresh for every run, so only the model
/// stays comparable across runs. A run without a usable choice, such as one the user did not vote on, or one whose
/// judge gave no usable verdict, counts toward TotalRunCount, but toward nothing else.
///
public sealed record ModelComparisonTally
{
///
/// How many runs there were, counted or not.
///
public required int TotalRunCount { get; init; }
public required int FirstModelWins { get; init; }
public required int SecondModelWins { get; init; }
public required int Ties { get; init; }
///
/// How many runs had a usable choice and were counted.
///
public int ScoredRunCount => this.FirstModelWins + this.SecondModelWins + this.Ties;
///
/// How many runs had no usable choice and were left out.
///
public int UnscoredRunCount => this.TotalRunCount - this.ScoredRunCount;
///
/// The tally of no runs at all.
///
public static readonly ModelComparisonTally NONE = new()
{
TotalRunCount = 0,
FirstModelWins = 0,
SecondModelWins = 0,
Ties = 0,
};
///
/// Counts the choices of several runs.
///
/// The choice of each run, already turned from a column into a model; null for a run without a usable choice.
/// The tally.
public static ModelComparisonTally Of(IEnumerable choices) => choices.Aggregate(NONE, (tally, choice) => tally.Plus(choice));
///
/// Counts one more run.
///
/// The choice of the run, already turned from a column into a model; null for a run without a usable choice.
/// The tally with the run.
public ModelComparisonTally Plus(ModelComparisonModelChoice? choice) => this with
{
TotalRunCount = this.TotalRunCount + 1,
FirstModelWins = this.FirstModelWins + (choice is ModelComparisonModelChoice.FIRST ? 1 : 0),
SecondModelWins = this.SecondModelWins + (choice is ModelComparisonModelChoice.SECOND ? 1 : 0),
Ties = this.Ties + (choice is ModelComparisonModelChoice.TIE ? 1 : 0),
};
}