kriging-rs 0.4.0

Geostatistical kriging library with WASM support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
use crate::Real;
use crate::distance::GeoCoord;
use crate::error::KrigingError;
use crate::geo_dataset::GeoDataset;
use crate::kriging::ordinary::OrdinaryKrigingModel;
use crate::utils::{Probability, logistic, logit};
use crate::variogram::models::VariogramModel;
use serde::Serialize;
use std::ops::Deref;

// Module overview (contract)
// ---------------------------------------------------------------------------
// **Prevalence mapping (default):** Empirical-Bayes Beta prior on each observed
// proportion, map to a logit working value, and apply **ordinary kriging with per-site
// logit observation variance** on the covariance diagonal (calibrated to binomial sampling
// on the link scale). The spatial signal is still modeled as a second-order stationary
// **Gaussian** random field on the logit link. The reported prevalence is the logistic of the
// predicted logit; the prevalence–scale variance uses a first-order **delta** approximation.
// This is not full binomial (hierarchical) maximum likelihood geostatistics, but is
// deterministic, defensible for mixed trial counts, and well-suited to interactive / WASM use.
//
// **Pre-computed logit path:** when logits are supplied without per-trial data, the library
// cannot form binomial observation variances; see [`BinomialKrigingModel::from_precomputed_logits`].

/// Contract version for binomial kriging calibration (`BinomialBuildNotes::calibration_version`).
pub const BINOMIAL_CALIBRATION_VERSION: u32 = 1;

/// A single binomial observation at a location: number of successes and trials.
///
/// Use with [`BinomialKrigingModel`] to build a prevalence surface from count data.
#[derive(Debug, Clone, Copy)]
pub struct BinomialObservation {
    coord: GeoCoord,
    successes: u32,
    trials: u32,
}

impl BinomialObservation {
    /// Creates an observation with validated `trials > 0` and `successes <= trials`.
    pub fn new(coord: GeoCoord, successes: u32, trials: u32) -> Result<Self, KrigingError> {
        if trials == 0 {
            return Err(KrigingError::InvalidBinomialData(
                "trials must be greater than 0".to_string(),
            ));
        }
        if successes > trials {
            return Err(KrigingError::InvalidBinomialData(format!(
                "successes ({}) cannot exceed trials ({})",
                successes, trials
            )));
        }
        Ok(Self {
            coord,
            successes,
            trials,
        })
    }

    #[inline]
    pub fn coord(self) -> GeoCoord {
        self.coord
    }

    #[inline]
    pub fn successes(self) -> u32 {
        self.successes
    }

    #[inline]
    pub fn trials(self) -> u32 {
        self.trials
    }

    pub fn smoothed_probability(&self) -> Real {
        self.smoothed_probability_with_prior(BinomialPrior::default())
    }

    pub fn smoothed_probability_with_prior(&self, prior: BinomialPrior) -> Real {
        let s = self.successes as Real;
        let n = self.trials as Real;
        (s + prior.alpha) / (n + prior.alpha + prior.beta)
    }

    pub fn smoothed_logit(&self) -> Real {
        self.smoothed_logit_with_prior(BinomialPrior::default())
    }

    pub fn smoothed_logit_with_prior(&self, prior: BinomialPrior) -> Real {
        let p = self.smoothed_probability_with_prior(prior);
        logit(Probability::from_known_in_range(p))
    }
}

/// Beta(alpha, beta) prior for smoothing binomial observations.
///
/// Used by [`BinomialObservation::smoothed_probability_with_prior`],
/// [`BinomialKrigingModel::new_with_prior`], and observation variance on the logit link.
/// Default is **Beta(1, 1)** (uniform on probability).
#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
pub struct BinomialPrior {
    alpha: Real,
    beta: Real,
}

impl Default for BinomialPrior {
    fn default() -> Self {
        Self {
            alpha: 1.0,
            beta: 1.0,
        }
    }
}

