use super::rng::{seeded_rng, unit_normal};
pub fn linspace(lo: f64, hi: f64, n: usize) -> Vec<f64> {
if n == 1 {
return vec![lo];
}
let denom = (n - 1) as f64;
(0..n)
.map(|i| lo + (hi - lo) * (i as f64) / denom)
.collect()
}
pub fn forrester(x: f64) -> f64 {
let t = 6.0 * x - 2.0;
t * t * (12.0 * x - 4.0).sin()
}
pub fn weighted_sphere(x0: f64, x1: f64) -> f64 {
let a = x0 / 0.25;
a * a + x1 * x1
}
pub fn forrester_xy(n: usize, seed: u64, noise_std: f64) -> (Vec<f64>, Vec<f64>) {
let x = linspace(0.0, 1.0, n);
let mut rng = seeded_rng(seed);
let y = x
.iter()
.map(|&xi| forrester(xi) + noise_std * unit_normal(&mut rng))
.collect();
(x, y)
}
pub fn sphere_xy(side: usize, seed: u64, noise_std: f64) -> (Vec<f64>, Vec<f64>) {
let n = side * side;
let mut x = vec![0.0; n * 2];
let denom = (side - 1) as f64;
for row in 0..n {
x[row] = (row % side) as f64 / denom;
x[n + row] = (row / side) as f64 / denom;
}
let mut rng = seeded_rng(seed);
let y = (0..n)
.map(|row| weighted_sphere(x[row], x[n + row]) + noise_std * unit_normal(&mut rng))
.collect();
(x, y)
}
pub struct DistanceBaseline {
pub n: usize,
pub d: usize,
pub q: usize,
pub m: usize,
pub x: Vec<f64>,
pub y: Vec<f64>,
pub xq: Vec<f64>,
pub z: Vec<f64>,
pub x_new: Vec<f64>,
pub y_new: f64,
}
pub fn distance_baseline() -> DistanceBaseline {
let (n, d, q, m) = (512, 4, 100, 64);
let mut rng = seeded_rng(0);
let mut points = |rows: usize| -> Vec<f64> { (0..rows * d).map(|_| rng.unit()).collect() };
let x = points(n + 1);
let xq = points(q);
let target = |x: &[f64], rows: usize, i: usize| -> f64 {
(0..d)
.map(|k| (2.0 * std::f64::consts::PI * x[i + k * rows]).sin())
.sum()
};
let mut noise = seeded_rng(1);
let y_all: Vec<f64> = (0..=n)
.map(|i| target(&x, n + 1, i) + 0.1 * unit_normal(&mut noise))
.collect();
let rows_of = |x: &[f64], rows: usize, range: std::ops::Range<usize>| -> Vec<f64> {
(0..d)
.flat_map(|k| range.clone().map(move |i| x[i + k * rows]))
.collect()
};
DistanceBaseline {
n,
d,
q,
m,
x: rows_of(&x, n + 1, 0..n),
y: y_all[..n].to_vec(),
xq,
z: rows_of(&x, n + 1, 0..m),
x_new: rows_of(&x, n + 1, n..n + 1),
y_new: y_all[n],
}
}
pub struct Supplied {
pub train: Vec<Vec<f64>>,
pub cross: Vec<Vec<f64>>,
pub new: Vec<Vec<f64>>,
pub sparse_cross: Vec<Vec<f64>>,
pub train_sum: Vec<f64>,
pub cross_sum: Vec<f64>,
pub new_sum: Vec<f64>,
pub sparse_cross_sum: Vec<f64>,
}
impl DistanceBaseline {
pub fn supplied(&self) -> Supplied {
let block = |a: &[f64], ra: usize, b: &[f64], rb: usize, k: usize| -> Vec<f64> {
(0..rb)
.flat_map(|j| (0..ra).map(move |i| (a[i + k * ra] - b[j + k * rb]).powi(2)))
.collect()
};
let train: Vec<Vec<f64>> = (0..self.d)
.map(|k| block(&self.x, self.n, &self.x, self.n, k))
.collect();
let cross: Vec<Vec<f64>> = (0..self.d)
.map(|k| block(&self.x, self.n, &self.xq, self.q, k))
.collect();
let new: Vec<Vec<f64>> = (0..self.d)
.map(|k| block(&self.x, self.n, &self.x_new, 1, k))
.collect();
let sparse_cross: Vec<Vec<f64>> = (0..self.d)
.map(|k| block(&self.z, self.m, &self.xq, self.q, k))
.collect();
let sum = |blocks: &[Vec<f64>]| -> Vec<f64> {
(0..blocks[0].len())
.map(|at| blocks.iter().map(|b| b[at]).sum())
.collect()
};
Supplied {
train_sum: sum(&train),
cross_sum: sum(&cross),
new_sum: sum(&new),
sparse_cross_sum: sum(&sparse_cross),
train,
cross,
new,
sparse_cross,
}
}
}