MCPcopy Create free account
hub / github.com/Open-Quant/openquant / fit

Method fit

crates/openquant/src/sb_bagging.rs:137–231  ·  view source on GitHub ↗
(
        &mut self,
        x: &DMatrix<f64>,
        y: &[u8],
        ind_mat: &[Vec<u8>],
        sample_weight: Option<&[f64]>,
    )

Source from the content-addressed store, hash-verified

135 }
136
137 pub fn fit(
138 &mut self,
139 x: &DMatrix<f64>,
140 y: &[u8],
141 ind_mat: &[Vec<u8>],
142 sample_weight: Option<&[f64]>,
143 ) -> Result<(), SbBaggingError> {
144 if x.nrows() == 0 || x.ncols() == 0 {
145 return Err(SbBaggingError::EmptyInput);
146 }
147 if y.len() != x.nrows() {
148 return Err(SbBaggingError::DimensionMismatch);
149 }
150 if self.n_estimators == 0 {
151 return Err(SbBaggingError::InvalidEstimators);
152 }
153 if !self.supports_sample_weight && sample_weight.is_some() {
154 return Err(SbBaggingError::SampleWeightNotSupported);
155 }
156 if self.warm_start && self.oob_score {
157 return Err(SbBaggingError::WarmStartWithOob);
158 }
159
160 let max_samples = validate_and_resolve_max_samples(self.max_samples, x.nrows())?;
161 let max_features = validate_and_resolve_max_features(self.max_features, x.ncols())?;
162
163 if !self.warm_start {
164 self.estimators.clear();
165 self.estimators_samples.clear();
166 }
167
168 let n_more = self.n_estimators as isize - self.estimators.len() as isize;
169 if n_more < 0 {
170 return Err(SbBaggingError::DecreasingEstimators);
171 }
172 if n_more == 0 {
173 return Ok(());
174 }
175
176 let mut rng = StdRng::seed_from_u64(self.random_state + self.estimators.len() as u64);
177
178 for _ in 0..(n_more as usize) {
179 let features =
180 sampled_features(&mut rng, x.ncols(), max_features, self.bootstrap_features);
181 let warmup = warmup_indices(
182 &mut rng,
183 ind_mat.first().map(|r| r.len()).unwrap_or(0).max(1),
184 max_samples,
185 );
186 let samples = seq_bootstrap(ind_mat, Some(max_samples), Some(warmup));
187
188 let feature_idx = *features.first().ok_or(SbBaggingError::EmptyInput)?;
189
190 let mut thr = 0.0;
191 for &i in &samples {
192 thr += x[(i, feature_idx)];
193 }
194 thr /= samples.len() as f64;

Callers

nothing calls this directly

Calls 8

sampled_featuresFunction · 0.85
warmup_indicesFunction · 0.85
seq_bootstrapFunction · 0.85
lenMethod · 0.80
clearMethod · 0.80
predictMethod · 0.45

Tested by

no test coverage detected