Skip to content

FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.

feat: Add CFA Quant Panel with advanced analytics

Aviral261019a77ccefc96
diff --git a/fincept-terminal-desktop/src-tauri/resources/scripts/Analytics/quant/__init__.py b/fincept-terminal-desktop/src-tauri/resources/scripts/Analytics/quant/__init__.py
new file mode 100644
index 000000000..4abb7e021
--- /dev/null
+++ b/fincept-terminal-desktop/src-tauri/resources/scripts/Analytics/quant/__init__.py
@@ -0,0 +1,67 @@
+"""
+Quantitative Analytics Module
+============================
+
+CFA-level quantitative analysis for financial modeling and analysis.
+
+Modules:
+- base_calculator: Base class for all quantitative calculators
+- data_validator: Data validation and quality control
+- exceptions: Custom exception classes
+- quant_modules_3042: Advanced quantitative analyzer
+- rate_calculations: Interest rate and yield calculations
+"""
+
+from .exceptions import (
+ QuantAnalyticsError,
+ DataValidationError,
+ InsufficientDataError,
+ CalculationError,
+ ConvergenceError,
+ ModelFitError,
+ ConfigurationError,
+ DependencyError,
+ handle_calculation_error,
+ safe_calculation
+)
+
+from .base_calculator import (
+ BaseCalculator,
+ CalculatorFactory,
+ CalculationResult,
+ validate_inputs,
+ cache_result,
+ timing_decorator
+)
+
+from .data_validator import (
+ DataValidator,
+ DataQualityReport,
+ validate_returns_series,
+ validate_positive_number,
+ validate_probability
+)
+
+__all__ = [
+ 'QuantAnalyticsError',
+ 'DataValidationError',
+ 'InsufficientDataError',
+ 'CalculationError',
+ 'ConvergenceError',
+ 'ModelFitError',
+ 'ConfigurationError',
+ 'DependencyError',
+ 'handle_calculation_error',
+ 'safe_calculation',
+ 'BaseCalculator',
+ 'CalculatorFactory',
+ 'CalculationResult',
+ 'validate_inputs',
+ 'cache_result',
+ 'timing_decorator',
+ 'DataValidator',
+ 'DataQualityReport',
+ 'validate_returns_series',
+ 'validate_positive_number',
+ 'validate_probability'
+]
diff --git a/fincept-terminal-desktop/src-tauri/resources/scripts/Analytics/quant/base_calculator.py b/fincept-terminal-desktop/src-tauri/resources/scripts/Analytics/quant/base_calculator.py
index a9c13e9df..1ad2e895c 100644
--- a/fincept-terminal-desktop/src-tauri/resources/scripts/Analytics/quant/base_calculator.py
+++ b/fincept-terminal-desktop/src-tauri/resources/scripts/Analytics/quant/base_calculator.py
@@ -219,6 +219,38 @@ class BaseCalculator(ABC):
else:
return result
+ def _sanitize_for_json(self, obj: Any) -> Any:
+ """
+ Recursively sanitize objects for JSON serialization.
+ Converts numpy types and Python booleans to JSON-compatible types.
+ """
+ if isinstance(obj, dict):
+ return {k: self._sanitize_for_json(v) for k, v in obj.items()}
+ elif isinstance(obj, list):
+ return [self._sanitize_for_json(x) for x in obj]
+ elif isinstance(obj, tuple):
+ return [self._sanitize_for_json(x) for x in obj]
+ elif isinstance(obj, np.ndarray):
+ return [self._sanitize_for_json(x) for x in obj.tolist()]
+ elif isinstance(obj, np.bool_):
+ return bool(obj)
+ elif isinstance(obj, bool):
+ return obj
+ elif isinstance(obj, (np.int64, np.int32, np.int16, np.int8)):
+ return int(obj)
+ elif isinstance(obj, (np.float64, np.float32, np.float16)):
+ val = float(obj)
+ if np.isnan(val) or np.isinf(val):
+ return None
+ return val
+ elif isinstance(obj, float):
+ if np.isnan(obj) or np.isinf(obj):
+ return None
+ return obj
+ elif isinstance(obj, datetime):
+ return obj.isoformat()
+ return obj
+
def _create_result_dict(self,
value: Union[float, np.ndarray],
method: str,
@@ -234,7 +266,7 @@ class BaseCalculator(ABC):
metadata: Additional metadata
Returns:
- Standardized result dictionary
+ Standardized result dictionary with all values sanitized for JSON
"""
result = {
'value': self._round_result(value),
@@ -244,10 +276,10 @@ class BaseCalculator(ABC):
}
if parameters:
- result['parameters'] = parameters
+ result['parameters'] = self._sanitize_for_json(parameters)
if metadata:
- result['metadata'] = metadata
+ result['metadata'] = self._sanitize_for_json(metadata)
return result
diff --git a/fincept-terminal-desktop/src-tauri/resources/scripts/Analytics/quant/exceptions.py b/fincept-terminal-desktop/src-tauri/resources/scripts/Analytics/quant/exceptions.py
new file mode 100644
index 000000000..d7199ee42
--- /dev/null
+++ b/fincept-terminal-desktop/src-tauri/resources/scripts/Analytics/quant/exceptions.py
@@ -0,0 +1,164 @@
+
+"""Quantitative Exceptions Module
+=================================
+
+Custom exception classes for quantitative analysis
+
+===== DATA SOURCES REQUIRED =====
+INPUT:
+ - High-frequency market data and price series
+ - Order book data and market microstructure
+ - Alternative data sources and sentiment indicators
+ - Economic data and market fundamentals
+ - Historical factor returns and premiums
+
+OUTPUT:
+ - Quantitative trading signals and strategies
+ - Factor model implementations and analysis
+ - Risk models and portfolio construction methods
+ - Backtest results and performance attribution
+ - Alpha generation and research insights
+
+PARAMETERS:
+ - factor_model: Factor model type (default: 'fama_french_5')
+ - lookback_period: Historical lookback window (default: 252 days)
+ - rebalance_frequency: Strategy rebalancing frequency (default: 'monthly')
+ - universe_size: Investment universe size (default: 1000)
+ - risk_model: Risk model for portfolio construction (default: 'barra')
+"""
+
+
+from typing import Optional, Any
+from functools import wraps
+
+
+class QuantAnalyticsError(Exception):
+ """Base exception for all quantitative analytics errors."""
+
+ def __init__(self, message: str, error_code: Optional[str] = None):
+ self.message = message
+ self.error_code = error_code
+ super().__init__(self.message)
+
+ def to_dict(self) -> dict:
+ return {
+ 'error': self.message,
+ 'error_code': self.error_code,
+ 'error_type': self.__class__.__name__
+ }
+
+
+class DataValidationError(QuantAnalyticsError):
+ """Raised when input data fails validation checks."""
+
+ def __init__(self, message: str, field_name: Optional[str] = None):
+ self.field_name = field_name
+ super().__init__(
+ message=f"{field_name}: {message}" if field_name else message,
+ error_code="DATA_VALIDATION_ERROR"
+ )
+
+
+class InsufficientDataError(QuantAnalyticsError):
+ """Raised when there is not enough data for the calculation."""
+
+ def __init__(self, required: int, provided: int, calculation: Optional[str] = None):
+ self.required = required
+ self.provided = provided
+ self.calculation = calculation
+ message = f"Insufficient data: need {required} observations, got {provided}"
+ if calculation:
+ message = f"{calculation}: {message}"
+ super().__init__(message=message, error_code="INSUFFICIENT_DATA")
+
+
+class CalculationError(QuantAnalyticsError):
+ """Raised when a calculation fails."""
+
+ def __init__(self, message: str, calculation_type: Optional[str] = None):
+ self.calculation_type = calculation_type
+ super().__init__(
+ message=f"{calculation_type}: {message}" if calculation_type else message,
+ error_code="CALCULATION_ERROR"
+ )
+
+
+class ConvergenceError(QuantAnalyticsError):
+ """Raised when an optimization or iterative method fails to converge."""
+
+ def __init__(self, message: str, iterations: Optional[int] = None):
+ self.iterations = iterations
+ if iterations:
+ message = f"{message} (after {iterations} iterations)"
+ super().__init__(message=message, error_code="CONVERGENCE_ERROR")
+
+
+class ModelFitError(QuantAnalyticsError):
+ """Raised when a statistical model fails to fit."""
+
+ def __init__(self, message: str, model_type: Optional[str] = None):
+ self.model_type = model_type
+ super().__init__(
+ message=f"{model_type}: {message}" if model_type else message,
+ error_code="MODEL_FIT_ERROR"
+ )
+
+
+class ConfigurationError(QuantAnalyticsError):
+ """Raised when configuration parameters are invalid."""
+
+ def __init__(self, message: str, parameter: Optional[str] = None):
+ self.parameter = parameter
+ super().__init__(
+ message=f"Invalid parameter '{parameter}': {message}" if parameter else message,
+ error_code="CONFIGURATION_ERROR"
+ )
+
+
+class DependencyError(QuantAnalyticsError):
+ """Raised when a required dependency is not available."""
+
+ def __init__(self, dependency: str, message: Optional[str] = None):
+ self.dependency = dependency
+ msg = f"Required dependency '{dependency}' not available"
+ if message:
+ msg = f"{msg}: {message}"
+ super().__init__(message=msg, error_code="DEPENDENCY_ERROR")
+
+
+def handle_calculation_error(func):
+ """Decorator to handle calculation errors gracefully."""
+ @wraps(func)
+ def wrapper(*args, **kwargs):
+ try:
+ return func(*args, **kwargs)
+ except DataValidationError:
+ raise
+ except InsufficientDataError:
+ raise
+ except CalculationError:
+ raise
+ except ConvergenceError:
+ raise
+ except ModelFitError:
+ raise
+ except Exception as e:
+ func_name = getattr(func, '__name__', 'unknown')
+ raise CalculationError(
+ message=str(e),
+ calculation_type=func_name
+ ) from e
+ return wrapper
+
+
+def safe_calculation(default_value: Any = None):
+ """Decorator that returns a default value on error instead of raising."""
+ def decorator(func):
+ @wraps(func)
+ def wrapper(*args, **kwargs):
+ try:
+ return func(*args, **kwargs)
+ except Exception:
+ return default_value
+ return wrapper
+ return decorator
diff --git a/fincept-terminal-desktop/src-tauri/resources/scripts/Analytics/quant/quant_modules_3042.py b/fincept-terminal-desktop/src-tauri/resources/scripts/Analytics/quant/quant_modules_3042.py
index 5f878c7bd..8e07642c0 100644
--- a/fincept-terminal-desktop/src-tauri/resources/scripts/Analytics/quant/quant_modules_3042.py
+++ b/fincept-terminal-desktop/src-tauri/resources/scripts/Analytics/quant/quant_modules_3042.py
@@ -214,13 +214,22 @@ class AdvancedQuantAnalyzer(BaseCalculator):
seasonal_order: Optional[Tuple[int, int, int, int]] = None) -> Dict[str, Any]:
"""Fit ARIMA model to time series data."""
data = self._validate_returns(data, "data")
+
+ # Validate order parameter
+ if order is None or not isinstance(order, (tuple, list)) or len(order) != 3:
+ raise DataValidationError("order must be a tuple/list of 3 integers (p, d, q)", "order")
+
+ order = tuple(order) # Ensure it's a tuple
self._check_data_length(data, min_length=max(20, sum(order) * 3))
try:
from statsmodels.tsa.arima.model import ARIMA
- # Fit ARIMA model
- model = ARIMA(data, order=order, seasonal_order=seasonal_order)
+ # Fit ARIMA model - only pass seasonal_order if it's not None
+ if seasonal_order is not None:
+ model = ARIMA(data, order=order, seasonal_order=seasonal_order)
+ else:
+ model = ARIMA(data, order=order)
fitted_model = model.fit()
# Model diagnostics
@@ -237,7 +246,7 @@ class AdvancedQuantAnalyzer(BaseCalculator):
return self._create_result_dict(
value={
- 'parameters': fitted_model.params.to_dict(),
+ 'parameters': fitted_model.params.tolist() if hasattr(fitted_model.params, 'tolist') else list(fitted_model.params),
'fitted_values': fitted_values.tolist(),
'residuals': residuals.tolist()
},
@@ -400,7 +409,7 @@ class AdvancedQuantAnalyzer(BaseCalculator):
raise DataValidationError(f"Invalid problem_type: {problem_type}", "problem_type")
# Split data
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=self.random_seed)
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, shuffle=False)
# Scale features
scaler = StandardScaler()
@@ -462,6 +471,7 @@ class AdvancedQuantAnalyzer(BaseCalculator):
metadata={
'train_size': len(X_train),
'test_size': len(X_test),
+ 'y_test': y_test.tolist(),
'n_features': X.shape[1],
'best_algorithm': max(results.keys(), key=lambda k: results[k].get(
'r2_score' if problem_type == 'regression' else 'accuracy', -np.inf)) if results else None
diff --git a/fincept-terminal-desktop/src-tauri/resources/scripts/Analytics/quant_analytics_cli.py b/fincept-terminal-desktop/src-tauri/resources/scripts/Analytics/quant_analytics_cli.py
new file mode 100644
index 000000000..3f4c1d7f6
--- /dev/null
+++ b/fincept-terminal-desktop/src-tauri/resources/scripts/Analytics/quant_analytics_cli.py
@@ -0,0 +1,520 @@
+"""
+CFA Quant Analytics CLI - Unified Command Line Interface
+Provides a unified entry point for CFA-level quantitative analytics in Fincept Terminal.
+
+Categories:
+- Time Series: Trend analysis, stationarity tests, ARIMA, forecasting
+- Machine Learning: Supervised/unsupervised learning, model evaluation
+- Sampling: Bootstrap, jackknife, CLT, sampling error analysis
+- Data Validation: Quality checks, outlier detection, missing data analysis
+
+Usage:
+ python quant_analytics_cli.py <command> [params_json]
+
+Commands:
+ list - List all available analyses
+ trend_analysis - Analyze linear/log-linear trends
+ stationarity_test - Test for stationarity (ADF, KPSS)
+ arima_model - Fit ARIMA model
+ forecasting - Generate forecasts
+ supervised_learning - Run supervised ML analysis
+ unsupervised_learning - Run unsupervised ML analysis
+ model_evaluation - Evaluate model performance
+ feature_engineering - Analyze features
+ sampling_techniques - Compare sampling methods
+ central_limit_theorem - Demonstrate CLT
+ resampling_methods - Bootstrap/jackknife analysis
+ sampling_error_analysis - Analyze sampling errors
+ validate_data - Validate financial data
+"""
+
+import sys
+import os
+
+script_dir = os.path.dirname(os.path.abspath(__file__))
+if script_dir not in sys.path:
+ sys.path.insert(0, script_dir)
+
+quant_dir = os.path.join(script_dir, 'quant')
+if quant_dir not in sys.path:
+ sys.path.insert(0, quant_dir)
+
+import json
+import numpy as np
+import pandas as pd
+from typing import Dict, Any, List, Optional, Union
+from datetime import datetime
+from decimal import Decimal
+
+
+class DecimalEncoder(json.JSONEncoder):
+ def default(self, obj):
+ if isinstance(obj, Decimal):
+ return float(obj)
+ if isinstance(obj, datetime):
+ return obj.isoformat()
+ if isinstance(obj, np.ndarray):
+ return [self._sanitize_value(x) for x in obj.tolist()]
+ if isinstance(obj, pd.Series):
+ return [self._sanitize_value(x) for x in obj.tolist()]
+ if isinstance(obj, pd.DataFrame):
+ return obj.to_dict('records')
+ if isinstance(obj, (np.int64, np.int32)):
+ return int(obj)
+ if isinstance(obj, (np.float64, np.float32)):
+ return self._sanitize_value(float(obj))
+ if isinstance(obj, (np.bool_, bool)):
+ return bool(obj)
+ if pd.isna(obj):
+ return None
+ return super().default(obj)
+
+ def _sanitize_value(self, val):
+ if isinstance(val, float):
+ if np.isnan(val) or np.isinf(val):
+ return None
+ return val
+
+ def encode(self, obj):
+ return super().encode(self._sanitize_obj(obj))
+
+ def _sanitize_obj(self, obj):
+ if isinstance(obj, dict):
+ return {k: self._sanitize_obj(v) for k, v in obj.items()}
+ elif isinstance(obj, list):
+ return [self._sanitize_obj(x) for x in obj]
+ elif isinstance(obj, tuple):
+ return [self._sanitize_obj(x) for x in obj]
+ elif isinstance(obj, np.ndarray):
+ return [self._sanitize_obj(x) for x in obj.tolist()]
+ elif isinstance(obj, pd.Series):
+ return [self._sanitize_obj(x) for x in obj.tolist()]
+ elif isinstance(obj, pd.DataFrame):
+ records = obj.to_dict('records')
+ return [self._sanitize_obj(r) for r in records]
+ elif isinstance(obj, np.bool_):
+ return bool(obj)
+ elif isinstance(obj, bool):
+ return obj
+ elif isinstance(obj, (np.int64, np.int32, np.int16, np.int8)):
+ return int(obj)
+ elif isinstance(obj, (np.float64, np.float32, np.float16)):
+ val = float(obj)
+ if np.isnan(val) or np.isinf(val):
+ return None
+ return val
+ elif isinstance(obj, float):
+ if np.isnan(obj) or np.isinf(obj):
+ return None
+ return obj
+ elif pd.isna(obj):
+ return None
+ return obj
+
+
+class CFAQuantEngine:
+ def __init__(self):
+ self.analyzer = None
+ self.validator = None
+ self._load_modules()
+
+ def _load_modules(self):
+ try:
+ from quant.quant_modules_3042 import AdvancedQuantAnalyzer
+ from quant.data_validator import DataValidator
+ self.analyzer = AdvancedQuantAnalyzer()
+ self.validator = DataValidator()
+ except ImportError as e:
+ self.import_error = str(e)
+
+ def _convert_numpy_types(self, obj: Any) -> Any:
+ """Recursively convert numpy types to Python native types."""
+ if isinstance(obj, dict):
+ return {k: self._convert_numpy_types(v) for k, v in obj.items()}
+ elif isinstance(obj, list):
+ return [self._convert_numpy_types(x) for x in obj]
+ elif isinstance(obj, tuple):
+ return [self._convert_numpy_types(x) for x in obj]
+ elif isinstance(obj, np.ndarray):
+ return [self._convert_numpy_types(x) for x in obj.tolist()]
+ elif isinstance(obj, np.bool_):
+ return bool(obj)
+ elif isinstance(obj, bool):
+ return obj
+ elif isinstance(obj, (np.int64, np.int32, np.int16, np.int8)):
+ return int(obj)
+ elif isinstance(obj, (np.float64, np.float32, np.float16)):
+ val = float(obj)
+ if np.isnan(val) or np.isinf(val):
+ return None
+ return val
+ elif isinstance(obj, pd.Series):
+ return [self._convert_numpy_types(x) for x in obj.tolist()]
+ elif isinstance(obj, pd.DataFrame):
+ records = obj.to_dict('records')
+ return [self._convert_numpy_types(r) for r in records]
+ elif isinstance(obj, float):
+ if np.isnan(obj) or np.isinf(obj):
+ return None
+ return obj
+ elif pd.isna(obj):
+ return None
+ return obj
+
+ def _parse_data(self, data: Union[str, List, Dict]) -> np.ndarray:
+ if isinstance(data, str):
+ try:
+ parsed = json.loads(data)
+ if isinstance(parsed, list):
+ return np.array(parsed, dtype=float)
+ return np.array(parsed, dtype=float)
+ except json.JSONDecodeError:
+ values = [float(x.strip()) for x in data.split(',') if x.strip()]
+ return np.array(values)
+ elif isinstance(data, list):
+ return np.array(data, dtype=float)
+ elif isinstance(data, dict):
+ return pd.DataFrame(data).values
+ return np.array(data, dtype=float)
+
+ def list_analyses(self) -> Dict[str, Any]:
+ return {
+ 'success': True,
+ 'available_analyses': {
+ 'time_series': {
+ 'trend_analysis': 'Analyze linear and log-linear trends',
+ 'stationarity_test': 'Test for stationarity (ADF, KPSS)',
+ 'arima_model': 'Fit ARIMA model to time series',
+ 'forecasting': 'Generate forecasts using various methods'
+ },
+ 'machine_learning': {
+ 'supervised_learning': 'Compare supervised ML algorithms',
+ 'unsupervised_learning': 'PCA, clustering analysis',
+ 'model_evaluation': 'Cross-validation and overfitting detection',
+ 'feature_engineering': 'Feature selection and analysis'
+ },
+ 'sampling': {
+ 'sampling_techniques': 'Simple, stratified, systematic sampling',
+ 'central_limit_theorem': 'CLT demonstration',
+ 'resampling_methods': 'Bootstrap, jackknife, permutation',
+ 'sampling_error_analysis': 'Sampling error and sample size'
+ },
+ 'data_quality': {
+ 'validate_data': 'Validate financial data quality',
+ 'validate_returns': 'Validate return series',
+ 'validate_prices': 'Validate price data',
+ 'validate_rates': 'Validate interest rate data'
+ }
+ },
+ 'timestamp': datetime.now().isoformat()
+ }
+
+ def trend_analysis(self, params: Dict[str, Any]) -> Dict[str, Any]:
+ if self.analyzer is None:
+ return {'success': False, 'error': f'Module not loaded: {getattr(self, "import_error", "unknown")}'}
+
+ data = self._parse_data(params.get('data', []))
+ trend_type = params.get('trend_type', 'linear')
+
+ result = self.analyzer.analyze_trend(data, trend_type=trend_type)
+ return {'success': True, 'analysis_type': 'trend_analysis', 'result': self._convert_numpy_types(result)}
+
+ def stationarity_test(self, params: Dict[str, Any]) -> Dict[str, Any]:
+ if self.analyzer is None:
+ return {'success': False, 'error': f'Module not loaded: {getattr(self, "import_error", "unknown")}'}
+
+ data = self._parse_data(params.get('data', []))
+ test_type = params.get('test_type', 'adf')
+
+ result = self.analyzer.test_stationarity(data, test_type=test_type)
+ return {'success': True, 'analysis_type': 'stationarity_test', 'result': self._convert_numpy_types(result)}
+
+ def arima_model(self, params: Dict[str, Any]) -> Dict[str, Any]:
+ if self.analyzer is None:
+ return {'success': False, 'error': f'Module not loaded: {getattr(self, "import_error", "unknown")}'}
+
+ data = self._parse_data(params.get('data', []))
+
+ # Handle order parameter - ensure it's a tuple
+ order_param = params.get('order', [1, 0, 1])
+ if order_param is None:
+ order = (1, 0, 1)
+ elif isinstance(order_param, (list, tuple)):
+ order = tuple(order_param)
+ else:
+ order = (1, 0, 1)
+
+ # Handle seasonal_order parameter
+ seasonal_order = params.get('seasonal_order')
+ if seasonal_order and isinstance(seasonal_order, (list, tuple)):
+ seasonal_order = tuple(seasonal_order)
+ else:
+ seasonal_order = None
+
+ result = self.analyzer.fit_arima_model(data, order=order, seasonal_order=seasonal_order)
+ return {'success': True, 'analysis_type': 'arima_model', 'result': self._convert_numpy_types(result)}
+
+ def forecasting(self, params: Dict[str, Any]) -> Dict[str, Any]:
+ if self.analyzer is None:
+ return {'success': False, 'error': f'Module not loaded: {getattr(self, "import_error", "unknown")}'}
+
+ data = self._parse_data(params.get('data', []))
+ horizon = params.get('horizon', 5)
+ method = params.get('method', 'simple_exponential')
+ train_size = params.get('train_size', 0.8)
+
+ result = self.analyzer.generate_forecasts(data, horizon=horizon, method=method, train_size=train_size)
+ return {'success': True, 'analysis_type': 'forecasting', 'result': self._convert_numpy_types(result)}
+
+ def _create_lag_features(self, data: np.ndarray, n_lags: int = 5) -> tuple:
+ """Create lag features from time series data for ML models."""
+ if len(data) < n_lags + 2:
+ raise ValueError(f"Need at least {n_lags + 2} data points for lag features, got {len(data)}")
+
+ X = []
+ y = []
+ for i in range(n_lags, len(data)):
+ X.append(data[i-n_lags:i])
+ y.append(data[i])
+ return np.array(X), np.array(y)
+
+ def supervised_learning(self, params: Dict[str, Any]) -> Dict[str, Any]:
+ if self.analyzer is None:
+ return {'success': False, 'error': f'Module not loaded: {getattr(self, "import_error", "unknown")}'}
+
+ X = params.get('X')
+ y = params.get('y')
+
+ if X is None or y is None:
+ data = params.get('data')
+ if data is not None:
+ data_arr = self._parse_data(data)
+ n_lags = params.get('n_lags')
+ if n_lags is None or n_lags < 1:
+ n_lags = 5
+ try:
+ X, y = self._create_lag_features(data_arr, n_lags=n_lags)
+ except ValueError as e:
+ return {'success': False, 'error': str(e)}
+ else:
+ return {'success': False, 'error': 'Either provide X/y arrays or a data time series'}
+ else:
+ X = np.array(X)
+ y = np.array(y)
+
+ problem_type = params.get('problem_type', 'regression')
+ algorithms = params.get('algorithms', ['ridge', 'random_forest'])
+
+ result = self.analyzer.supervised_learning_analysis(X, y, problem_type=problem_type, algorithms=algorithms)
+ return {'success': True, 'analysis_type': 'supervised_learning', 'result': self._convert_numpy_types(result)}
+
+ def unsupervised_learning(self, params: Dict[str, Any]) -> Dict[str, Any]:
+ if self.analyzer is None:
+ return {'success': False, 'error': f'Module not loaded: {getattr(self, "import_error", "unknown")}'}
+
+ X = params.get('X')
+
+ if X is None:
+ data = params.get('data')
+ if data is not None:
+ data_arr = self._parse_data(data)
+ n_lags = params.get('n_lags')
+ if n_lags is None or n_lags < 1:
+ n_lags = 5
+ try:
+ X, _ = self._create_lag_features(data_arr, n_lags=n_lags)
+ except ValueError as e:
+ return {'success': False, 'error': str(e)}
+ else:
+ return {'success': False, 'error': 'Either provide X array or a data time series'}
+ else:
+ X = np.array(X)
+
+ methods = params.get('methods', ['pca', 'kmeans'])
+
+ result = self.analyzer.unsupervised_learning_analysis(X, methods=methods)
+ return {'success': True, 'analysis_type': 'unsupervised_learning', 'result': self._convert_numpy_types(result)}
+
+ def model_evaluation(self, params: Dict[str, Any]) -> Dict[str, Any]:
+ if self.analyzer is None:
+ return {'success': False, 'error': f'Module not loaded: {getattr(self, "import_error", "unknown")}'}
+
+ X = params.get('X')
+ y = params.get('y')
+
+ if X is None or y is None:
+ data = params.get('data')
+ if data is not None:
+ data_arr = self._parse_data(data)
+ n_lags = params.get('n_lags')
+ if n_lags is None or n_lags < 1:
+ n_lags = 5
+ try:
+ X, y = self._create_lag_features(data_arr, n_lags=n_lags)
+ except ValueError as e:
+ return {'success': False, 'error': str(e)}
+ else:
+ return {'success': False, 'error': 'Either provide X/y arrays or a data time series'}
+ else:
+ X = np.array(X)
+ y = np.array(y)
+
+ model_type = params.get('model_type', 'random_forest')
+ cv_folds = params.get('cv_folds', 5)
+
+ result = self.analyzer.evaluate_model_performance(X, y, model_type=model_type, cv_folds=cv_folds)
+ return {'success': True, 'analysis_type': 'model_evaluation', 'result': self._convert_numpy_types(result)}
+
+ def feature_engineering(self, params: Dict[str, Any]) -> Dict[str, Any]:
+ if self.analyzer is None:
+ return {'success': False, 'error': f'Module not loaded: {getattr(self, "import_error", "unknown")}'}
+
+ data = pd.DataFrame(params.get('data', {}))
+ target_column = params.get('target_column')
+ text_columns = params.get('text_columns')
+
+ result = self.analyzer.feature_engineering_analysis(data, target_column=target_column, text_columns=text_columns)
+ return {'success': True, 'analysis_type': 'feature_engineering', 'result': self._convert_numpy_types(result)}
+
+ def sampling_techniques(self, params: Dict[str, Any]) -> Dict[str, Any]:
+ if self.analyzer is None:
+ return {'success': False, 'error': f'Module not loaded: {getattr(self, "import_error", "unknown")}'}
+
+ population = self._parse_data(params.get('population', []))
+ sample_size = params.get('sample_size', 100)
+ sampling_methods = params.get('sampling_methods', ['simple', 'stratified', 'systematic'])
+
+ result = self.analyzer.analyze_sampling_techniques(population, sample_size=sample_size, sampling_methods=sampling_methods)
+ return {'success': True, 'analysis_type': 'sampling_techniques', 'result': self._convert_numpy_types(result)}
+
+ def central_limit_theorem(self, params: Dict[str, Any]) -> Dict[str, Any]:
+ if self.analyzer is None:
+ return {'success': False, 'error': f'Module not loaded: {getattr(self, "import_error", "unknown")}'}
+
+ population_dist = params.get('population_dist', 'exponential')
+ population_params = params.get('population_params', {'scale': 1.0})
+ sample_sizes = params.get('sample_sizes', [5, 10, 30, 100])
+ n_samples = params.get('n_samples', 1000)
+
+ result = self.analyzer.demonstrate_central_limit_theorem(
+ population_dist=population_dist,
+ population_params=population_params,
+ sample_sizes=sample_sizes,
+ n_samples=n_samples
+ )
+ return {'success': True, 'analysis_type': 'central_limit_theorem', 'result': self._convert_numpy_types(result)}
+
+ def resampling_methods(self, params: Dict[str, Any]) -> Dict[str, Any]:
+ if self.analyzer is None:
+ return {'success': False, 'error': f'Module not loaded: {getattr(self, "import_error", "unknown")}'}
+
+ data = self._parse_data(params.get('data', []))
+ methods = params.get('methods', ['bootstrap', 'jackknife', 'permutation'])
+ n_resamples = params.get('n_resamples', 1000)
+
+ result = self.analyzer.advanced_resampling_analysis(data, methods=methods, n_resamples=n_resamples)
+ return {'success': True, 'analysis_type': 'resampling_methods', 'result': self._convert_numpy_types(result)}
+
+ def sampling_error_analysis(self, params: Dict[str, Any]) -> Dict[str, Any]:
+ if self.analyzer is None:
+ return {'success': False, 'error': f'Module not loaded: {getattr(self, "import_error", "unknown")}'}
+
+ population_mean = params.get('population_mean', 0.0)
+ population_std = params.get('population_std', 1.0)
+ sample_sizes = params.get('sample_sizes', [10, 30, 50, 100, 500])
+ confidence_level = params.get('confidence_level', 0.95)
+ n_simulations = params.get('n_simulations', 1000)
+
+ result = self.analyzer.calculate_sampling_error_analysis(
+ population_mean=population_mean,
+ population_std=population_std,
+ sample_sizes=sample_sizes,
+ confidence_level=confidence_level,
+ n_simulations=n_simulations
+ )
+ return {'success': True, 'analysis_type': 'sampling_error_analysis', 'result': self._convert_numpy_types(result)}
+
+ def validate_data(self, params: Dict[str, Any]) -> Dict[str, Any]:
+ if self.validator is None:
+ return {'success': False, 'error': f'Module not loaded: {getattr(self, "import_error", "unknown")}'}
+
+ data = params.get('data', [])
+ if isinstance(data, list):
+ data = pd.Series(data)
+ elif isinstance(data, dict):
+ data = pd.DataFrame(data)
+
+ data_type = params.get('data_type', 'general')
+ data_name = params.get('data_name', 'data')
+
+ cleaned_data, report = self.validator.validate_financial_data(data, data_type=data_type, data_name=data_name)
+
+ return {
+ 'success': True,
+ 'analysis_type': 'validate_data',
+ 'result': self._convert_numpy_types(report.to_dict()),
+ 'data_cleaned': isinstance(cleaned_data, pd.DataFrame) or isinstance(cleaned_data, pd.Series)
+ }
+
+
+def main():
+ if len(sys.argv) < 2:
+ result = {'success': False, 'error': 'No command provided', 'usage': 'python quant_analytics_cli.py <command> [params_json]'}
+ print(json.dumps(result, cls=DecimalEncoder))
+ return
+
+ command = sys.argv[1].lower()
+ params = {}
+
+ if len(sys.argv) > 2:
+ try:
+ params = json.loads(sys.argv[2])
+ except json.JSONDecodeError as e:
+ result = {'success': False, 'error': f'Invalid JSON parameters: {e}'}
+ print(json.dumps(result, cls=DecimalEncoder))
+ return
+
+ engine = CFAQuantEngine()
+
+ command_map = {
+ 'list': engine.list_analyses,
+ 'trend_analysis': lambda: engine.trend_analysis(params),
+ 'stationarity_test': lambda: engine.stationarity_test(params),
+ 'arima_model': lambda: engine.arima_model(params),
+ 'forecasting': lambda: engine.forecasting(params),
+ 'supervised_learning': lambda: engine.supervised_learning(params),
+ 'unsupervised_learning': lambda: engine.unsupervised_learning(params),
+ 'model_evaluation': lambda: engine.model_evaluation(params),
+ 'feature_engineering': lambda: engine.feature_engineering(params),
+ 'sampling_techniques': lambda: engine.sampling_techniques(params),
+ 'central_limit_theorem': lambda: engine.central_limit_theorem(params),
+ 'resampling_methods': lambda: engine.resampling_methods(params),
+ 'sampling_error_analysis': lambda: engine.sampling_error_analysis(params),
+ 'validate_data': lambda: engine.validate_data(params),
+ }
+
+ if command not in command_map:
+ result = {
+ 'success': False,
+ 'error': f'Unknown command: {command}',
+ 'available_commands': list(command_map.keys())
+ }
+ print(json.dumps(result, cls=DecimalEncoder))
+ return
+
+ try:
+ result = command_map[command]()
+ print(json.dumps(result, cls=DecimalEncoder))
+ except Exception as e:
+ import traceback
+ result = {
+ 'success': False,
+ 'error': str(e),
+ 'traceback': traceback.format_exc(),
+ 'command': command
+ }
+ print(json.dumps(result, cls=DecimalEncoder))
+
+
+if __name__ == '__main__':
+ main()
diff --git a/fincept-terminal-desktop/src-tauri/src/commands/analytics.rs b/fincept-terminal-desktop/src-tauri/src/commands/analytics.rs
index 50e289c6e..8ac8b8f4d 100644
--- a/fincept-terminal-desktop/src-tauri/src/commands/analytics.rs
+++ b/fincept-terminal-desktop/src-tauri/src/commands/analytics.rs
@@ -532,3 +532,21 @@ pub async fn execute_statsmodels_analytics(
Some("statsmodels")
)
}
+
+#[tauri::command]
+pub async fn execute_quant_analytics(
+ app: tauri::AppHandle,
+ command: String,
+ params: Option<String>,
+) -> Result<String, String> {
+ let mut args = vec![command];
+ if let Some(p) = params {
+ args.push(p);
+ }
+ crate::utils::python::execute_python_subprocess(
+ &app,
+ "Analytics/quant_analytics_cli.py",
+ &args,
+ Some("quant_analytics")
+ )
+}
diff --git a/fincept-terminal-desktop/src-tauri/src/lib.rs b/fincept-terminal-desktop/src-tauri/src/lib.rs
index 942895904..238a81c9c 100644
--- a/fincept-terminal-desktop/src-tauri/src/lib.rs
+++ b/fincept-terminal-desktop/src-tauri/src/lib.rs
@@ -1495,6 +1495,7 @@ pub fn run() {
commands::analytics::calculate_rates,
commands::analytics::execute_economics_analytics,
commands::analytics::execute_statsmodels_analytics,
+ commands::analytics::execute_quant_analytics,
// FinancePy - Derivatives Pricing Commands
commands::financepy::financepy_create_date,
commands::financepy::financepy_date_range,
diff --git a/fincept-terminal-desktop/src/components/tabs/ai-quant-lab/AIQuantLabTab.tsx b/fincept-terminal-desktop/src/components/tabs/ai-quant-lab/AIQuantLabTab.tsx
index 18f1111b4..2fd951684 100644
--- a/fincept-terminal-desktop/src/components/tabs/ai-quant-lab/AIQuantLabTab.tsx
+++ b/fincept-terminal-desktop/src/components/tabs/ai-quant-lab/AIQuantLabTab.tsx
@@ -20,7 +20,8 @@ import {
Sparkles,
CheckCircle2,
Clock,
- Sigma
+ Sigma,
+ Calculator
} from 'lucide-react';
import { TabFooter } from '@/components/common/TabFooter';
import { qlibService } from '@/services/aiQuantLab/qlibService';
@@ -35,6 +36,7 @@ import { FFNAnalyticsPanel } from './FFNAnalyticsPanel';
import { FunctimePanel } from './FunctimePanel';
import { FortitudoPanel } from './FortitudoPanel';
import { StatsmodelsPanel } from './StatsmodelsPanel';
+import { CFAQuantPanel } from './CFAQuantPanel';
import { StatusBar } from './StatusBar';
// Bloomberg Professional Color Palette - Consistent across all tabs
@@ -56,7 +58,7 @@ const BLOOMBERG = {
MUTED: '#4A4A4A'
};
-type ViewMode = 'factor_discovery' | 'model_library' | 'backtesting' | 'live_signals' | 'ffn_analytics' | 'functime' | 'fortitudo' | 'statsmodels';
+type ViewMode = 'factor_discovery' | 'model_library' | 'backtesting' | 'live_signals' | 'ffn_analytics' | 'functime' | 'fortitudo' | 'statsmodels' | 'cfa_quant';
export default function AIQuantLabTab() {
// State
@@ -161,7 +163,8 @@ export default function AIQuantLabTab() {
{ id: 'ffn_analytics', label: 'FFN ANALYTICS', icon: TrendingUp, description: 'Portfolio performance & risk metrics' },
{ id: 'functime', label: 'FUNCTIME', icon: Zap, description: 'ML time series forecasting' },
{ id: 'fortitudo', label: 'FORTITUDO', icon: Database, description: 'VaR, CVaR, option pricing, entropy pooling' },
- { id: 'statsmodels', label: 'STATSMODELS', icon: Sigma, description: 'ARIMA, regression, statistical tests, PCA' }
+ { id: 'statsmodels', label: 'STATSMODELS', icon: Sigma, description: 'ARIMA, regression, statistical tests, PCA' },
+ { id: 'cfa_quant', label: 'CFA QUANT', icon: Calculator, description: 'CFA-level quant analytics, ML, sampling' }
];
return (
@@ -288,6 +291,7 @@ export default function AIQuantLabTab() {
{activeView === 'functime' && <FunctimePanel />}
{activeView === 'fortitudo' && <FortitudoPanel />}
{activeView === 'statsmodels' && <StatsmodelsPanel />}
+ {activeView === 'cfa_quant' && <CFAQuantPanel />}
</div>
{/* Status Bar */}
diff --git a/fincept-terminal-desktop/src/components/tabs/ai-quant-lab/CFAQuantPanel.tsx b/fincept-terminal-desktop/src/components/tabs/ai-quant-lab/CFAQuantPanel.tsx
new file mode 100644
index 000000000..ce5506b0d
--- /dev/null
+++ b/fincept-terminal-desktop/src/components/tabs/ai-quant-lab/CFAQuantPanel.tsx
@@ -0,0 +1,3065 @@
+import React, { useState, useEffect, useCallback, useMemo } from 'react';
+import {
+ TrendingUp,
+ Calculator,
+ RefreshCw,
+ AlertCircle,
+ CheckCircle2,
+ ChevronRight,
+ ChevronLeft,
+ Search,
+ Play,
+ Download,
+ Database,
+ Brain,
+ Shuffle,
+ Target,
+ BarChart3,
+ Info,
+ Sparkles,
+ ArrowUpRight,
+ ArrowDownRight,
+ Minus,
+ Copy,
+ HelpCircle,
+ Zap,
+ LineChart,
+ Activity,
+ TrendingDown,
+ ZoomIn,
+ ZoomOut,
+ RotateCcw
+} from 'lucide-react';
+import {
+ LineChart as RechartsLineChart,
+ Line,
+ XAxis,
+ YAxis,
+ CartesianGrid,
+ Tooltip,
+ ResponsiveContainer,
+ Area,
+ AreaChart,
+ ReferenceLine,
+ ComposedChart,
+ Bar,
+ Scatter,
+ Brush,
+ ReferenceArea
+} from 'recharts';
+import { quantAnalyticsService, type QuantAnalyticsResult } from '@/services/aiQuantLab/quantAnalyticsService';
+import { yfinanceService } from '@/services/yfinanceService';
+
+// Bloomberg-style color palette
+const BB = {
+ // Core Bloomberg colors
+ black: '#000000',
+ darkBg: '#0a0a0a',
+ panelBg: '#121212',
+ cardBg: '#1a1a1a',
+ borderDark: '#2a2a2a',
+ borderLight: '#3a3a3a',
+
+ // Accent colors
+ amber: '#FF8C00',
+ amberLight: '#FFA500',
+ amberDim: '#CC7000',
+
+ // Status colors
+ green: '#00C853',
+ greenDim: '#00A040',
+ red: '#FF3D00',
+ redDim: '#CC3000',
+
+ // Text colors
+ textPrimary: '#FFFFFF',
+ textSecondary: '#B0B0B0',
+ textMuted: '#707070',
+ textAmber: '#FF9500',
+
+ // Chart colors
+ chartLine: '#00D4FF',
+ chartArea: 'rgba(0, 212, 255, 0.1)',
+ chartGrid: '#1f1f1f',
+
+ // Bloomberg blue
+ blue: '#0066CC',
+ blueLight: '#0088FF',
+};
+
+type Step = 'data' | 'analysis' | 'results';
+type DataSourceType = 'symbol' | 'manual';
+
+interface AnalysisConfig {
+ id: string;
+ name: string;
+ shortName: string;
+ description: string;
+ icon: React.ElementType;
+ category: 'time_series' | 'machine_learning' | 'sampling' | 'data_quality';
+ color: string;
+ minDataPoints: number;
+ helpText: string;
+}
+
+const analysisConfigs: AnalysisConfig[] = [
+ {
+ id: 'trend_analysis',
+ name: 'Trend Analysis',
+ shortName: 'TREND',
+ description: 'Detect upward/downward trends',
+ icon: TrendingUp,
+ category: 'time_series',
+ color: BB.green,
+ minDataPoints: 5,
+ helpText: 'Linear regression to identify significant trends in price movements.'
+ },
+ {
+ id: 'stationarity_test',
+ name: 'Stationarity Test',
+ shortName: 'ADF',
+ description: 'Unit root test (ADF)',
+ icon: BarChart3,
+ category: 'time_series',
+ color: BB.blue,
+ minDataPoints: 20,
+ helpText: 'Tests if time series has constant mean/variance. Important for ARIMA modeling.'
+ },
+ {
+ id: 'arima_model',
+ name: 'ARIMA Forecast',
+ shortName: 'ARIMA',
+ description: 'Time series prediction',
+ icon: LineChart,
+ category: 'time_series',
+ color: BB.amber,
+ minDataPoints: 20,
+ helpText: 'AutoRegressive Integrated Moving Average for forecasting.'
+ },
+ {
+ id: 'forecasting',
+ name: 'Exp. Smoothing',
+ shortName: 'ETS',
+ description: 'Simple forecasting',
+ icon: Sparkles,
+ category: 'time_series',
+ color: BB.amberLight,
+ minDataPoints: 10,
+ helpText: 'Exponential smoothing methods for quick predictions.'
+ },
+ {
+ id: 'supervised_learning',
+ name: 'ML Prediction',
+ shortName: 'ML',
+ description: 'Machine learning models',
+ icon: Brain,
+ category: 'machine_learning',
+ color: BB.blueLight,
+ minDataPoints: 30,
+ helpText: 'Compare Ridge, Lasso, Random Forest for predictions.'
+ },
+ {
+ id: 'unsupervised_learning',
+ name: 'Pattern Discovery',
+ shortName: 'PCA/K',
+ description: 'PCA and clustering',
+ icon: Target,
+ category: 'machine_learning',
+ color: BB.amber,
+ minDataPoints: 20,
+ helpText: 'Find hidden patterns with PCA and K-Means clustering.'
+ },
+ {
+ id: 'resampling_methods',
+ name: 'Bootstrap',
+ shortName: 'BOOT',
+ description: 'Confidence intervals',
+ icon: Shuffle,
+ category: 'sampling',
+ color: BB.green,
+ minDataPoints: 10,
+ helpText: 'Bootstrap/jackknife for robust statistical inference.'
+ },
+ {
+ id: 'validate_data',
+ name: 'Data Quality',
+ shortName: 'QA',
+ description: 'Validate data quality',
+ icon: CheckCircle2,
+ category: 'data_quality',
+ color: BB.greenDim,
+ minDataPoints: 5,
+ helpText: 'Check for missing values, outliers, and data issues.'
+ },
+];
+
+export function CFAQuantPanel() {
+ const [currentStep, setCurrentStep] = useState<Step>('data');
+ const [isLoading, setIsLoading] = useState(false);
+ const [error, setError] = useState<string | null>(null);
+ const [analysisResult, setAnalysisResult] = useState<QuantAnalyticsResult<Record<string, unknown>> | null>(null);
+
+ const [dataSourceType, setDataSourceType] = useState<DataSourceType>('symbol');
+ const [symbolInput, setSymbolInput] = useState('AAPL');
+ const [historicalDays, setHistoricalDays] = useState(365);
+ const [manualData, setManualData] = useState('');
+ const [priceData, setPriceData] = useState<number[]>([]);
+ const [priceDates, setPriceDates] = useState<string[]>([]);
+ const [priceDataLoading, setPriceDataLoading] = useState(false);
+ const [dataStats, setDataStats] = useState<{ min: number; max: number; mean: number; std: number; change: number; changePercent: number } | null>(null);
+
+ const [selectedAnalysis, setSelectedAnalysis] = useState<string>('trend_analysis');
+ const [showHelp, setShowHelp] = useState<string | null>(null);
+
+ // Zoom state for charts
+ const [dataChartZoom, setDataChartZoom] = useState<{ startIndex: number; endIndex: number } | null>(null);
+ const [resultsChartZoom, setResultsChartZoom] = useState<{ startIndex: number; endIndex: number } | null>(null);
+ const [refAreaLeft, setRefAreaLeft] = useState<number | null>(null);
+ const [refAreaRight, setRefAreaRight] = useState<number | null>(null);
+ const [isSelecting, setIsSelecting] = useState(false);
+ const [activeChart, setActiveChart] = useState<'data' | 'results' | null>(null);
+
+ // Zoom helper functions
+ const handleZoomIn = useCallback((chartType: 'data' | 'results') => {
+ const currentZoom = chartType === 'data' ? dataChartZoom : resultsChartZoom;
+ const setZoom = chartType === 'data' ? setDataChartZoom : setResultsChartZoom;
+ const dataLength = chartType === 'results' && analysisResult ?
+ (getResultChartDataLength()) : priceData.length;
+
+ if (dataLength <= 5) return;
+
+ const currentStart = currentZoom?.startIndex ?? 0;
+ const currentEnd = currentZoom?.endIndex ?? dataLength - 1;
+ const range = currentEnd - currentStart;
+ const center = Math.floor((currentStart + currentEnd) / 2);
+ const newRange = Math.max(5, Math.floor(range * 0.7));
+ const newStart = Math.max(0, center - Math.floor(newRange / 2));
+ const newEnd = Math.min(dataLength - 1, newStart + newRange);
+
+ setZoom({ startIndex: newStart, endIndex: newEnd });
+ }, [priceData.length, dataChartZoom, resultsChartZoom, analysisResult]);
+
+ const handleZoomOut = useCallback((chartType: 'data' | 'results') => {
+ const currentZoom = chartType === 'data' ? dataChartZoom : resultsChartZoom;
+ const setZoom = chartType === 'data' ? setDataChartZoom : setResultsChartZoom;
+ const dataLength = chartType === 'results' && analysisResult ?
+ (getResultChartDataLength()) : priceData.length;
+
+ if (!currentZoom) return;
+
+ const currentStart = currentZoom.startIndex;
+ const currentEnd = currentZoom.endIndex;
+ const range = currentEnd - currentStart;
+ const center = Math.floor((currentStart + currentEnd) / 2);
+ const newRange = Math.min(dataLength - 1, Math.floor(range * 1.5));
+ const newStart = Math.max(0, center - Math.floor(newRange / 2));
+ const newEnd = Math.min(dataLength - 1, newStart + newRange);
+
+ if (newStart === 0 && newEnd === dataLength - 1) {
+ setZoom(null);
+ } else {
+ setZoom({ startIndex: newStart, endIndex: newEnd });
+ }
+ }, [priceData.length, dataChartZoom, resultsChartZoom, analysisResult]);
+
+ const handleResetZoom = useCallback((chartType: 'data' | 'results') => {
+ const setZoom = chartType === 'data' ? setDataChartZoom : setResultsChartZoom;
+ setZoom(null);
+ }, []);
+
+ // Pan navigation functions
+ const handlePanLeft = useCallback((chartType: 'data' | 'results') => {
+ const currentZoom = chartType === 'data' ? dataChartZoom : resultsChartZoom;
+ const setZoom = chartType === 'data' ? setDataChartZoom : setResultsChartZoom;
+
+ if (!currentZoom) return;
+
+ const range = currentZoom.endIndex - currentZoom.startIndex;
+ const step = Math.max(1, Math.floor(range * 0.2)); // Pan by 20% of visible range
+ const newStart = Math.max(0, currentZoom.startIndex - step);
+ const newEnd = newStart + range;
+
+ setZoom({ startIndex: newStart, endIndex: newEnd });
+ }, [dataChartZoom, resultsChartZoom]);
+
+ const handlePanRight = useCallback((chartType: 'data' | 'results') => {
+ const currentZoom = chartType === 'data' ? dataChartZoom : resultsChartZoom;
+ const setZoom = chartType === 'data' ? setDataChartZoom : setResultsChartZoom;
+ const dataLength = chartType === 'results' && analysisResult ?
+ getResultChartDataLength() : priceData.length;
+
+ if (!currentZoom) return;
+
+ const range = currentZoom.endIndex - currentZoom.startIndex;
+ const step = Math.max(1, Math.floor(range * 0.2)); // Pan by 20% of visible range
+ const newEnd = Math.min(dataLength - 1, currentZoom.endIndex + step);
+ const newStart = newEnd - range;
+
+ setZoom({ startIndex: Math.max(0, newStart), endIndex: newEnd });
+ }, [dataChartZoom, resultsChartZoom, priceData.length, analysisResult]);
+
+ // Check if we can pan in each direction
+ const canPanLeft = useCallback((chartType: 'data' | 'results') => {
+ const currentZoom = chartType === 'data' ? dataChartZoom : resultsChartZoom;
+ return currentZoom !== null && currentZoom.startIndex > 0;
+ }, [dataChartZoom, resultsChartZoom]);
+
+ const canPanRight = useCallback((chartType: 'data' | 'results') => {
+ const currentZoom = chartType === 'data' ? dataChartZoom : resultsChartZoom;
+ const dataLength = chartType === 'results' && analysisResult ?
+ getResultChartDataLength() : priceData.length;
+ return currentZoom !== null && currentZoom.endIndex < dataLength - 1;
+ }, [dataChartZoom, resultsChartZoom, priceData.length, analysisResult]);
+
+ const getResultChartDataLength = useCallback(() => {
+ if (!analysisResult?.result) return priceData.length;
+ if (selectedAnalysis === 'forecasting') {
+ const forecasts = (analysisResult.result as Record<string, unknown>).value as number[];
+ return priceData.length + (forecasts?.length || 0);
+ }
+ if (selectedAnalysis === 'trend_analysis') {
+ // Account for 5-period projection
+ return priceData.length + 5;
+ }
+ if (selectedAnalysis === 'arima_model') {
+ // Account for forecast projection (5 periods default or actual forecasts length)
+ const result = analysisResult.result as Record<string, unknown>;
+ const value = result.value as Record<string, unknown>;
+ const forecasts = value?.forecasts as number[];
+ return priceData.length + (forecasts?.length || 5);
+ }
+ return priceData.length;
+ }, [analysisResult, priceData.length, selectedAnalysis]);
+
+ // Mouse selection handlers for drag zoom
+ const handleMouseDown = useCallback((e: { activeLabel?: number }, chartType: 'data' | 'results') => {
+ if (e.activeLabel !== undefined) {
+ setRefAreaLeft(e.activeLabel);
+ setIsSelecting(true);
+ setActiveChart(chartType);
+ }
+ }, []);
+
+ const handleMouseMove = useCallback((e: { activeLabel?: number }) => {
+ if (isSelecting && e.activeLabel !== undefined) {
+ setRefAreaRight(e.activeLabel);
+ }
+ }, [isSelecting]);
+
+ const handleMouseUp = useCallback(() => {
+ if (refAreaLeft !== null && refAreaRight !== null && activeChart) {
+ const left = Math.min(refAreaLeft, refAreaRight);
+ const right = Math.max(refAreaLeft, refAreaRight);
+
+ if (right - left >= 2) {
+ const setZoom = activeChart === 'data' ? setDataChartZoom : setResultsChartZoom;
+ setZoom({ startIndex: left, endIndex: right });
+ }
+ }
+ setRefAreaLeft(null);
+ setRefAreaRight(null);
+ setIsSelecting(false);
+ setActiveChart(null);
+ }, [refAreaLeft, refAreaRight, activeChart]);
+
+ // Mouse wheel handler for zoom and horizontal scroll
+ const handleWheel = useCallback((e: React.WheelEvent, chartType: 'data' | 'results') => {
+ e.preventDefault();
+
+ // Shift + wheel = horizontal pan
+ if (e.shiftKey) {
+ if (e.deltaY > 0) {
+ handlePanRight(chartType);
+ } else if (e.deltaY < 0) {
+ handlePanLeft(chartType);
+ }
+ return;
+ }
+
+ // Ctrl + wheel = zoom in/out
+ if (e.ctrlKey || e.metaKey) {
+ if (e.deltaY < 0) {
+ handleZoomIn(chartType);
+ } else if (e.deltaY > 0) {
+ handleZoomOut(chartType);
+ }
+ return;
+ }
+
+ // Plain wheel = horizontal pan (more intuitive for time series)
+ if (e.deltaY > 0) {
+ handlePanRight(chartType);
+ } else if (e.deltaY < 0) {
+ handlePanLeft(chartType);
+ }
+ }, [handlePanLeft, handlePanRight, handleZoomIn, handleZoomOut]);
+
+ const calculateStats = useCallback((data: number[]) => {
+ if (data.length === 0) return null;
+ const mean = data.reduce((a, b) => a + b, 0) / data.length;
+ const variance = data.reduce((a, b) => a + Math.pow(b - mean, 2), 0) / data.length;
+ const first = data[0];
+ const last = data[data.length - 1];
+ const change = last - first;
+ const changePercent = (change / first) * 100;
+ return {
+ min: Math.min(...data),
+ max: Math.max(...data),
+ mean,
+ std: Math.sqrt(variance),
+ change,
+ changePercent,
+ };
+ }, []);
+
+ const chartData = useMemo(() => {
+ return priceData.map((value, index) => ({
+ index,
+ value,
+ date: priceDates[index] || `Day ${index + 1}`,
+ }));
+ }, [priceData, priceDates]);
+
+ // Helper to get date for a given index
+ const getDateForIndex = useCallback((index: number): string => {
+ if (index >= 0 && index < priceDates.length) {
+ return priceDates[index];
+ }
+ return `Day ${index + 1}`;
+ }, [priceDates]);
+
+ // Zoomed chart data for data step
+ const zoomedChartData = useMemo(() => {
+ if (!dataChartZoom) return chartData;
+ return chartData.slice(dataChartZoom.startIndex, dataChartZoom.endIndex + 1);
+ }, [chartData, dataChartZoom]);
+
+ // Zoom controls component with navigation
+ const ZoomControls = ({ chartType, isZoomed }: { chartType: 'data' | 'results'; isZoomed: boolean }) => {
+ const canGoLeft = canPanLeft(chartType);
+ const canGoRight = canPanRight(chartType);
+
+ return (
+ <div className="flex items-center gap-1">
+ {/* Pan Left */}
+ <button
+ onClick={() => handlePanLeft(chartType)}
+ disabled={!canGoLeft}
+ className="p-1.5 transition-colors hover:opacity-80"
+ style={{
+ backgroundColor: BB.cardBg,
+ border: `1px solid ${BB.borderDark}`,
+ opacity: canGoLeft ? 1 : 0.4,
+ cursor: canGoLeft ? 'pointer' : 'not-allowed'
+ }}
+ title="Pan Left"
+ >
+ <ChevronLeft size={14} style={{ color: canGoLeft ? BB.amber : BB.textSecondary }} />
+ </button>
+ {/* Zoom In */}
+ <button
+ onClick={() => handleZoomIn(chartType)}
+ className="p-1.5 transition-colors hover:opacity-80"
+ style={{ backgroundColor: BB.cardBg, border: `1px solid ${BB.borderDark}` }}
+ title="Zoom In"
+ >
+ <ZoomIn size={14} style={{ color: BB.textSecondary }} />
+ </button>
+ {/* Zoom Out */}
+ <button
+ onClick={() => handleZoomOut(chartType)}
+ disabled={!isZoomed}
+ className="p-1.5 transition-colors hover:opacity-80"
+ style={{
+ backgroundColor: BB.cardBg,
+ border: `1px solid ${BB.borderDark}`,
+ opacity: isZoomed ? 1 : 0.4,
+ cursor: isZoomed ? 'pointer' : 'not-allowed'
+ }}
+ title="Zoom Out"
+ >
+ <ZoomOut size={14} style={{ color: BB.textSecondary }} />
+ </button>
+ {/* Reset */}
+ <button
+ onClick={() => handleResetZoom(chartType)}
+ disabled={!isZoomed}
+ className="p-1.5 transition-colors hover:opacity-80"
+ style={{
+ backgroundColor: BB.cardBg,
+ border: `1px solid ${BB.borderDark}`,
+ opacity: isZoomed ? 1 : 0.4,
+ cursor: isZoomed ? 'pointer' : 'not-allowed'
+ }}
+ title="Reset Zoom"
+ >
+ <RotateCcw size={14} style={{ color: BB.textSecondary }} />
+ </button>
+ {/* Pan Right */}
+ <button
+ onClick={() => handlePanRight(chartType)}
+ disabled={!canGoRight}
+ className="p-1.5 transition-colors hover:opacity-80"
+ style={{
+ backgroundColor: BB.cardBg,
+ border: `1px solid ${BB.borderDark}`,
+ opacity: canGoRight ? 1 : 0.4,
+ cursor: canGoRight ? 'pointer' : 'not-allowed'
+ }}
+ title="Pan Right"
+ >
+ <ChevronRight size={14} style={{ color: canGoRight ? BB.amber : BB.textSecondary }} />
+ </button>
+ </div>
+ );
+ };
+
+ const fetchSymbolData = useCallback(async () => {
+ if (!symbolInput.trim()) return;
+
+ setPriceDataLoading(true);
+ setError(null);
+
+ try {
+ const endDate = new Date();
+ const startDate = new Date();
+ startDate.setDate(startDate.getDate() - historicalDays);
+
+ const data = await yfinanceService.getHistoricalData(
+ symbolInput.toUpperCase(),
+ startDate.toISOString().split('T')[0],
+ endDate.toISOString().split('T')[0]
+ );
+
+ if (data && data.length > 0) {
+ const closes = data.map(d => d.close);
+ const dates = data.map(d => {
+ const date = new Date(d.timestamp);
+ return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: '2-digit' });
+ });
+ setPriceData(closes);
+ setPriceDates(dates);
+ setDataStats(calculateStats(closes));
+ setDataChartZoom(null); // Reset zoom when new data loads
+ } else {
+ setError('No data returned for symbol');
+ }
+ } catch (err) {
+ setError(`Failed to fetch data: ${err}`);
+ } finally {
+ setPriceDataLoading(false);
+ }
+ }, [symbolInput, historicalDays, calculateStats]);
+
+ useEffect(() => {
+ if (dataSourceType === 'manual' && manualData) {
+ const parsed = manualData.split(',').map(v => parseFloat(v.trim())).filter(v => !isNaN(v));
+ setPriceData(parsed);
+ setDataStats(calculateStats(parsed));
+ }
+ }, [manualData, dataSourceType, calculateStats]);
+
+ const getDataArray = (): number[] => {
+ if (dataSourceType === 'manual') {
+ return manualData.split(',').map(v => parseFloat(v.trim())).filter(v => !isNaN(v));
+ }
+ return priceData;
+ };
+
+ const runAnalysis = async () => {
+ setIsLoading(true);
+ setError(null);
+ setAnalysisResult(null);
+
+ try {
+ const data = getDataArray();
+ const config = analysisConfigs.find(a => a.id === selectedAnalysis);
+
+ if (data.length === 0) {
+ setError('No data available');
+ setIsLoading(false);
+ return;
+ }
+
+ if (config && data.length < config.minDataPoints) {
+ setError(`Need ${config.minDataPoints} data points (have ${data.length})`);
+ setIsLoading(false);
+ return;
+ }
+
+ const params: Record<string, unknown> = { data };
+ const result = await quantAnalyticsService.executeCustomCommand(selectedAnalysis, params);
+ setAnalysisResult(result as QuantAnalyticsResult<Record<string, unknown>>);
+
+ if (result.success) {
+ setCurrentStep('results');
+ } else if (result.error) {
+ setError(result.error);
+ }
+ } catch (err) {
+ setError(`Analysis failed: ${err}`);
+ } finally {
+ setIsLoading(false);
+ }
+ };
+
+ const exportResults = () => {
+ if (!analysisResult) return;
+ const content = JSON.stringify(analysisResult, null, 2);
+ const blob = new Blob([content], { type: 'application/json' });
+ const url = URL.createObjectURL(blob);
+ const a = document.createElement('a');
+ a.href = url;
+ a.download = `${selectedAnalysis}_${symbolInput}_results.json`;
+ a.click();
+ URL.revokeObjectURL(url);
+ };
+
+ const copyToClipboard = (text: string) => {
+ navigator.clipboard.writeText(text);
+ };
+
+ const formatNumber = (val: number, decimals = 4): string => {
+ if (val === null || val === undefined || isNaN(val)) return '—';
+ return val.toFixed(decimals);
+ };
+
+ const formatPrice = (val: number): string => {
+ if (val === null || val === undefined || isNaN(val)) return '—';
+ return val.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
+ };
+
+ // Bloomberg-style header
+ const renderHeader = () => (
+ <div className="flex items-center justify-between px-4 py-2" style={{ backgroundColor: BB.black, borderBottom: `1px solid ${BB.borderDark}` }}>
+ <div className="flex items-center gap-4">
+ <div className="flex items-center gap-2">
+ <Activity size={18} style={{ color: BB.amber }} />
+ <span className="text-sm font-bold tracking-wider" style={{ color: BB.textAmber }}>CFA QUANT ANALYTICS</span>
+ </div>
+ <div className="h-4 w-px" style={{ backgroundColor: BB.borderLight }} />
+ <div className="flex items-center gap-2">
+ {(['data', 'analysis', 'results'] as Step[]).map((step, idx) => {
+ const isActive = currentStep === step;
+ const isPast = ['data', 'analysis', 'results'].indexOf(currentStep) > idx;
+ const labels = { data: '1 DATA', analysis: '2 MODEL', results: '3 OUTPUT' };
+ return (
+ <React.Fragment key={step}>
+ <button
+ onClick={() => {
+ if (step === 'results' && !analysisResult) return;
+ setCurrentStep(step);
+ }}
+ className="px-3 py-1 text-xs font-mono transition-colors"
+ style={{
+ backgroundColor: isActive ? BB.amber : 'transparent',
+ color: isActive ? BB.black : isPast ? BB.textAmber : BB.textMuted,
+ cursor: step === 'results' && !analysisResult ? 'not-allowed' : 'pointer',
+ }}
+ >
+ {labels[step]}
+ </button>
+ {idx < 2 && <span style={{ color: BB.textMuted }}>›</span>}
+ </React.Fragment>
+ );
+ })}
+ </div>
+ </div>
+ {dataSourceType === 'symbol' && symbolInput && (
+ <div className="flex items-center gap-3">
+ <span className="text-sm font-mono font-bold" style={{ color: BB.textPrimary }}>{symbolInput.toUpperCase()}</span>
+ {dataStats && (
+ <>
+ <span className="text-sm font-mono" style={{ color: BB.textSecondary }}>{formatPrice(dataStats.mean)}</span>
+ <span className="text-sm font-mono" style={{ color: dataStats.changePercent >= 0 ? BB.green : BB.red }}>
+ {dataStats.changePercent >= 0 ? '▲' : '▼'} {Math.abs(dataStats.changePercent).toFixed(2)}%
+ </span>
+ </>
+ )}
+ </div>
+ )}
+ </div>
+ );
+
+ // Data Step - Bloomberg style
+ const renderDataStep = () => (
+ <div className="flex h-full">
+ {/* Left Panel - Input Controls */}
+ <div className="w-80 flex-shrink-0 p-4 overflow-y-auto" style={{ backgroundColor: BB.panelBg, borderRight: `1px solid ${BB.borderDark}` }}>
+ <div className="mb-4">
+ <div className="text-xs font-mono mb-2" style={{ color: BB.textAmber }}>DATA SOURCE</div>
+ <div className="flex gap-2">
+ <button
+ onClick={() => setDataSourceType('symbol')}
+ className="flex-1 px-3 py-2 text-xs font-mono transition-colors"
+ style={{
+ backgroundColor: dataSourceType === 'symbol' ? BB.amber : BB.cardBg,
+ color: dataSourceType === 'symbol' ? BB.black : BB.textSecondary,
+ border: `1px solid ${dataSourceType === 'symbol' ? BB.amber : BB.borderDark}`,
+ }}
+ >
+ <Database size={14} className="inline mr-2" />
+ MARKET
+ </button>
+ <button
+ onClick={() => setDataSourceType('manual')}
+ className="flex-1 px-3 py-2 text-xs font-mono transition-colors"
+ style={{
+ backgroundColor: dataSourceType === 'manual' ? BB.amber : BB.cardBg,
+ color: dataSourceType === 'manual' ? BB.black : BB.textSecondary,
+ border: `1px solid ${dataSourceType === 'manual' ? BB.amber : BB.borderDark}`,
+ }}
+ >
+ <Calculator size={14} className="inline mr-2" />
+ MANUAL
+ </button>
+ </div>
+ </div>
+
+ {dataSourceType === 'symbol' ? (
+ <div className="space-y-4">
+ <div>
+ <label className="block text-xs font-mono mb-1" style={{ color: BB.textMuted }}>TICKER</label>
+ <input
+ type="text"
+ value={symbolInput}
+ onChange={(e) => setSymbolInput(e.target.value.toUpperCase())}
+ placeholder="AAPL"
+ className="w-full px-3 py-2 text-sm font-mono"
+ style={{
+ backgroundColor: BB.black,
+ color: BB.textPrimary,
+ border: `1px solid ${BB.borderLight}`,
+ }}
+ />
+ </div>
+ <div>
+ <label className="block text-xs font-mono mb-1" style={{ color: BB.textMuted }}>LOOKBACK (DAYS)</label>
+ <input
+ type="number"
+ value={historicalDays}
+ onChange={(e) => setHistoricalDays(parseInt(e.target.value) || 365)}
+ className="w-full px-3 py-2 text-sm font-mono"
+ style={{
+ backgroundColor: BB.black,
+ color: BB.textPrimary,
+ border: `1px solid ${BB.borderLight}`,
+ }}
+ />
+ </div>
+ <button
+ onClick={fetchSymbolData}
+ disabled={priceDataLoading}
+ className="w-full py-2 text-sm font-mono font-bold flex items-center justify-center gap-2"
+ style={{
+ backgroundColor: BB.amber,
+ color: BB.black,
+ opacity: priceDataLoading ? 0.7 : 1,
+ }}
+ >
+ {priceDataLoading ? (
+ <>
+ <RefreshCw size={14} className="animate-spin" />
+ LOADING...
+ </>
+ ) : (
+ <>
+ <Search size={14} />
+ FETCH DATA
+ </>
+ )}
+ </button>
+ </div>
+ ) : (
+ <div>
+ <label className="block text-xs font-mono mb-1" style={{ color: BB.textMuted }}>VALUES (COMMA SEP)</label>
+ <textarea
+ value={manualData}
+ onChange={(e) => setManualData(e.target.value)}
+ placeholder="100, 102, 98, 105..."
+ rows={8}
+ className="w-full px-3 py-2 text-sm font-mono"
+ style={{
+ backgroundColor: BB.black,
+ color: BB.textPrimary,
+ border: `1px solid ${BB.borderLight}`,
+ }}
+ />
+ </div>
+ )}
+
+ {/* Statistics Panel */}
+ {priceData.length > 0 && dataStats && (
+ <div className="mt-4 p-3" style={{ backgroundColor: BB.cardBg, border: `1px solid ${BB.borderDark}` }}>
+ <div className="text-xs font-mono mb-3" style={{ color: BB.textAmber }}>STATISTICS</div>
+ <div className="grid grid-cols-2 gap-2 text-xs font-mono">
+ <div>
+ <div style={{ color: BB.textMuted }}>COUNT</div>
+ <div style={{ color: BB.textPrimary }}>{priceData.length}</div>
+ </div>
+ <div>
+ <div style={{ color: BB.textMuted }}>MEAN</div>
+ <div style={{ color: BB.textPrimary }}>{formatPrice(dataStats.mean)}</div>
+ </div>
+ <div>
+ <div style={{ color: BB.textMuted }}>LOW</div>
+ <div style={{ color: BB.red }}>{formatPrice(dataStats.min)}</div>
+ </div>
+ <div>
+ <div style={{ color: BB.textMuted }}>HIGH</div>
+ <div style={{ color: BB.green }}>{formatPrice(dataStats.max)}</div>
+ </div>
+ <div>
+ <div style={{ color: BB.textMuted }}>STD DEV</div>
+ <div style={{ color: BB.textPrimary }}>{formatNumber(dataStats.std, 2)}</div>
+ </div>
+ <div>
+ <div style={{ color: BB.textMuted }}>CHANGE</div>
+ <div style={{ color: dataStats.changePercent >= 0 ? BB.green : BB.red }}>
+ {dataStats.changePercent >= 0 ? '+' : ''}{dataStats.changePercent.toFixed(2)}%
+ </div>
+ </div>
+ </div>
+ </div>
+ )}
+
+ {error && (
+ <div className="mt-4 p-3 text-xs font-mono" style={{ backgroundColor: `${BB.red}20`, border: `1px solid ${BB.red}`, color: BB.red }}>
+ <AlertCircle size={14} className="inline mr-2" />
+ {error}
+ </div>
+ )}
+
+ <button
+ onClick={() => setCurrentStep('analysis')}
+ disabled={priceData.length === 0}
+ className="w-full mt-4 py-2 text-sm font-mono font-bold flex items-center justify-center gap-2"
+ style={{
+ backgroundColor: priceData.length > 0 ? BB.green : BB.cardBg,
+ color: priceData.length > 0 ? BB.black : BB.textMuted,
+ cursor: priceData.length > 0 ? 'pointer' : 'not-allowed',
+ }}
+ >
+ CONTINUE <ChevronRight size={14} />
+ </button>
+ </div>
+
+ {/* Right Panel - Chart */}
+ <div className="flex-1 p-4" style={{ backgroundColor: BB.darkBg }}>
+ {priceData.length > 0 ? (
+ <div className="h-full flex flex-col">
+ <div className="flex items-center justify-between mb-2">
+ <div className="flex items-center gap-3">
+ <div className="text-xs font-mono" style={{ color: BB.textAmber }}>PRICE CHART</div>
+ {dataChartZoom && (
+ <div className="text-xs font-mono px-2 py-0.5" style={{ backgroundColor: BB.amber + '20', color: BB.amber }}>
+ {getDateForIndex(dataChartZoom.startIndex)} - {getDateForIndex(dataChartZoom.endIndex)}
+ </div>
+ )}
+ </div>
+ <div className="flex items-center gap-3">
+ <ZoomControls chartType="data" isZoomed={dataChartZoom !== null} />
+ <div className="text-xs font-mono" style={{ color: BB.textMuted }}>{priceData.length} POINTS</div>
+ </div>
+ </div>
+ <div
+ className="flex-1"
+ style={{ minHeight: 300 }}
+ onWheel={(e) => handleWheel(e, 'data')}
+ >
+ <ResponsiveContainer width="100%" height="100%">
+ <AreaChart
+ data={zoomedChartData}
+ margin={{ top: 10, right: 10, left: 0, bottom: 30 }}
+ onMouseDown={(e) => handleMouseDown(e, 'data')}
+ onMouseMove={handleMouseMove}
+ onMouseUp={handleMouseUp}
+ onMouseLeave={handleMouseUp}
+ >
+ <defs>
+ <linearGradient id="colorValue" x1="0" y1="0" x2="0" y2="1">
+ <stop offset="5%" stopColor={dataStats && dataStats.changePercent >= 0 ? BB.green : BB.red} stopOpacity={0.3}/>
+ <stop offset="95%" stopColor={dataStats && dataStats.changePercent >= 0 ? BB.green : BB.red} stopOpacity={0}/>
+ </linearGradient>
+ </defs>
+ <CartesianGrid strokeDasharray="3 3" stroke={BB.chartGrid} />
+ <XAxis
+ dataKey="date"
+ tick={{ fill: BB.textMuted, fontSize: 9 }}
+ axisLine={{ stroke: BB.borderDark }}
+ tickLine={{ stroke: BB.borderDark }}
+ interval="preserveStartEnd"
+ />
+ <YAxis
+ tick={{ fill: BB.textMuted, fontSize: 10 }}
+ axisLine={{ stroke: BB.borderDark }}
+ tickLine={{ stroke: BB.borderDark }}
+ domain={['auto', 'auto']}
+ tickFormatter={(val) => formatPrice(val)}
+ />
+ <Tooltip
+ contentStyle={{
+ backgroundColor: BB.cardBg,
+ border: `1px solid ${BB.borderLight}`,
+ borderRadius: 0,
+ }}
+ labelFormatter={(label) => `Date: ${label}`}
+ labelStyle={{ color: BB.textAmber, fontSize: 11, fontWeight: 'bold' }}
+ itemStyle={{ color: BB.textPrimary, fontSize: 12 }}
+ formatter={(value: number) => [formatPrice(value), 'Price']}
+ />
+ <ReferenceLine y={dataStats?.mean} stroke={BB.amber} strokeDasharray="5 5" />
+ <Area
+ type="monotone"
+ dataKey="value"
+ stroke={dataStats && dataStats.changePercent >= 0 ? BB.green : BB.red}
+ fillOpacity={1}
+ fill="url(#colorValue)"
+ strokeWidth={1.5}
+ />
+ {/* Brush for horizontal pan/zoom */}
+ {!dataChartZoom && (
+ <Brush
+ dataKey="index"
+ height={20}
+ stroke={BB.amber}
+ fill={BB.cardBg}
+ tickFormatter={(val) => `${val}`}
+ onChange={(e) => {
+ if (e.startIndex !== undefined && e.endIndex !== undefined && e.startIndex !== e.endIndex) {
+ setDataChartZoom({ startIndex: e.startIndex, endIndex: e.endIndex });
+ }
+ }}
+ />
+ )}
+ {/* Selection area indicator */}
+ {isSelecting && activeChart === 'data' && refAreaLeft !== null && refAreaRight !== null && (
+ <ReferenceArea
+ x1={refAreaLeft}
+ x2={refAreaRight}
+ strokeOpacity={0.3}
+ fill={BB.amber}
+ fillOpacity={0.2}
+ />
+ )}
+ </AreaChart>
+ </ResponsiveContainer>
+ </div>
+ <div className="text-xs font-mono mt-1 text-center" style={{ color: BB.textMuted }}>
+ Scroll to pan | Ctrl+Scroll to zoom | Drag to select range
+ </div>
+ </div>
+ ) : (
+ <div className="h-full flex items-center justify-center">
+ <div className="text-center">
+ <Database size={48} style={{ color: BB.textMuted, margin: '0 auto 16px' }} />
+ <div className="text-sm font-mono" style={{ color: BB.textMuted }}>NO DATA LOADED</div>
+ <div className="text-xs font-mono mt-1" style={{ color: BB.textMuted }}>Fetch market data or enter values manually</div>
+ </div>
+ </div>
+ )}
+ </div>
+ </div>
+ );
+
+ // Analysis Step - Bloomberg style
+ const renderAnalysisStep = () => {
+ const selectedConfig = analysisConfigs.find(a => a.id === selectedAnalysis);
+ const canRun = priceData.length >= (selectedConfig?.minDataPoints || 0);
+
+ return (
+ <div className="flex h-full">
+ {/* Left Panel - Analysis Selection */}
+ <div className="w-80 flex-shrink-0 p-4 overflow-y-auto" style={{ backgroundColor: BB.panelBg, borderRight: `1px solid ${BB.borderDark}` }}>
+ <div className="text-xs font-mono mb-3" style={{ color: BB.textAmber }}>SELECT MODEL</div>
+
+ <div className="space-y-1">
+ {analysisConfigs.map((config) => {
+ const isSelected = selectedAnalysis === config.id;
+ const hasEnoughData = priceData.length >= config.minDataPoints;
+ const Icon = config.icon;
+
+ return (
+ <button
+ key={config.id}
+ onClick={() => hasEnoughData && setSelectedAnalysis(config.id)}
+ className="w-full px-3 py-2 text-left flex items-center gap-3 transition-colors"
+ style={{
+ backgroundColor: isSelected ? BB.cardBg : 'transparent',
+ borderLeft: `3px solid ${isSelected ? config.color : 'transparent'}`,
+ opacity: hasEnoughData ? 1 : 0.4,
+ cursor: hasEnoughData ? 'pointer' : 'not-allowed',
+ }}
+ >
+ <Icon size={16} style={{ color: config.color }} />
+ <div className="flex-1 min-w-0">
+ <div className="text-xs font-mono font-bold" style={{ color: BB.textPrimary }}>{config.shortName}</div>
+ <div className="text-xs font-mono truncate" style={{ color: BB.textMuted }}>{config.name}</div>
+ </div>
+ {!hasEnoughData && (
+ <span className="text-xs font-mono" style={{ color: BB.red }}>
+ {config.minDataPoints}+
+ </span>
+ )}
+ </button>
+ );
+ })}
+ </div>
+
+ <div className="mt-4 flex gap-2">
+ <button
+ onClick={() => setCurrentStep('data')}
+ className="flex-1 py-2 text-xs font-mono"
+ style={{ backgroundColor: BB.cardBg, color: BB.textSecondary, border: `1px solid ${BB.borderDark}` }}
+ >
+ <ChevronLeft size={12} className="inline" /> BACK
+ </button>
+ <button
+ onClick={runAnalysis}
+ disabled={isLoading || !canRun}
+ className="flex-1 py-2 text-xs font-mono font-bold"
+ style={{
+ backgroundColor: canRun ? BB.green : BB.cardBg,
+ color: canRun ? BB.black : BB.textMuted,
+ cursor: canRun ? 'pointer' : 'not-allowed',
+ }}
+ >
+ {isLoading ? 'RUNNING...' : 'RUN'} <Zap size={12} className="inline ml-1" />
+ </button>
+ </div>
+
+ {error && (
+ <div className="mt-4 p-3 text-xs font-mono" style={{ backgroundColor: `${BB.red}20`, border: `1px solid ${BB.red}`, color: BB.red }}>
+ {error}
+ </div>
+ )}
+ </div>
+
+ {/* Right Panel - Model Details */}
+ <div className="flex-1 p-4" style={{ backgroundColor: BB.darkBg }}>
+ {selectedConfig && (
+ <div className="h-full flex flex-col">
+ <div className="mb-4 p-4" style={{ backgroundColor: BB.cardBg, border: `1px solid ${BB.borderDark}` }}>
+ <div className="flex items-center gap-3 mb-3">
+ <div className="p-2" style={{ backgroundColor: `${selectedConfig.color}20` }}>
+ <selectedConfig.icon size={24} style={{ color: selectedConfig.color }} />
+ </div>
+ <div>
+ <div className="text-sm font-mono font-bold" style={{ color: BB.textPrimary }}>{selectedConfig.name}</div>
+ <div className="text-xs font-mono" style={{ color: BB.textMuted }}>{selectedConfig.category.replace('_', ' ').toUpperCase()}</div>
+ </div>
+ </div>
+ <div className="text-xs font-mono" style={{ color: BB.textSecondary }}>{selectedConfig.helpText}</div>
+
+ <div className="mt-3 flex items-center gap-4 text-xs font-mono">
+ <div>
+ <span style={{ color: BB.textMuted }}>MIN POINTS: </span>
+ <span style={{ color: priceData.length >= selectedConfig.minDataPoints ? BB.green : BB.red }}>
+ {selectedConfig.minDataPoints}
+ </span>
+ </div>
+ <div>
+ <span style={{ color: BB.textMuted }}>YOUR DATA: </span>
+ <span style={{ color: BB.textPrimary }}>{priceData.length}</span>
+ </div>
+ </div>
+ </div>
+
+ {/* Mini Chart Preview */}
+ <div className="flex-1" style={{ minHeight: 200 }}>
+ <div className="flex items-center justify-between mb-2">
+ <div className="text-xs font-mono" style={{ color: BB.textAmber }}>DATA PREVIEW</div>
+ {dataChartZoom && (
+ <button
+ onClick={() => setDataChartZoom(null)}
+ className="text-xs font-mono px-2 py-0.5 flex items-center gap-1"
+ style={{ backgroundColor: BB.cardBg, color: BB.textMuted, border: `1px solid ${BB.borderDark}` }}
+ >
+ <RotateCcw size={10} /> RESET ZOOM
+ </button>
+ )}
+ </div>
+ <ResponsiveContainer width="100%" height="100%">
+ <RechartsLineChart data={zoomedChartData} margin={{ top: 5, right: 5, left: 0, bottom: 5 }}>
+ <CartesianGrid strokeDasharray="3 3" stroke={BB.chartGrid} />
+ <XAxis dataKey="index" hide />
+ <YAxis hide domain={['auto', 'auto']} />
+ <Line
+ type="monotone"
+ dataKey="value"
+ stroke={selectedConfig.color}
+ dot={false}
+ strokeWidth={1.5}
+ />
+ </RechartsLineChart>
+ </ResponsiveContainer>
+ </div>
+ </div>
+ )}
+ </div>
+ </div>
+ );
+ };
+
+ // Results Step - Bloomberg style
+ const renderResultsStep = () => {
+ if (!analysisResult?.result) return null;
+
+ const config = analysisConfigs.find(a => a.id === selectedAnalysis);
+ const result = analysisResult.result as Record<string, unknown>;
+
+ // Extract key metrics based on analysis type
+ const getKeyMetrics = () => {
+ const metrics: { label: string; value: string; color: string; icon?: React.ElementType }[] = [];
+
+ if (selectedAnalysis === 'trend_analysis' && result.metadata) {
+ const meta = result.metadata as Record<string, unknown>;
+ const value = result.value as Record<string, unknown>;
+ const slope = value?.slope as number;
+ const intercept = value?.intercept as number;
+ const rSquared = meta?.r_squared as number;
+ const significant = meta?.trend_significant as boolean;
+ const direction = meta?.trend_direction as string;
+ const pValue = meta?.slope_p_value as number;
+ const tStat = meta?.slope_t_statistic as number;
+
+ // Main result - direction with icon
+ metrics.push({
+ label: 'DIRECTION',
+ value: direction === 'upward' ? 'UP' : direction === 'downward' ? 'DOWN' : 'FLAT',
+ color: direction === 'upward' ? BB.green : direction === 'downward' ? BB.red : BB.textMuted,
+ icon: direction === 'upward' ? ArrowUpRight : direction === 'downward' ? ArrowDownRight : Minus,
+ });
+
+ // Statistical significance
+ metrics.push({
+ label: 'SIGNIFICANT',
+ value: significant ? 'YES' : 'NO',
+ color: significant ? BB.green : BB.textMuted,
+ icon: significant ? CheckCircle2 : undefined
+ });
+
+ // Model fit
+ metrics.push({
+ label: 'R-SQUARED',
+ value: `${(rSquared * 100).toFixed(1)}%`,
+ color: rSquared > 0.7 ? BB.green : rSquared > 0.4 ? BB.amber : BB.textMuted
+ });
+
+ // P-value with color coding
+ if (pValue !== undefined && pValue !== null) {
+ metrics.push({
+ label: 'P-VALUE',
+ value: pValue < 0.001 ? '<0.001' : formatNumber(pValue, 4),
+ color: pValue < 0.01 ? BB.green : pValue < 0.05 ? BB.greenDim : pValue < 0.1 ? BB.amber : BB.red
+ });
+ }
+
+ // T-statistic
+ if (tStat !== undefined && tStat !== null) {
+ metrics.push({
+ label: 'T-STATISTIC',
+ value: formatNumber(tStat, 2),
+ color: Math.abs(tStat) > 2 ? BB.green : BB.textMuted
+ });
+ }
+
+ // Regression parameters
+ metrics.push({ label: 'SLOPE', value: formatNumber(slope, 6), color: BB.textPrimary });
+ if (intercept !== undefined && intercept !== null) {
+ metrics.push({ label: 'INTERCEPT', value: formatNumber(intercept, 2), color: BB.textSecondary });
+ }
+ } else if (selectedAnalysis === 'stationarity_test' && result.value) {
+ const value = result.value as Record<string, unknown>;
+ const meta = result.metadata as Record<string, unknown>;
+ const isStationary = value?.is_stationary as boolean;
+ const pValue = value?.p_value as number;
+ const testStat = value?.test_statistic as number;
+ const criticalValues = value?.critical_values as Record<string, number>;
+ const nullHypothesis = value?.null_hypothesis as string;
+
+ // Main result
+ metrics.push({
+ label: 'STATIONARY',
+ value: isStationary ? 'YES' : 'NO',
+ color: isStationary ? BB.green : BB.red,
+ icon: isStationary ? CheckCircle2 : AlertCircle
+ });
+
+ // Test statistics
+ metrics.push({ label: 'TEST STAT', value: formatNumber(testStat, 4), color: BB.textPrimary });
+ metrics.push({
+ label: 'P-VALUE',
+ value: formatNumber(pValue, 4),
+ color: pValue < 0.01 ? BB.green : pValue < 0.05 ? BB.greenDim : pValue < 0.1 ? BB.amber : BB.red
+ });
+
+ // Critical values comparison
+ if (criticalValues) {
+ const cv1 = criticalValues['1%'];
+ const cv5 = criticalValues['5%'];
+ const cv10 = criticalValues['10%'];
+
+ if (cv1 !== undefined) {
+ const beats1 = testStat < cv1;
+ metrics.push({ label: 'CV 1%', value: `${formatNumber(cv1, 2)} ${beats1 ? 'PASS' : ''}`, color: beats1 ? BB.green : BB.textMuted });
+ }
+ if (cv5 !== undefined) {
+ const beats5 = testStat < cv5;
+ metrics.push({ label: 'CV 5%', value: `${formatNumber(cv5, 2)} ${beats5 ? 'PASS' : ''}`, color: beats5 ? BB.green : BB.textMuted });
+ }
+ if (cv10 !== undefined) {
+ const beats10 = testStat < cv10;
+ metrics.push({ label: 'CV 10%', value: `${formatNumber(cv10, 2)} ${beats10 ? 'PASS' : ''}`, color: beats10 ? BB.amber : BB.textMuted });
+ }
+ }
+
+ // Metadata - stability measures
+ if (meta) {
+ const rollingMeanStability = meta.rolling_mean_stability as number;
+ const rollingStdStability = meta.rolling_std_stability as number;
+ const meanReversionStrength = meta.mean_reversion_strength as number;
+
+ if (rollingMeanStability !== undefined) {
+ metrics.push({
+ label: 'MEAN STABILITY',
+ value: formatNumber(rollingMeanStability, 2),
+ color: rollingMeanStability < 1 ? BB.green : rollingMeanStability < 5 ? BB.amber : BB.red
+ });
+ }
+ if (rollingStdStability !== undefined) {
+ metrics.push({
+ label: 'VOL STABILITY',
+ value: formatNumber(rollingStdStability, 2),
+ color: rollingStdStability < 0.5 ? BB.green : rollingStdStability < 2 ? BB.amber : BB.red
+ });
+ }
+ if (meanReversionStrength !== undefined) {
+ metrics.push({
+ label: 'MEAN REVERSION',
+ value: `${(meanReversionStrength * 100).toFixed(1)}%`,
+ color: meanReversionStrength > 0.3 ? BB.green : meanReversionStrength > 0.1 ? BB.amber : BB.textMuted
+ });
+ }
+ }
+ } else if (selectedAnalysis === 'arima_model' && result.metadata) {
+ const meta = result.metadata as Record<string, unknown>;
+ const params = result.parameters as Record<string, unknown>;
+ const value = result.value as Record<string, unknown>;
+ const residuals = value?.residuals as number[];
+
+ // Model order
+ const order = params?.order as number[];
+ if (order && order.length === 3) {
+ metrics.push({
+ label: 'MODEL ORDER',
+ value: `(${order[0]},${order[1]},${order[2]})`,
+ color: BB.amber
+ });
+ }
+
+ // Seasonal order if present
+ const seasonalOrder = params?.seasonal_order as number[];
+ if (seasonalOrder && seasonalOrder.length === 4) {
+ metrics.push({
+ label: 'SEASONAL',
+ value: `(${seasonalOrder[0]},${seasonalOrder[1]},${seasonalOrder[2]},${seasonalOrder[3]})`,
+ color: BB.blueLight
+ });
+ }
+
+ // Information criteria (lower is better)
+ const aic = meta?.aic as number;
+ const bic = meta?.bic as number;
+ metrics.push({ label: 'AIC', value: formatNumber(aic, 2), color: BB.textPrimary });
+ metrics.push({ label: 'BIC', value: formatNumber(bic, 2), color: BB.textPrimary });
+
+ // Log-likelihood
+ const logLik = meta?.log_likelihood as number;
+ if (logLik !== undefined && logLik !== null) {
+ metrics.push({ label: 'LOG-LIKELIHOOD', value: formatNumber(logLik, 2), color: BB.textSecondary });
+ }
+
+ // Ljung-Box test for residual autocorrelation
+ const ljungBoxPValue = meta?.ljung_box_p_value as number;
+ const residualsAutocorrelated = meta?.residuals_autocorrelated as boolean;
+
+ metrics.push({
+ label: 'RESIDUALS OK',
+ value: !residualsAutocorrelated ? 'YES' : 'NO',
+ color: !residualsAutocorrelated ? BB.green : BB.red,
+ icon: !residualsAutocorrelated ? CheckCircle2 : AlertCircle
+ });
+
+ if (ljungBoxPValue !== undefined && ljungBoxPValue !== null) {
+ metrics.push({
+ label: 'LJUNG-BOX P',
+ value: formatNumber(ljungBoxPValue, 4),
+ color: ljungBoxPValue > 0.05 ? BB.green : ljungBoxPValue > 0.01 ? BB.amber : BB.red
+ });
+ }
+
+ // Residual statistics
+ if (residuals && residuals.length > 0) {
+ const residMean = residuals.reduce((a, b) => a + b, 0) / residuals.length;
+ const residStd = Math.sqrt(residuals.reduce((a, b) => a + Math.pow(b - residMean, 2), 0) / residuals.length);
+ metrics.push({ label: 'RESID MEAN', value: formatNumber(residMean, 4), color: Math.abs(residMean) < 0.01 ? BB.green : BB.textMuted });
+ metrics.push({ label: 'RESID STD', value: formatNumber(residStd, 4), color: BB.textSecondary });
+ }
+ } else if (selectedAnalysis === 'forecasting' && result.metadata) {
+ const meta = result.metadata as Record<string, unknown>;
+ const params = result.parameters as Record<string, unknown>;
+ const forecasts = result.value as number[];
+ const trainSize = meta?.train_data_size as number;
+ const testSize = meta?.test_data_size as number;
+ const horizon = meta?.forecast_horizon as number;
+ const method = params?.method as string;
+ const mae = meta?.mae as number;
+ const rmse = meta?.rmse as number;
+ const mape = meta?.mape as number;
+
+ // Method with icon
+ if (method) {
+ const methodLabels: Record<string, string> = {
+ 'simple_exponential': 'EXP SMOOTH',
+ 'linear_trend': 'LINEAR TREND',
+ 'moving_average': 'MOVING AVG'
+ };
+ const methodDescriptions: Record<string, string> = {
+ 'simple_exponential': 'Level',
+ 'linear_trend': 'Trend',
+ 'moving_average': 'Average'
+ };
+ metrics.push({
+ label: 'METHOD',
+ value: methodLabels[method] || method.toUpperCase(),
+ color: BB.amber,
+ icon: method === 'linear_trend' ? TrendingUp : method === 'simple_exponential' ? Sparkles : Activity
+ });
+ metrics.push({ label: 'MODEL TYPE', value: methodDescriptions[method] || 'Custom', color: BB.textSecondary });
+ }
+
+ // Forecast accuracy rating based on MAPE
+ if (mape !== undefined && mape !== null) {
+ let accuracy = 'EXCELLENT';
+ let accuracyColor = BB.green;
+ if (mape > 30) { accuracy = 'POOR'; accuracyColor = BB.red; }
+ else if (mape > 20) { accuracy = 'FAIR'; accuracyColor = BB.amber; }
+ else if (mape > 10) { accuracy = 'GOOD'; accuracyColor = BB.greenDim; }
+ metrics.push({ label: 'ACCURACY', value: accuracy, color: accuracyColor, icon: mape <= 10 ? CheckCircle2 : mape <= 20 ? AlertCircle : undefined });
+ }
+
+ // Forecast value info
+ if (forecasts && forecasts.length > 0) {
+ const forecastValue = forecasts[0];
+ metrics.push({ label: 'FORECAST VALUE', value: formatPrice(forecastValue), color: BB.green });
+
+ // If we have test data, show comparison
+ if (testSize > 0 && priceData.length > 0) {
+ const testStartIdx = trainSize;
+ const lastActual = priceData[priceData.length - 1];
+ const diff = forecastValue - lastActual;
+ const diffPct = (diff / lastActual) * 100;
+ metrics.push({
+ label: 'VS LAST ACTUAL',
+ value: `${diffPct >= 0 ? '+' : ''}${diffPct.toFixed(2)}%`,
+ color: diffPct >= 0 ? BB.green : BB.red,
+ icon: diffPct >= 0 ? ArrowUpRight : ArrowDownRight
+ });
+ }
+ }
+
+ // Data split info with visual representation
+ if (trainSize && testSize) {
+ const trainPct = ((trainSize / (trainSize + testSize)) * 100).toFixed(0);
+ metrics.push({ label: 'TRAIN/TEST', value: `${trainPct}/${100 - parseInt(trainPct)}%`, color: BB.blueLight });
+ }
+ if (trainSize) metrics.push({ label: 'TRAIN SIZE', value: String(trainSize), color: BB.textSecondary });
+ if (testSize) metrics.push({ label: 'TEST SIZE', value: String(testSize), color: BB.textSecondary });
+ if (horizon) metrics.push({ label: 'HORIZON', value: `${horizon} periods`, color: BB.green });
+
+ // Accuracy metrics with color coding
+ if (mae !== undefined && mae !== null) {
+ metrics.push({ label: 'MAE', value: formatNumber(mae, 2), color: BB.textPrimary });
+ }
+ if (rmse !== undefined && rmse !== null) {
+ metrics.push({ label: 'RMSE', value: formatNumber(rmse, 2), color: BB.textPrimary });
+ // RMSE to MAE ratio (closer to 1 means less outliers)
+ if (mae !== undefined && mae !== null && mae > 0) {
+ const rmseToMae = rmse / mae;
+ metrics.push({
+ label: 'RMSE/MAE',
+ value: formatNumber(rmseToMae, 2),
+ color: rmseToMae < 1.2 ? BB.green : rmseToMae < 1.5 ? BB.amber : BB.red
+ });
+ }
+ }
+ if (mape !== undefined && mape !== null) {
+ metrics.push({
+ label: 'MAPE',
+ value: `${formatNumber(mape, 2)}%`,
+ color: mape < 10 ? BB.green : mape < 20 ? BB.amber : BB.red
+ });
+ }
+ } else if (selectedAnalysis === 'supervised_learning' && result.value) {
+ const meta = result.metadata as Record<string, unknown>;
+ const value = result.value as Record<string, Record<string, unknown>>;
+ const bestAlgo = meta?.best_algorithm as string;
+ const nFeatures = meta?.n_features as number;
+ const yTest = meta?.y_test as number[];
+
+ // Best model highlight
+ if (bestAlgo) {
+ metrics.push({ label: 'BEST MODEL', value: bestAlgo.toUpperCase().replace('_', ' '), color: BB.green, icon: CheckCircle2 });
+ }
+
+ // Best model detailed metrics
+ if (bestAlgo && value[bestAlgo]) {
+ const bestResult = value[bestAlgo];
+ if (bestResult.r2_score !== undefined) {
+ const r2 = bestResult.r2_score as number;
+ const r2Quality = r2 > 0.8 ? 'Excellent' : r2 > 0.6 ? 'Good' : r2 > 0.4 ? 'Fair' : 'Poor';
+ metrics.push({ label: 'R² SCORE', value: `${(r2 * 100).toFixed(1)}%`, color: r2 > 0.7 ? BB.green : r2 > 0.5 ? BB.amber : BB.red });
+ metrics.push({ label: 'FIT QUALITY', value: r2Quality.toUpperCase(), color: r2 > 0.7 ? BB.green : r2 > 0.5 ? BB.amber : BB.red });
+ }
+ if (bestResult.rmse !== undefined) {
+ metrics.push({ label: 'RMSE', value: formatNumber(bestResult.rmse as number, 4), color: BB.textPrimary });
+ }
+ if (bestResult.mse !== undefined) {
+ metrics.push({ label: 'MSE', value: formatNumber(bestResult.mse as number, 4), color: BB.textSecondary });
+ }
+ }
+
+ // Compare all algorithms
+ const algoNames = Object.keys(value).filter(k => !value[k].error);
+ if (algoNames.length > 1) {
+ metrics.push({ label: 'MODELS TESTED', value: String(algoNames.length), color: BB.blueLight });
+
+ // Show comparison of R² scores
+ algoNames.forEach(algo => {
+ if (algo !== bestAlgo && value[algo]?.r2_score !== undefined) {
+ const r2 = value[algo].r2_score as number;
+ metrics.push({
+ label: algo.toUpperCase().replace('_', ' ').substring(0, 12),
+ value: `R²: ${(r2 * 100).toFixed(1)}%`,
+ color: BB.textMuted
+ });
+ }
+ });
+ }
+
+ // Data split info
+ if (meta?.train_size) {
+ metrics.push({ label: 'TRAIN SIZE', value: String(meta.train_size), color: BB.textMuted });
+ }
+ if (meta?.test_size) {
+ metrics.push({ label: 'TEST SIZE', value: String(meta.test_size), color: BB.textMuted });
+ }
+ if (nFeatures) {
+ metrics.push({ label: 'FEATURES', value: String(nFeatures), color: BB.textMuted });
+ }
+
+ // Calculate prediction error statistics if y_test is available
+ if (yTest && bestAlgo && value[bestAlgo]?.predictions) {
+ const predictions = value[bestAlgo].predictions as number[];
+ if (predictions.length === yTest.length) {
+ const errors = yTest.map((actual, i) => actual - predictions[i]);
+ const meanError = errors.reduce((a, b) => a + b, 0) / errors.length;
+ const maxAbsError = Math.max(...errors.map(Math.abs));
+
+ metrics.push({ label: 'MEAN ERROR', value: formatNumber(meanError, 4), color: Math.abs(meanError) < 0.01 ? BB.green : BB.textMuted });
+ metrics.push({ label: 'MAX |ERROR|', value: formatNumber(maxAbsError, 4), color: BB.textSecondary });
+ }
+ }
+ } else if (selectedAnalysis === 'unsupervised_learning' && result.value) {
+ const value = result.value as Record<string, Record<string, unknown>>;
+ const meta = result.metadata as Record<string, unknown>;
+
+ if (value.pca) {
+ const pca = value.pca;
+ const explainedVariance = pca.explained_variance_ratio as number[];
+ const componentsFor95 = pca.components_for_95_variance as number;
+
+ if (explainedVariance && explainedVariance.length > 0) {
+ const firstComponentVar = (explainedVariance[0] * 100).toFixed(1);
+ const totalComponents = explainedVariance.length;
+ metrics.push({ label: 'PC1 VARIANCE', value: `${firstComponentVar}%`, color: BB.green });
+ metrics.push({ label: 'COMPONENTS (95%)', value: String(componentsFor95), color: BB.amber });
+ metrics.push({ label: 'TOTAL PCs', value: String(totalComponents), color: BB.textSecondary });
+ }
+ }
+
+ if (value.kmeans) {
+ const kmeans = value.kmeans;
+ const optimalK = kmeans.optimal_k as number;
+ const silhouetteByK = kmeans.silhouette_by_k as [number, number][];
+
+ metrics.push({ label: 'OPTIMAL K', value: String(optimalK), color: BB.green });
+
+ if (silhouetteByK && silhouetteByK.length > 0) {
+ const optimalSilhouette = silhouetteByK.find(([k]) => k === optimalK);
+ if (optimalSilhouette) {
+ const score = optimalSilhouette[1];
+ const quality = score > 0.7 ? 'STRONG' : score > 0.5 ? 'GOOD' : score > 0.25 ? 'FAIR' : 'WEAK';
+ metrics.push({ label: 'SILHOUETTE', value: formatNumber(score, 3), color: score > 0.5 ? BB.green : BB.amber });
+ metrics.push({ label: 'CLUSTERING', value: quality, color: score > 0.5 ? BB.green : BB.amber });
+ }
+ }
+
+ const clusterLabels = kmeans.cluster_labels as number[];
+ if (clusterLabels) {
+ metrics.push({ label: 'DATA POINTS', value: String(clusterLabels.length), color: BB.textSecondary });
+ }
+ }
+
+ if (meta?.n_samples) {
+ metrics.push({ label: 'SAMPLES', value: String(meta.n_samples), color: BB.textMuted });
+ }
+ if (meta?.n_features) {
+ metrics.push({ label: 'FEATURES', value: String(meta.n_features), color: BB.textMuted });
+ }
+ } else if (selectedAnalysis === 'resampling_methods' && result.value) {
+ const value = result.value as Record<string, Record<string, unknown>>;
+ const meta = result.metadata as Record<string, unknown>;
+
+ // Bootstrap metrics
+ if (value.bootstrap) {
+ const bootstrap = value.bootstrap;
+ const mean = bootstrap.mean as number;
+ const std = bootstrap.std as number;
+ const bias = bootstrap.bias as number;
+ const ci = bootstrap.confidence_interval as [number, number];
+
+ metrics.push({ label: 'BOOT MEAN', value: formatNumber(mean, 4), color: BB.green });
+ metrics.push({ label: 'BOOT STD', value: formatNumber(std, 4), color: BB.textPrimary });
+ if (bias !== undefined && bias !== null) {
+ metrics.push({ label: 'BIAS', value: formatNumber(bias, 6), color: Math.abs(bias) < 0.01 ? BB.green : BB.amber });
+ }
+ if (ci && ci.length === 2) {
+ metrics.push({ label: '95% CI LOW', value: formatNumber(ci[0], 4), color: BB.textSecondary });
+ metrics.push({ label: '95% CI HIGH', value: formatNumber(ci[1], 4), color: BB.textSecondary });
+ }
+ }
+
+ // Jackknife metrics
+ if (value.jackknife) {
+ const jackknife = value.jackknife;
+ const biasCorr = jackknife.bias_corrected_estimate as number;
+ const stdErr = jackknife.std_error as number;
+ const jackBias = jackknife.bias as number;
+
+ if (biasCorr !== undefined) {
+ metrics.push({ label: 'JACK ESTIMATE', value: formatNumber(biasCorr, 4), color: BB.amber });
+ }
+ if (stdErr !== undefined) {
+ metrics.push({ label: 'STD ERROR', value: formatNumber(stdErr, 4), color: BB.textPrimary });
+ }
+ if (jackBias !== undefined) {
+ metrics.push({ label: 'JACK BIAS', value: formatNumber(jackBias, 6), color: Math.abs(jackBias) < 0.01 ? BB.green : BB.amber });
+ }
+ }
+
+ // Permutation test metrics
+ if (value.permutation) {
+ const perm = value.permutation;
+ const pValue = perm.p_value as number;
+ const significant = perm.significant as boolean;
+
+ metrics.push({ label: 'P-VALUE', value: formatNumber(pValue, 4), color: pValue < 0.05 ? BB.green : BB.textMuted });
+ metrics.push({ label: 'SIGNIFICANT', value: significant ? 'YES' : 'NO', color: significant ? BB.green : BB.textMuted });
+ }
+
+ // Metadata
+ if (meta?.n_bootstrap) {
+ metrics.push({ label: 'ITERATIONS', value: String(meta.n_bootstrap), color: BB.textMuted });
+ }
+ if (meta?.data_size) {
+ metrics.push({ label: 'DATA SIZE', value: String(meta.data_size), color: BB.textMuted });
+ }
+ } else if (selectedAnalysis === 'validate_data' && result) {
+ // Data Quality metrics
+ const qualityScore = result.quality_score as number;
+ const issues = result.issues as { type: string; severity: string; description: string }[];
+ const warnings = result.warnings as { type: string; message: string }[];
+ const statistics = result.statistics as Record<string, unknown>;
+ const recommendations = result.recommendations as string[];
+
+ // Quality Score with color coding
+ if (qualityScore !== undefined && qualityScore !== null) {
+ let scoreColor = BB.green;
+ let scoreLabel = 'EXCELLENT';
+ if (qualityScore < 50) { scoreColor = BB.red; scoreLabel = 'POOR'; }
+ else if (qualityScore < 70) { scoreColor = BB.amber; scoreLabel = 'FAIR'; }
+ else if (qualityScore < 90) { scoreColor = BB.green; scoreLabel = 'GOOD'; }
+ metrics.push({ label: 'QUALITY SCORE', value: `${qualityScore.toFixed(0)}/100`, color: scoreColor });
+ metrics.push({ label: 'STATUS', value: scoreLabel, color: scoreColor });
+ }
+
+ // Statistics
+ if (statistics) {
+ if (statistics.total_observations !== undefined) {
+ metrics.push({ label: 'OBSERVATIONS', value: String(statistics.total_observations), color: BB.textPrimary });
+ }
+ if (statistics.missing_data_ratio !== undefined) {
+ const missingRatio = statistics.missing_data_ratio as number;
+ const missingPct = (missingRatio * 100).toFixed(1);
+ metrics.push({
+ label: 'MISSING DATA',
+ value: `${missingPct}%`,
+ color: missingRatio > 0.05 ? BB.red : missingRatio > 0.01 ? BB.amber : BB.green
+ });
+ }
+ if (statistics.mean !== undefined) {
+ metrics.push({ label: 'MEAN', value: formatNumber(statistics.mean as number, 2), color: BB.textSecondary });
+ }
+ if (statistics.std !== undefined) {
+ metrics.push({ label: 'STD DEV', value: formatNumber(statistics.std as number, 2), color: BB.textSecondary });
+ }
+ }
+
+ // Issues count by severity
+ if (issues && issues.length > 0) {
+ const criticalCount = issues.filter(i => i.severity === 'critical').length;
+ const highCount = issues.filter(i => i.severity === 'high').length;
+ const mediumCount = issues.filter(i => i.severity === 'medium').length;
+ const totalIssues = issues.length;
+
+ metrics.push({ label: 'TOTAL ISSUES', value: String(totalIssues), color: totalIssues > 0 ? BB.amber : BB.green });
+ if (criticalCount > 0) {
+ metrics.push({ label: 'CRITICAL', value: String(criticalCount), color: BB.red });
+ }
+ if (highCount > 0) {
+ metrics.push({ label: 'HIGH SEV', value: String(highCount), color: BB.red });
+ }
+ } else {
+ metrics.push({ label: 'ISSUES', value: 'NONE', color: BB.green });
+ }
+
+ // Warnings count
+ if (warnings && warnings.length > 0) {
+ metrics.push({ label: 'WARNINGS', value: String(warnings.length), color: BB.amber });
+ }
+
+ // Recommendations count
+ if (recommendations && recommendations.length > 0) {
+ metrics.push({ label: 'RECOMMENDATIONS', value: String(recommendations.length), color: BB.blueLight });
+ }
+ }
+
+ return metrics;
+ };
+
+ // Build chart data for results
+ const getResultChartData = () => {
+ if (selectedAnalysis === 'trend_analysis') {
+ const meta = result.metadata as Record<string, unknown>;
+ const value = result.value as Record<string, unknown>;
+ const fitted = meta?.fitted_values as number[];
+ const residuals = meta?.residuals as number[];
+ const slope = value?.slope as number;
+ const intercept = value?.intercept as number;
+
+ if (fitted && fitted.length > 0) {
+ // Calculate standard error for confidence bands
+ const n = priceData.length;
+ const residualsArr = residuals || priceData.map((v, i) => v - fitted[i]);
+ const mse = residualsArr.reduce((sum, r) => sum + r * r, 0) / (n - 2);
+ const se = Math.sqrt(mse);
+
+ // Mean of time index for confidence interval calculation
+ const meanX = (n - 1) / 2;
+ const sumXX = Array.from({ length: n }, (_, i) => Math.pow(i - meanX, 2)).reduce((a, b) => a + b, 0);
+
+ // Build enhanced chart data with confidence bands
+ const enhancedData = chartData.map((d, i) => {
+ // Standard error of prediction at each point
+ const sePred = se * Math.sqrt(1 + 1/n + Math.pow(i - meanX, 2) / sumXX);
+ const ciWidth = 1.96 * sePred; // 95% confidence
+
+ return {
+ ...d,
+ fitted: fitted[i],
+ residual: residualsArr[i],
+ upperBand: fitted[i] + ciWidth,
+ lowerBand: fitted[i] - ciWidth,
+ };
+ });
+
+ // Add future projection (5 periods)
+ if (slope !== undefined && intercept !== undefined) {
+ const projectionPeriods = 5;
+ for (let i = 0; i < projectionPeriods; i++) {
+ const futureIndex = n + i;
+ const projectedValue = intercept + slope * futureIndex;
+ const sePred = se * Math.sqrt(1 + 1/n + Math.pow(futureIndex - meanX, 2) / sumXX);
+ const ciWidth = 1.96 * sePred;
+
+ enhancedData.push({
+ index: futureIndex,
+ value: null as unknown as number,
+ date: `+${i + 1}d`,
+ fitted: null as unknown as number,
+ residual: null as unknown as number,
+ upperBand: null as unknown as number,
+ lowerBand: null as unknown as number,
+ projection: projectedValue,
+ projectionUpper: projectedValue + ciWidth,
+ projectionLower: projectedValue - ciWidth,
+ });
+ }
+ }
+
+ return enhancedData;
+ }
+ } else if (selectedAnalysis === 'stationarity_test') {
+ // Create rolling mean and rolling std visualization
+ const windowSize = Math.min(20, Math.floor(priceData.length / 4));
+ if (windowSize < 2) return chartData;
+
+ // Calculate rolling statistics
+ const rollingMean: (number | null)[] = [];
+ const rollingStd: (number | null)[] = [];
+ const upperBand: (number | null)[] = [];
+ const lowerBand: (number | null)[] = [];
+
+ for (let i = 0; i < priceData.length; i++) {
+ if (i < windowSize - 1) {
+ rollingMean.push(null);
+ rollingStd.push(null);
+ upperBand.push(null);
+ lowerBand.push(null);
+ } else {
+ const windowData = priceData.slice(i - windowSize + 1, i + 1);
+ const mean = windowData.reduce((a, b) => a + b, 0) / windowData.length;
+ const variance = windowData.reduce((a, b) => a + Math.pow(b - mean, 2), 0) / windowData.length;
+ const std = Math.sqrt(variance);
+
+ rollingMean.push(mean);
+ rollingStd.push(std);
+ upperBand.push(mean + 2 * std);
+ lowerBand.push(mean - 2 * std);
+ }
+ }
+
+ // Overall mean for reference
+ const overallMean = priceData.reduce((a, b) => a + b, 0) / priceData.length;
+
+ return chartData.map((d, i) => ({
+ ...d,
+ rollingMean: rollingMean[i],
+ upperBand: upperBand[i],
+ lowerBand: lowerBand[i],
+ overallMean,
+ }));
+ } else if (selectedAnalysis === 'arima_model') {
+ const value = result.value as Record<string, unknown>;
+ const meta = result.metadata as Record<string, unknown>;
+ const fitted = value?.fitted_values as number[];
+ const residuals = value?.residuals as number[];
+ const forecasts = value?.forecasts as number[];
+ const forecastCI = value?.forecast_confidence_interval as { lower: number[]; upper: number[] };
+
+ if (fitted && fitted.length > 0) {
+ const n = priceData.length;
+
+ // Calculate residual standard deviation for confidence bands
+ const residualsArr = residuals || priceData.map((v, i) => v - (fitted[i] || v));
+ const residMean = residualsArr.reduce((a, b) => a + b, 0) / residualsArr.length;
+ const residStd = Math.sqrt(residualsArr.reduce((a, b) => a + Math.pow(b - residMean, 2), 0) / residualsArr.length);
+
+ // Build enhanced chart data with fitted values, residuals, and confidence bands
+ const enhancedData = chartData.map((d, i) => {
+ const fittedVal = fitted[i];
+ const residual = residualsArr[i];
+ // 95% confidence band around fitted values
+ const ciWidth = 1.96 * residStd;
+
+ return {
+ ...d,
+ fitted: fittedVal,
+ residual: residual,
+ upperBand: fittedVal !== undefined ? fittedVal + ciWidth : null,
+ lowerBand: fittedVal !== undefined ? fittedVal - ciWidth : null,
+ // Residual bar height (scaled for visibility)
+ residualBar: residual,
+ };
+ });
+
+ // Add future forecast points if available
+ if (forecasts && forecasts.length > 0) {
+ forecasts.forEach((forecast, i) => {
+ const ciLower = forecastCI?.lower?.[i];
+ const ciUpper = forecastCI?.upper?.[i];
+ // Default CI if not provided (widening uncertainty)
+ const defaultCiWidth = 1.96 * residStd * Math.sqrt(1 + (i + 1) * 0.1);
+
+ enhancedData.push({
+ index: n + i,
+ value: null as unknown as number,
+ date: `+${i + 1}d`,
+ fitted: null as unknown as number,
+ residual: null as unknown as number,
+ upperBand: null as unknown as number,
+ lowerBand: null as unknown as number,
+ residualBar: null as unknown as number,
+ forecast: forecast,
+ forecastUpper: ciUpper ?? (forecast + defaultCiWidth),
+ forecastLower: ciLower ?? (forecast - defaultCiWidth),
+ });
+ });
+ } else {
+ // Generate simple forecast projection if not provided by backend
+ // Use last fitted value as base and project forward
+ const lastFitted = fitted[fitted.length - 1];
+ const forecastHorizon = 5;
+
+ for (let i = 0; i < forecastHorizon; i++) {
+ // Simple projection using last fitted value (ARIMA(0,1,0) like behavior)
+ const forecastVal = lastFitted;
+ const ciWidth = 1.96 * residStd * Math.sqrt(1 + (i + 1) * 0.2);
+
+ enhancedData.push({
+ index: n + i,
+ value: null as unknown as number,
+ date: `+${i + 1}d`,
+ fitted: null as unknown as number,
+ residual: null as unknown as number,
+ upperBand: null as unknown as number,
+ lowerBand: null as unknown as number,
+ residualBar: null as unknown as number,
+ forecast: forecastVal,
+ forecastUpper: forecastVal + ciWidth,
+ forecastLower: forecastVal - ciWidth,
+ });
+ }
+ }
+
+ return enhancedData;
+ }
+ } else if (selectedAnalysis === 'forecasting') {
+ const forecasts = result.value as number[];
+ const meta = result.metadata as Record<string, unknown>;
+ const params = result.parameters as Record<string, unknown>;
+ const trainSize = (meta?.train_data_size as number) || Math.floor(chartData.length * 0.8);
+ const testSize = (meta?.test_data_size as number) || chartData.length - trainSize;
+ const horizon = (meta?.forecast_horizon as number) || forecasts?.length || 5;
+ const rmse = meta?.rmse as number;
+ const method = params?.method as string;
+
+ if (forecasts && forecasts.length > 0) {
+ // For ETS forecasting, show:
+ // 1. Training data (gray line)
+ // 2. Test actual data (amber line with dots)
+ // 3. Forecast level through test period (dashed blue)
+ // 4. Future forecast projection with confidence bands (green)
+
+ const forecastValue = forecasts[0]; // For simple ETS, all forecasts are the same
+
+ // Calculate standard deviation from training data for confidence bands
+ const trainData = priceData.slice(0, trainSize);
+ const trainMean = trainData.reduce((a, b) => a + b, 0) / trainData.length;
+ const trainStd = Math.sqrt(trainData.reduce((a, b) => a + Math.pow(b - trainMean, 2), 0) / trainData.length);
+
+ // Use RMSE if available, otherwise estimate from training std
+ const errorStd = rmse || trainStd * 0.3;
+
+ // Calculate test period errors for visualization
+ const testData = priceData.slice(trainSize);
+ const testErrors = testData.map(actual => actual - forecastValue);
+ const meanTestError = testErrors.length > 0 ? testErrors.reduce((a, b) => a + b, 0) / testErrors.length : 0;
+
+ const enhancedData = chartData.map((d, i) => {
+ const isTrainData = i < trainSize;
+ const isTestData = i >= trainSize;
+ const testIdx = i - trainSize;
+
+ // Calculate error for test points
+ const error = isTestData && testIdx < testErrors.length ? testErrors[testIdx] : null;
+ const absError = error !== null ? Math.abs(error) : null;
+
+ return {
+ ...d,
+ trainValue: isTrainData ? d.value : null,
+ testActual: isTestData ? d.value : null,
+ // Show forecast level as a reference line through the test period
+ forecastLevel: isTestData ? forecastValue : null,
+ // Confidence bands in test period (95% CI)
+ forecastUpper: isTestData ? forecastValue + 1.96 * errorStd : null,
+ forecastLower: isTestData ? forecastValue - 1.96 * errorStd : null,
+ // Error bar (absolute value for visualization)
+ forecastError: error,
+ absError: absError,
+ };
+ });
+
+ // Add future forecast points beyond the historical data with widening confidence bands
+ const futureForecasts = forecasts.map((v, i) => {
+ // Confidence bands widen over time for future forecasts
+ const ciWidth = 1.96 * errorStd * Math.sqrt(1 + (i + 1) * 0.15);
+
+ return {
+ index: chartData.length + i,
+ value: null,
+ trainValue: null,
+ testActual: null,
+ forecastLevel: null,
+ forecastUpper: null,
+ forecastLower: null,
+ forecastError: null,
+ absError: null,
+ forecast: v,
+ futureForecastUpper: v + ciWidth,
+ futureForecastLower: v - ciWidth,
+ date: `+${i + 1}d`,
+ };
+ });
+
+ // Add a connecting point at the last training data point
+ if (enhancedData.length > 0 && trainSize > 0) {
+ const lastTrainIdx = trainSize - 1;
+ enhancedData[lastTrainIdx] = {
+ ...enhancedData[lastTrainIdx],
+ forecastStart: enhancedData[lastTrainIdx].value,
+ };
+ }
+
+ return [...enhancedData, ...futureForecasts];
+ }
+ } else if (selectedAnalysis === 'supervised_learning') {
+ const meta = result.metadata as Record<string, unknown>;
+ const value = result.value as Record<string, Record<string, unknown>>;
+ const bestAlgo = meta?.best_algorithm as string;
+ if (bestAlgo && value[bestAlgo]) {
+ const predictions = value[bestAlgo].predictions as number[];
+ const yTest = meta?.y_test as number[];
+ const rmse = value[bestAlgo].rmse as number;
+
+ if (predictions && predictions.length > 0) {
+ const nLags = 5;
+ const totalSamples = chartData.length - nLags;
+ const trainSize = (meta?.train_size as number) || Math.floor(totalSamples * 0.8);
+ const testStartIndex = nLags + trainSize;
+
+ // Calculate prediction standard error for confidence bands
+ const predictionErrors = yTest ? yTest.map((actual, i) => actual - predictions[i]) : [];
+ const predStd = rmse || (predictionErrors.length > 0 ?
+ Math.sqrt(predictionErrors.reduce((sum, e) => sum + e * e, 0) / predictionErrors.length) : 0);
+
+ return chartData.map((d, i) => {
+ const testIdx = i - testStartIndex;
+ const isTestPoint = testIdx >= 0 && testIdx < predictions.length;
+ const isTrainPoint = i >= nLags && i < testStartIndex;
+ const pred = isTestPoint ? predictions[testIdx] : null;
+ const actual = isTestPoint && yTest ? yTest[testIdx] : null;
+ const error = (pred !== null && actual !== null) ? actual - pred : null;
+
+ // 95% confidence band (approximately ±2 * RMSE)
+ const ciWidth = 1.96 * predStd;
+
+ return {
+ ...d,
+ trainValue: isTrainPoint ? d.value : null,
+ prediction: pred,
+ testActual: actual,
+ predictionError: error,
+ upperBand: pred !== null ? pred + ciWidth : null,
+ lowerBand: pred !== null ? pred - ciWidth : null,
+ // Zero line for error reference
+ zeroLine: isTestPoint ? 0 : null,
+ };
+ });
+ }
+ }
+ } else if (selectedAnalysis === 'unsupervised_learning') {
+ const value = result.value as Record<string, Record<string, unknown>>;
+ if (value?.kmeans) {
+ const clusterLabels = value.kmeans.cluster_labels as number[];
+ if (clusterLabels && clusterLabels.length > 0) {
+ return chartData.map((d, i) => ({
+ ...d,
+ cluster: i < clusterLabels.length ? clusterLabels[i] : 0,
+ }));
+ }
+ }
+ return chartData;
+ } else if (selectedAnalysis === 'resampling_methods') {
+ const value = result.value as Record<string, Record<string, unknown>>;
+ if (value?.bootstrap) {
+ const bootstrap = value.bootstrap;
+ const resampledStats = bootstrap.resampled_statistics as number[];
+ const mean = bootstrap.mean as number;
+ const ci = bootstrap.confidence_interval as [number, number];
+
+ if (resampledStats && resampledStats.length > 0) {
+ // Create histogram data from resampled statistics
+ const sortedStats = [...resampledStats].sort((a, b) => a - b);
+ const min = sortedStats[0];
+ const max = sortedStats[sortedStats.length - 1];
+ const binCount = Math.min(30, Math.ceil(Math.sqrt(sortedStats.length)));
+ const binWidth = (max - min) / binCount;
+
+ // Find bin indices for mean and CI
+ const findBinIndex = (val: number) => {
+ const binIdx = Math.floor((val - min) / binWidth);
+ return Math.max(0, Math.min(binCount - 1, binIdx));
+ };
+
+ const meanBinIdx = findBinIndex(mean);
+ const ciLowIdx = ci ? findBinIndex(ci[0]) : 0;
+ const ciHighIdx = ci ? findBinIndex(ci[1]) : binCount - 1;
+
+ const bins: { binCenter: number; count: number; frequency: number; inCI: boolean; isMean: boolean }[] = [];
+ for (let i = 0; i < binCount; i++) {
+ const binStart = min + i * binWidth;
+ const binEnd = binStart + binWidth;
+ const binCenter = (binStart + binEnd) / 2;
+ const count = sortedStats.filter(v => v >= binStart && (i === binCount - 1 ? v <= binEnd : v < binEnd)).length;
+ const inCI = i >= ciLowIdx && i <= ciHighIdx;
+ const isMean = i === meanBinIdx;
+ bins.push({
+ binCenter,
+ count,
+ frequency: count / sortedStats.length,
+ inCI,
+ isMean,
+ });
+ }
+
+ return bins.map((bin, i) => ({
+ index: i,
+ value: bin.count,
+ binCenter: bin.binCenter,
+ frequency: bin.frequency,
+ inCI: bin.inCI,
+ isMean: bin.isMean,
+ date: formatNumber(bin.binCenter, 2),
+ meanBinIdx,
+ ciLowIdx,
+ ciHighIdx,
+ }));
+ }
+ }
+ // If no bootstrap data, show original data with jackknife leave-one-out visualization
+ return chartData;
+ } else if (selectedAnalysis === 'validate_data') {
+ // Data Quality visualization: show data with outlier highlighting
+ // Compute z-scores for outlier detection
+ if (priceData.length < 3) return chartData;
+
+ const mean = priceData.reduce((a, b) => a + b, 0) / priceData.length;
+ const variance = priceData.reduce((a, b) => a + Math.pow(b - mean, 2), 0) / priceData.length;
+ const std = Math.sqrt(variance);
+
+ const outlierThreshold = 3.0; // Z-score threshold for outliers
+
+ return chartData.map((d, i) => {
+ const zScore = std > 0 ? Math.abs((d.value - mean) / std) : 0;
+ const isOutlier = zScore > outlierThreshold;
+ const isSuspicious = zScore > 2.0 && zScore <= outlierThreshold;
+
+ return {
+ ...d,
+ zScore,
+ isOutlier,
+ isSuspicious,
+ normalValue: !isOutlier && !isSuspicious ? d.value : null,
+ suspiciousValue: isSuspicious ? d.value : null,
+ outlierValue: isOutlier ? d.value : null,
+ mean,
+ };
+ });
+ }
+ return chartData;
+ };
+
+ const keyMetrics = getKeyMetrics();
+ const resultChartData = getResultChartData();
+
+ // Compute cluster boundary separately (not in chart data to avoid breaking the chart)
+ let clusterBoundary: number | null = null;
+ if (selectedAnalysis === 'unsupervised_learning' && analysisResult?.result) {
+ const result = analysisResult.result as Record<string, unknown>;
+ const value = result.value as Record<string, Record<string, unknown>>;
+ if (value?.kmeans) {
+ const clusterLabels = value.kmeans.cluster_labels as number[];
+ if (clusterLabels && clusterLabels.length > 0 && priceData.length > 0) {
+ const clusterSums: Record<number, { sum: number; count: number }> = {};
+ priceData.forEach((price, i) => {
+ const cluster = i < clusterLabels.length ? clusterLabels[i] : 0;
+ if (!clusterSums[cluster]) clusterSums[cluster] = { sum: 0, count: 0 };
+ clusterSums[cluster].sum += price;
+ clusterSums[cluster].count += 1;
+ });
+ const means = Object.values(clusterSums).map(s => s.sum / s.count).sort((a, b) => a - b);
+ if (means.length >= 2) {
+ clusterBoundary = (means[0] + means[1]) / 2;
+ }
+ }
+ }
+ }
+
+ // Compute Bootstrap reference values separately
+ let bootstrapMean: number | null = null;
+ let bootstrapCiLow: number | null = null;
+ let bootstrapCiHigh: number | null = null;
+ let bootstrapMeanIdx: number | null = null;
+ let bootstrapCiLowIdx: number | null = null;
+ let bootstrapCiHighIdx: number | null = null;
+ if (selectedAnalysis === 'resampling_methods' && analysisResult?.result) {
+ const result = analysisResult.result as Record<string, unknown>;
+ const value = result.value as Record<string, Record<string, unknown>>;
+ if (value?.bootstrap) {
+ bootstrapMean = value.bootstrap.mean as number;
+ const ci = value.bootstrap.confidence_interval as [number, number];
+ if (ci && ci.length === 2) {
+ bootstrapCiLow = ci[0];
+ bootstrapCiHigh = ci[1];
+ }
+ // Get indices from chart data
+ if (resultChartData.length > 0 && resultChartData[0]?.meanBinIdx !== undefined) {
+ bootstrapMeanIdx = resultChartData[0].meanBinIdx as number;
+ bootstrapCiLowIdx = resultChartData[0].ciLowIdx as number;
+ bootstrapCiHighIdx = resultChartData[0].ciHighIdx as number;
+ }
+ }
+ }
+
+ return (
+ <div className="flex h-full">
+ {/* Left Panel - Metrics */}
+ <div className="w-80 flex-shrink-0 p-4 overflow-y-auto" style={{ backgroundColor: BB.panelBg, borderRight: `1px solid ${BB.borderDark}` }}>
+ <div className="flex items-center justify-between mb-3">
+ <div className="text-xs font-mono" style={{ color: BB.textAmber }}>RESULTS</div>
+ <div className="flex gap-2">
+ <button onClick={() => copyToClipboard(JSON.stringify(analysisResult, null, 2))} className="p-1.5" style={{ backgroundColor: BB.cardBg }}>
+ <Copy size={14} style={{ color: BB.textMuted }} />
+ </button>
+ <button onClick={exportResults} className="p-1.5" style={{ backgroundColor: BB.cardBg }}>
+ <Download size={14} style={{ color: BB.textMuted }} />
+ </button>
+ </div>
+ </div>
+
+ {/* Status Banner */}
+ <div className="p-3 mb-4" style={{ backgroundColor: `${BB.green}10`, border: `1px solid ${BB.green}` }}>
+ <div className="flex items-center gap-2">
+ <CheckCircle2 size={16} style={{ color: BB.green }} />
+ <span className="text-xs font-mono font-bold" style={{ color: BB.green }}>COMPLETE</span>
+ </div>
+ <div className="text-xs font-mono mt-1" style={{ color: BB.textMuted }}>
+ {config?.name} • {analysisResult.timestamp ? new Date(analysisResult.timestamp).toLocaleTimeString() : 'Just now'}
+ </div>
+ </div>
+
+ {/* Key Metrics */}
+ {keyMetrics.length > 0 && (
+ <div className="space-y-2 mb-4">
+ {keyMetrics.map((metric, i) => (
+ <div key={i} className="flex items-center justify-between p-3" style={{ backgroundColor: BB.cardBg, border: `1px solid ${BB.borderDark}` }}>
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>{metric.label}</span>
+ <div className="flex items-center gap-2">
+ {metric.icon && <metric.icon size={14} style={{ color: metric.color }} />}
+ <span className="text-sm font-mono font-bold" style={{ color: metric.color }}>{metric.value}</span>
+ </div>
+ </div>
+ ))}
+ </div>
+ )}
+
+ {/* Raw Data Section */}
+ <div className="mb-4">
+ <div className="text-xs font-mono mb-2" style={{ color: BB.textAmber }}>RAW OUTPUT</div>
+ <div className="p-3 text-xs font-mono overflow-x-auto" style={{ backgroundColor: BB.black, border: `1px solid ${BB.borderDark}`, maxHeight: 200, overflowY: 'auto' }}>
+ {Object.entries(result).map(([key, value]) => {
+ if (key === 'model_summary' || (Array.isArray(value) && value.length > 20)) return null;
+ return (
+ <div key={key} className="flex gap-2 py-1" style={{ borderBottom: `1px solid ${BB.borderDark}` }}>
+ <span style={{ color: BB.textMuted }}>{key}:</span>
+ <span style={{ color: BB.textPrimary }} className="truncate">
+ {typeof value === 'object' ? JSON.stringify(value).substring(0, 50) + '...' : String(value)}
+ </span>
+ </div>
+ );
+ })}
+ </div>
+ </div>
+
+ <div className="flex gap-2">
+ <button
+ onClick={() => setCurrentStep('analysis')}
+ className="flex-1 py-2 text-xs font-mono"
+ style={{ backgroundColor: BB.cardBg, color: BB.textSecondary, border: `1px solid ${BB.borderDark}` }}
+ >
+ <ChevronLeft size={12} className="inline" /> MODELS
+ </button>
+ <button
+ onClick={() => {
+ setCurrentStep('data');
+ setAnalysisResult(null);
+ }}
+ className="flex-1 py-2 text-xs font-mono font-bold"
+ style={{ backgroundColor: BB.amber, color: BB.black }}
+ >
+ NEW <Play size={12} className="inline ml-1" />
+ </button>
+ </div>
+ </div>
+
+ {/* Right Panel - Chart with Results */}
+ <div className="flex-1 p-4" style={{ backgroundColor: BB.darkBg }}>
+ <div className="h-full flex flex-col">
+ <div className="flex items-center justify-between mb-2">
+ <div className="flex items-center gap-3">
+ <div className="text-xs font-mono" style={{ color: BB.textAmber }}>{config?.shortName} OUTPUT</div>
+ {resultsChartZoom && (
+ <div className="text-xs font-mono px-2 py-0.5" style={{ backgroundColor: BB.amber + '20', color: BB.amber }}>
+ {getDateForIndex(resultsChartZoom.startIndex)} - {getDateForIndex(resultsChartZoom.endIndex)}
+ </div>
+ )}
+ </div>
+ <div className="flex items-center gap-3">
+ <ZoomControls chartType="results" isZoomed={resultsChartZoom !== null} />
+ <div className="text-xs font-mono" style={{ color: BB.textMuted }}>{symbolInput.toUpperCase()}</div>
+ </div>
+ </div>
+ <div
+ className="flex-1"
+ style={{ minHeight: 300 }}
+ onWheel={(e) => handleWheel(e, 'results')}
+ >
+ <ResponsiveContainer width="100%" height="100%">
+ <ComposedChart
+ data={resultsChartZoom ? resultChartData.slice(resultsChartZoom.startIndex, resultsChartZoom.endIndex + 1) : resultChartData}
+ margin={{ top: 10, right: 10, left: 0, bottom: 30 }}
+ onMouseDown={(e) => handleMouseDown(e, 'results')}
+ onMouseMove={handleMouseMove}
+ onMouseUp={handleMouseUp}
+ onMouseLeave={handleMouseUp}
+ >
+ <CartesianGrid strokeDasharray="3 3" stroke={BB.chartGrid} />
+ <XAxis
+ dataKey={selectedAnalysis === 'resampling_methods' ? 'index' : 'date'}
+ tick={{ fill: BB.textMuted, fontSize: 9 }}
+ axisLine={{ stroke: BB.borderDark }}
+ interval={selectedAnalysis === 'resampling_methods' ? 'preserveStartEnd' : 'preserveStartEnd'}
+ tickFormatter={(val) => {
+ if (selectedAnalysis === 'resampling_methods' && resultChartData[val]?.binCenter !== undefined) {
+ return formatNumber(resultChartData[val].binCenter as number, 1);
+ }
+ return val;
+ }}
+ />
+ <YAxis
+ tick={{ fill: BB.textMuted, fontSize: 10 }}
+ axisLine={{ stroke: BB.borderDark }}
+ domain={['auto', 'auto']}
+ tickFormatter={(val) => selectedAnalysis === 'resampling_methods' ? String(Math.round(val)) : formatPrice(val)}
+ label={selectedAnalysis === 'resampling_methods' ? { value: 'COUNT', angle: -90, position: 'insideLeft', fill: BB.textMuted, fontSize: 9 } : undefined}
+ />
+ <Tooltip
+ contentStyle={{
+ backgroundColor: BB.cardBg,
+ border: `1px solid ${BB.borderLight}`,
+ borderRadius: 0,
+ fontSize: 11,
+ }}
+ labelFormatter={(label) => selectedAnalysis === 'resampling_methods' ? `Value: ${label}` : `Date: ${label}`}
+ labelStyle={{ color: BB.textAmber, fontWeight: 'bold' }}
+ formatter={(value: number, name: string, props: { payload?: { frequency?: number; inCI?: boolean; binCenter?: number; zScore?: number; isOutlier?: boolean; isSuspicious?: boolean; value?: number } }) => {
+ if (selectedAnalysis === 'resampling_methods' && props.payload) {
+ const freq = props.payload.frequency;
+ const inCI = props.payload.inCI;
+ return [
+ <span key="val">
+ Count: <strong>{value}</strong> ({freq ? (freq * 100).toFixed(1) : 0}%)
+ {inCI && <span style={{ color: BB.blueLight, marginLeft: 8 }}>In 95% CI</span>}
+ </span>,
+ ''
+ ];
+ }
+ if (selectedAnalysis === 'validate_data' && props.payload) {
+ const zScore = props.payload.zScore;
+ const isOutlier = props.payload.isOutlier;
+ const isSuspicious = props.payload.isSuspicious;
+ const priceVal = props.payload.value;
+ let status = 'Normal';
+ let statusColor = BB.green;
+ if (isOutlier) { status = 'OUTLIER'; statusColor = BB.red; }
+ else if (isSuspicious) { status = 'Suspicious'; statusColor = BB.amber; }
+ return [
+ <span key="val">
+ <span style={{ color: BB.textPrimary }}>Price: <strong>{priceVal !== undefined ? formatPrice(priceVal) : formatPrice(value)}</strong></span>
+ <br />
+ <span style={{ color: BB.textSecondary }}>Z-Score: {zScore !== undefined ? zScore.toFixed(2) : '—'}</span>
+ <br />
+ <span style={{ color: statusColor }}>Status: {status}</span>
+ </span>,
+ ''
+ ];
+ }
+ return [value, name];
+ }}
+ />
+ {/* Original data (for non-forecasting/non-unsupervised/non-bootstrap/non-validate/non-stationarity analyses) */}
+ {selectedAnalysis !== 'forecasting' && selectedAnalysis !== 'unsupervised_learning' && selectedAnalysis !== 'resampling_methods' && selectedAnalysis !== 'validate_data' && selectedAnalysis !== 'stationarity_test' && (
+ <Line
+ type="monotone"
+ dataKey="value"
+ stroke={BB.textSecondary}
+ strokeWidth={1}
+ dot={false}
+ name="Actual"
+ />
+ )}
+ {/* Stationarity Test: Price line */}
+ {selectedAnalysis === 'stationarity_test' && (
+ <Line
+ type="monotone"
+ dataKey="value"
+ stroke={BB.textSecondary}
+ strokeWidth={1}
+ dot={false}
+ name="Price"
+ />
+ )}
+ {/* Stationarity Test: Upper Bollinger Band */}
+ {selectedAnalysis === 'stationarity_test' && (
+ <Line
+ type="monotone"
+ dataKey="upperBand"
+ stroke={BB.amber}
+ strokeWidth={1}
+ strokeDasharray="3 3"
+ dot={false}
+ name="Upper Band (+2σ)"
+ connectNulls={false}
+ />
+ )}
+ {/* Stationarity Test: Lower Bollinger Band */}
+ {selectedAnalysis === 'stationarity_test' && (
+ <Line
+ type="monotone"
+ dataKey="lowerBand"
+ stroke={BB.amber}
+ strokeWidth={1}
+ strokeDasharray="3 3"
+ dot={false}
+ name="Lower Band (-2σ)"
+ connectNulls={false}
+ />
+ )}
+ {/* Stationarity Test: Rolling Mean */}
+ {selectedAnalysis === 'stationarity_test' && (
+ <Line
+ type="monotone"
+ dataKey="rollingMean"
+ stroke={BB.green}
+ strokeWidth={2}
+ dot={false}
+ name="Rolling Mean"
+ connectNulls={false}
+ />
+ )}
+ {/* Stationarity Test: Overall Mean Reference */}
+ {selectedAnalysis === 'stationarity_test' && resultChartData.length > 0 && (resultChartData[0] as { overallMean?: number }).overallMean !== undefined && (
+ <ReferenceLine
+ y={(resultChartData[0] as { overallMean: number }).overallMean}
+ stroke={BB.blueLight}
+ strokeWidth={2}
+ strokeDasharray="5 5"
+ label={{ value: 'OVERALL MEAN', fill: BB.blueLight, fontSize: 9, position: 'right' }}
+ />
+ )}
+ {/* Data Quality: Normal data line */}
+ {selectedAnalysis === 'validate_data' && (
+ <Line
+ type="monotone"
+ dataKey="normalValue"
+ stroke={BB.green}
+ strokeWidth={1.5}
+ dot={false}
+ name="Normal"
+ connectNulls={false}
+ />
+ )}
+ {/* Data Quality: Suspicious data (2-3 std dev) */}
+ {selectedAnalysis === 'validate_data' && (
+ <Scatter
+ dataKey="suspiciousValue"
+ fill={BB.amber}
+ name="Suspicious"
+ shape={(props: { cx: number; cy: number }) => {
+ if (!props.cx || !props.cy) return null;
+ return (
+ <circle
+ cx={props.cx}
+ cy={props.cy}
+ r={5}
+ fill={BB.amber}
+ stroke={BB.black}
+ strokeWidth={1}
+ />
+ );
+ }}
+ />
+ )}
+ {/* Data Quality: Outlier data (>3 std dev) */}
+ {selectedAnalysis === 'validate_data' && (
+ <Scatter
+ dataKey="outlierValue"
+ fill={BB.red}
+ name="Outlier"
+ shape={(props: { cx: number; cy: number }) => {
+ if (!props.cx || !props.cy) return null;
+ return (
+ <g>
+ <circle
+ cx={props.cx}
+ cy={props.cy}
+ r={7}
+ fill={BB.red}
+ stroke={BB.black}
+ strokeWidth={1}
+ />
+ <text
+ x={props.cx}
+ y={props.cy + 3}
+ textAnchor="middle"
+ fill={BB.textPrimary}
+ fontSize={10}
+ fontWeight="bold"
+ >
+ !
+ </text>
+ </g>
+ );
+ }}
+ />
+ )}
+ {/* Data Quality: Mean reference line */}
+ {selectedAnalysis === 'validate_data' && resultChartData.length > 0 && (resultChartData[0] as { mean?: number }).mean !== undefined && (
+ <ReferenceLine
+ y={(resultChartData[0] as { mean: number }).mean}
+ stroke={BB.blueLight}
+ strokeWidth={2}
+ strokeDasharray="5 5"
+ label={{ value: 'MEAN', fill: BB.blueLight, fontSize: 10, position: 'right' }}
+ />
+ )}
+ {/* Bootstrap: CI shaded area */}
+ {selectedAnalysis === 'resampling_methods' && typeof bootstrapCiLowIdx === 'number' && typeof bootstrapCiHighIdx === 'number' && (
+ <ReferenceArea
+ x1={bootstrapCiLowIdx}
+ x2={bootstrapCiHighIdx}
+ fill={BB.blueLight}
+ fillOpacity={0.15}
+ stroke={BB.blueLight}
+ strokeOpacity={0.3}
+ />
+ )}
+ {/* Bootstrap: Histogram bars with conditional coloring */}
+ {selectedAnalysis === 'resampling_methods' && (
+ <Bar
+ dataKey="value"
+ name="Frequency"
+ shape={(props: { x: number; y: number; width: number; height: number; payload?: { inCI?: boolean; isMean?: boolean } }) => {
+ const { x, y, width, height, payload } = props;
+ const inCI = payload?.inCI;
+ const isMean = payload?.isMean;
+ let fill = BB.green;
+ let opacity = 0.5;
+ if (inCI) {
+ fill = BB.green;
+ opacity = 0.8;
+ }
+ if (isMean) {
+ fill = BB.amber;
+ opacity = 1;
+ }
+ return (
+ <rect
+ x={x}
+ y={y}
+ width={width}
+ height={height}
+ fill={fill}
+ fillOpacity={opacity}
+ stroke={isMean ? BB.amber : 'none'}
+ strokeWidth={isMean ? 2 : 0}
+ />
+ );
+ }}
+ />
+ )}
+ {/* Bootstrap: Mean reference line */}
+ {selectedAnalysis === 'resampling_methods' && typeof bootstrapMeanIdx === 'number' && (
+ <ReferenceLine
+ x={bootstrapMeanIdx}
+ stroke={BB.amber}
+ strokeWidth={2}
+ label={{ value: `MEAN: ${formatNumber(bootstrapMean || 0, 2)}`, fill: BB.amber, fontSize: 10, position: 'top' }}
+ />
+ )}
+ {/* Unsupervised Learning: Cluster boundary line */}
+ {selectedAnalysis === 'unsupervised_learning' && typeof clusterBoundary === 'number' && (
+ <ReferenceLine
+ y={clusterBoundary}
+ stroke={BB.amber}
+ strokeWidth={2}
+ strokeDasharray="6 3"
+ />
+ )}
+ {/* Unsupervised Learning: Price line with cluster colors */}
+ {selectedAnalysis === 'unsupervised_learning' && (
+ <Line
+ type="monotone"
+ dataKey="value"
+ stroke={BB.textMuted}
+ strokeWidth={1}
+ strokeOpacity={0.5}
+ dot={(props) => {
+ const p = props as { cx: number; cy: number; payload: { cluster?: number } };
+ if (!p.cx || !p.cy) return <g />;
+ const colors = [BB.green, BB.amber, BB.blueLight, BB.red];
+ const c = p.payload?.cluster ?? 0;
+ return <circle cx={p.cx} cy={p.cy} r={4} fill={colors[c % colors.length]} />;
+ }}
+ name="Clustered Data"
+ />
+ )}
+ {/* ETS/Forecasting: Training data */}
+ <Line
+ type="monotone"
+ dataKey="trainValue"
+ stroke={BB.textSecondary}
+ dot={false}
+ strokeWidth={1.5}
+ name="Training Data"
+ />
+ {/* ETS/Forecasting: Test period actual values */}
+ <Line
+ type="monotone"
+ dataKey="testActual"
+ stroke={BB.amber}
+ dot={{ fill: BB.amber, r: 2 }}
+ strokeWidth={2}
+ name="Actual (Test)"
+ />
+ {/* ETS/Forecasting: Forecast level line (shows expected value) */}
+ <Line
+ type="monotone"
+ dataKey="forecastLevel"
+ stroke={BB.blueLight}
+ dot={false}
+ strokeWidth={2}
+ strokeDasharray="5 5"
+ name="Forecast Level"
+ />
+ {/* ETS/Forecasting: Upper confidence band in test period */}
+ {selectedAnalysis === 'forecasting' && (
+ <Line
+ type="monotone"
+ dataKey="forecastUpper"
+ stroke={BB.amber}
+ strokeWidth={1}
+ strokeDasharray="3 3"
+ dot={false}
+ name="95% CI Upper"
+ connectNulls={false}
+ />
+ )}
+ {/* ETS/Forecasting: Lower confidence band in test period */}
+ {selectedAnalysis === 'forecasting' && (
+ <Line
+ type="monotone"
+ dataKey="forecastLower"
+ stroke={BB.amber}
+ strokeWidth={1}
+ strokeDasharray="3 3"
+ dot={false}
+ name="95% CI Lower"
+ connectNulls={false}
+ />
+ )}
+ {/* ETS/Forecasting: Future forecast upper band (widening) */}
+ {selectedAnalysis === 'forecasting' && (
+ <Line
+ type="monotone"
+ dataKey="futureForecastUpper"
+ stroke={BB.greenDim}
+ strokeWidth={1}
+ strokeDasharray="3 3"
+ dot={false}
+ name="Forecast CI Upper"
+ connectNulls={false}
+ />
+ )}
+ {/* ETS/Forecasting: Future forecast lower band (widening) */}
+ {selectedAnalysis === 'forecasting' && (
+ <Line
+ type="monotone"
+ dataKey="futureForecastLower"
+ stroke={BB.greenDim}
+ strokeWidth={1}
+ strokeDasharray="3 3"
+ dot={false}
+ name="Forecast CI Lower"
+ connectNulls={false}
+ />
+ )}
+ {/* Trend Analysis: Upper confidence band */}
+ {selectedAnalysis === 'trend_analysis' && (
+ <Line
+ type="monotone"
+ dataKey="upperBand"
+ stroke={BB.amber}
+ strokeWidth={1}
+ strokeDasharray="3 3"
+ dot={false}
+ name="95% CI Upper"
+ connectNulls={false}
+ />
+ )}
+ {/* Trend Analysis: Lower confidence band */}
+ {selectedAnalysis === 'trend_analysis' && (
+ <Line
+ type="monotone"
+ dataKey="lowerBand"
+ stroke={BB.amber}
+ strokeWidth={1}
+ strokeDasharray="3 3"
+ dot={false}
+ name="95% CI Lower"
+ connectNulls={false}
+ />
+ )}
+ {/* Fitted/trend line */}
+ <Line
+ type="monotone"
+ dataKey="fitted"
+ stroke={config?.color || BB.amber}
+ dot={false}
+ strokeWidth={2}
+ strokeDasharray="5 5"
+ name="Fitted"
+ />
+ {/* Trend Analysis: Future projection line */}
+ {selectedAnalysis === 'trend_analysis' && (
+ <Line
+ type="monotone"
+ dataKey="projection"
+ stroke={BB.green}
+ strokeWidth={2}
+ dot={{ fill: BB.green, r: 4 }}
+ name="Projection"
+ connectNulls={false}
+ />
+ )}
+ {/* Trend Analysis: Projection upper band */}
+ {selectedAnalysis === 'trend_analysis' && (
+ <Line
+ type="monotone"
+ dataKey="projectionUpper"
+ stroke={BB.greenDim}
+ strokeWidth={1}
+ strokeDasharray="3 3"
+ dot={false}
+ name="Projection CI Upper"
+ connectNulls={false}
+ />
+ )}
+ {/* Trend Analysis: Projection lower band */}
+ {selectedAnalysis === 'trend_analysis' && (
+ <Line
+ type="monotone"
+ dataKey="projectionLower"
+ stroke={BB.greenDim}
+ strokeWidth={1}
+ strokeDasharray="3 3"
+ dot={false}
+ name="Projection CI Lower"
+ connectNulls={false}
+ />
+ )}
+ {/* ARIMA: Upper confidence band */}
+ {selectedAnalysis === 'arima_model' && (
+ <Line
+ type="monotone"
+ dataKey="upperBand"
+ stroke={BB.amber}
+ strokeWidth={1}
+ strokeDasharray="3 3"
+ dot={false}
+ name="95% CI Upper"
+ connectNulls={false}
+ />
+ )}
+ {/* ARIMA: Lower confidence band */}
+ {selectedAnalysis === 'arima_model' && (
+ <Line
+ type="monotone"
+ dataKey="lowerBand"
+ stroke={BB.amber}
+ strokeWidth={1}
+ strokeDasharray="3 3"
+ dot={false}
+ name="95% CI Lower"
+ connectNulls={false}
+ />
+ )}
+ {/* ARIMA: Forecast projection line */}
+ {selectedAnalysis === 'arima_model' && (
+ <Line
+ type="monotone"
+ dataKey="forecast"
+ stroke={BB.green}
+ strokeWidth={2}
+ dot={{ fill: BB.green, r: 4 }}
+ name="ARIMA Forecast"
+ connectNulls={false}
+ />
+ )}
+ {/* ARIMA: Forecast upper band */}
+ {selectedAnalysis === 'arima_model' && (
+ <Line
+ type="monotone"
+ dataKey="forecastUpper"
+ stroke={BB.greenDim}
+ strokeWidth={1}
+ strokeDasharray="3 3"
+ dot={false}
+ name="Forecast CI Upper"
+ connectNulls={false}
+ />
+ )}
+ {/* ARIMA: Forecast lower band */}
+ {selectedAnalysis === 'arima_model' && (
+ <Line
+ type="monotone"
+ dataKey="forecastLower"
+ stroke={BB.greenDim}
+ strokeWidth={1}
+ strokeDasharray="3 3"
+ dot={false}
+ name="Forecast CI Lower"
+ connectNulls={false}
+ />
+ )}
+ {/* Future Forecast */}
+ <Line
+ type="monotone"
+ dataKey="forecast"
+ stroke={BB.green}
+ dot={{ fill: BB.green, r: 4 }}
+ strokeWidth={2}
+ name="Forecast"
+ />
+ {/* ML Prediction: Training data line */}
+ {selectedAnalysis === 'supervised_learning' && (
+ <Line
+ type="monotone"
+ dataKey="trainValue"
+ stroke={BB.textSecondary}
+ strokeWidth={1}
+ dot={false}
+ name="Training Data"
+ connectNulls={false}
+ />
+ )}
+ {/* ML Prediction: Upper confidence band */}
+ {selectedAnalysis === 'supervised_learning' && (
+ <Line
+ type="monotone"
+ dataKey="upperBand"
+ stroke={BB.amber}
+ strokeWidth={1}
+ strokeDasharray="3 3"
+ dot={false}
+ name="95% CI Upper"
+ connectNulls={false}
+ />
+ )}
+ {/* ML Prediction: Lower confidence band */}
+ {selectedAnalysis === 'supervised_learning' && (
+ <Line
+ type="monotone"
+ dataKey="lowerBand"
+ stroke={BB.amber}
+ strokeWidth={1}
+ strokeDasharray="3 3"
+ dot={false}
+ name="95% CI Lower"
+ connectNulls={false}
+ />
+ )}
+ {/* ML Prediction: Test actual values */}
+ {selectedAnalysis === 'supervised_learning' && (
+ <Line
+ type="monotone"
+ dataKey="testActual"
+ stroke={BB.green}
+ strokeWidth={2}
+ dot={{ fill: BB.green, r: 3 }}
+ name="Test Actual"
+ connectNulls={false}
+ />
+ )}
+ {/* ML Prediction */}
+ {selectedAnalysis === 'supervised_learning' && (
+ <Line
+ type="monotone"
+ dataKey="prediction"
+ stroke={BB.blueLight}
+ dot={{ fill: BB.blueLight, r: 3 }}
+ strokeWidth={2}
+ name="ML Prediction"
+ connectNulls={false}
+ />
+ )}
+ {/* ML Prediction (for other analysis types that use it) */}
+ {selectedAnalysis !== 'supervised_learning' && (
+ <Line
+ type="monotone"
+ dataKey="prediction"
+ stroke={BB.blueLight}
+ dot={{ fill: BB.blueLight, r: 3 }}
+ strokeWidth={2}
+ name="ML Prediction"
+ />
+ )}
+ {/* Brush for horizontal pan/zoom */}
+ {!resultsChartZoom && (
+ <Brush
+ dataKey="index"
+ height={20}
+ stroke={BB.amber}
+ fill={BB.cardBg}
+ tickFormatter={(val) => `${val}`}
+ onChange={(e) => {
+ if (e.startIndex !== undefined && e.endIndex !== undefined && e.startIndex !== e.endIndex) {
+ setResultsChartZoom({ startIndex: e.startIndex, endIndex: e.endIndex });
+ }
+ }}
+ />
+ )}
+ {/* Selection area indicator */}
+ {isSelecting && activeChart === 'results' && refAreaLeft !== null && refAreaRight !== null && (
+ <ReferenceArea
+ x1={refAreaLeft}
+ x2={refAreaRight}
+ strokeOpacity={0.3}
+ fill={BB.amber}
+ fillOpacity={0.2}
+ />
+ )}
+ </ComposedChart>
+ </ResponsiveContainer>
+ </div>
+
+ {/* Legend */}
+ <div className="flex items-center justify-between mt-2 px-2">
+ <div className="flex items-center gap-4 flex-wrap">
+ {selectedAnalysis === 'forecasting' && (
+ <>
+ <div className="flex items-center gap-2">
+ <div className="w-4 h-0.5" style={{ backgroundColor: BB.textSecondary }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>TRAIN DATA</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-2 h-2 rounded-full" style={{ backgroundColor: BB.amber }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>TEST ACTUAL</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-4 border-t-2 border-dashed" style={{ borderColor: BB.blueLight }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>ETS LEVEL</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-4 border-t border-dashed" style={{ borderColor: BB.amber }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>95% CI</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-2 h-2 rounded-full" style={{ backgroundColor: BB.green }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>FORECAST</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-4 border-t border-dashed" style={{ borderColor: BB.greenDim }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>FORECAST CI</span>
+ </div>
+ </>
+ )}
+ {selectedAnalysis === 'resampling_methods' && (
+ <>
+ <div className="flex items-center gap-2">
+ <div className="w-3 h-3" style={{ backgroundColor: BB.green, opacity: 0.7 }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>DISTRIBUTION</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-4 h-0.5" style={{ backgroundColor: BB.amber }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>MEAN</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-4 h-3" style={{ backgroundColor: BB.blueLight, opacity: 0.2 }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>95% CI</span>
+ </div>
+ </>
+ )}
+ {selectedAnalysis === 'unsupervised_learning' && (
+ <>
+ <div className="flex items-center gap-2">
+ <div className="w-4 h-0.5" style={{ backgroundColor: BB.textSecondary }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>PRICE</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-4 border-t-2 border-dashed" style={{ borderColor: BB.amber }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>BOUNDARY</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-2 h-2 rounded-full" style={{ backgroundColor: BB.green }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>CLUSTER 0</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-2 h-2 rounded-full" style={{ backgroundColor: BB.amber }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>CLUSTER 1</span>
+ </div>
+ </>
+ )}
+ {selectedAnalysis === 'supervised_learning' && (
+ <>
+ <div className="flex items-center gap-2">
+ <div className="w-4 h-0.5" style={{ backgroundColor: BB.textSecondary }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>TRAIN DATA</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-2 h-2 rounded-full" style={{ backgroundColor: BB.green }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>TEST ACTUAL</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-2 h-2 rounded-full" style={{ backgroundColor: BB.blueLight }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>PREDICTION</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-4 border-t border-dashed" style={{ borderColor: BB.amber }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>95% CI</span>
+ </div>
+ </>
+ )}
+ {selectedAnalysis === 'validate_data' && (
+ <>
+ <div className="flex items-center gap-2">
+ <div className="w-4 h-0.5" style={{ backgroundColor: BB.green }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>NORMAL DATA</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-2 h-2 rounded-full" style={{ backgroundColor: BB.amber }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>SUSPICIOUS (2-3σ)</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-2 h-2 rounded-full" style={{ backgroundColor: BB.red }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>OUTLIER (&gt;3σ)</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-4 border-t-2 border-dashed" style={{ borderColor: BB.blueLight }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>MEAN</span>
+ </div>
+ </>
+ )}
+ {selectedAnalysis === 'stationarity_test' && (
+ <>
+ <div className="flex items-center gap-2">
+ <div className="w-4 h-0.5" style={{ backgroundColor: BB.textSecondary }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>PRICE</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-4 h-0.5" style={{ backgroundColor: BB.green }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>ROLLING MEAN</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-4 border-t-2 border-dashed" style={{ borderColor: BB.amber }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>±2σ BANDS</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-4 border-t-2 border-dashed" style={{ borderColor: BB.blueLight }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>OVERALL MEAN</span>
+ </div>
+ </>
+ )}
+ {selectedAnalysis === 'trend_analysis' && (
+ <>
+ <div className="flex items-center gap-2">
+ <div className="w-4 h-0.5" style={{ backgroundColor: BB.textSecondary }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>ACTUAL</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-4 border-t-2 border-dashed" style={{ borderColor: config?.color || BB.amber }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>TREND</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-4 border-t border-dashed" style={{ borderColor: BB.amber }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>95% CI</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-2 h-2 rounded-full" style={{ backgroundColor: BB.green }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>PROJECTION</span>
+ </div>
+ </>
+ )}
+ {selectedAnalysis === 'arima_model' && (
+ <>
+ <div className="flex items-center gap-2">
+ <div className="w-4 h-0.5" style={{ backgroundColor: BB.textSecondary }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>ACTUAL</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-4 border-t-2 border-dashed" style={{ borderColor: BB.amber }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>FITTED</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-4 border-t border-dashed" style={{ borderColor: BB.amber }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>95% CI</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-2 h-2 rounded-full" style={{ backgroundColor: BB.green }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>FORECAST</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-4 border-t border-dashed" style={{ borderColor: BB.greenDim }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>FORECAST CI</span>
+ </div>
+ </>
+ )}
+ {selectedAnalysis !== 'forecasting' && selectedAnalysis !== 'resampling_methods' && selectedAnalysis !== 'unsupervised_learning' && selectedAnalysis !== 'supervised_learning' && selectedAnalysis !== 'validate_data' && selectedAnalysis !== 'stationarity_test' && selectedAnalysis !== 'trend_analysis' && selectedAnalysis !== 'arima_model' && (
+ <>
+ <div className="flex items-center gap-2">
+ <div className="w-4 h-0.5" style={{ backgroundColor: BB.textSecondary }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>ACTUAL</span>
+ </div>
+ <div className="flex items-center gap-2">
+ <div className="w-4 h-0.5" style={{ backgroundColor: config?.color, borderStyle: 'dashed', borderWidth: 1, borderColor: config?.color }} />
+ <span className="text-xs font-mono" style={{ color: BB.textMuted }}>FITTED</span>
+ </div>
+ </>
+ )}
+ </div>
+ <div className="text-xs font-mono" style={{ color: BB.textMuted }}>
+ Scroll to pan | Ctrl+Scroll to zoom | Drag to select
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+ );
+ };
+
+ return (
+ <div className="h-full flex flex-col" style={{ backgroundColor: BB.black }}>
+ {renderHeader()}
+ <div className="flex-1 overflow-hidden">
+ {currentStep === 'data' && renderDataStep()}
+ {currentStep === 'analysis' && renderAnalysisStep()}
+ {currentStep === 'results' && renderResultsStep()}
+ </div>
+ </div>
+ );
+}
+
+export default CFAQuantPanel;
diff --git a/fincept-terminal-desktop/src/services/aiQuantLab/quantAnalyticsService.ts b/fincept-terminal-desktop/src/services/aiQuantLab/quantAnalyticsService.ts
new file mode 100644
index 000000000..12f7b6ef6
--- /dev/null
+++ b/fincept-terminal-desktop/src/services/aiQuantLab/quantAnalyticsService.ts
@@ -0,0 +1,282 @@
+import { invoke } from '@tauri-apps/api/core';
+
+export interface QuantAnalyticsResult<T = Record<string, unknown>> {
+ success: boolean;
+ analysis_type?: string;
+ result?: T;
+ error?: string;
+ traceback?: string;
+ timestamp?: string;
+ available_analyses?: Record<string, Record<string, string>>;
+ available_commands?: string[];
+ [key: string]: unknown;
+}
+
+export interface TrendAnalysisParams extends Record<string, unknown> {
+ data: number[] | string;
+ trend_type?: 'linear' | 'log_linear';
+}
+
+export interface StationarityTestParams extends Record<string, unknown> {
+ data: number[] | string;
+ test_type?: 'adf' | 'kpss' | 'pp';
+}
+
+export interface ARIMAModelParams extends Record<string, unknown> {
+ data: number[] | string;
+ order?: [number, number, number];
+ seasonal_order?: [number, number, number, number];
+}
+
+export interface ForecastingParams extends Record<string, unknown> {
+ data: number[] | string;
+ horizon?: number;
+ method?: 'simple_exponential' | 'linear_trend' | 'moving_average';
+ train_size?: number;
+}
+
+export interface SupervisedLearningParams extends Record<string, unknown> {
+ X: number[][];
+ y: number[];
+ problem_type?: 'regression' | 'classification';
+ algorithms?: string[];
+}
+
+export interface UnsupervisedLearningParams extends Record<string, unknown> {
+ X: number[][];
+ methods?: ('pca' | 'kmeans' | 'hierarchical')[];
+}
+
+export interface ModelEvaluationParams extends Record<string, unknown> {
+ X: number[][];
+ y: number[];
+ model_type?: string;
+ cv_folds?: number;
+}
+
+export interface FeatureEngineeringParams extends Record<string, unknown> {
+ data: Record<string, number[]>;
+ target_column?: string;
+ text_columns?: string[];
+}
+
+export interface SamplingTechniquesParams extends Record<string, unknown> {
+ population: number[];
+ sample_size: number;
+ sampling_methods?: ('simple' | 'stratified' | 'systematic' | 'cluster')[];
+}
+
+export interface CLTParams extends Record<string, unknown> {
+ population_dist?: 'exponential' | 'uniform' | 'binomial' | 'normal';
+ population_params?: Record<string, number>;
+ sample_sizes?: number[];
+ n_samples?: number;
+}
+
+export interface ResamplingParams extends Record<string, unknown> {
+ data: number[];
+ methods?: ('bootstrap' | 'jackknife' | 'permutation')[];
+ n_resamples?: number;
+}
+
+export interface SamplingErrorParams extends Record<string, unknown> {
+ population_mean: number;
+ population_std: number;
+ sample_sizes?: number[];
+ confidence_level?: number;
+ n_simulations?: number;
+}
+
+export interface ValidateDataParams extends Record<string, unknown> {
+ data: number[] | Record<string, number[]>;
+ data_type?: 'general' | 'returns' | 'prices' | 'rates';
+ data_name?: string;
+}
+
+export interface TrendResult {
+ value: {
+ slope: number;
+ intercept: number;
+ };
+ method: string;
+ metadata: {
+ fitted_values: number[];
+ residuals: number[];
+ r_squared: number;
+ slope_t_statistic: number;
+ slope_p_value: number;
+ trend_significant: boolean;
+ trend_direction: 'upward' | 'downward' | 'flat';
+ };
+}
+
+export interface StationarityResult {
+ value: {
+ test_statistic: number;
+ p_value: number;
+ critical_values: Record<string, number>;
+ is_stationary: boolean;
+ null_hypothesis: string;
+ };
+ method: string;
+ metadata: {
+ rolling_mean_stability: number;
+ rolling_std_stability: number;
+ mean_reversion_strength: number;
+ };
+}
+
+export interface SupervisedLearningResult {
+ value: Record<string, {
+ mse?: number;
+ rmse?: number;
+ r2_score?: number;
+ accuracy?: number;
+ precision?: number;
+ recall?: number;
+ predictions: number[];
+ error?: string;
+ }>;
+ method: string;
+ metadata: {
+ train_size: number;
+ test_size: number;
+ n_features: number;
+ best_algorithm: string | null;
+ };
+}
+
+export interface UnsupervisedLearningResult {
+ value: {
+ pca?: {
+ explained_variance_ratio: number[];
+ cumulative_variance: number[];
+ components_for_95_variance: number;
+ principal_components: number[][];
+ };
+ kmeans?: {
+ optimal_k: number;
+ cluster_labels: number[];
+ cluster_centers: number[][];
+ inertia_by_k: [number, number][];
+ silhouette_by_k: [number, number][];
+ };
+ hierarchical?: {
+ cluster_labels: number[];
+ n_clusters: number;
+ };
+ };
+ method: string;
+ metadata: {
+ n_samples: number;
+ n_features: number;
+ data_scaled: boolean;
+ };
+}
+
+export interface DataQualityResult {
+ data_name: string;
+ timestamp: string;
+ quality_score: number;
+ issues: Array<{
+ type: string;
+ description: string;
+ severity: string;
+ timestamp: string;
+ }>;
+ warnings: Array<{
+ type: string;
+ message: string;
+ timestamp: string;
+ }>;
+ statistics: Record<string, unknown>;
+ recommendations: string[];
+}
+
+async function executeQuantAnalytics<T>(
+ command: string,
+ params?: Record<string, unknown>
+): Promise<QuantAnalyticsResult<T>> {
+ try {
+ const paramsJson = params ? JSON.stringify(params) : undefined;
+
+ const result = await invoke<string>('execute_quant_analytics', {
+ command,
+ params: paramsJson,
+ });
+
+ return JSON.parse(result) as QuantAnalyticsResult<T>;
+ } catch (error) {
+ return {
+ success: false,
+ error: error instanceof Error ? error.message : String(error),
+ };
+ }
+}
+
+export const quantAnalyticsService = {
+ listAvailableAnalyses(): Promise<QuantAnalyticsResult> {
+ return executeQuantAnalytics('list');
+ },
+
+ analyzeTrend(params: TrendAnalysisParams): Promise<QuantAnalyticsResult<TrendResult>> {
+ return executeQuantAnalytics('trend_analysis', params);
+ },
+
+ testStationarity(params: StationarityTestParams): Promise<QuantAnalyticsResult<StationarityResult>> {
+ return executeQuantAnalytics('stationarity_test', params);
+ },
+
+ fitARIMAModel(params: ARIMAModelParams): Promise<QuantAnalyticsResult> {
+ return executeQuantAnalytics('arima_model', params);
+ },
+
+ generateForecasts(params: ForecastingParams): Promise<QuantAnalyticsResult> {
+ return executeQuantAnalytics('forecasting', params);
+ },
+
+ supervisedLearning(params: SupervisedLearningParams): Promise<QuantAnalyticsResult<SupervisedLearningResult>> {
+ return executeQuantAnalytics('supervised_learning', params);
+ },
+
+ unsupervisedLearning(params: UnsupervisedLearningParams): Promise<QuantAnalyticsResult<UnsupervisedLearningResult>> {
+ return executeQuantAnalytics('unsupervised_learning', params);
+ },
+
+ evaluateModel(params: ModelEvaluationParams): Promise<QuantAnalyticsResult> {
+ return executeQuantAnalytics('model_evaluation', params);
+ },
+
+ featureEngineering(params: FeatureEngineeringParams): Promise<QuantAnalyticsResult> {
+ return executeQuantAnalytics('feature_engineering', params);
+ },
+
+ samplingTechniques(params: SamplingTechniquesParams): Promise<QuantAnalyticsResult> {
+ return executeQuantAnalytics('sampling_techniques', params);
+ },
+
+ demonstrateCLT(params: CLTParams): Promise<QuantAnalyticsResult> {
+ return executeQuantAnalytics('central_limit_theorem', params);
+ },
+
+ resamplingMethods(params: ResamplingParams): Promise<QuantAnalyticsResult> {
+ return executeQuantAnalytics('resampling_methods', params);
+ },
+
+ samplingErrorAnalysis(params: SamplingErrorParams): Promise<QuantAnalyticsResult> {
+ return executeQuantAnalytics('sampling_error_analysis', params);
+ },
+
+ validateData(params: ValidateDataParams): Promise<QuantAnalyticsResult<DataQualityResult>> {
+ return executeQuantAnalytics('validate_data', params);
+ },
+
+ executeCustomCommand<T>(
+ command: string,
+ params?: Record<string, unknown>
+ ): Promise<QuantAnalyticsResult<T>> {
+ return executeQuantAnalytics<T>(command, params);
+ },
+};
+
+export default quantAnalyticsService;