fdars-core 0.13.0

Functional Data Analysis algorithms in Rust
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
use super::nonparametric::{
    compute_pairwise_distances, compute_scalar_distances, gaussian_kernel, select_bandwidth_loo,
};
use super::{FregreBasisCvResult, FregreNpCvResult};
use crate::error::FdarError;
use crate::matrix::FdMatrix;

// ---------------------------------------------------------------------------
// Basis regression CV (R's fregre.basis.cv)
// ---------------------------------------------------------------------------

/// Compute penalized OLS coefficients: (X'X + lam*P) \ X'y.
fn compute_penalized_ols(
    train_coefs: &FdMatrix,
    train_y: &[f64],
    penalty: &[f64],
    lam: f64,
    k: usize,
) -> Vec<f64> {
    let n_train = train_y.len();
    let mut xtx = vec![0.0; k * k];
    let mut xty_vec = vec![0.0; k];
    for i in 0..n_train {
        for j in 0..k {
            xty_vec[j] += train_coefs[(i, j)] * train_y[i];
            for l in 0..k {
                xtx[j * k + l] += train_coefs[(i, j)] * train_coefs[(i, l)];
            }
        }
    }
    for j in 0..k {
        for l in 0..k {
            xtx[j * k + l] += lam * penalty[j * k + l];
        }
    }
    crate::smoothing::solve_gaussian_pub(&mut xtx, &mut xty_vec, k)
}

/// Evaluate test-set MSE given OLS coefficients and test data.
fn evaluate_fold_error(beta: &[f64], test_coefs: &FdMatrix, test_y: &[f64], k: usize) -> f64 {
    let n_test = test_y.len();
    let mut mse = 0.0;
    for i in 0..n_test {
        let mut yhat = 0.0;
        for j in 0..k {
            yhat += test_coefs[(i, j)] * beta[j];
        }
        mse += (test_y[i] - yhat).powi(2);
    }
    mse / n_test as f64
}

/// Compute CV mean, SE, and best index from per-fold error vectors.
fn compute_cv_statistics(
    cv_fold_errors: &[Vec<f64>],
) -> (Vec<f64>, Vec<f64>, Option<(usize, f64)>) {
    let n_params = cv_fold_errors.len();
    let mut cv_errors = vec![0.0; n_params];
    let mut cv_se = vec![0.0; n_params];
    for li in 0..n_params {
        let errs = &cv_fold_errors[li];
        if errs.is_empty() {
            cv_errors[li] = f64::INFINITY;
            continue;
        }
        let mean = errs.iter().sum::<f64>() / errs.len() as f64;
        cv_errors[li] = mean;
        if errs.len() > 1 {
            let var =
                errs.iter().map(|&e| (e - mean).powi(2)).sum::<f64>() / (errs.len() - 1) as f64;
            cv_se[li] = (var / errs.len() as f64).sqrt();
        }
    }
    let best = cv_errors
        .iter()
        .enumerate()
        .min_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
        .map(|(idx, &val)| (idx, val));
    (cv_errors, cv_se, best)
}

/// Validate inputs for [`fregre_basis_cv`] and resolve the lambda grid.
///
/// Returns `(n, m, lambdas)` on success.
fn validate_basis_cv_inputs(
    data: &FdMatrix,
    y: &[f64],
    argvals: &[f64],
    n_folds: usize,
    lambda_range: Option<&[f64]>,
    nbasis: usize,
) -> Result<(usize, usize, Vec<f64>), FdarError> {
    let (n, m) = data.shape();
    if n < n_folds {
        return Err(FdarError::InvalidDimension {
            parameter: "data",
            expected: format!("at least {n_folds} rows (n_folds)"),
            actual: format!("{n}"),
        });
    }
    if m == 0 || y.len() != n || argvals.len() != m {
        return Err(FdarError::InvalidDimension {
            parameter: "data/y/argvals",
            expected: format!("m > 0, y.len() == n={n}, argvals.len() == m={m}"),
            actual: format!(
                "m={}, y.len()={}, argvals.len()={}",
                m,
                y.len(),
                argvals.len()
            ),
        });
    }
    if nbasis < 2 {
        return Err(FdarError::InvalidParameter {
            parameter: "nbasis",
            message: format!("must be >= 2, got {nbasis}"),
        });
    }

    let lambdas: Vec<f64> = if let Some(lr) = lambda_range {
        if lr.is_empty() {
            return Err(FdarError::InvalidParameter {
                parameter: "lambda_range",
                message: "must not be empty".to_string(),
            });
        }
        lr.to_vec()
    } else {
        (0..20)
            .map(|i| {
                let log_lam = -4.0 + 8.0 * f64::from(i) / 19.0;
                10.0_f64.powf(log_lam)
            })
            .collect()
    };

    Ok((n, m, lambdas))
}