impl BinomialPrior {
    /// Creates a prior with validated `alpha > 0` and `beta > 0`.
    pub fn new(alpha: Real, beta: Real) -> Result<Self, KrigingError> {
        if alpha <= 0.0 || !alpha.is_finite() {
            return Err(KrigingError::InvalidBinomialData(
                "prior alpha must be finite and positive".to_string(),
            ));
        }
        if beta <= 0.0 || !beta.is_finite() {
            return Err(KrigingError::InvalidBinomialData(
                "prior beta must be finite and positive".to_string(),
            ));
        }
        Ok(Self { alpha, beta })
    }

    #[inline]
    pub fn alpha(self) -> Real {
        self.alpha
    }

    #[inline]
    pub fn beta(self) -> Real {
        self.beta
    }
}

/// Logit–scale “observation” variance for a binomial at one location: variance of a Beta
/// `Beta(s+α, f+β)` posterior on `p`, propagated through the delta method for `logit(p)`.
/// Use as extra diagonal noise for ordinary kriging in logit space.
pub fn logit_observation_variance_empirical_bayes(
    prior: BinomialPrior,
    successes: u32,
    trials: u32,
) -> Real {
    if trials == 0 {
        return 0.0;
    }
    let a = prior.alpha;
    let b = prior.beta;
    let s = successes as Real;
    let f = (trials - successes) as Real;
    // Posterior for p under Beta(α,β) prior: Beta(s+α, f+β)
    let ap = s + a;
    let bt = f + b;
    let n = ap + bt;
    // Var(p) for Beta(ap,bt)
    let var_p = (ap * bt) / (n * n * (n + 1.0));
    let p = ap / n;
    let d = p * (1.0 - p);
    if d <= 0.0 || !d.is_finite() {
        0.0
    } else {
        (var_p / (d * d)).max(0.0)
    }
}

/// Tuning and stability for heteroskedastic (calibrated) binomial kriging.
#[derive(Debug, Clone, Copy)]
pub struct HeteroskedasticBinomialConfig {
    /// Floor for each per-site logit observation variance.
    pub min_logit_observation_variance: Real,
    /// If the first ordinary-kriging build fails, multiply all observation variances by
    /// `2^(attempt-1)` for up to this many total attempts. Default: 6 (up to 32×).
    pub max_build_attempts: u32,
}

impl Default for HeteroskedasticBinomialConfig {
    fn default() -> Self {
        Self {
            min_logit_observation_variance: 1e-12,
            max_build_attempts: 6,
        }
    }
}

/// What happened when building a calibrated binomial model (always return from [`BinomialFit`]).
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BinomialBuildNotes {
    /// Contract version; bump when the statistical pipeline meaningfully changes.
    pub calibration_version: u32,
    /// Total multiplier on base logit observation variances: `1` = first try, `2` = after one inflation, etc.
    pub logit_inflation: Real,
    /// Number of factorization attempts (1-based).
    pub n_build_attempts: u32,
    /// Prior used for EB-smoothed logits and observation variances.
    pub prior: BinomialPrior,
    /// Original input indices with `trials == 0` (dropped; no information).
    pub zero_trial_dropped_indices: Vec<usize>,
    /// `true` if the model was built from caller-supplied logits only (no per-site var).
    pub from_precomputed_logits_only: bool,
}

/// Fitted model plus the same auditable build notes for geographic, projected, and
/// space–time binomial families.
#[derive(Debug, Clone)]
pub struct BinomialCalibratedResult<T> {
    /// Fitted model (geographic, projected, or space–time binomial as `T`).
    pub model: T,
    /// Build diagnostics: include in logs, WASM responses, and UIs.
    pub notes: BinomialBuildNotes,
}

impl<T> BinomialCalibratedResult<T> {
    /// Keep only the model (e.g. for internal prediction or legacy call sites).
    pub fn into_model(self) -> T {
        self.model
    }
}

impl<T> Deref for BinomialCalibratedResult<T> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        &self.model
    }
}

/// Geographic **bin**omial kriging: [`BinomialCalibratedResult`] specialized to
/// [`BinomialKrigingModel`].
pub type BinomialFit = BinomialCalibratedResult<BinomialKrigingModel>;

