File size: 1,977 Bytes
5786fb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""Finite-candidate admission only. No automatic claim of a global neural controller."""
from dataclasses import dataclass
import numpy as np
from .joint import witness_scores, witness_radius

@dataclass
class Candidate:
    name: str
    K: np.ndarray
    predictions: np.ndarray
    incremental_price: float
    feasible: bool = True

def select_candidate(residuals, baseline_K, baseline_predictions, candidates,
                     R_bound, M_bound, delta=.05, eta=1.):
    """Requires candidates fixed before an independent bounded evaluation batch.
    Prices must already include every incremental resource and opportunity cost.
    Returns a conservative candidate or None, plus the complete score audit.
    """
    residuals=np.asarray(residuals)
    all_predictions=[baseline_predictions]+[c.predictions for c in candidates]
    if np.max(np.linalg.norm(residuals,axis=1))>R_bound+1e-12:
        raise ValueError('residual bound violated; no bounded-data certificate')
    if any(np.max(np.linalg.norm(p,axis=1))>M_bound+1e-12 for p in all_predictions):
        raise ValueError('prediction bound violated')
    eps=witness_radius(len(residuals),len(candidates)+1,R_bound,M_bound,delta)
    base=float(witness_scores(residuals,baseline_predictions,baseline_K,eta).mean())
    audit=[];best=None;best_lcb=0.
    for candidate in candidates:
        score=float(witness_scores(residuals,candidate.predictions,candidate.K,eta).mean())
        lcb=score-base-candidate.incremental_price-2*eps
        audit.append(dict(name=candidate.name,empirical_gain=score-base,
                          price=candidate.incremental_price,net_lower_bound=lcb,
                          feasible=candidate.feasible))
        if candidate.feasible and lcb>best_lcb:
            best,best_lcb=candidate.name,lcb
    return best,dict(baseline=base,per_candidate_radius=eps,candidates=audit,
                    assumptions='independent bounded samples; designs fixed before evaluation')