infomeasure 0.3.0-beta.1

Information theory measures and entropy calculations for 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
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
// SPDX-FileCopyrightText: 2025-2026 Carlson Büth <code@cbueth.de>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Utilities for slicing and embedding time series data for Transfer Entropy (TE) and
//! Conditional Transfer Entropy (CTE) estimators.
//!
//! This module provides functions to transform raw time series into the embedding formats
//! required by various estimators. It handles:
//! - Time-delay embeddings with configurable history lengths and step sizes.
//! - Slicing data into future, history, and (optionally) conditioning components.
//! - Random permutation of source history for significance testing and local TE/CTE calculations.
//! - Support for both scalar (`Array1`) and multivariate (`Array2`) time series.
//!
//! The core embedding logic follows the "oldest first" column ordering convention, where
//! column 0 contains the most distant past sample and the last column contains the most
//! recent past sample.

use ndarray::{Array1, Array2};
use rand::seq::SliceRandom;
use rand::thread_rng;

/// Build the embedding arrays used by transfer-entropy estimators (const generic version).
///
/// This is the multivariate version of [`te_slices`], using const generics for
/// efficiency and working with [`Array2`] inputs.
///
/// # Returns
/// `(dest_future, dest_history, src_history)`:
/// - `dest_future`: shape $N \times D_{\mathrm{target}}$
/// - `dest_history`: shape $N \times (k \cdot D_{\mathrm{target}})$
/// - `src_history`: shape $N \times (l \cdot D_{\mathrm{source}})$
pub fn te_observations_const<
    T: Clone + Default,
    const SRC_HIST: usize,
    const DEST_HIST: usize,
    const STEP_SIZE: usize,
    const D_SOURCE: usize,
    const D_TARGET: usize,
    const D_JOINT: usize,
    const D_XP_YP: usize,
    const D_YP: usize,
    const D_YF_YP: usize,
>(
    source: &Array2<T>,
    destination: &Array2<T>,
    permute_src: bool,
) -> (Array2<T>, Array2<T>, Array2<T>) {
    let max_delay = SRC_HIST.max(DEST_HIST) * STEP_SIZE;
    let n = destination.nrows();

    if max_delay >= n {
        return (
            Array2::default((0, D_TARGET)),
            Array2::default((0, DEST_HIST * D_TARGET)),
            Array2::default((0, SRC_HIST * D_SOURCE)),
        );
    }

    let base_indices: Vec<usize> = (max_delay..n).step_by(STEP_SIZE).collect();
    let n_samples = base_indices.len();

    let mut dest_future = Array2::default((n_samples, D_TARGET));
    let mut dest_history = Array2::default((n_samples, DEST_HIST * D_TARGET));
    let mut src_history = Array2::default((n_samples, SRC_HIST * D_SOURCE));

    for (idx, &base_idx) in base_indices.iter().enumerate() {
        for d in 0..D_TARGET {
            dest_future[(idx, d)] = destination[(base_idx, d)].clone();
        }

        for j in 0..DEST_HIST {
            let offset = (j + 1) * STEP_SIZE;
            for d in 0..D_TARGET {
                dest_history[(idx, (DEST_HIST - 1 - j) * D_TARGET + d)] =
                    destination[(base_idx - offset, d)].clone();
            }
        }

        for j in 0..SRC_HIST {
            let offset = (j + 1) * STEP_SIZE;
            for d in 0..D_SOURCE {
                src_history[(idx, (SRC_HIST - 1 - j) * D_SOURCE + d)] =
                    source[(base_idx - offset, d)].clone();
            }
        }
    }

    if permute_src {
        let mut rng = thread_rng();
        let mut indices: Vec<usize> = (0..n_samples).collect();
        indices.shuffle(&mut rng);

        let mut permuted_src_history = Array2::default((n_samples, SRC_HIST * D_SOURCE));
        for (i, &new_idx) in indices.iter().enumerate() {
            for j in 0..SRC_HIST * D_SOURCE {
                permuted_src_history[(i, j)] = src_history[(new_idx, j)].clone();
            }
        }
        src_history = permuted_src_history;
    }

    (dest_future, dest_history, src_history)
}

