Skip to main content

prospicio_models/
model.rs

1//! The interface every model implements, whatever its engine.
2
3use prospicio_core::Result;
4use prospicio_prob::PredictiveDistribution;
5
6use crate::design::Design;
7
8/// A model specification that can be fitted: a GLM, a GAM, a network.
9///
10/// `fit` takes the design (with offset and weights) and the response, and
11/// returns the fitted model. Specs are cheap values; fitting does the work.
12pub trait Model {
13    type Fitted: Fitted;
14
15    /// Fits the model to `design` and response `y`.
16    fn fit(&self, design: &Design, y: &[f64]) -> Result<Self::Fitted>;
17}
18
19/// A fitted model: predictions, joint predictive distributions, scores.
20pub trait Fitted {
21    /// Expected response for each row of `design`.
22    fn predict(&self, design: &Design) -> Result<Vec<f64>>;
23
24    /// Joint predictive distribution of the response across the rows of
25    /// `design`: `n_sims` simulations × rows, each simulation drawn from
26    /// stream `sim` of `seed`, so results do not depend on thread count.
27    /// Draws carry parameter and process uncertainty, and are joint because
28    /// anything summed later (cells into reserves, policies into a
29    /// portfolio) needs the joint draws, not per-row marginals.
30    fn predict_distribution(
31        &self,
32        design: &Design,
33        n_sims: usize,
34        seed: u64,
35    ) -> Result<PredictiveDistribution>;
36}