/// Result of a binomial kriging prediction: prevalence (0–1), logit value, logit-space variance,
/// and a delta-method approximation of the variance on the prevalence scale.
#[derive(Debug, Clone, Copy)]
pub struct BinomialPrediction {
    /// Estimated prevalence at the target location (probability in (0, 1)).
    pub prevalence: Real,
    /// Logit of the prevalence (unbounded).
    pub logit_value: Real,
    /// Kriging variance of the logit prediction. This is **not** the variance of `prevalence`;
    /// see [`prevalence_variance`](Self::prevalence_variance) for a delta-method approximation.
    pub variance: Real,
    /// Delta-method approximation of the variance of `prevalence`:
    /// `Var(p) ≈ [p(1 - p)]² · Var(logit)`. Use for approximate CIs on the probability scale.
    pub prevalence_variance: Real,
}

#[inline]
fn delta_prevalence_variance(prevalence: Real, logit_variance: Real) -> Real {
    let factor = prevalence * (1.0 - prevalence);
    factor * factor * logit_variance.max(0.0)
}

/// Fitted binomial kriging model for prevalence surface estimation.
///
/// Build from [`BinomialObservation`]s and a [`VariogramModel`] with
/// [`new`](Self::new) or [`new_with_prior`](Self::new_with_prior) (returning [`BinomialFit`]
/// with build notes), then use [`BinomialKrigingModel::predict`].
#[derive(Debug, Clone)]
pub struct BinomialKrigingModel {
    ordinary_model: OrdinaryKrigingModel,
}

/// Collect indices with `trials == 0` in parallel with `successes` / `trials` slices.
pub fn indices_of_zero_trials(trials: &[u32]) -> Vec<usize> {
    trials
        .iter()
        .enumerate()
        .filter_map(|(i, &t)| if t == 0 { Some(i) } else { None })
        .collect()
}

/// Build valid [`BinomialObservation`]s from parallel slices, **dropping** rows with
/// `trials == 0` and recording their original indices.
pub fn build_binomial_observations_dropping_zero_trials(
    coords: Vec<GeoCoord>,
    successes: &[u32],
    trials: &[u32],
) -> Result<(Vec<BinomialObservation>, Vec<usize>), KrigingError> {
    if coords.len() != successes.len() || successes.len() != trials.len() {
        return Err(KrigingError::DimensionMismatch(format!(
            "coords ({}), successes ({}), trials ({}) must have equal length",
            coords.len(),
            successes.len(),
            trials.len()
        )));
    }
    let mut dropped: Vec<usize> = Vec::new();
    let mut out: Vec<BinomialObservation> = Vec::new();
    for i in 0..coords.len() {
        if trials[i] == 0 {
            dropped.push(i);
            continue;
        }
        if successes[i] > trials[i] {
            return Err(KrigingError::InvalidBinomialData(format!(
                "successes ({}) cannot exceed trials ({}) at index {}",
                successes[i], trials[i], i
            )));
        }
        out.push(BinomialObservation::new(
            coords[i],
            successes[i],
            trials[i],
        )?);
    }
    Ok((out, dropped))
}

impl BinomialKrigingModel {
    /// Build a calibrated (heteroskedastic) model with the default `Beta(1, 1)` prior and
    /// default stability config.
    pub fn new(
        observations: Vec<BinomialObservation>,
        variogram: VariogramModel,
    ) -> Result<BinomialFit, KrigingError> {
        Self::new_with_config(
            observations,
            variogram,
            BinomialPrior::default(),
            HeteroskedasticBinomialConfig::default(),
            &[],
        )
    }

    /// As [`new`](Self::new), with an explicit Beta prior.
    pub fn new_with_prior(
        observations: Vec<BinomialObservation>,
        variogram: VariogramModel,
        prior: BinomialPrior,
    ) -> Result<BinomialFit, KrigingError> {
        Self::new_with_config(
            observations,
            variogram,
            prior,
            HeteroskedasticBinomialConfig::default(),
            &[],
        )
    }