/// Build the embedding arrays used by conditional transfer-entropy estimators (const generic version).
///
/// This is the multivariate version of [`cte_slices`], using const generics for
/// efficiency and working with [`Array2`] inputs.
///
/// # Returns
/// `(dest_future, dest_history, src_history, cond_history)`:
/// - `dest_future`: shape $N \times D_{\mathrm{target}}$
/// - `dest_history`: shape $N \times (k \cdot D_{\mathrm{target}})$
/// - `src_history`: shape $N \times (l \cdot D_{\mathrm{source}})$
/// - `cond_history`: shape $N \times (m \cdot D_{\mathrm{cond}})$
pub fn cte_observations_const<
    T: Clone + Default,
    const SRC_HIST: usize,
    const DEST_HIST: usize,
    const COND_HIST: usize,
    const STEP_SIZE: usize,
    const D_SOURCE: usize,
    const D_TARGET: usize,
    const D_COND: usize,
    const D_JOINT: usize,
    const D_XP_YP_ZP: usize,
    const D_YP_ZP: usize,
    const D_YF_YP_ZP: usize,
>(
    source: &Array2<T>,
    destination: &Array2<T>,
    condition: &Array2<T>,
    permute_src: bool,
) -> (Array2<T>, Array2<T>, Array2<T>, Array2<T>) {
    let max_delay = SRC_HIST.max(DEST_HIST).max(COND_HIST) * STEP_SIZE;
    let n = destination.nrows();

    if max_delay >= n {
        return (
            Array2::default((0, D_TARGET)),
            Array2::default((0, DEST_HIST * D_TARGET)),
            Array2::default((0, SRC_HIST * D_SOURCE)),
            Array2::default((0, COND_HIST * D_COND)),
        );
    }

    let base_indices: Vec<usize> = (max_delay..n).step_by(STEP_SIZE).collect();
    let n_samples = base_indices.len();

    let mut dest_future = Array2::default((n_samples, D_TARGET));
    let mut dest_history = Array2::default((n_samples, DEST_HIST * D_TARGET));
    let mut src_history = Array2::default((n_samples, SRC_HIST * D_SOURCE));
    let mut cond_history = Array2::default((n_samples, COND_HIST * D_COND));

    for (idx, &base_idx) in base_indices.iter().enumerate() {
        for d in 0..D_TARGET {
            dest_future[(idx, d)] = destination[(base_idx, d)].clone();
        }

        for j in 0..DEST_HIST {
            let offset = (j + 1) * STEP_SIZE;
            for d in 0..D_TARGET {
                dest_history[(idx, (DEST_HIST - 1 - j) * D_TARGET + d)] =
                    destination[(base_idx - offset, d)].clone();
            }
        }

        for j in 0..SRC_HIST {
            let offset = (j + 1) * STEP_SIZE;
            for d in 0..D_SOURCE {
                src_history[(idx, (SRC_HIST - 1 - j) * D_SOURCE + d)] =
                    source[(base_idx - offset, d)].clone();
            }
        }

        for j in 0..COND_HIST {
            let offset = (j + 1) * STEP_SIZE;
            for d in 0..D_COND {
                cond_history[(idx, (COND_HIST - 1 - j) * D_COND + d)] =
                    condition[(base_idx - offset, d)].clone();
            }
        }
    }

    if permute_src {
        let mut rng = thread_rng();
        let mut indices: Vec<usize> = (0..n_samples).collect();
        indices.shuffle(&mut rng);

        let mut permuted_src_history = Array2::default((n_samples, SRC_HIST * D_SOURCE));
        for (i, &new_idx) in indices.iter().enumerate() {
            for j in 0..SRC_HIST * D_SOURCE {
                permuted_src_history[(i, j)] = src_history[(new_idx, j)].clone();
            }
        }
        src_history = permuted_src_history;
    }

    (dest_future, dest_history, src_history, cond_history)
}

