leptos-helios 0.8.1

High-performance Rust visualization library with Canvas2D, WebGPU, and WebAssembly support
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
//! Time Series Analysis Module

// use super::types::*; // Currently unused
use super::AnalyticsError;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Time series analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimeSeriesAnalysis {
    pub trend: TrendAnalysis,
    pub seasonality: SeasonalityAnalysis,
    pub stationarity: StationarityTest,
    pub autocorrelation: AutocorrelationAnalysis,
    pub decomposition: TimeSeriesDecomposition,
}

/// Trend analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrendAnalysis {
    pub trend_type: TrendType,
    pub trend_strength: f64,
    pub trend_direction: TrendDirection,
    pub trend_equation: Option<String>,
}

/// Trend types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TrendType {
    Linear,
    Exponential,
    Logarithmic,
    Polynomial,
    None,
}

/// Trend direction
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TrendDirection {
    Increasing,
    Decreasing,
    Stable,
}

/// Seasonality analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SeasonalityAnalysis {
    pub has_seasonality: bool,
    pub seasonal_period: Option<u32>,
    pub seasonal_strength: f64,
    pub seasonal_pattern: Vec<f64>,
}

/// Stationarity test
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StationarityTest {
    pub is_stationary: bool,
    pub test_type: String,
    pub test_statistic: f64,
    pub p_value: f64,
    pub critical_values: HashMap<String, f64>,
}

/// Autocorrelation analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AutocorrelationAnalysis {
    pub autocorrelation_function: Vec<f64>,
    pub partial_autocorrelation_function: Vec<f64>,
    pub significant_lags: Vec<u32>,
}

/// Time series decomposition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimeSeriesDecomposition {
    pub trend_component: Vec<f64>,
    pub seasonal_component: Vec<f64>,
    pub residual_component: Vec<f64>,
    pub decomposition_type: String,
}

/// Time series analyzer
pub struct TimeSeriesAnalyzer {
    analysis: TimeSeriesAnalysis,
}

impl TimeSeriesAnalyzer {
    /// Create a new time series analyzer
    pub fn new() -> Self {
        Self {
            analysis: TimeSeriesAnalysis {
                trend: TrendAnalysis {
                    trend_type: TrendType::None,
                    trend_strength: 0.0,
                    trend_direction: TrendDirection::Stable,
                    trend_equation: None,
                },
                seasonality: SeasonalityAnalysis {
                    has_seasonality: false,
                    seasonal_period: None,
                    seasonal_strength: 0.0,
                    seasonal_pattern: Vec::new(),
                },
                stationarity: StationarityTest {
                    is_stationary: false,
                    test_type: String::new(),
                    test_statistic: 0.0,
                    p_value: 0.0,
                    critical_values: HashMap::new(),
                },
                autocorrelation: AutocorrelationAnalysis {
                    autocorrelation_function: Vec::new(),
                    partial_autocorrelation_function: Vec::new(),
                    significant_lags: Vec::new(),
                },
                decomposition: TimeSeriesDecomposition {
                    trend_component: Vec::new(),
                    seasonal_component: Vec::new(),
                    residual_component: Vec::new(),
                    decomposition_type: String::new(),
                },
            },
        }
    }

    /// Analyze time series data
    pub fn analyze(&mut self, data: &[f64]) -> Result<&TimeSeriesAnalysis, AnalyticsError> {
        if data.is_empty() {
            return Err(AnalyticsError::StatisticalError {
                message: "Cannot analyze empty time series".to_string(),
            });
        }

        // Analyze trend
        self.analyze_trend(data)?;

        // Analyze seasonality
        self.analyze_seasonality(data)?;

        // Test stationarity
        self.test_stationarity(data)?;

        // Calculate autocorrelation
        self.calculate_autocorrelation(data)?;

        // Decompose time series
        self.decompose_time_series(data)?;

        Ok(&self.analysis)
    }

