# Binomial

`Binomial` defaults to the logit link and the averaged Bernoulli negative log likelihood: 

$$

  F(y,\eta)
  = \frac1n\sum_{i=1}^n \left[ \log(1 + e^{\eta_i}) - y_i\eta_i \right], \qquad p_i
            = \frac{1}{1 + e^{-\eta_i}}.

$$

The predictor derivatives are 

$$

  \frac{\partial F}{\partial\eta_i}
  = \frac{p_i - y_i}{n}, \qquad \frac{\partial^2 F}{\partial\eta_i^2}
                                    = \frac{p_i(1 - p_i)}{n} \leq \frac{1}{4n}.

$$

The implementation evaluates the loss and derivatives stably for large logits. Responses must be finite and lie in $[0,1]$. Fractional values represent proportions with equal observation weights; this API does not accept trial counts or arbitrary observation weights. The intercept is fitted by default. 

<a id="evaluate-the-loss"></a>

## Evaluate the loss

```rust
use shrinkage::{Binomial, Datafit};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let y = [0.0, 1.0];
    let datafit = Binomial::new(&y);
    assert!((datafit.value(&[0.0, 0.0])? - 2.0_f64.ln()).abs() < 1e-14);
    Ok(())
}
```

Fitted predictions are **logits**. Apply the logistic function to obtain probabilities. See [logistic regression](<../models/logistic>) for a stable conversion and a complete fit. 

[Proximal gradient](<../reference/solvers/proximal-gradient>) supports the built-in proximal penalties. [IRLS](<../reference/solvers/irls>) supports L1, ridge, and elastic net. The [hybrid solver](<../reference/solvers/hybrid>) supports clustered lasso, chain fused lasso, and SLOPE. Unpenalized separated data may have no finite minimizer; inspect termination and choose a regularized model when appropriate. 

Use `.with_link(Probit)` or a custom Rust link for IRLS fitting and paths. Predictions then use the selected predictor scale; call the datafit's `.inverse_link(eta)` to obtain probabilities. 

API: [`shrinkage::Binomial`](<https://shrinkage.cc/packages/rust/reference/cabade7e6bb5cb30e1afe92b7012a4d0f67c7bec0235c94ef5bca1dda3c635f8>).