/// Build the embedding arrays used by transfer-entropy estimators.
///
/// Given a source process $X$ and destination process $Y$, this returns the
/// triple $(Y_t,\\ \mathbf{y}_{t-1}^{(k,\tau)},\\ \mathbf{x}_{t-1}^{(l,\tau)})$
/// for every valid time index $t$, where $\tau$ is the embedding delay
/// (`step_size`), $k$ = `dest_hist_len`, and $l$ = `src_hist_len`.
///
/// Only time indices for which all required past samples exist are kept.
/// The retained base indices are `t = max_delay, max_delay + τ, max_delay + 2τ, …`
/// where `max_delay = max(k, l) · τ`, yielding
/// $N = \lceil (n - \max(k, l)\,\tau) / \tau \rceil$ rows (with $n$ the input length).
///
/// Returns `(dest_future, dest_history, src_history)`:
/// - `dest_future`: $Y_t$, shape $N \times 1$
/// - `dest_history`: the embedding vector of $Y$ ending one step before $t$,
///   with $k$ samples spaced by $\tau$:
///   $$\mathbf{y}_{t-1}^{(k,\tau)} = \bigl(Y_{t-k\tau},\ \ldots,\ Y_{t-2\tau},\ Y_{t-\tau}\bigr)$$
///   shape $N \times k$. Column ordering is **oldest first**: column 0 holds
///   $Y_{t-k\tau}$ and column $k-1$ holds the most recent past sample $Y_{t-\tau}$.
/// - `src_history`: analogously
///   $\mathbf{x}_{t-1}^{(l,\tau)} = (X_{t-l\tau}, \ldots, X_{t-2\tau}, X_{t-\tau})$,
///   shape $N \times l$, same column ordering.
///
/// Note that the embedding excludes the sample at time $t$ itself; $Y_t$ is
/// returned separately as `dest_future`. This matches the standard TE
/// convention where the source and destination histories are strictly past
/// observations used to predict the next destination value.
///
/// For `step_size = 1` and writing $n+1$ for the future index, this is the
/// usual embedding $\mathbf{y}_n^{(k)} = \{y_n, y_{n-1}, \ldots, y_{n-k+1}\}$
/// from the [Transfer Entropy Guide](crate::guide::transfer_entropy)
/// (modulo column order).
///
/// # Example
/// With `dest_hist_len = 3`, `step_size = 2`, one row of `dest_history`
/// (with `t` the corresponding base index) contains
/// $(Y_{t-6},\ Y_{t-4},\ Y_{t-2})$ in columns `[0, 1, 2]`, and the matching
/// `dest_future` entry is $Y_t$.
pub fn te_slices<T: Clone + Default>(
    source: &Array1<T>,
    destination: &Array1<T>,
    src_hist_len: usize,
    dest_hist_len: usize,
    step_size: usize,
) -> (Array2<T>, Array2<T>, Array2<T>) {
    te_observations(
        source,
        destination,
        src_hist_len,
        dest_hist_len,
        step_size,
        false,
    )
}