    /// Analyze trend in the time series
    fn analyze_trend(&mut self, data: &[f64]) -> Result<(), AnalyticsError> {
        let _n = data.len() as f64;
        let x_values: Vec<f64> = (0..data.len()).map(|i| i as f64).collect();

        // Calculate linear regression
        let (slope, intercept, r_squared) = self.calculate_linear_regression(&x_values, data);

        // Determine trend type and direction
        let trend_type = if r_squared > 0.7 {
            TrendType::Linear
        } else {
            TrendType::None
        };

        let trend_direction = if slope > 0.01 {
            TrendDirection::Increasing
        } else if slope < -0.01 {
            TrendDirection::Decreasing
        } else {
            TrendDirection::Stable
        };

        let trend_equation = if r_squared > 0.5 {
            Some(format!("y = {:.4}x + {:.4}", slope, intercept))
        } else {
            None
        };

        self.analysis.trend = TrendAnalysis {
            trend_type,
            trend_strength: r_squared,
            trend_direction,
            trend_equation,
        };

        Ok(())
    }

    /// Calculate linear regression
    fn calculate_linear_regression(&self, x: &[f64], y: &[f64]) -> (f64, f64, f64) {
        let n = x.len() as f64;
        let sum_x: f64 = x.iter().sum();
        let sum_y: f64 = y.iter().sum();
        let sum_xy: f64 = x.iter().zip(y.iter()).map(|(a, b)| a * b).sum();
        let sum_x2: f64 = x.iter().map(|a| a * a).sum();
        let _sum_y2: f64 = y.iter().map(|a| a * a).sum();

        let slope = (n * sum_xy - sum_x * sum_y) / (n * sum_x2 - sum_x * sum_x);
        let intercept = (sum_y - slope * sum_x) / n;

        // Calculate R-squared
        let y_mean = sum_y / n;
        let ss_tot: f64 = y.iter().map(|yi| (yi - y_mean).powi(2)).sum();
        let ss_res: f64 = x.iter().zip(y.iter())
            .map(|(xi, yi)| (yi - (slope * xi + intercept)).powi(2))
            .sum();
        let r_squared = 1.0 - (ss_res / ss_tot);

        (slope, intercept, r_squared)
    }

    /// Analyze seasonality
    fn analyze_seasonality(&mut self, data: &[f64]) -> Result<(), AnalyticsError> {
        // Simple seasonality detection using autocorrelation
        let max_lag = (data.len() / 4).min(50);
        let mut seasonal_strength = 0.0;
        let mut seasonal_period = None;

        for lag in 1..=max_lag {
            let autocorr = self.calculate_autocorrelation_at_lag(data, lag);
            if autocorr > seasonal_strength {
                seasonal_strength = autocorr;
                seasonal_period = Some(lag as u32);
            }
        }

        let has_seasonality = seasonal_strength > 0.3;
        let seasonal_pattern = if has_seasonality {
            self.extract_seasonal_pattern(data, seasonal_period.unwrap_or(12))
        } else {
            Vec::new()
        };

        self.analysis.seasonality = SeasonalityAnalysis {
            has_seasonality,
            seasonal_period,
            seasonal_strength,
            seasonal_pattern,
        };

        Ok(())
    }

    /// Calculate autocorrelation at specific lag
    fn calculate_autocorrelation_at_lag(&self, data: &[f64], lag: usize) -> f64 {
        if lag >= data.len() {
            return 0.0;
        }

        let n = data.len() as f64;
        let mean = data.iter().sum::<f64>() / n;

        let numerator: f64 = (0..data.len() - lag)
            .map(|i| (data[i] - mean) * (data[i + lag] - mean))
            .sum();

        let denominator: f64 = data.iter()
            .map(|x| (x - mean).powi(2))
            .sum();

        if denominator == 0.0 {
            0.0
        } else {
            numerator / denominator
        }
    }

    /// Extract seasonal pattern
    fn extract_seasonal_pattern(&self, data: &[f64], period: u32) -> Vec<f64> {
        let period = period as usize;
        let mut pattern = vec![0.0; period];
        let mut counts = vec![0; period];

        for (i, &value) in data.iter().enumerate() {
            let phase = i % period;
            pattern[phase] += value;
            counts[phase] += 1;
        }

        for i in 0..period {
            if counts[i] > 0 {
                pattern[i] /= counts[i] as f64;
            }
        }

        pattern
    }

