use ndarray::prelude::*;
use crate::{
datasets::{Dataset, GaussSample, GaussTable},
models::Labelled,
types::{Labels, Set},
};
pub type GaussWtdSample = (GaussSample, f64);
#[derive(Clone, Debug)]
pub struct GaussWtdTable {
dataset: GaussTable,
weights: Array1<f64>,
}
impl Labelled for GaussWtdTable {
#[inline]
fn labels(&self) -> &Labels {
self.dataset.labels()
}
}
impl GaussWtdTable {
pub fn new(dataset: GaussTable, weights: Array1<f64>) -> Self {
assert_eq!(
dataset.values().nrows(),
weights.len(),
"The number of weights must be equal to the number of samples."
);
assert!(
weights.iter().all(|&w| (0.0..=1.0).contains(&w)),
"All weights must be in the range [0, 1]."
);
Self { dataset, weights }
}
#[inline]
pub const fn weights(&self) -> &Array1<f64> {
&self.weights
}
}
impl Dataset for GaussWtdTable {
type Values = GaussTable;
#[inline]
fn values(&self) -> &Self::Values {
&self.dataset
}
#[inline]
fn sample_size(&self) -> f64 {
self.weights.sum()
}
fn select(&self, x: &Set<usize>) -> Self {
let dataset = self.dataset.select(x);
let weights = self.weights.clone();
Self::new(dataset, weights)
}
}