/// K-fold CV for selecting the regularization parameter lambda
/// in basis-regression (R's `fregre.basis.cv`).
#[must_use = "expensive computation whose result should not be discarded"]
pub fn fregre_basis_cv(
    data: &FdMatrix,
    y: &[f64],
    argvals: &[f64],
    n_folds: usize,
    lambda_range: Option<&[f64]>,
    nbasis: usize,
    basis_type: &crate::smooth_basis::BasisType,
) -> Result<FregreBasisCvResult, FdarError> {
    use crate::smooth_basis::{smooth_basis, BasisType, FdPar};

    let (n, _m, lambdas) =
        validate_basis_cv_inputs(data, y, argvals, n_folds, lambda_range, nbasis)?;

    let penalty = match basis_type {
        BasisType::Bspline { order } => {
            crate::smooth_basis::bspline_penalty_matrix(argvals, nbasis, *order, 2)
        }
        BasisType::Fourier { period } => {
            crate::smooth_basis::fourier_penalty_matrix(nbasis, *period, 2)
        }
    };

    let folds = crate::cv::create_folds(n, n_folds, 42);
    let mut cv_fold_errors: Vec<Vec<f64>> = vec![Vec::with_capacity(n_folds); lambdas.len()];

    for fold in 0..n_folds {
        let (train_idx, test_idx) = crate::cv::fold_indices(&folds, fold);
        if train_idx.is_empty() || test_idx.is_empty() {
            continue;
        }

        let train_data = crate::cv::subset_rows(data, &train_idx);
        let train_y = crate::cv::subset_vec(y, &train_idx);
        let test_data = crate::cv::subset_rows(data, &test_idx);
        let test_y = crate::cv::subset_vec(y, &test_idx);

        for (li, &lam) in lambdas.iter().enumerate() {
            let fdpar = FdPar {
                basis_type: basis_type.clone(),
                nbasis,
                lambda: lam,
                lfd_order: 2,
                penalty_matrix: penalty.clone(),
            };

            let Ok(train_result) = smooth_basis(&train_data, argvals, &fdpar) else {
                cv_fold_errors[li].push(f64::INFINITY);
                continue;
            };
            let Ok(test_result) = smooth_basis(&test_data, argvals, &fdpar) else {
                cv_fold_errors[li].push(f64::INFINITY);
                continue;
            };

            let k = train_result.coefficients.ncols();
            let beta =
                compute_penalized_ols(&train_result.coefficients, &train_y, &penalty, lam, k);
            let fold_mse = evaluate_fold_error(&beta, &test_result.coefficients, &test_y, k);
            cv_fold_errors[li].push(fold_mse);
        }
    }

    let (cv_errors, cv_se, best) = compute_cv_statistics(&cv_fold_errors);
    let (best_idx, min_cv_error) = best.ok_or_else(|| FdarError::ComputationFailed {
        operation: "fregre_basis_cv",
        detail: "no valid lambda values produced CV errors; try widening the lambda range or check data quality".to_string(),
    })?;

    Ok(FregreBasisCvResult {
        optimal_lambda: lambdas[best_idx],
        cv_errors,
        cv_se,
        lambda_values: lambdas,
        min_cv_error,
    })
}

// ---------------------------------------------------------------------------
// Nonparametric regression bandwidth CV (R's fregre.np.cv)
// ---------------------------------------------------------------------------

/// Build default bandwidth grid from 20 quantiles of pairwise distances.
fn select_default_bandwidth_grid(func_dists: &[f64], n: usize) -> Result<Vec<f64>, FdarError> {
    let mut nonzero: Vec<f64> = Vec::new();
    for i in 0..n {
        for j in (i + 1)..n {
            let d = func_dists[i * n + j];
            if d > 0.0 {
                nonzero.push(d);
            }
        }
    }
    crate::helpers::sort_nan_safe(&mut nonzero);
    if nonzero.is_empty() {
        return Err(FdarError::ComputationFailed {
            operation: "select_default_bandwidth_grid",
            detail: "all pairwise distances are zero; curves may be identical — check input data"
                .to_string(),
        });
    }
    Ok((1..=20)
        .map(|qi| {
            let q = 0.05 + 0.90 * f64::from(qi - 1) / 19.0;
            let idx = ((nonzero.len() as f64 * q) as usize).min(nonzero.len() - 1);
            nonzero[idx].max(1e-10)
        })
        .collect())
}

