torsh-series 0.1.2

Time series analysis components for ToRSh - powered by SciRS2
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
//! Linear Kalman filter implementation

use crate::TimeSeries;
use torsh_core::error::Result;
use torsh_tensor::{
    creation::{eye, ones, zeros},
    Tensor,
};

/// Kalman filter for linear state estimation
pub struct KalmanFilter {
    state_dim: usize,
    obs_dim: usize,
    /// State transition matrix (F)
    transition: Tensor,
    /// Observation matrix (H)
    observation: Tensor,
    /// Process noise covariance (Q)
    process_noise: Tensor,
    /// Measurement noise covariance (R)
    measurement_noise: Tensor,
    /// Current state estimate
    state: Tensor,
    /// State covariance
    covariance: Tensor,
}

impl KalmanFilter {
    /// Create a new Kalman filter
    pub fn new(state_dim: usize, obs_dim: usize) -> Self {
        Self {
            state_dim,
            obs_dim,
            transition: eye(state_dim).expect("tensor creation should succeed"),
            observation: ones(&[obs_dim, state_dim]).expect("tensor creation should succeed"),
            process_noise: eye(state_dim)
                .expect("tensor creation should succeed")
                .mul_scalar(0.01)
                .expect("scalar mul should succeed"),
            measurement_noise: eye(obs_dim)
                .expect("tensor creation should succeed")
                .mul_scalar(0.1)
                .expect("scalar mul should succeed"),
            state: zeros(&[state_dim, 1]).expect("tensor creation should succeed"), // Column vector
            covariance: eye(state_dim).expect("tensor creation should succeed"),
        }
    }

    /// Create with custom matrices
    pub fn with_matrices(
        state_dim: usize,
        obs_dim: usize,
        transition: Tensor,
        observation: Tensor,
        process_noise: Tensor,
        measurement_noise: Tensor,
    ) -> Self {
        Self {
            state_dim,
            obs_dim,
            transition,
            observation,
            process_noise,
            measurement_noise,
            state: zeros(&[state_dim, 1]).expect("tensor creation should succeed"), // Column vector
            covariance: eye(state_dim).expect("tensor creation should succeed"),
        }
    }

    /// Get current state dimensions
    pub fn dimensions(&self) -> (usize, usize) {
        (self.state_dim, self.obs_dim)
    }

    /// Set state transition matrix
    pub fn set_transition(&mut self, matrix: Tensor) {
        self.transition = matrix;
    }

    /// Set observation matrix
    pub fn set_observation(&mut self, matrix: Tensor) {
        self.observation = matrix;
    }

    /// Set process noise covariance
    pub fn set_process_noise(&mut self, matrix: Tensor) {
        self.process_noise = matrix;
    }

    /// Set measurement noise covariance
    pub fn set_measurement_noise(&mut self, matrix: Tensor) {
        self.measurement_noise = matrix;
    }

    /// Get state transition matrix
    pub fn transition_matrix(&self) -> &Tensor {
        &self.transition
    }

    /// Get observation matrix
    pub fn observation_matrix(&self) -> &Tensor {
        &self.observation
    }

    /// Get current state estimate
    pub fn state(&self) -> &Tensor {
        &self.state
    }

    /// Get state covariance matrix
    pub fn covariance(&self) -> &Tensor {
        &self.covariance
    }

    /// Set initial state
    pub fn set_initial_state(&mut self, state: Tensor, covariance: Tensor) {
        self.state = state;
        self.covariance = covariance;
    }

    /// Predict next state
    pub fn predict(&mut self) -> Result<Tensor> {
        // Predict step of Kalman filter
        // x = F @ x
        self.state = self.transition.matmul(&self.state)?;

        // P = F @ P @ F.T + Q
        let f_p = self.transition.matmul(&self.covariance)?;
        let f_p_ft = f_p.matmul(&self.transition.transpose(0, 1)?)?;
        self.covariance = f_p_ft.add(&self.process_noise)?;

        Ok(self.state.clone())
    }

