Files
Anubis/src/Anubis.Server/Components/Pages/Apprendimento.razor
T
Alby96andClaude Opus 5.5 0b0f4a5e75
ci / test (postgres) (push) Successful in 6m37s
ci / test (sqlite) (push) Successful in 7m51s
ci / test (postgres) (pull_request) Successful in 5m40s
ci / test (sqlite) (pull_request) Successful in 7m31s
ci / image (push) Successful in 2m29s
ci / image (pull_request) Successful in 59s
Fix what the independent review of Phase 8 found and close the phase
Two reviewers with a clean context read the engine and the screens. The
isotonic calibration took its floor from the first step, even of two cases, so
with few winners it flattened every estimate: the first model is rebuilt with
the fix. Learning failures no longer forget the multiplier an operation was
born with, nor stop a decision or the nightly check; recovered decisions read
the outside world as it was when due; models estimate only the strategies they
studied; a newer history does not repeat what was lived. The screens count
examples instead of verifications, show the next threshold and never round an
alarm to 50%; two forms now check the antiforgery token.

Each defect has its test, on both engines. ADR 0018 point 17 has the list.

refs #67

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 13:36:26 +01:00

311 lines
15 KiB
Plaintext

@inherits LoadedComponent
@page "/apprendimento"
@inject AnubisHost Host
@inject ShellData Shell
@using Anubis.Learning
<PageTitle>Apprendimento · Anubis</PageTitle>
@{
var v = _view;
var s = v.Summary;
var c = _currency;
}
<p class="lead page-lead">Il modello impara dall'esito di ogni segnale.</p>
@if (Ok == "promozione")
{
<p class="notice notice-gain" role="status"><Icon Name="check" Size="16" />Promozione annullata: è tornata in carica la versione precedente.</p>
}
else if (Errore is not null)
{
<p class="notice notice-loss" role="status"><Icon Name="alert" Size="16" />@(Errore == "motore" ? "Nella modalità dimostrativa non si cambia nulla." : "Non c'è nessuna promozione da annullare.")</p>
}
<section class="panel head-block learning-head" aria-labelledby="stato">
<div class="half learning-state">
<span class="state-badge @(s.State is LearningStates.Aside or LearningStates.Manual ? "warn" : s.State == LearningStates.Active ? "on" : null)"><Icon Name="@BadgeIcon(s.State)" Size="16" />@Badge(s.State)</span>
<h2 id="stato">@v.Title</h2>
<p class="learning-text">@v.Text</p>
@if (s.State != LearningStates.None)
{
<div class="limit learning-progress">
@if (s.Signals < s.Needed)
{
<div class="limit-line"><span>Segnali già valutati</span><span><strong>@s.Signals</strong> <span class="muted">su @s.Needed necessari</span></span></div>
}
else
{
<div class="limit-line"><span>Segnali già valutati</span><span><strong>@Format.Number(s.Signals)</strong> <span class="muted">(ne servivano @s.Needed)</span></span></div>
}
<span class="usage"><span style="width: @Css(Math.Min(100m, s.Signals * 100m / Math.Max(1, s.Needed)))%"></span></span>
@if (v.Pace is { } pace)
{
<span class="note">@pace</span>
}
</div>
}
@if (v.Missing.Count > 0)
{
<ul class="checks">
@foreach (var m in v.Missing)
{
<li class="wait"><Icon Name="clock" Size="16" />@m</li>
}
</ul>
}
@if (s.State == LearningStates.Manual)
{
<p><a href="/impostazioni#modello">Vai a Impostazioni</a></p>
}
</div>
<dl class="half learning-facts">
<div>
<dt>Previsioni azzeccate su casi mai visti</dt>
<dd class="learning-precision">@(s.Precision is { } precision ? precision + "%" : "—")</dd>
<dd class="note">@(s.Precision is null ? "Si misura al primo addestramento" : "Tirando a caso sarebbe il 50%")</dd>
</div>
<div><dt>Ultimo addestramento</dt><dd><strong>@Capital(v.LastTraining)</strong></dd></div>
<div><dt>@s.NextLabel</dt><dd><strong>@Capital(s.NextCheck)</strong></dd></div>
</dl>
</section>
<div class="learning-row">
<section class="panel" aria-labelledby="fiducia">
<h2 id="fiducia">Ci si può fidare delle sue probabilità?</h2>
@if (v.Bands.Count == 0)
{
<p class="note">@v.BandsText Qui vedrai quante volte va bene davvero quando il modello dice che una cosa è probabile.</p>
}
else
{
<p class="note">Più i punti stanno sulla linea, più la stima corrisponde ai fatti.@(v.BandsFromHistory ? " Per ora sono i casi storici che non aveva studiato: quelli dal vivo sono ancora troppo pochi." : "")</p>
<CalibrationChart Bands="v.Bands" Text="@v.BandsText" />
}
</section>
<section class="panel learning-weights" aria-labelledby="indizi">
<h2 id="indizi">Cosa guarda di più</h2>
@if (v.Champion is null)
{
<p class="note">Si vedrà con il primo modello.</p>
}
else
{
<p class="note">Quanto pesa ogni informazione nelle sue stime, su 100.</p>
@if (v.Weights.Count > 0)
{
var most = v.Weights.Max(w => w.Share);
<ul class="weights">
@foreach (var w in v.Weights)
{
<li class="@(w.Rest ? "rest" : null)"><span>@w.Name</span><span class="weight-bar" aria-hidden="true"><span style="width: @Css(w.Share * 100m / most)%"></span></span><strong>@w.Share</strong></li>
}
</ul>
}
<p class="note weights-text">@v.WeightsText</p>
}
</section>
</div>
<div class="learning-row learning-row-wide">
<section class="panel" aria-labelledby="versioni">
<h2 id="versioni">Il modello in carica e lo sfidante</h2>
<p class="note">Lo sfidante gira in ombra accanto al modello in carica. Ne prende il posto solo se è chiaramente migliore.</p>
@if (v.Champion is { } now)
{
var other = v.Challenger;
var same = other is null || other.GainWithout == now.GainWithout;
<div class="table-wrap">
<table class="table versions">
<thead><tr><td></td><th scope="col" class="num">In carica, versione @now.Version</th>@if (other is not null){<th scope="col" class="num">Sfidante, versione @other.Version</th>}</tr></thead>
<tbody>
<tr><th scope="row">Addestrato</th><td class="num">@now.Trained</td>@if (other is not null){<td class="num">@other.Trained</td>}</tr>
<tr><th scope="row">Casi studiati</th><td class="num">@Format.Number(now.Examples)</td>@if (other is not null){<td class="num">@Format.Number(other.Examples)</td>}</tr>
<tr><th scope="row">Previsioni azzeccate su casi mai visti</th><td class="num"><strong>@Share(now.Precision)</strong></td>@if (other is not null){<td class="num"><strong>@Share(other.Precision)</strong></td>}</tr>
<tr><th scope="row">Errore sulle probabilità <span class="muted">(più basso è meglio)</span></th><td class="num">@Error(now.Error)</td>@if (other is not null){<td class="num">@Error(other.Error)</td>}</tr>
<tr><th scope="row">Guadagno medio per operazione se filtrasse lui @if (same && now.GainWithout is { } without){<span class="muted">(senza filtro: @Shell.Money(without, c, withSign: true))</span>}</th>
<td class="num">@Gain(now, same, c)</td>@if (other is not null){<td class="num">@Gain(other, same, c)</td>}</tr>
@if (other is not null)
{
<tr><th scope="row">Casi di confronto diretto</th><td class="num muted">non serve</td><td class="num">@Format.Number(v.Pairs) su @v.PairsNeeded</td></tr>
}
</tbody>
</table>
</div>
}
@if (v.GainOnReference)
{
<p class="note">Nessuna strategia con capitale affidato ha ancora casi: il guadagno è calcolato su tutte, con il capitale di riferimento.</p>
}
<p class="aside"><strong>@v.TrialTitle</strong> @v.TrialText</p>
@if (v.Choice is { } choice)
{
<details class="chart-table">
<summary>Come è stato scelto il modello in carica</summary>
<p class="note">@choice Sono tutte prove su casi che il modello non aveva studiato; contano anche quelle scartate.</p>
<div class="table-wrap">
<table class="table">
<thead><tr><th scope="col">Tipo di modello</th><th scope="col" class="num">Previsioni azzeccate</th><th scope="col" class="num">Errore</th><th scope="col" class="num">Guadagno per operazione</th><th scope="col">Esito</th></tr></thead>
<tbody>
@foreach (var a in v.Attempts)
{
<tr><td>@a.Learner</td><td class="num">@Share(a.Precision)</td><td class="num">@Error(a.Error)</td><td class="num nowrap">@Shell.Money(a.Gain, c, withSign: true)</td><td>@(a.Chosen ? "scelto" : "non scelto")</td></tr>
}
</tbody>
</table>
</div>
</details>
}
@if (v.UndoTo is { } back && !Shell.IsDemo)
{
<form method="post" action="/azioni/modello/annulla-promozione" class="form-inline">
<AntiforgeryToken />
<button class="btn" type="submit" data-confirm="Torna in carica la versione @back e la versione @v.Champion!.Version viene tolta. Continuare?">Annulla l'ultima promozione</button>
<span class="hint">Tornerebbe in carica la versione @back.</span>
</form>
}
</section>
<section class="panel learning-watch" aria-labelledby="sorveglianza">
<div class="panel-head">
<h2 id="sorveglianza">Sorveglianza</h2>
@if (v.Checked)
{
<span class="status-pill @(v.Anomaly ? "attention" : "gain")"><Icon Name="@(v.Anomaly ? "alert" : "check")" Size="16" />@(v.Anomaly ? "Anomalia" : "Nessuna anomalia")</span>
}
</div>
@if (!v.Checked)
{
<p class="note">Il controllo gira ogni notte alle 03:00: confronta le stime con gli esiti e il mercato di oggi con quello su cui ha studiato.</p>
}
else
{
<ul class="watch-list">
@foreach (var check in v.Checks)
{
<li class="@check.State"><Icon Name="@(check.State switch { "ok" => "tick", "warn" => "alert", _ => "clock" })" Size="16" /><span>@check.Text</span></li>
}
</ul>
<WeeklyPrecision Weeks="v.Weeks" />
}
<p class="aside">Se la precisione cala, il modello viene messo da parte da solo e le strategie proseguono senza di lui.</p>
</section>
</div>
<div class="learning-row learning-open">
<section class="plain" aria-labelledby="imparato">
<h2 id="imparato">Cosa ha imparato finora</h2>
@if (v.Found.Count == 0)
{
<p class="note">Ancora niente: le relazioni tra indizi ed esiti si cercano ogni notte, quando i casi bastano per un confronto.</p>
}
else
{
<ul class="found">
@foreach (var d in v.Found)
{
<li>
<strong>@d.Title</strong>
<span class="found-detail">@d.Detail</span>
<span class="@(d.Cases < 20 ? "attention" : "note")">@if (d.Cases < 20){<Icon Name="alert" Size="16" />}@(d.Cases == 1 ? "Un caso" : Format.Number(d.Cases) + " casi"): @d.Trust</span>
</li>
}
</ul>
}
@if (v.News.Count > 0)
{
<h3>Cosa succede ai prezzi dopo una notizia</h3>
<p class="note">Rendimento medio dalla prima chiusura dopo la notizia. È solo un'informazione: non cambia nessuna regola.</p>
<div class="table-wrap">
<table class="table">
<thead><tr><th scope="col">Tipo di notizia</th><th scope="col" class="num">Dopo 1 giorno</th><th scope="col" class="num">Dopo 5 giorni</th><th scope="col">Casi</th></tr></thead>
<tbody>
@foreach (var n in v.News)
{
<tr><td>@NewsEventNames.Label(n.Kind)</td><td class="num">@Move(n.OneDay)</td><td class="num">@Move(n.FiveDays)</td><td class="@(n.Cases < 20 ? "attention-text" : null)">@n.Cases: @n.Trust</td></tr>
}
</tbody>
</table>
</div>
}
</section>
<section class="plain" aria-labelledby="storia">
<h2 id="storia">Storia</h2>
@if (v.Story.Count == 0)
{
<p class="note">La storia comincia con il primo modello.</p>
}
else
{
<dl class="events">
@foreach (var e in v.Story.Take(LearningView.StoryRows))
{
<dt>@e.When</dt><dd>@e.Text</dd>
}
</dl>
@if (v.Story.Count > LearningView.StoryRows)
{
<details class="chart-table">
<summary>Mostra i @(v.Story.Count - LearningView.StoryRows) eventi precedenti</summary>
<dl class="events">
@foreach (var e in v.Story.Skip(LearningView.StoryRows))
{
<dt>@e.When</dt><dd>@e.Text</dd>
}
</dl>
</details>
}
}
</section>
</div>
@code {
private LearningView _view = null!;
private string _currency = "EUR";
[CascadingParameter]
public HttpContext? HttpContext { get; set; }
[SupplyParameterFromQuery(Name = "ok")]
public string? Ok { get; set; }
[SupplyParameterFromQuery(Name = "errore")]
public string? Errore { get; set; }
protected override async Task LoadAsync()
{
var ct = HttpContext?.RequestAborted ?? CancellationToken.None;
_currency = Shell.Effective((await Host.Services!.Settings.GetAsync(SettingsStore.Currency.Key, ct)).Value);
_view = await LearningView.LoadAsync(Host, Shell, ct);
}
private string Gain(LearningVersion version, bool same, string currency) => version.Gain is not { } gain ? "—"
: Shell.Money(gain, currency, withSign: true) + (same || version.GainWithout is not { } without ? "" : $" (senza: {Shell.Money(without, currency, withSign: true)})");
private static string Badge(string state) => state switch
{
LearningStates.None => "Non ancora partito",
LearningStates.Active => "Attivo",
LearningStates.Aside => "Messo da parte",
LearningStates.Manual => "Messo da parte da te",
_ => "In osservazione",
};
private static string BadgeIcon(string state) => state switch
{
LearningStates.None => "clock",
LearningStates.Active => "check",
LearningStates.Aside => "alert",
LearningStates.Manual => "pause",
_ => "eye",
};
private static string Share(int? points) => points is { } p ? p + "%" : "—";
private static string Error(decimal? value) => value is { } e ? Format.Number(e, 3) : "—";
private static string Move(double value) => Format.Percent((decimal)Math.Round(value * 100, 2), 2, withSign: true);
private static string Capital(string text) => text.Length == 0 ? text : char.ToUpper(text[0], CultureInfo.GetCultureInfo("it-IT")) + text[1..];
private static string Css(decimal v) => v.ToString("0.##", CultureInfo.InvariantCulture);
}