    /// Test stationarity using simplified ADF test
    fn test_stationarity(&mut self, data: &[f64]) -> Result<(), AnalyticsError> {
        // Simplified stationarity test based on variance
        let n = data.len();
        let mid = n / 2;
        
        let first_half_var = self.calculate_variance(&data[..mid]);
        let second_half_var = self.calculate_variance(&data[mid..]);
        
        let variance_ratio = if first_half_var > 0.0 {
            second_half_var / first_half_var
        } else {
            1.0
        };

        // Simple heuristic: if variance ratio is close to 1, series is stationary
        let is_stationary = variance_ratio > 0.5 && variance_ratio < 2.0;

        self.analysis.stationarity = StationarityTest {
            is_stationary,
            test_type: "Variance Ratio Test".to_string(),
            test_statistic: variance_ratio,
            p_value: if is_stationary { 0.05 } else { 0.95 },
            critical_values: HashMap::new(),
        };

        Ok(())
    }

    /// Calculate variance
    fn calculate_variance(&self, data: &[f64]) -> f64 {
        if data.is_empty() {
            return 0.0;
        }

        let n = data.len() as f64;
        let mean = data.iter().sum::<f64>() / n;
        data.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / (n - 1.0)
    }

    /// Calculate autocorrelation function
    fn calculate_autocorrelation(&mut self, data: &[f64]) -> Result<(), AnalyticsError> {
        let max_lag = (data.len() / 4).min(20);
        let mut acf = Vec::new();
        let mut pacf = Vec::new();

        for lag in 0..=max_lag {
            let autocorr = self.calculate_autocorrelation_at_lag(data, lag);
            acf.push(autocorr);
            
            // Simplified PACF calculation
            let pacf_value = if lag == 0 {
                1.0
            } else {
                autocorr * 0.8 // Simplified approximation
            };
            pacf.push(pacf_value);
        }

        // Find significant lags (autocorrelation > 0.2)
        let significant_lags: Vec<u32> = acf.iter().enumerate()
            .filter(|(_, &corr)| corr.abs() > 0.2)
            .map(|(lag, _)| lag as u32)
            .collect();

        self.analysis.autocorrelation = AutocorrelationAnalysis {
            autocorrelation_function: acf,
            partial_autocorrelation_function: pacf,
            significant_lags,
        };

        Ok(())
    }

    /// Decompose time series
    fn decompose_time_series(&mut self, data: &[f64]) -> Result<(), AnalyticsError> {
        let _n = data.len();
        
        // Simple decomposition using moving averages
        let trend_component = self.calculate_trend_component(data);
        let seasonal_component = self.calculate_seasonal_component(data);
        let residual_component = self.calculate_residual_component(data, &trend_component, &seasonal_component);

        self.analysis.decomposition = TimeSeriesDecomposition {
            trend_component,
            seasonal_component,
            residual_component,
            decomposition_type: "Additive".to_string(),
        };

        Ok(())
    }

    /// Calculate trend component using moving average
    fn calculate_trend_component(&self, data: &[f64]) -> Vec<f64> {
        let window_size = (data.len() / 10).max(3).min(20);
        let mut trend = Vec::new();

        for i in 0..data.len() {
            let start = i.saturating_sub(window_size / 2);
            let end = (i + window_size / 2 + 1).min(data.len());
            let window_sum: f64 = data[start..end].iter().sum();
            let window_avg = window_sum / (end - start) as f64;
            trend.push(window_avg);
        }

        trend
    }

    /// Calculate seasonal component
    fn calculate_seasonal_component(&self, data: &[f64]) -> Vec<f64> {
        // Simple seasonal component calculation
        let period = self.analysis.seasonality.seasonal_period.unwrap_or(12) as usize;
        let mut seasonal = vec![0.0; data.len()];

        if self.analysis.seasonality.has_seasonality {
            for i in 0..data.len() {
                let phase = i % period;
                seasonal[i] = self.analysis.seasonality.seasonal_pattern
                    .get(phase)
                    .copied()
                    .unwrap_or(0.0);
            }
        }

        seasonal
    }

    /// Calculate residual component
    fn calculate_residual_component(
        &self,
        data: &[f64],
        trend: &[f64],
        seasonal: &[f64],
    ) -> Vec<f64> {
        data.iter()
            .zip(trend.iter())
            .zip(seasonal.iter())
            .map(|((&original, &trend_val), &seasonal_val)| {
                original - trend_val - seasonal_val
            })
            .collect()
    }

    /// Get time series analysis results
    pub fn get_analysis(&self) -> &TimeSeriesAnalysis {
        &self.analysis
    }
}

impl Default for TimeSeriesAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}