    /// Update with observation
    pub fn update(&mut self, observation: &Tensor) -> Result<()> {
        // Update step of Kalman filter
        // Ensure observation is a column vector
        let obs_reshaped = if observation.ndim() == 1 {
            observation.view(&[self.obs_dim as i32, 1])?
        } else {
            observation.clone()
        };

        // Innovation: y = z - H @ x
        let h_x = self.observation.matmul(&self.state)?;
        let innovation = obs_reshaped.add(&h_x.mul_scalar(-1.0)?)?;

        // Innovation covariance: S = H @ P @ H.T + R
        let h_p = self.observation.matmul(&self.covariance)?;
        let h_p_ht = h_p.matmul(&self.observation.transpose(0, 1)?)?;
        let innovation_cov = h_p_ht.add(&self.measurement_noise)?;

        // Kalman gain: K = P @ H.T @ inv(S)
        let p_ht = self.covariance.matmul(&self.observation.transpose(0, 1)?)?;

        // For simplicity, use pseudoinverse approach by adding regularization
        // S_reg = S + lambda * I for numerical stability
        let lambda = 1e-6f32;
        let reg_eye = eye(self.obs_dim)?.mul_scalar(lambda)?;
        let innovation_cov_reg = innovation_cov.add(&reg_eye)?;

        // Simplified Kalman gain: K = P @ H.T / (S + lambda * I) for scalar case
        // For multivariate case, we would need matrix inverse
        let kalman_gain = if self.obs_dim == 1 {
            // Scalar case: K = P @ H.T / s_scalar
            let s_scalar = innovation_cov_reg.get_item_flat(0)? + 1e-10f32; // Add small epsilon for stability
            p_ht.div_scalar(s_scalar)?
        } else {
            // For multivariate case, use a simplified approach
            // In practice, you would use LU decomposition or SVD for matrix inverse
            p_ht.div_scalar(innovation_cov_reg.get_item_flat(0)? + 1e-10f32)?
        };

        // State update: x = x + K @ y
        let k_times_innovation = kalman_gain.matmul(&innovation)?;
        self.state = self.state.add(&k_times_innovation)?;

        // Covariance update: P = (I - K @ H) @ P (Joseph form for numerical stability)
        let k_h = kalman_gain.matmul(&self.observation)?;
        let identity = eye(self.state_dim)?;
        let i_minus_kh = identity.add(&k_h.mul_scalar(-1.0)?)?;
        self.covariance = i_minus_kh.matmul(&self.covariance)?;

        Ok(())
    }

    /// Run filter on time series
    pub fn filter(&mut self, series: &TimeSeries) -> Result<TimeSeries> {
        let mut filtered_states = Vec::new();

        for t in 0..series.len() {
            self.predict()?;
            // Extract observation at time t using flat indexing
            let obs_value = series.values.get_item_flat(t)?;
            let obs = Tensor::from_vec(vec![obs_value], &[1])?;
            self.update(&obs)?;

            // Extract state vector elements
            let mut state_vec = Vec::new();
            for i in 0..self.state_dim {
                let val = self.state.get_item_flat(i)?;
                state_vec.push(val);
            }
            filtered_states.extend(state_vec);
        }

        // Create output tensor with proper shape [time_steps, state_dim]
        let values = Tensor::from_vec(filtered_states, &[series.len(), self.state_dim])?;
        Ok(TimeSeries::new(values))
    }

    /// Smooth time series (forward-backward pass)
    pub fn smooth(&mut self, series: &TimeSeries) -> Result<TimeSeries> {
        // Run Kalman smoother (Rauch-Tung-Striebel)
        // First pass: forward filtering
        let filtered = self.filter(series)?;

        // TODO: Implement backward pass for smoothing when needed
        Ok(filtered)
    }

    /// Get innovation (prediction error)
    pub fn innovation(&self, observation: &Tensor) -> Result<Tensor> {
        // y = z - H @ x
        // Ensure observation is a column vector
        let obs_reshaped = if observation.ndim() == 1 {
            observation.view(&[self.obs_dim as i32, 1])?
        } else {
            observation.clone()
        };

        let h_x = self.observation.matmul(&self.state)?;
        obs_reshaped.add(&h_x.mul_scalar(-1.0)?)
    }