    /// Calibrated build with full control over stability; `zero_trial_drops` is appended to
    /// the returned [`BinomialBuildNotes::zero_trial_dropped_indices`].
    pub fn new_with_config(
        observations: Vec<BinomialObservation>,
        variogram: VariogramModel,
        prior: BinomialPrior,
        config: HeteroskedasticBinomialConfig,
        extra_zero_trial_drops: &[usize],
    ) -> Result<BinomialFit, KrigingError> {
        if observations.len() < 2 {
            return Err(KrigingError::InsufficientData(2));
        }
        let coords: Vec<GeoCoord> = observations.iter().map(|o| o.coord()).collect();
        let logits: Vec<Real> = observations
            .iter()
            .map(|o| o.smoothed_logit_with_prior(prior))
            .collect();
        let base: Vec<Real> = observations
            .iter()
            .map(|o| logit_observation_variance_empirical_bayes(prior, o.successes(), o.trials()))
            .map(|v| v.max(config.min_logit_observation_variance))
            .collect();

        let n_tries = config.max_build_attempts.max(1);
        let mut last_err: Option<KrigingError> = None;
        let mut inflation = 1.0 as Real;
        for attempt in 0..n_tries {
            let extra: Vec<Real> = base
                .iter()
                .map(|&v| (v * inflation).max(config.min_logit_observation_variance))
                .collect();
            let dataset = GeoDataset::new(coords.clone(), logits.clone())?;
            match OrdinaryKrigingModel::new_with_extra_diagonal(dataset, variogram, extra) {
                Ok(ordinary_model) => {
                    let mut z = extra_zero_trial_drops.to_vec();
                    z.sort_unstable();
                    return Ok(BinomialCalibratedResult {
                        model: Self { ordinary_model },
                        notes: BinomialBuildNotes {
                            calibration_version: BINOMIAL_CALIBRATION_VERSION,
                            logit_inflation: inflation,
                            n_build_attempts: attempt + 1,
                            prior,
                            zero_trial_dropped_indices: z,
                            from_precomputed_logits_only: false,
                        },
                    });
                }
                Err(e) => {
                    last_err = Some(e);
                }
            }
            inflation *= 2.0 as Real;
        }
        Err(last_err.unwrap_or_else(|| {
            KrigingError::MatrixError("binomial kriging build failed".to_string())
        }))
    }

    /// Build a binomial kriging model from pre-computed logit values.
    ///
    /// No per-trial information is available: the covariance uses **no** per-site
    /// observation noise beyond nugget, jitter, and the variogram (see notes).
    pub fn from_precomputed_logits(
        coords: Vec<GeoCoord>,
        logits: Vec<Real>,
        variogram: VariogramModel,
    ) -> Result<BinomialFit, KrigingError> {
        if logits.iter().any(|v| !v.is_finite()) {
            return Err(KrigingError::InvalidInput(
                "logits must all be finite (no NaN/inf)".to_string(),
            ));
        }
        let dataset = GeoDataset::new(coords, logits)?;
        let ordinary_model = OrdinaryKrigingModel::new(dataset, variogram)?;
        Ok(BinomialCalibratedResult {
            model: Self { ordinary_model },
            notes: BinomialBuildNotes {
                calibration_version: BINOMIAL_CALIBRATION_VERSION,
                logit_inflation: 1.0,
                n_build_attempts: 1,
                prior: BinomialPrior::default(),
                zero_trial_dropped_indices: Vec::new(),
                from_precomputed_logits_only: true,
            },
        })
    }