/// Build the embedding arrays used by transfer-entropy estimators and optionally permute the source.
///
/// Given a source process $X$ and destination process $Y$, this returns the
/// triple $(Y_t,\ \mathbf{y}_{t-1}^{(k,\tau)},\ \mathbf{x}_{t-1}^{(l,\tau)})$
/// for every valid time index $t$, where $\tau$ is the embedding delay
/// (`step_size`), $k$ = `dest_hist_len`, and $l$ = `src_hist_len`.
///
/// Only time indices for which all required past samples exist are kept.
/// The retained base indices are `t = max_delay, max_delay + τ, max_delay + 2τ, …`
/// where `max_delay = max(k, l) · τ`, yielding
/// $N = \lceil (n - \max(k, l)\,\tau) / \tau \rceil$ rows (with $n$ the input length).
///
/// Returns `(dest_future, dest_history, src_history)`:
/// - `dest_future`: $Y_t$, shape $N \times 1$
/// - `dest_history`: the embedding vector of $Y$ ending one step before $t$,
///   with $k$ samples spaced by $\tau$:
///   $$\mathbf{y}_{t-1}^{(k,\tau)} = \bigl(Y_{t-k\tau},\ \ldots,\ Y_{t-2\tau},\ Y_{t-\tau}\bigr)$$
///   shape $N \times k$. Column ordering is **oldest first**: column 0 holds
///   $Y_{t-k\tau}$ and column $k-1$ holds the most recent past sample $Y_{t-\tau}$.
/// - `src_history`: analogously
///   $\mathbf{x}_{t-1}^{(l,\tau)} = (X_{t-l\tau}, \ldots, X_{t-2\tau}, X_{t-\tau})$,
///   shape $N \times l$, same column ordering.
///   If `permute_src` is true, the rows of `src_history` are randomly shuffled.
///
/// Note that the embedding excludes the sample at time $t$ itself; $Y_t$ is
/// returned separately as `dest_future`. This matches the standard TE
/// convention where the source and destination histories are strictly past
/// observations used to predict the next destination value.
///
/// For `step_size = 1` and writing $n+1$ for the future index, this is the
/// usual embedding $\mathbf{y}_n^{(k)} = \{y_n, y_{n-1}, \ldots, y_{n-k+1}\}$
/// from the [Transfer Entropy Guide](crate::guide::transfer_entropy)
/// (modulo column order).
///
/// # Example
/// With `dest_hist_len = 3`, `step_size = 2`, one row of `dest_history`
/// (with `t` the corresponding base index) contains
/// $(Y_{t-6},\ Y_{t-4},\ Y_{t-2})$ in columns `[0, 1, 2]`, and the matching
/// `dest_future` entry is $Y_t$.
pub fn te_observations<T: Clone + Default>(
    source: &Array1<T>,
    destination: &Array1<T>,
    src_hist_len: usize,
    dest_hist_len: usize,
    step_size: usize,
    permute_src: bool,
) -> (Array2<T>, Array2<T>, Array2<T>) {
    let max_delay = src_hist_len.max(dest_hist_len) * step_size;
    let n = destination.len();

    if max_delay >= n {
        return (
            Array2::default((0, 1)),
            Array2::default((0, dest_hist_len)),
            Array2::default((0, src_hist_len)),
        );
    }

    let base_indices: Vec<usize> = (max_delay..n).step_by(step_size).collect();
    let n_samples = base_indices.len();

    let mut dest_future = Array2::default((n_samples, 1));
    let mut dest_history = Array2::default((n_samples, dest_hist_len));
    let mut src_history = Array2::default((n_samples, src_hist_len));

    for (idx, &base_idx) in base_indices.iter().enumerate() {
        dest_future[(idx, 0)] = destination[base_idx].clone();

        for j in 0..dest_hist_len {
            let offset = (j + 1) * step_size;
            dest_history[(idx, dest_hist_len - 1 - j)] = destination[base_idx - offset].clone();
        }

        for j in 0..src_hist_len {
            let offset = (j + 1) * step_size;
            src_history[(idx, src_hist_len - 1 - j)] = source[base_idx - offset].clone();
        }
    }

    if permute_src {
        let mut rng = thread_rng();
        let mut indices: Vec<usize> = (0..n_samples).collect();
        indices.shuffle(&mut rng);

        let mut permuted_src_history = Array2::default((n_samples, src_hist_len));
        for (i, &new_idx) in indices.iter().enumerate() {
            for j in 0..src_hist_len {
                permuted_src_history[(i, j)] = src_history[(new_idx, j)].clone();
            }
        }
        src_history = permuted_src_history;
    }

    (dest_future, dest_history, src_history)
}