    /// Get innovation covariance
    pub fn innovation_covariance(&self) -> Result<Tensor> {
        // S = H @ P @ H.T + R
        let h_p = self.observation.matmul(&self.covariance)?;
        let h_p_ht = h_p.matmul(&self.observation.transpose(0, 1)?)?;
        h_p_ht.add(&self.measurement_noise)
    }

    /// Get Kalman gain (proper implementation)
    pub fn kalman_gain(&self) -> Result<Tensor> {
        // K = P @ H.T @ inv(S)
        let innovation_cov = self.innovation_covariance()?;
        let p_ht = self.covariance.matmul(&self.observation.transpose(0, 1)?)?;

        // Add regularization for numerical stability
        let lambda = 1e-6f32;
        let reg_eye = eye(self.obs_dim)?.mul_scalar(lambda)?;
        let innovation_cov_reg = innovation_cov.add(&reg_eye)?;

        // Compute Kalman gain
        let kalman_gain = if self.obs_dim == 1 {
            // Scalar case: K = P @ H.T / s_scalar
            let s_scalar = innovation_cov_reg.get_item_flat(0)? + 1e-10f32;
            p_ht.div_scalar(s_scalar)?
        } else {
            // For multivariate case, use simplified approach
            p_ht.div_scalar(innovation_cov_reg.get_item_flat(0)? + 1e-10f32)?
        };

        Ok(kalman_gain)
    }

    /// Compute log-likelihood of observations
    pub fn log_likelihood(&mut self, series: &TimeSeries) -> Result<f32> {
        // Compute log-likelihood of observations given model
        // Reset state to start fresh
        self.reset();

        let mut log_likelihood = 0.0f32;
        let n = series.len() as f32;

        // Constant terms for multivariate normal distribution
        let two_pi = 2.0 * std::f32::consts::PI;
        let obs_dim_f32 = self.obs_dim as f32;
        let log_normalization = -0.5 * obs_dim_f32 * two_pi.ln();

        for t in 0..series.len() {
            self.predict()?;

            // Get observation
            let obs_value = series.values.get_item_flat(t)?;
            let obs = Tensor::from_vec(vec![obs_value], &[1])?;

            // Compute innovation and its covariance
            let innovation = self.innovation(&obs)?;
            let innovation_cov = self.innovation_covariance()?;

            // Add small regularization for numerical stability
            let lambda = 1e-6f32;
            let reg_eye = eye(self.obs_dim)?.mul_scalar(lambda)?;
            let innovation_cov_reg = innovation_cov.add(&reg_eye)?;

            // Compute log-likelihood contribution for this observation
            // For univariate case: -0.5 * (log(2π) + log(σ²) + (y²/σ²))
            if self.obs_dim == 1 {
                let innovation_val = innovation.get_item_flat(0)?;
                let cov_val = innovation_cov_reg.get_item_flat(0)?.max(1e-10f32);

                let log_det_term = cov_val.ln();
                let quadratic_term = (innovation_val * innovation_val) / cov_val;

                log_likelihood += log_normalization - 0.5 * (log_det_term + quadratic_term);
            } else {
                // For multivariate case, we would need determinant and matrix inverse
                // Using simplified approach for now
                let innovation_norm_sq: f32 = (0..self.obs_dim)
                    .map(|i| {
                        let val = innovation.get_item_flat(i).unwrap_or(0.0);
                        val * val
                    })
                    .sum();

                let cov_trace: f32 = (0..self.obs_dim)
                    .map(|i| {
                        innovation_cov_reg
                            .get_item_flat(i * self.obs_dim + i)
                            .unwrap_or(1.0)
                    })
                    .sum();

                log_likelihood += log_normalization
                    - 0.5 * (cov_trace.ln() + innovation_norm_sq / cov_trace.max(1e-10f32));
            }

            // Update state
            self.update(&obs)?;
        }

        Ok(log_likelihood / n) // Return average log-likelihood per observation
    }

