#!/usr/bin/env python3
"""Reproduce the arithmetic in Model Fieldnotes' Jev / Strands comparison.

Python 3, standard library only. No network, model inference or file writes.
All workload assumptions are illustrative, not benchmark observations.
Jev's advertised input price was checked on 2026-10-05: https://typesafe.ai/
Confidence definitions:
  https://docs.typesafe.ai/confidence
  https://github.com/strands-labs/strands-decider/blob/3e94e9d84c620ed5a95f1a3310c3decb971e261c/src/strands_decider/schema.py
These independent calculations apply those definitions to invented distributions.
Article: https://modelfieldnotes.com/developments/jev-structured-decisions/
"""

from decimal import Decimal as D
from math import isclose, sqrt
import json

# Choice: concentration above the uniform probability, not measured correctness.
choice = [0.8, 0.1, 0.1]
choice_confidence = (len(choice) * max(choice) - 1) / (len(choice) - 1)

# Score: the two services use different definitions.
score = [0.0, 0.5, 0.5]
modal_level = score.index(max(score))
distance = sum(p * abs(i - modal_level) for i, p in enumerate(score))
uniform_distance = sum(abs(i - (len(score) - 1) / 2) for i in range(len(score))) / len(score)
typesafe_score = max(0, 1 - distance / uniform_distance)
mean = sum(i * p for i, p in enumerate(score))
sigma = sqrt(sum(p * (i - mean) ** 2 for i, p in enumerate(score)))
sigma_max = (len(score) - 1) / 2
strands_unsmoothed = (sigma_max - sigma) / sigma_max
strands_smoothed = min(1, max(0, (sigma_max - sigma) / (sigma_max - sqrt(0.1))))

# Monthly input-fee illustration. Tokens include the entire billed request.
requests = 100_000
tokens_per_request = 1_000
input_price_per_million = D('0.042')
larger_call_cost = D('0.02')
fallback_fraction = D('0.20')
baseline = requests * larger_call_cost
decision = requests * tokens_per_request / D(1_000_000) * input_price_per_million
fallback = requests * fallback_fraction * larger_call_cost
hybrid = decision + fallback
savings = baseline - hybrid
automatic_requests = requests * (1 - fallback_fraction)
additional_errors = D(320)
correction_cost = additional_errors * D(5)

# Fixed compute cost only: no capacity, availability or equal-quality claim.
instance_hourly = D('0.50')
monthly_compute = instance_hourly * 24 * 30
input_fee_per_request = tokens_per_request / D(1_000_000) * input_price_per_million
crossing_requests = monthly_compute / input_fee_per_request

# Fixed illustrative durations, not percentiles or measured model latency.
decision_ms = D(120)
larger_ms = D(800)
average_ms = decision_ms + fallback_fraction * larger_ms
fallback_ms = decision_ms + larger_ms

# One-sided 95% binomial upper bound after zero events in n independent trials:
# (1 - upper)^n = 0.05. Real traffic may violate the assumed sampling model.
n = 1_000
upper = 1 - 0.05 ** (1 / n)

assert isclose(choice_confidence, 0.7)
assert isclose(typesafe_score, 0.25)
assert isclose(strands_unsmoothed, 0.5)
assert round(strands_smoothed, 3) == 0.731
assert decision == D('4.20') and hybrid == D('404.20')
assert savings == D('1595.80') and savings / baseline * 100 == D('79.79')
assert additional_errors / automatic_requests * 100 == D('0.4')
assert correction_cost == D(1600) and hybrid + correction_cost == D('2004.20')
assert monthly_compute == D(360)
assert round(crossing_requests / D(1_000_000), 2) == D('8.57')
assert average_ms == 280 and fallback_ms == 920
assert round(upper * 100, 1) == 0.3

print(json.dumps({
    'evidence': 'Illustrative arithmetic only; no model was run',
    'confidence': {'choice': choice_confidence, 'typesafe_score': typesafe_score,
                   'strands_score_unsmoothed': strands_unsmoothed,
                   'strands_score_smoothing_0_1': strands_smoothed},
    'monthly_usd': {'baseline': baseline, 'decision_input_fee': decision,
                    'fallback': fallback, 'hybrid': hybrid, 'saving': savings,
                    'correction_cost': correction_cost, 'hybrid_with_corrections': hybrid + correction_cost},
    'compute_fee_crossing': {'monthly_compute_usd': monthly_compute,
                             'requests_per_month': crossing_requests},
    'illustrative_latency_ms': {'average': average_ms, 'fallback': fallback_ms},
    'zero_in_1000_one_sided_95_percent_upper_bound': upper,
}, indent=2, default=str))