    /// Pre-computed logit field with a **per-station** logit observation variance (diagonal) at
    /// every index (e.g. a simulation pool: data sites use [`logit_observation_variance_empirical_bayes`], latent draws use `0`).
    /// Reuses the same factorization / inflation policy as [`Self::new_with_config`].
    pub fn from_precomputed_logits_with_logit_observation_variances(
        coords: Vec<GeoCoord>,
        logits: Vec<Real>,
        variogram: VariogramModel,
        base_logit_observation_variance: Vec<Real>,
        config: HeteroskedasticBinomialConfig,
        prior_for_notes: BinomialPrior,
    ) -> Result<BinomialFit, KrigingError> {
        if logits.len() != coords.len() {
            return Err(KrigingError::DimensionMismatch(format!(
                "coords ({}) and logits ({}) must have equal length",
                coords.len(),
                logits.len()
            )));
        }
        if logits.iter().any(|v| !v.is_finite()) {
            return Err(KrigingError::InvalidInput(
                "logits must all be finite (no NaN/inf)".to_string(),
            ));
        }
        if base_logit_observation_variance.len() != coords.len() {
            return Err(KrigingError::InvalidInput(
                "base logit observation variance must match coords length".to_string(),
            ));
        }
        for &v in &base_logit_observation_variance {
            if !v.is_finite() || v < 0.0 {
                return Err(KrigingError::InvalidInput(
                    "logit observation variances must be finite and non-negative".to_string(),
                ));
            }
        }
        let n_tries = config.max_build_attempts.max(1);
        let mut last_err: Option<KrigingError> = None;
        let mut inflation = 1.0 as Real;
        for attempt in 0..n_tries {
            let extra: Vec<Real> = base_logit_observation_variance
                .iter()
                .map(|&v| (v * inflation).max(config.min_logit_observation_variance))
                .collect();
            let dataset = GeoDataset::new(coords.clone(), logits.clone())?;
            match OrdinaryKrigingModel::new_with_extra_diagonal(dataset, variogram, extra) {
                Ok(ordinary_model) => {
                    return Ok(BinomialCalibratedResult {
                        model: Self { ordinary_model },
                        notes: BinomialBuildNotes {
                            calibration_version: BINOMIAL_CALIBRATION_VERSION,
                            logit_inflation: inflation,
                            n_build_attempts: attempt + 1,
                            prior: prior_for_notes,
                            zero_trial_dropped_indices: Vec::new(),
                            from_precomputed_logits_only: false,
                        },
                    });
                }
                Err(e) => {
                    last_err = Some(e);
                }
            }
            inflation *= 2.0 as Real;
        }
        Err(last_err.unwrap_or_else(|| {
            KrigingError::MatrixError(
                "from_precomputed with observation variances: build failed".to_string(),
            )
        }))
    }

    pub fn predict(&self, coord: GeoCoord) -> Result<BinomialPrediction, KrigingError> {
        let pred = self.ordinary_model.predict(coord)?;
        Ok(to_binomial_prediction(pred))
    }

    pub fn predict_batch(
        &self,
        coords: &[GeoCoord],
    ) -> Result<Vec<BinomialPrediction>, KrigingError> {
        let ordinary = self.ordinary_model.predict_batch(coords)?;
        Ok(ordinary.into_iter().map(to_binomial_prediction).collect())
    }

    #[cfg(feature = "gpu")]
    pub async fn predict_batch_gpu(
        &self,
        coords: &[GeoCoord],
    ) -> Result<Vec<BinomialPrediction>, KrigingError> {
        let ordinary = self.ordinary_model.predict_batch_gpu(coords).await?;
        Ok(ordinary.into_iter().map(to_binomial_prediction).collect())
    }

    #[cfg(feature = "gpu")]
    pub async fn predict_batch_gpu_or_cpu(
        &self,
        coords: &[GeoCoord],
    ) -> Result<Vec<BinomialPrediction>, KrigingError> {
        let ordinary = self.ordinary_model.predict_batch_gpu_or_cpu(coords).await?;
        Ok(ordinary.into_iter().map(to_binomial_prediction).collect())
    }

    #[cfg(all(feature = "gpu-blocking", not(target_arch = "wasm32")))]
    pub fn predict_batch_gpu_blocking(
        &self,
        coords: &[GeoCoord],
    ) -> Result<Vec<BinomialPrediction>, KrigingError> {
        let ordinary = self.ordinary_model.predict_batch_gpu_blocking(coords)?;
        Ok(ordinary.into_iter().map(to_binomial_prediction).collect())
    }