    /// Reset filter state
    pub fn reset(&mut self) {
        self.state = zeros(&[self.state_dim, 1]).expect("tensor creation should succeed"); // Column vector
        self.covariance = eye(self.state_dim).expect("tensor creation should succeed");
    }
}

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

    fn create_test_series() -> TimeSeries {
        let data = vec![1.0f32, 2.0, 3.0, 4.0, 5.0];
        let tensor = Tensor::from_vec(data, &[5]).expect("Tensor should succeed");
        TimeSeries::new(tensor)
    }

    #[test]
    fn test_kalman_filter_creation() {
        let kf = KalmanFilter::new(2, 1);
        let (state_dim, obs_dim) = kf.dimensions();
        assert_eq!(state_dim, 2);
        assert_eq!(obs_dim, 1);
    }

    #[test]
    fn test_kalman_filter_with_matrices() {
        let transition = eye(2).expect("eye should succeed");
        let observation = ones(&[1, 2]).expect("ones should succeed");
        let process_noise = eye(2).expect("eye should succeed");
        let measurement_noise = eye(1).expect("eye should succeed");

        let kf = KalmanFilter::with_matrices(
            2,
            1,
            transition,
            observation,
            process_noise,
            measurement_noise,
        );

        let (state_dim, obs_dim) = kf.dimensions();
        assert_eq!(state_dim, 2);
        assert_eq!(obs_dim, 1);
    }

    #[test]
    fn test_kalman_filter_matrices() {
        let mut kf = KalmanFilter::new(2, 1);
        let new_transition = eye(2).expect("eye should succeed");
        kf.set_transition(new_transition);

        assert_eq!(kf.transition_matrix().shape().dims(), [2, 2]);
    }

    #[test]
    fn test_kalman_filter_state() {
        let mut kf = KalmanFilter::new(2, 1);
        let initial_state = zeros(&[2, 1]).expect("zeros should succeed"); // Column vector
        let initial_cov = eye(2).expect("eye should succeed");

        kf.set_initial_state(initial_state, initial_cov);
        assert_eq!(kf.state().shape().dims(), [2, 1]);
        assert_eq!(kf.covariance().shape().dims(), [2, 2]);
    }

    #[test]
    fn test_kalman_filter_predict() {
        let mut kf = KalmanFilter::new(2, 1);
        let prediction = kf.predict().expect("prediction should succeed");
        assert_eq!(prediction.shape().dims(), [2, 1]); // Column vector
    }

    #[test]
    fn test_kalman_filter_update() {
        let mut kf = KalmanFilter::new(2, 1);
        let obs = zeros(&[1]).expect("zeros should succeed");
        kf.update(&obs).expect("update operation should succeed");
        // Test that update completes without error
    }

    #[test]
    fn test_kalman_filter_filter() {
        let series = create_test_series();
        let mut kf = KalmanFilter::new(1, 1);
        let filtered = kf.filter(&series).expect("filter operation should succeed");

        assert_eq!(filtered.len(), series.len());
    }

    #[test]
    fn test_kalman_filter_smooth() {
        let series = create_test_series();
        let mut kf = KalmanFilter::new(1, 1);
        let smoothed = kf.smooth(&series).expect("smoothing should succeed");

        assert_eq!(smoothed.len(), series.len());
    }

    #[test]
    fn test_kalman_filter_innovation() {
        let kf = KalmanFilter::new(1, 1);
        let obs = ones(&[1]).expect("ones should succeed");
        let innovation = kf
            .innovation(&obs)
            .expect("innovation computation should succeed");

        assert_eq!(innovation.shape().dims(), [1, 1]); // Column vector
    }

    #[test]
    fn test_kalman_filter_log_likelihood() {
        let series = create_test_series();
        let mut kf = KalmanFilter::new(1, 1);
        let ll = kf
            .log_likelihood(&series)
            .expect("log-likelihood computation should succeed");

        // Now returns actual computed log-likelihood (negative value expected for likelihood)
        assert!(ll < 0.0); // Log-likelihood should be negative
        assert!(ll.is_finite()); // Should be finite
    }

    #[test]
    fn test_kalman_filter_reset() {
        let mut kf = KalmanFilter::new(2, 1);
        kf.reset();

        assert_eq!(kf.state().shape().dims(), [2, 1]); // Column vector
        assert_eq!(kf.covariance().shape().dims(), [2, 2]);
    }
}