/// Nadaraya-Watson prediction for a single test observation.
fn compute_nw_prediction(
    train_idx: &[usize],
    ti: usize,
    func_dists: &[f64],
    scalar_dists: &[f64],
    train_y: &[f64],
    n: usize,
    h: f64,
    h_scalar: f64,
    has_scalar: bool,
    fallback_y: f64,
) -> f64 {
    let mut num = 0.0;
    let mut den = 0.0;
    for (local_j, &tj) in train_idx.iter().enumerate() {
        let kf = gaussian_kernel(func_dists[ti * n + tj], h);
        let ks = if has_scalar {
            gaussian_kernel(scalar_dists[ti * n + tj], h_scalar)
        } else {
            1.0
        };
        let w = kf * ks;
        num += w * train_y[local_j];
        den += w;
    }
    if den > 1e-15 {
        num / den
    } else {
        fallback_y
    }
}

/// Validate inputs for [`fregre_np_cv`] and resolve the bandwidth grid.
///
/// Returns `(n, func_dists, scalar_dists, has_scalar, h_values)` on success.
fn validate_np_cv_inputs(
    data: &FdMatrix,
    y: &[f64],
    argvals: &[f64],
    n_folds: usize,
    h_range: Option<&[f64]>,
    scalar_covariates: Option<&FdMatrix>,
) -> Result<(usize, Vec<f64>, Vec<f64>, bool, Vec<f64>), FdarError> {
    let (n, m) = data.shape();
    if n < n_folds || n < 3 {
        return Err(FdarError::InvalidDimension {
            parameter: "data",
            expected: format!("at least {} rows (max(n_folds, 3))", n_folds.max(3)),
            actual: format!("{n}"),
        });
    }
    if m == 0 || y.len() != n || argvals.len() != m {
        return Err(FdarError::InvalidDimension {
            parameter: "data/y/argvals",
            expected: format!("m > 0, y.len() == n={n}, argvals.len() == m={m}"),
            actual: format!(
                "m={}, y.len()={}, argvals.len()={}",
                m,
                y.len(),
                argvals.len()
            ),
        });
    }

    let func_dists = compute_pairwise_distances(data, argvals);
    let has_scalar = scalar_covariates.is_some();
    let scalar_dists = scalar_covariates
        .map(compute_scalar_distances)
        .unwrap_or_default();

    let h_values: Vec<f64> = if let Some(hr) = h_range {
        if hr.is_empty() {
            return Err(FdarError::InvalidParameter {
                parameter: "h_range",
                message: "must not be empty".to_string(),
            });
        }
        hr.to_vec()
    } else {
        select_default_bandwidth_grid(&func_dists, n)?
    };

    Ok((n, func_dists, scalar_dists, has_scalar, h_values))
}

/// K-fold CV for selecting the bandwidth in functional nonparametric
/// regression (R's `fregre.np.cv`).
#[must_use = "expensive computation whose result should not be discarded"]
pub fn fregre_np_cv(
    data: &FdMatrix,
    y: &[f64],
    argvals: &[f64],
    n_folds: usize,
    h_range: Option<&[f64]>,
    scalar_covariates: Option<&FdMatrix>,
) -> Result<FregreNpCvResult, FdarError> {
    let (n, func_dists, scalar_dists, has_scalar, h_values) =
        validate_np_cv_inputs(data, y, argvals, n_folds, h_range, scalar_covariates)?;

    let folds = crate::cv::create_folds(n, n_folds, 42);
    let mut cv_fold_errors: Vec<Vec<f64>> = vec![Vec::with_capacity(n_folds); h_values.len()];

    let h_scalar = if has_scalar {
        select_bandwidth_loo(&scalar_dists, y, n, Some(&func_dists))
    } else {
        1.0
    };

    for fold in 0..n_folds {
        let (train_idx, test_idx) = crate::cv::fold_indices(&folds, fold);
        if train_idx.is_empty() || test_idx.is_empty() {
            continue;
        }
        let train_y: Vec<f64> = train_idx.iter().map(|&i| y[i]).collect();

        for (hi, &h) in h_values.iter().enumerate() {
            let mut fold_mse = 0.0;
            for &ti in &test_idx {
                let y_hat = compute_nw_prediction(
                    &train_idx,
                    ti,
                    &func_dists,
                    &scalar_dists,
                    &train_y,
                    n,
                    h,
                    h_scalar,
                    has_scalar,
                    y[ti],
                );
                fold_mse += (y[ti] - y_hat).powi(2);
            }
            fold_mse /= test_idx.len() as f64;
            cv_fold_errors[hi].push(fold_mse);
        }
    }

    let (cv_errors, cv_se, best) = compute_cv_statistics(&cv_fold_errors);
    let (best_idx, min_cv_error) = best.ok_or_else(|| FdarError::ComputationFailed {
        operation: "fregre_np_cv",
        detail:
            "no valid bandwidth values produced CV errors; try providing a wider bandwidth grid"
                .to_string(),
    })?;

    Ok(FregreNpCvResult {
        optimal_h: h_values[best_idx],
        cv_errors,
        cv_se,
        h_values,
        min_cv_error,
    })
}