    #[cfg(all(feature = "gpu-blocking", not(target_arch = "wasm32")))]
    pub fn predict_batch_gpu_or_cpu_blocking(
        &self,
        coords: &[GeoCoord],
    ) -> Result<Vec<BinomialPrediction>, KrigingError> {
        let ordinary = self
            .ordinary_model
            .predict_batch_gpu_or_cpu_blocking(coords)?;
        Ok(ordinary.into_iter().map(to_binomial_prediction).collect())
    }
}

fn to_binomial_prediction(pred: crate::kriging::ordinary::Prediction) -> BinomialPrediction {
    let prevalence = logistic(pred.value);
    BinomialPrediction {
        prevalence,
        logit_value: pred.value,
        variance: pred.variance,
        prevalence_variance: delta_prevalence_variance(prevalence, pred.variance),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn default_prior_is_beta_1_1() {
        let p = BinomialPrior::default();
        assert!((p.alpha() - 1.0).abs() < 1e-5 && (p.beta() - 1.0).abs() < 1e-5);
    }

    #[test]
    fn handles_zero_and_all_successes_with_smoothing() {
        let o1 = BinomialObservation::new(GeoCoord::try_new(0.0, 0.0).unwrap(), 0, 10).unwrap();
        let o2 = BinomialObservation::new(GeoCoord::try_new(0.0, 1.0).unwrap(), 10, 10).unwrap();
        let p1 = o1.smoothed_probability();
        let p2 = o2.smoothed_probability();
        assert!(p1 > 0.0 && p1 < 1.0);
        assert!(p2 > 0.0 && p2 < 1.0);
    }

    #[test]
    fn calibrated_uses_extra_diagonal_on_covariance() {
        let p = BinomialPrior::default();
        let v = crate::variogram::models::VariogramModel::new(
            0.04,
            2.0,
            0.15,
            crate::variogram::models::VariogramType::Exponential,
        )
        .unwrap();
        let mut obs: Vec<BinomialObservation> = (0i32..18)
            .map(|i| {
                let lat = 40.0 as Real + (i as Real) * 0.12;
                let lon = -80.0 as Real + (i as Real) * 0.02;
                let t = 5u32 + ((i as u32) % 2) * 200;
                let s = t / 3;
                BinomialObservation::new(GeoCoord::try_new(lat, lon).unwrap(), s, t).expect("o")
            })
            .collect();
        let last = obs.len() - 1;
        let c = obs[last].coord();
        obs[last] = BinomialObservation::new(c, 0, 8).expect("o");
        let coords: Vec<GeoCoord> = obs.iter().map(|o| o.coord()).collect();
        let logits: Vec<Real> = obs.iter().map(|o| o.smoothed_logit_with_prior(p)).collect();
        let fit = super::BinomialKrigingModel::new_with_prior(obs, v, p).expect("fit");
        let fit2 = super::BinomialKrigingModel::from_precomputed_logits(coords, logits, v)
            .expect("logits only");
        let t = GeoCoord::try_new(40.1, -79.9).unwrap();
        let a = fit.model.predict(t).unwrap();
        let b = fit2.model.predict(t).unwrap();
        let d = (a.logit_value - b.logit_value).abs();
        assert!(
            d > 0.01,
            "expected precomputed (no obs var) to differ, got d={d}"
        );
    }

    #[test]
    fn from_precomputed_notes_flag() {
        let c = vec![
            GeoCoord::try_new(0.0, 0.0).unwrap(),
            GeoCoord::try_new(1.0, 0.0).unwrap(),
        ];
        let f = super::BinomialKrigingModel::from_precomputed_logits(c, vec![0.0, 0.0], {
            crate::variogram::models::VariogramModel::new(
                0.05,
                2.0,
                100.0,
                crate::variogram::models::VariogramType::Exponential,
            )
            .unwrap()
        })
        .expect("f");
        assert!(f.notes.from_precomputed_logits_only);
    }
}