/// Build the embedding arrays used by conditional transfer-entropy estimators.
///
/// Given a source process $X$, destination process $Y$, and conditioning
/// process $Z$, this returns the tuple
/// $(Y_t,\ \mathbf{y}_{t-1}^{(k,\tau)},\ \mathbf{x}_{t-1}^{(l,\tau)},\ \mathbf{z}_{t-1}^{(m,\tau)})$
/// for every valid time index $t$, where $\tau$ is the embedding delay
/// (`step_size`), $k$ = `dest_hist_len`, $l$ = `src_hist_len`, and
/// $m$ = `cond_hist_len`.
///
/// Only time indices for which all required past samples exist are kept.
/// The retained base indices are `t = max_delay, max_delay + τ, max_delay + 2τ, …`
/// where `max_delay = max(k, l, m) · τ`, yielding
/// $N = \lceil (n - \max(k, l, m)\,\tau) / \tau \rceil$ rows (with $n$ the input length).
///
/// Returns `(dest_future, dest_history, src_history, cond_history)`:
/// - `dest_future`: $Y_t$, shape $N \times 1$.
/// - `dest_history`: embedding of $Y$ ending one step before $t$:
///   $$\mathbf{y}_{t-1}^{(k,\tau)} = \bigl(Y_{t-k\tau},\ \ldots,\ Y_{t-2\tau},\ Y_{t-\tau}\bigr)$$
///   shape $N \times k$. Column ordering is **oldest first** (column 0 is
///   $Y_{t-k\tau}$, column $k-1$ is the most recent past sample $Y_{t-\tau}$).
/// - `src_history`: analogously
///   $\mathbf{x}_{t-1}^{(l,\tau)} = (X_{t-l\tau}, \ldots, X_{t-2\tau}, X_{t-\tau})$,
///   shape $N \times l$.
/// - `cond_history`: analogously
///   $\mathbf{z}_{t-1}^{(m,\tau)} = (Z_{t-m\tau}, \ldots, Z_{t-2\tau}, Z_{t-\tau})$,
///   shape $N \times m$.
///
/// All three input series are assumed to have the same length; out-of-bounds
/// indices would panic. The number of valid samples $N$ is governed by the
/// longest of the three embeddings.
///
/// For `step_size = 1` this matches the standard contiguous embeddings used
/// in the [Conditional Transfer Entropy Guide](crate::guide::cond_te),
/// modulo column order.
///
/// # Example
/// With `dest_hist_len = 3`, `cond_hist_len = 2`, `step_size = 2`, one row of
/// `dest_history` contains $(Y_{t-6},\ Y_{t-4},\ Y_{t-2})$ and the matching
/// row of `cond_history` contains $(Z_{t-4},\ Z_{t-2})$, while `dest_future`
/// holds $Y_t$.
pub fn cte_slices<T: Clone + Default>(
    source: &Array1<T>,
    destination: &Array1<T>,
    condition: &Array1<T>,
    src_hist_len: usize,
    dest_hist_len: usize,
    cond_hist_len: usize,
    step_size: usize,
) -> (Array2<T>, Array2<T>, Array2<T>, Array2<T>) {
    cte_observations(
        source,
        destination,
        condition,
        src_hist_len,
        dest_hist_len,
        cond_hist_len,
        step_size,
        false,
    )
}

