use nalgebra::{DMatrix, DVector, RealField, Scalar};
use num_traits::Float;
use rand::prelude::Distribution;
use rand::Rng;
use rand_distr::StandardNormal;
pub fn gen_ar_1<R: Rng + ?Sized, F: RealField + Scalar + Float>(
mut rng: &mut R,
size: usize,
mu: F,
delta: F,
sigma: F,
) -> DVector<F>
where
StandardNormal: Distribution<F>,
{
let mut y = DVector::zeros(size);
let epsilon: F = StandardNormal.sample(&mut rng);
y[0] = mu + delta * F::from(0.0).unwrap() + sigma * epsilon;
for i in 1..size {
let epsilon: F = StandardNormal.sample(&mut rng);
y[i] = mu + delta * y[i - 1] + sigma * epsilon;
}
y
}
fn gen_x<F: RealField + Float>(sz: usize) -> DMatrix<F> {
DMatrix::from_row_slice(
sz,
1,
Vec::from_iter(0..sz)
.into_iter()
.map(|x| F::from(x).unwrap())
.collect::<Vec<F>>()
.as_slice(),
)
}
pub fn gen_affine_data<F: RealField + Scalar + Float>(
sz: usize,
mu: F,
beta: F,
) -> (DMatrix<F>, DVector<F>) {
let x = gen_x(sz);
let y = (&x * beta).add_scalar(mu);
let y = DVector::from_row_slice(y.as_slice());
(x, y)
}
pub fn gen_affine_data_with_whitenoise<R: Rng + ?Sized, F: RealField + Scalar + Float>(
mut rng: &mut R,
sz: usize,
mu: F,
beta: F,
) -> (DMatrix<F>, DVector<F>)
where
StandardNormal: Distribution<F>,
{
let x = gen_x(sz);
let y = x.clone() * beta;
let noise = DVector::from_iterator(sz, StandardNormal.sample_iter(&mut rng).take(sz));
let y = (y + noise).add_scalar(mu);
(x, y)
}