Problem definition¶
The bilevel problem is a single Problem dataclass that carries the
datafit + penalty + design + target. The hyperparameter α is not
stored — that’s what the outer search tunes.
The Datafit and Penalty families are tagged unions of frozen
dataclasses. Algorithms dispatch on them via match statements with
typing.assert_never exhaustiveness; mypy will flag any missing case.
- class sparho.Problem[source]¶
Bases:
objectA bilevel inner problem
argmin_β L(Xβ, y) + R(β; α).The hyperparameter
αis not stored here — it is what the outer search tunes. The problem captures the fixed structure: which loss, which regularizer family, which design matrix, which target vector.- datafit: SquaredLoss | LogisticLoss¶
- penalty: L1 | ElasticNet | WeightedL1 | GroupL1¶
- __init__(datafit, penalty, design, target)¶
- Parameters:
datafit (SquaredLoss | LogisticLoss)
penalty (L1 | ElasticNet | WeightedL1 | GroupL1)
design (ndarray[tuple[Any, ...], dtype[floating]] | csc_matrix | csc_array)
- Return type:
None
Datafits¶
- class sparho.SquaredLoss[source]¶
Bases:
objectL(Xβ, y) = 0.5 · ‖Xβ − y‖².- __init__()¶
- Return type:
None
- class sparho.LogisticLoss[source]¶
Bases:
objectL(Xβ, y) = Σᵢ log(1 + exp(−yᵢ (Xβ)ᵢ))withyᵢ ∈ {−1, +1}.- __init__()¶
- Return type:
None
- sparho.Datafit = SquaredLoss | LogisticLoss¶
Represent a PEP 604 union type
E.g. for int | str
Penalties¶
- class sparho.L1[source]¶
Bases:
objectR(β; α) = α · ‖β‖₁with scalar hyperparameterα > 0.- __init__()¶
- Return type:
None
- class sparho.ElasticNet[source]¶
Bases:
objectR(β; α) = α · (ρ · ‖β‖₁ + (1 − ρ)/2 · ‖β‖²).The mixing weight
ρ ∈ (0, 1]is structural (carried here, not tuned). The hyperparameter optimized bygrad_searchis the scalarα.
- class sparho.WeightedL1[source]¶
Bases:
objectR(β; α) = Σⱼ αⱼ · |βⱼ|with per-feature hyperparameter vectorα.- __init__()¶
- Return type:
None
- sparho.Penalty = L1 | ElasticNet | WeightedL1¶
Represent a PEP 604 union type
E.g. for int | str