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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
"""
 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}%")