/// Build the embedding arrays used by conditional transfer-entropy estimators and optionally permute the source.
///
/// Given a source process $X$, destination process $Y$, and conditioning
/// process $Z$, this returns the tuple
/// $(Y_t,\ \mathbf{y}_{t-1}^{(k,\tau)},\ \mathbf{x}_{t-1}^{(l,\tau)},\ \mathbf{z}_{t-1}^{(m,\tau)})$
/// for every valid time index $t$, where $\tau$ is the embedding delay
/// (`step_size`), $k$ = `dest_hist_len`, $l$ = `src_hist_len`, and
/// $m$ = `cond_hist_len`.
///
/// Only time indices for which all required past samples exist are kept.
/// The retained base indices are `t = max_delay, max_delay + τ, max_delay + 2τ, …`
/// where `max_delay = max(k, l, m) · τ`, yielding
/// $N = \lceil (n - \max(k, l, m)\,\tau) / \tau \rceil$ rows (with $n$ the input length).
///
/// Returns `(dest_future, dest_history, src_history, cond_history)`:
/// - `dest_future`: $Y_t$, shape $N \times 1$.
/// - `dest_history`: embedding of $Y$ ending one step before $t$:
///   $$\mathbf{y}_{t-1}^{(k,\tau)} = \bigl(Y_{t-k\tau},\ \ldots,\ Y_{t-2\tau},\ Y_{t-\tau}\bigr)$$
///   shape $N \times k$. Column ordering is **oldest first** (column 0 is
///   $Y_{t-k\tau}$, column $k-1$ is the most recent past sample $Y_{t-\tau}$).
/// - `src_history`: analogously
///   $\mathbf{x}_{t-1}^{(l,\tau)} = (X_{t-l\tau}, \ldots, X_{t-2\tau}, X_{t-\tau})$,
///   shape $N \times l$.
///   If `permute_src` is true, the rows of `src_history` are randomly shuffled.
/// - `cond_history`: analogously
///   $\mathbf{z}_{t-1}^{(m,\tau)} = (Z_{t-m\tau}, \ldots, Z_{t-2\tau}, Z_{t-\tau})$,
///   shape $N \times m$.
///
/// All three input series are assumed to have the same length; out-of-bounds
/// indices would panic. The number of valid samples $N$ is governed by the
/// longest of the three embeddings.
///
/// For `step_size = 1` this matches the standard contiguous embeddings used
/// in the [Conditional Transfer Entropy Guide](crate::guide::cond_te),
/// modulo column order.
///
/// # Example
/// With `dest_hist_len = 3`, `cond_hist_len = 2`, `step_size = 2`, one row of
/// `dest_history` contains $(Y_{t-6},\ Y_{t-4},\ Y_{t-2})$ and the matching
/// row of `cond_history` contains $(Z_{t-4},\ Z_{t-2})$, while `dest_future`
/// holds $Y_t$.
#[allow(clippy::too_many_arguments)]
pub fn cte_observations<T: Clone + Default>(
    source: &Array1<T>,
    destination: &Array1<T>,
    condition: &Array1<T>,
    src_hist_len: usize,
    dest_hist_len: usize,
    cond_hist_len: usize,
    step_size: usize,
    permute_src: bool,
) -> (Array2<T>, Array2<T>, Array2<T>, Array2<T>) {
    let max_delay = src_hist_len.max(dest_hist_len).max(cond_hist_len) * step_size;
    let n = destination.len();

    if max_delay >= n {
        return (
            Array2::default((0, 1)),
            Array2::default((0, dest_hist_len)),
            Array2::default((0, src_hist_len)),
            Array2::default((0, cond_hist_len)),
        );
    }

    let base_indices: Vec<usize> = (max_delay..n).step_by(step_size).collect();
    let n_samples = base_indices.len();

    let mut dest_future = Array2::default((n_samples, 1));
    let mut dest_history = Array2::default((n_samples, dest_hist_len));
    let mut src_history = Array2::default((n_samples, src_hist_len));
    let mut cond_history = Array2::default((n_samples, cond_hist_len));

    for (idx, &base_idx) in base_indices.iter().enumerate() {
        dest_future[(idx, 0)] = destination[base_idx].clone();

        for j in 0..dest_hist_len {
            let offset = (j + 1) * step_size;
            dest_history[(idx, dest_hist_len - 1 - j)] = destination[base_idx - offset].clone();
        }

        for j in 0..src_hist_len {
            let offset = (j + 1) * step_size;
            src_history[(idx, src_hist_len - 1 - j)] = source[base_idx - offset].clone();
        }

        for j in 0..cond_hist_len {
            let offset = (j + 1) * step_size;
            cond_history[(idx, cond_hist_len - 1 - j)] = condition[base_idx - offset].clone();
        }
    }

    if permute_src {
        let mut rng = thread_rng();
        let mut indices: Vec<usize> = (0..n_samples).collect();
        indices.shuffle(&mut rng);

        let mut permuted_src_history = Array2::default((n_samples, src_hist_len));
        for (i, &new_idx) in indices.iter().enumerate() {
            for j in 0..src_hist_len {
                permuted_src_history[(i, j)] = src_history[(new_idx, j)].clone();
            }
        }
        src_history = permuted_src_history;
    }

    (dest_future, dest_history, src_history, cond_history)
}