// Énumération des intervalles de prix compatibles avec la participation.
record AkerlofEquilibre(string Regime, string[] Participants, double? PriceLower, double? PriceUpper, bool? UpperClosed, double? ConditionalValue);
record ScenarioAkerlof(string Scenario, double Pi, double CLow, double CHigh, double VLow, double VHigh);
record LigneAkerlof(string Scenario, double Pi, AkerlofEquilibre Equilibre);
List<AkerlofEquilibre> AkerlofEquilibria(double pi, double cLow, double cHigh, double vLow, double vHigh)
{
if (!(0 <= pi && pi <= 1)) throw new ArgumentOutOfRangeException(nameof(pi));
if (!(cLow < cHigh)) throw new ArgumentException("Le modèle suppose c_low < c_high");
if (!(vLow < vHigh)) throw new ArgumentException("Le modèle suppose v_low < v_high");
var resultat = new List<AkerlofEquilibre>();
double lemonsUpper = Math.Min(cHigh, vLow);
if (cLow <= lemonsUpper)
resultat.Add(new AkerlofEquilibre("lemons_only", new[] { "L" }, cLow, lemonsUpper, vLow < cHigh, vLow));
double valeurPool = pi * vHigh + (1.0 - pi) * vLow;
if (cHigh <= valeurPool)
resultat.Add(new AkerlofEquilibre("pooling", new[] { "L", "H" }, cHigh, valeurPool, true, valeurPool));
if (resultat.Count == 0)
resultat.Add(new AkerlofEquilibre("no_trade", Array.Empty<string>(), null, null, null, null));
return resultat;
}
// Les quatre scénarios et la grille sont ceux du carnet Python.
var scenarios = new[] {
new ScenarioAkerlof("faible part de H", 0.20, 1, 5, 3, 9),
new ScenarioAkerlof("seuil exact", 1.0 / 3.0, 1, 5, 3, 9),
new ScenarioAkerlof("forte part de H", 0.50, 1, 5, 3, 9),
new ScenarioAkerlof("aucun échange", 0.50, 4, 7, 3, 6)
};
var akerlof_results = scenarios.SelectMany(s => AkerlofEquilibria(s.Pi, s.CLow, s.CHigh, s.VLow, s.VHigh).Select(e => new LigneAkerlof(s.Scenario, s.Pi, e))).ToArray();
var pi_values = Enumerable.Range(0, 201).Select(i => i / 200.0).ToArray();
var pooling_values = pi_values.Select(p => p * 9 + (1 - p) * 3).ToArray();
double pi_min = (5.0 - 3.0) / (9.0 - 3.0);
var regime_map = pi_values.Select(p => {
var eq = AkerlofEquilibria(p, 1, 5, 3, 9);
return (Pi: p, Lemons: eq.Any(e => e.Regime == "lemons_only"), Pooling: eq.Any(e => e.Regime == "pooling"));
}).ToArray();
// Contrôles des valeurs et des régimes, y compris les bornes ouvertes.
var attendusRegimes = new[] { "lemons_only", "lemons_only", "pooling", "lemons_only", "pooling", "no_trade" };
var attendusBas = new double?[] { 1, 1, 5, 1, 5, null };
var attendusHaut = new double?[] { 3, 3, 5, 3, 6, null };
var attendusValeur = new double?[] { 3, 3, 5, 3, 6, null };
Verifier("Nombre de lignes Akerlof", akerlof_results.Length == attendusRegimes.Length);
for (int i = 0; i < akerlof_results.Length; i++)
{
var e = akerlof_results[i].Equilibre;
Verifier("Régime Akerlof", e.Regime == attendusRegimes[i]);
Verifier("Participants Akerlof", string.Join(",", e.Participants) == (e.Regime == "pooling" ? "L,H" : e.Regime == "lemons_only" ? "L" : ""));
Verifier("Présence des bornes", e.PriceLower.HasValue == attendusBas[i].HasValue && e.PriceUpper.HasValue == attendusHaut[i].HasValue && e.ConditionalValue.HasValue == attendusValeur[i].HasValue);
if (attendusBas[i].HasValue)
{
VerifierProche("Akerlof", e.PriceLower!.Value, attendusBas[i]!.Value);
VerifierProche("Akerlof", e.PriceUpper!.Value, attendusHaut[i]!.Value);
VerifierProche("Akerlof", e.ConditionalValue!.Value, attendusValeur[i]!.Value);
Verifier("Borne fermée", e.UpperClosed == true);
}
else Verifier("Absence de borne fermée", e.UpperClosed == null);
}
VerifierProche("Akerlof", pi_min, 1.0 / 3.0);
Verifier("Carte des régimes", regime_map.All(r => r.Lemons && r.Pooling == (r.Pi * 9 + (1 - r.Pi) * 3 >= 5)));
// Tableau utilisé pour la lecture et les exercices.
Console.WriteLine($"Seuil analytique : pi_min = {pi_min:F3}");
Console.WriteLine("scenario | pi | regime | participants | price_lower | price_upper | upper_closed | conditional_value");
foreach (var row in akerlof_results)
{
var e = row.Equilibre;
string participants = e.Participants.Length == 0 ? "()" : e.Participants.Length == 1 ? "(L,)" : "(L, H)";
string FormatNullable(double? v) => v.HasValue ? v.Value.ToString("F1", CultureInfo.InvariantCulture) : "NaN";
Console.WriteLine($"{row.Scenario} | {row.Pi:F6} | {e.Regime} | {participants} | {FormatNullable(e.PriceLower)} | {FormatNullable(e.PriceUpper)} | {(e.UpperClosed.HasValue ? (e.UpperClosed.Value ? "True" : "False") : "NaN")} | {FormatNullable(e.ConditionalValue)}");
}
// Deux panneaux SVG calculés depuis les mêmes tableaux que les résultats imprimés.
var svg = new StringBuilder();
svg.Append("<svg xmlns='http://www.w3.org/2000/svg' width='1200' height='400' viewBox='0 0 1200 400'><rect width='1200' height='400' fill='white'/><g font-family='sans-serif' font-size='13' fill='black'>");
svg.Append("<text x='150' y='24'>Seuil de possibilité du pooling</text><text x='740' y='24'>Carte de multiplicité du point fixe</text>");
svg.Append("<path d='M60 340 H550 M60 340 V45 M650 340 H1140 M650 340 V45' stroke='black' fill='none'/>");
string Points(IEnumerable<(double x, double y)> points) => string.Join(" ", points.Select(p => $"{Nombre(p.x)},{Nombre(p.y)}"));
double X1(double p) => 60 + 490 * p;
double Y1(double v) => 340 - (v - 2) * 40;
double X2(double p) => 650 + 490 * p;
double Y2(double n) => 340 - n * 130;
svg.Append($"<polygon points='{Points(new[] { (X1(0), Y1(3)), (X1(pi_min), Y1(3)), (X1(pi_min), Y1(5)), (X1(0), Y1(5)) })}' fill='orange' opacity='.16'/>");
svg.Append($"<polygon points='{Points(new[] { (X1(pi_min), Y1(5)), (X1(1), Y1(5)), (X1(1), Y1(9)) })}' fill='green' opacity='.18'/>");
svg.Append($"<polyline points='{Points(pi_values.Select((p, i) => (X1(p), Y1(pooling_values[i]))))}' fill='none' stroke='steelblue' stroke-width='2'/>");
svg.Append($"<line x1='{Nombre(X1(0))}' y1='{Nombre(Y1(5))}' x2='{Nombre(X1(1))}' y2='{Nombre(Y1(5))}' stroke='red' stroke-dasharray='6,4'/>");
svg.Append($"<line x1='{Nombre(X1(pi_min))}' y1='45' x2='{Nombre(X1(pi_min))}' y2='340' stroke='green' stroke-dasharray='2,4'/>");
svg.Append($"<polygon points='{Points(new[] { (X2(0), Y2(0)), (X2(1), Y2(0)), (X2(1), Y2(1)), (X2(0), Y2(1)) })}' fill='steelblue' opacity='.65'/>");
var poolPoints = new List<(double x, double y)> { (X2(pi_min), Y2(1)), (X2(1), Y2(1)), (X2(1), Y2(2)), (X2(pi_min), Y2(2)) };
svg.Append($"<polygon points='{Points(poolPoints)}' fill='orange' opacity='.65'/><line x1='{Nombre(X2(pi_min))}' y1='45' x2='{Nombre(X2(pi_min))}' y2='340' stroke='green' stroke-dasharray='2,4'/><circle cx='{Nombre(X2(pi_min))}' cy='{Nombre(Y2(2))}' r='4' fill='black'/>");
svg.Append("<text x='180' y='380'>Probabilité du type H</text><text x='770' y='380'>Probabilité du type H</text><text x='65' y='60'>Valeur / prix</text><text x='655' y='60'>Nombre de régimes compatibles</text><text x='65' y='355'>0</text><text x='535' y='355'>1</text><text x='655' y='355'>0</text><text x='1125' y='355'>1</text><text x='35' y='224'>5</text><text x='630' y='214'>1</text><text x='630' y='84'>2</text><text x='75' y='85' fill='steelblue'>Valeur moyenne du pool</text><text x='75' y='105' fill='red'>c_H = 5</text><text x='75' y='125' fill='green'>pi_min = 0.333</text><text x='665' y='85' fill='steelblue'>Lemons-only</text><text x='665' y='105' fill='darkorange'>Pooling</text><text x='665' y='125'>Point noir : coexistence au seuil</text></g></svg>");
// Une chaîne n'a pas de formateur SVG natif : enregistrer explicitement son rendu.
Microsoft.DotNet.Interactive.Formatting.Formatter.Register<string>((value, writer) => writer.Write(value), "image/svg+xml");
display(svg.ToString(), "image/svg+xml");