""" Testing & Validation Dashboard - Dynamic Version Comprehensive testing interface with real test execution: - Live model validation tests - Dynamic performance benchmarks - Real-time chaos engineering experiments - Actual integration test results """ import streamlit as st import pandas as pd import numpy as np import plotly.graph_objects as go import plotly.express as px from plotly.subplots import make_subplots import time from datetime import datetime # Page config st.set_page_config( page_title="Testing & Validation", page_icon="", layout="wide", ) st.title("Testing & Validation Dashboard") st.markdown("Live testing with actual model validation and performance benchmarks") # Tabs for different testing types tab1, tab2, tab3, tab4 = st.tabs([ " Live Model Tests", " Performance Benchmarks", " Stress Testing", " Integration Tests" ]) # ============================================================================ # Tab 1: Live Model Tests # ============================================================================ with tab1: st.header("Live Model Validation") col1, col2 = st.columns(2) with col1: test_categories = st.multiselect( "Select Test Categories", ["Options Pricing", "Portfolio Optimization", "Risk Models", "Macro Models"], default=["Options Pricing", "Portfolio Optimization"] ) with col2: tolerance = st.slider("Error Tolerance (%)", 0.1, 10.0, 2.0, 0.1) if st.button(" Run Live Tests", type="primary"): progress_bar = st.progress(0) status_text = st.empty() # Container for results results_container = st.container() validation_results = [] # Test Options Pricing if "Options Pricing" in test_categories: status_text.text("Testing Options Pricing Models...") progress_bar.progress(0.25) try: from quantlib_pro.options.black_scholes import BlackScholesModel from quantlib_pro.options.bachelier import BachelierModel # Black-Scholes tests bs_model = BlackScholesModel( spot=100, strike=100, rate=0.05, dividend=0.02, volatility=0.2, time_to_expiry=0.25 ) # Test ATM call price call_price = bs_model.call_price() expected_range = (6.0, 8.0) # Reasonable range for ATM call bs_call_passed = expected_range[0] <= call_price <= expected_range[1] validation_results.append({ 'Model': 'Black-Scholes', 'Test': 'ATM Call Price Range', 'Expected': f"{expected_range[0]:.2f}-{expected_range[1]:.2f}", 'Actual': f"{call_price:.2f}", 'Error %': 0.0 if bs_call_passed else 100.0, 'Passed': bs_call_passed }) # Test put-call parity put_price = bs_model.put_price() parity_diff = abs(call_price - put_price - (100 - 100*np.exp(-0.05*0.25))) parity_passed = parity_diff < 0.01 validation_results.append({ 'Model': 'Black-Scholes', 'Test': 'Put-Call Parity', 'Expected': '< 0.01', 'Actual': f"{parity_diff:.4f}", 'Error %': parity_diff * 100, 'Passed': parity_passed }) # Test Greeks delta = bs_model.delta_call() delta_passed = 0.4 <= delta <= 0.7 # Reasonable ATM delta range validation_results.append({ 'Model': 'Black-Scholes', 'Test': 'Call Delta Range', 'Expected': '0.40-0.70', 'Actual': f"{delta:.4f}", 'Error %': 0.0 if delta_passed else 50.0, 'Passed': delta_passed }) # Bachelier tests bachelier_model = BachelierModel(sigma=20.0) bach_call = bachelier_model.price( F0=100, K=100, T=0.25, option_type='call' ) bach_call_passed = 5.0 <= bach_call <= 15.0 validation_results.append({ 'Model': 'Bachelier', 'Test': 'ATM Call Price Range', 'Expected': '5.00-15.00', 'Actual': f"{bach_call:.2f}", 'Error %': 0.0 if bach_call_passed else 100.0, 'Passed': bach_call_passed }) except Exception as e: st.error(f"Options pricing tests failed: {e}") # Test Portfolio Optimization if "Portfolio Optimization" in test_categories: status_text.text("Testing Portfolio Optimization...") progress_bar.progress(0.5) try: from quantlib_pro.portfolio.optimizer import PortfolioOptimizer # Generate test data n_assets = 5 returns = np.random.multivariate_normal( mean=np.array([0.08, 0.10, 0.12, 0.06, 0.09]), cov=np.random.rand(n_assets, n_assets) * 0.01 + np.eye(n_assets) * 0.04, size=252 ) expected_returns = pd.Series(returns.mean(axis=0)) cov_matrix = pd.DataFrame(np.cov(returns.T)) optimizer = PortfolioOptimizer(expected_returns, cov_matrix) # Test max Sharpe portfolio max_sharpe = optimizer.max_sharpe() weights_sum = abs(max_sharpe.weights.sum() - 1.0) weights_passed = weights_sum < 0.001 validation_results.append({ 'Model': 'Portfolio Optimizer', 'Test': 'Weights Sum to 1', 'Expected': '1.000', 'Actual': f"{max_sharpe.weights.sum():.3f}", 'Error %': weights_sum * 100, 'Passed': weights_passed }) # Test positive Sharpe ratio sharpe_passed = max_sharpe.sharpe_ratio > 0 validation_results.append({ 'Model': 'Portfolio Optimizer', 'Test': 'Positive Sharpe Ratio', 'Expected': '> 0', 'Actual': f"{max_sharpe.sharpe_ratio:.3f}", 'Error %': 0.0 if sharpe_passed else 100.0, 'Passed': sharpe_passed }) # Test min variance portfolio min_var = optimizer.min_variance() min_var_passed = min_var.volatility < max_sharpe.volatility validation_results.append({ 'Model': 'Portfolio Optimizer', 'Test': 'Min Var < Max Sharpe Vol', 'Expected': f"< {max_sharpe.volatility:.3f}", 'Actual': f"{min_var.volatility:.3f}", 'Error %': 0.0 if min_var_passed else 50.0, 'Passed': min_var_passed }) except Exception as e: st.error(f"Portfolio optimization tests failed: {e}") # Test Risk Models if "Risk Models" in test_categories: status_text.text("Testing Risk Models...") progress_bar.progress(0.75) try: from quantlib_pro.risk.var import calculate_var # Generate test portfolio returns returns = np.random.normal(-0.0005, 0.02, 1000) # Daily returns # Test VaR calculation var_95 = calculate_var(returns, confidence_level=0.95, method="historical") var_99 = calculate_var(returns, confidence_level=0.99, method="historical") # VaR 99% should be more negative than VaR 95% var_ordering_passed = var_99.var < var_95.var # More negative validation_results.append({ 'Model': 'VaR', 'Test': 'VaR 99% < VaR 95%', 'Expected': f"< {var_95.var:.4f}", 'Actual': f"{var_99.var:.4f}", 'Error %': 0.0 if var_ordering_passed else 100.0, 'Passed': var_ordering_passed }) # CVaR should be more negative than VaR cvar_passed = var_95.cvar < var_95.var validation_results.append({ 'Model': 'VaR', 'Test': 'CVaR < VaR', 'Expected': f"< {var_95.var:.4f}", 'Actual': f"{var_95.cvar:.4f}", 'Error %': 0.0 if cvar_passed else 100.0, 'Passed': cvar_passed }) except Exception as e: st.error(f"Risk model tests failed: {e}") progress_bar.progress(1.0) status_text.text("Tests completed!") # Display results if validation_results: val_df = pd.DataFrame(validation_results) # Summary metrics total_tests = len(val_df) passed_tests = val_df['Passed'].sum() pass_rate = passed_tests / total_tests * 100 col1, col2, col3, col4 = st.columns(4) with col1: st.metric("Total Tests", total_tests) with col2: st.metric("Passed", passed_tests, delta=f"{pass_rate:.1f}%") with col3: failed_tests = total_tests - passed_tests st.metric("Failed", failed_tests) with col4: timestamp = datetime.now().strftime("%H:%M:%S") st.metric("Last Run", timestamp) # Styled results table def highlight_passed(row): return ['background-color: #d4edda' if row['Passed'] else 'background-color: #f8d7da' for _ in row] styled_df = val_df.style.apply(highlight_passed, axis=1) st.dataframe(styled_df, use_container_width=True) # Pass rate by model if len(val_df) > 0: model_stats = val_df.groupby('Model').agg({ 'Passed': ['sum', 'count'] }).round(2) model_stats.columns = ['Passed', 'Total'] model_stats['Pass Rate %'] = (model_stats['Passed'] / model_stats['Total'] * 100).round(1) st.subheader("Pass Rate by Model") st.dataframe(model_stats, use_container_width=True) # ============================================================================ # Tab 2: Performance Benchmarks # ============================================================================ with tab2: st.header("Performance Benchmarks") col1, col2 = st.columns(2) with col1: benchmark_type = st.selectbox( "Benchmark Type", ["Compute Performance", "Memory Usage", "I/O Performance"] ) with col2: iterations = st.number_input("Iterations", 100, 10000, 1000, 100) if st.button(" Run Benchmark", type="primary"): if benchmark_type == "Compute Performance": st.info("Running compute performance benchmark...") # Benchmark matrix operations sizes = [100, 200, 500, 1000] times = [] progress_bar = st.progress(0) for i, size in enumerate(sizes): start_time = time.time() for _ in range(10): a = np.random.rand(size, size) b = np.random.rand(size, size) np.dot(a, b) end_time = time.time() times.append((end_time - start_time) / 10 * 1000) # ms per operation progress_bar.progress((i + 1) / len(sizes)) # Display results benchmark_df = pd.DataFrame({ 'Matrix Size': sizes, 'Avg Time (ms)': times, 'Operations/sec': [1000/t for t in times] }) st.dataframe(benchmark_df, use_container_width=True) # Plot performance fig = px.line(benchmark_df, x='Matrix Size', y='Avg Time (ms)', title="Matrix Multiplication Performance") st.plotly_chart(fig, use_container_width=True) elif benchmark_type == "Memory Usage": st.info("Running memory usage benchmark...") import psutil # Test memory allocation patterns allocation_sizes = [1, 10, 50, 100, 500] # MB memory_usage = [] peak_usage = [] progress_bar = st.progress(0) for i, size_mb in enumerate(allocation_sizes): # Get baseline memory baseline = psutil.Process().memory_info().rss / 1024 / 1024 # Allocate memory size_elements = int(size_mb * 1024 * 1024 / 8) # 8 bytes per float64 data = np.random.rand(size_elements) # Measure peak memory peak = psutil.Process().memory_info().rss / 1024 / 1024 memory_usage.append(baseline) peak_usage.append(peak - baseline) # Clean up del data progress_bar.progress((i + 1) / len(allocation_sizes)) # Display results memory_df = pd.DataFrame({ 'Allocation Size (MB)': allocation_sizes, 'Baseline Memory (MB)': memory_usage, 'Additional Usage (MB)': peak_usage, 'Peak Memory (MB)': [b + p for b, p in zip(memory_usage, peak_usage)] }) st.dataframe(memory_df, use_container_width=True) # Plot memory usage fig = px.bar(memory_df, x='Allocation Size (MB)', y='Additional Usage (MB)', title="Memory Allocation Benchmark") st.plotly_chart(fig, use_container_width=True) elif benchmark_type == "I/O Performance": st.info("Running I/O performance benchmark...") import tempfile import os # Test different I/O operations file_sizes = [1, 5, 10, 25] # MB write_times = [] read_times = [] progress_bar = st.progress(0) with tempfile.TemporaryDirectory() as temp_dir: for i, size_mb in enumerate(file_sizes): # Generate test data data_size = size_mb * 1024 * 1024 # bytes test_data = np.random.bytes(data_size) file_path = os.path.join(temp_dir, f"test_{size_mb}mb.bin") # Test write performance start_time = time.time() with open(file_path, 'wb') as f: f.write(test_data) write_time = (time.time() - start_time) * 1000 # ms write_times.append(write_time) # Test read performance start_time = time.time() with open(file_path, 'rb') as f: _ = f.read() read_time = (time.time() - start_time) * 1000 # ms read_times.append(read_time) progress_bar.progress((i + 1) / len(file_sizes)) # Display results io_df = pd.DataFrame({ 'File Size (MB)': file_sizes, 'Write Time (ms)': write_times, 'Read Time (ms)': read_times, 'Write Throughput (MB/s)': [size/(time/1000) for size, time in zip(file_sizes, write_times)], 'Read Throughput (MB/s)': [size/(time/1000) for size, time in zip(file_sizes, read_times)] }) st.dataframe(io_df, use_container_width=True) # Plot I/O performance fig = go.Figure() fig.add_trace(go.Scatter(x=io_df['File Size (MB)'], y=io_df['Write Throughput (MB/s)'], mode='lines+markers', name='Write Throughput')) fig.add_trace(go.Scatter(x=io_df['File Size (MB)'], y=io_df['Read Throughput (MB/s)'], mode='lines+markers', name='Read Throughput')) fig.update_layout(title="I/O Performance Benchmark", xaxis_title="File Size (MB)", yaxis_title="Throughput (MB/s)") st.plotly_chart(fig, use_container_width=True) # ============================================================================ # Tab 3: Stress Testing # ============================================================================ with tab3: st.header("Stress Testing") col1, col2 = st.columns(2) with col1: stress_type = st.selectbox( "Stress Test Type", ["Memory Stress", "CPU Stress", "Concurrent Load"] ) with col2: stress_duration = st.slider("Duration (seconds)", 5, 60, 10) if st.button(" Start Stress Test", type="primary"): progress_bar = st.progress(0) if stress_type == "Memory Stress": st.info("Running memory stress test...") memory_usage = [] for i in range(stress_duration): # Allocate memory data = np.random.rand(1000000) # 8MB per iteration memory_usage.append(len(data) * 8 / 1024 / 1024) # MB time.sleep(1) progress_bar.progress((i + 1) / stress_duration) # Display memory usage over time fig = go.Figure() fig.add_trace(go.Scatter( y=memory_usage, mode='lines+markers', name='Memory Usage (MB)' )) fig.update_layout(title="Memory Usage Over Time", yaxis_title="MB") st.plotly_chart(fig, use_container_width=True) # ============================================================================ # Tab 4: Integration Tests # ============================================================================ with tab4: st.header("Integration Tests") if st.button(" Run Integration Tests", type="primary"): integration_results = [] # Test data provider connectivity try: from quantlib_pro.data.providers_legacy import DataProviderFactory provider = DataProviderFactory.create('simulated', config={'seed': 42}) test_data = provider.fetch_historical('AAPL', '2020-01-01', '2020-12-31') data_test_passed = len(test_data) > 0 and 'Close' in test_data.columns integration_results.append({ 'Component': 'Data Provider', 'Test': 'Fetch Historical Data', 'Status': 'Pass' if data_test_passed else 'Fail', 'Details': f"Retrieved {len(test_data)} rows" if data_test_passed else "No data returned" }) except Exception as e: integration_results.append({ 'Component': 'Data Provider', 'Test': 'Fetch Historical Data', 'Status': 'Fail', 'Details': str(e) }) # Test model integration try: from quantlib_pro.options.black_scholes import BlackScholesModel model = BlackScholesModel(100, 100, 0.05, 0.02, 0.2, 0.25) price = model.call_price() model_test_passed = 0 < price < 100 # Reasonable range integration_results.append({ 'Component': 'Options Models', 'Test': 'BS Model Integration', 'Status': 'Pass' if model_test_passed else 'Fail', 'Details': f"Call price: {price:.2f}" if model_test_passed else "Invalid price" }) except Exception as e: integration_results.append({ 'Component': 'Options Models', 'Test': 'BS Model Integration', 'Status': 'Fail', 'Details': str(e) }) # Display results if integration_results: int_df = pd.DataFrame(integration_results) def highlight_status(row): return ['background-color: #d4edda' if row['Status'] == 'Pass' else 'background-color: #f8d7da' for _ in row] styled_int_df = int_df.style.apply(highlight_status, axis=1) st.dataframe(styled_int_df, use_container_width=True) # Summary total_integration_tests = len(int_df) passed_integration_tests = len(int_df[int_df['Status'] == 'Pass']) col1, col2 = st.columns(2) with col1: st.metric("Integration Tests", total_integration_tests) with col2: pass_rate_int = passed_integration_tests / total_integration_tests * 100 st.metric("Pass Rate", f"{pass_rate_int:.1f}%")