-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
492 lines (410 loc) · 19.8 KB
/
app.py
File metadata and controls
492 lines (410 loc) · 19.8 KB
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
import streamlit as st
import json
import plotly.graph_objects as go
import pandas as pd
import numpy as np
from chebychev import ChebychevForecast
from scipy.stats import entropy, gaussian_kde
from datetime import datetime, timedelta
import os
from groq import Groq
# -------------------------------
# 1. Page Configuration & CSS
# -------------------------------
st.set_page_config(
layout="wide",
page_title="Chebyshev Pricing Engine",
page_icon="📊",
initial_sidebar_state="expanded"
)
# Professional Styling
st.markdown(
"""
<style>
.stApp { background-color: #0e1117; }
/* Metrics Styling */
div[data-testid="stMetric"] {
background-color: #1f2937; border: 1px solid #374151;
padding: 15px; border-radius: 8px;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}
[data-testid="stMetricLabel"] { font-size: 13px; color: #9ca3af; font-weight: 500; }
[data-testid="stMetricValue"] {
font-size: 20px !important;
color: #f3f4f6;
font-family: 'Source Code Pro', monospace;
overflow-wrap: break-word;
white-space: pre-wrap;
}
/* Sidebar */
[data-testid="stSidebar"] { background-color: #111827; border-right: 1px solid #374151; }
h1, h2, h3 { font-family: 'Inter', sans-serif; }
/* CUSTOM AI CARD STYLING (Fixes the format issue) */
.ai-card {
background-color: #1f2937;
border: 1px solid #374151;
border-left: 5px solid #3b82f6; /* Blue Accent Line */
border-radius: 8px;
padding: 20px;
margin-top: 10px;
color: #f3f4f6; /* Light Text */
font-size: 16px;
line-height: 1.6;
font-family: 'Inter', sans-serif;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.2);
}
.ai-header {
font-weight: 600;
color: #60a5fa;
margin-bottom: 8px;
display: flex;
align-items: center;
gap: 8px;
}
</style>
""",
unsafe_allow_html=True
)
# -------------------------------
# 2. Helper Functions & State
# -------------------------------
@st.cache_data
def load_tickers():
try:
with open("sector_tickers.json", "r") as tick:
return json.load(tick)
except FileNotFoundError:
return {}
def calculate_kl_divergence(p, q):
p_norm = p / np.sum(p)
q_norm = q / np.sum(q)
return entropy(p_norm, q_norm)
# --- AI ANALYST GENERATOR (For Streaming) ---
def stream_ai_memo(api_key, ticker, price, lower, upper, status, outliers, volatility):
"""
Generator function that yields chunks of text for streaming.
"""
if not api_key:
yield "⚠️ API Key missing. Please enter it in the sidebar or secrets."
return
try:
client = Groq(api_key=api_key)
prompt = f"""
You are a Senior Quantitative Analyst at Insight Partners.
Analyze this data for {ticker}:
- Price: ${price:.2f}
- Chebyshev Bounds (k=10): ${lower:.2f} - ${upper:.2f}
- Regime: {status}
- Volatility Spread: {volatility:.2f}%
- Anomalies Detected: {len(outliers)}
Write a "Flash Note" for the Portfolio Manager.
1. Interpret the regime.
2. Recommend an action (Hold, Mean Reversion Trade, or Hedge).
3. Be concise (max 80 words).
IMPORTANT: Do NOT include a title like "Flash Note". Start directly with the analysis text.
"""
# Request Streaming Response
stream = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[{"role": "user", "content": prompt}],
temperature=2,
max_tokens=250,
stream=True # Enable Streaming
)
for chunk in stream:
if chunk.choices[0].delta.content is not None:
yield chunk.choices[0].delta.content
except Exception as e:
yield f"AI Connection Error: {e}"
# Initialize Session State
if 'analysis_data' not in st.session_state:
st.session_state['analysis_data'] = None
if 'ticker_symbol' not in st.session_state:
st.session_state['ticker_symbol'] = None
if 'ai_memo' not in st.session_state:
st.session_state['ai_memo'] = None
# -------------------------------
# 3. Sidebar & Navigation
# -------------------------------
tickers = load_tickers()
page = st.sidebar.radio("📍 Navigation", ["Dashboard", "Mathematical Framework"])
if page == "Dashboard":
st.markdown("<h1 style='text-align: center; color: white;'>📊 Chebyshev Price Forecast</h1>", unsafe_allow_html=True)
# --- Sidebar Inputs ---
st.sidebar.markdown("---")
st.sidebar.header("⚙️ Asset Configuration")
# --- API KEY LOGIC ---
api_key = st.secrets.get("GROQ_API_KEY")
if not api_key:
api_key = st.sidebar.text_input("🔑 Groq API Key (for AI)", type="password")
if tickers:
sector = st.sidebar.selectbox("Sector", list(tickers.keys()))
company_dict = tickers[sector]
company_name = st.sidebar.selectbox("Asset", list(company_dict.keys()))
ticker_symbol = company_dict[company_name]
st.sidebar.caption(f"🔒 Model Parameter: **k = 10** (Fixed)")
time_view = st.sidebar.radio(
"Timeframe",
["Last Trading Session", "Full History"],
index=0
)
st.sidebar.markdown("###")
col_run, col_reset = st.sidebar.columns(2)
with col_run:
run_clicked = st.button("⚡ Run", type="primary", use_container_width=True)
with col_reset:
reset_clicked = st.button("🔄 Reset", type="secondary", use_container_width=True)
# RESET LOGIC
if reset_clicked:
st.session_state['analysis_data'] = None
st.session_state['ticker_symbol'] = None
st.session_state['ai_memo'] = None # Clear AI memo on reset
st.rerun()
# RUN LOGIC
if run_clicked:
with st.spinner(f"Calculating Chebyshev bounds for {ticker_symbol}..."):
try:
model = ChebychevForecast(ticker=ticker_symbol, k=10)
forecast_data, _ = model._forecast()
st.session_state['analysis_data'] = forecast_data
st.session_state['ticker_symbol'] = ticker_symbol
st.session_state['ai_memo'] = None # Clear old memo on new run
except Exception as e:
st.error(f"Computation Error: {e}")
else:
st.warning("Please upload 'sector_tickers.json' to proceed.")
# --- Main Dashboard Render ---
if st.session_state['analysis_data'] is not None:
full_data = st.session_state['analysis_data']
current_ticker = st.session_state['ticker_symbol']
if time_view == "Last Trading Session":
last_date = full_data.index[-1].date()
plot_data = full_data[full_data.index.date == last_date].copy()
else:
plot_data = full_data.copy()
# --- METRIC CALCULATIONS ---
kl_div = calculate_kl_divergence(plot_data['Close'], plot_data['close_avg'])
plot_data['spread_pct'] = ((plot_data['close_max'] - plot_data['close_min']) / plot_data['Close']) * 100
avg_vol_spread = plot_data['spread_pct'].mean()
last_row = plot_data.iloc[-1]
current_price = last_row['Close']
upper_b = last_row['close_max']
lower_b = last_row['close_min']
if current_price > upper_b:
status = "⚠️ OVERBOUGHT"
status_color = "red"
elif current_price < lower_b:
status = "⚠️ OVERSOLD"
status_color = "red"
else:
status = "✅ STABLE"
status_color = "green"
# --- UI: Metrics Row ---
st.markdown("---")
col1, col2, col3, col4 = st.columns([1, 1, 1, 1.5])
with col1: st.metric("Asset Price", f"${current_price:.2f}")
with col2: st.metric("KL Divergence", f"{kl_div:.2e}")
with col3: st.metric("Avg Vol Spread", f"{avg_vol_spread:.2f}%")
with col4: st.metric("99% Bounds", f"${lower_b:.2f} — ${upper_b:.2f}")
st.markdown(f"**Market Regime:** :{status_color}[**{status}**]")
# --- OUTLIER DETECTION ---
outliers = plot_data[~plot_data['in_range']].copy()
has_breaches = not outliers.empty
# --- AI ANALYST SECTION (STREAMING) ---
st.markdown("###")
# Button to Trigger Generation
if st.button("🤖 Generate AI Analyst Memo"):
# Create a container for the streaming output
with st.container():
st.markdown(f"""
<div class="ai-card">
<div class="ai-header">🤖 AI Flash Note (Live Generation...)</div>
</div>
""", unsafe_allow_html=True)
# Generate the stream
stream = stream_ai_memo(
api_key=api_key,
ticker=current_ticker,
price=current_price,
lower=lower_b,
upper=upper_b,
status=status,
outliers=outliers,
volatility=avg_vol_spread
)
# Stream to UI and capture final text
response = st.write_stream(stream)
# Save to state so it persists
st.session_state['ai_memo'] = response
st.rerun() # Rerun to render the final static card
# Display Persistent Memo (If it exists)
if st.session_state['ai_memo']:
st.markdown(f"""
<div class="ai-card">
<div class="ai-header">🤖 AI Flash Note</div>
{st.session_state['ai_memo']}
</div>
""", unsafe_allow_html=True)
# --- DYNAMIC TABS ---
tab_names = ["📈 Time Series", "🔔 Distribution (KDE)", "📉 Correlation Matrix"]
if has_breaches:
tab_names.append("⚠️ Breach Analysis")
tabs = st.tabs(tab_names)
# ==================================================
# TAB 1: TIME SERIES
# ==================================================
with tabs[0]:
fig = go.Figure()
fig.add_trace(go.Scatter(x=plot_data.index, y=plot_data['close_min'], mode="lines", line=dict(width=0), showlegend=False, hoverinfo='skip'))
fig.add_trace(go.Scatter(x=plot_data.index, y=plot_data['close_max'], mode="lines", line=dict(width=0), fill='tonexty', fillcolor='rgba(46, 204, 113, 0.15)', name="99% Confidence"))
fig.add_trace(go.Scatter(x=plot_data.index, y=plot_data['close_avg'], mode="lines", line=dict(color='orange', dash='dash', width=1), name="Forecast Mean"))
fig.add_trace(go.Scatter(x=plot_data.index, y=plot_data['Close'], mode="lines", line=dict(color='#F8FAFC', width=2), name="Actual Price"))
if has_breaches:
fig.add_trace(go.Scatter(x=outliers.index, y=outliers['Close'], mode="markers", marker=dict(color='#EF4444', size=6, symbol='x'), name="Breach"))
fig.update_layout(height=500, margin=dict(l=20, r=20, t=30, b=20), paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', xaxis=dict(showgrid=False), yaxis=dict(showgrid=True, gridcolor='#374151'), hovermode="x unified", legend=dict(orientation="h", y=1.05, xanchor="right", x=1))
st.plotly_chart(fig, use_container_width=True)
# ==================================================
# TAB 2: DISTRIBUTION
# ==================================================
with tabs[1]:
st.markdown(f"##### Kernel Density Estimation for {ticker_symbol}")
clean_close = plot_data['Close'].dropna()
clean_avg = plot_data['close_avg'].dropna()
kde_close = gaussian_kde(clean_close)
kde_avg = gaussian_kde(clean_avg)
x_range = np.linspace(min(clean_close.min(), clean_avg.min()), max(clean_close.max(), clean_avg.max()), 200)
fig_kde = go.Figure()
fig_kde.add_trace(go.Scatter(x=x_range, y=kde_avg(x_range), mode='lines', fill='tozeroy', name='Forecast Model', line=dict(color='orange', dash='dot', width=1), fillcolor='rgba(255, 165, 0, 0.1)'))
fig_kde.add_trace(go.Scatter(x=x_range, y=kde_close(x_range), mode='lines', fill='tozeroy', name='Actual Price', line=dict(color='#3b82f6', width=2), fillcolor='rgba(59, 130, 246, 0.1)'))
fig_kde.update_layout(height=500, margin=dict(l=20, r=20, t=30, b=20), paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', xaxis_title="Price ($)", yaxis_title="Density", xaxis=dict(showgrid=False, gridcolor='#374151'), yaxis=dict(showgrid=True, gridcolor='#374151'), legend=dict(orientation="h", y=1.05, xanchor="right", x=1))
st.plotly_chart(fig_kde, use_container_width=True)
# ==================================================
# TAB 3: CORRELATION MAP
# ==================================================
with tabs[2]:
st.markdown(f"##### Feature Correlation Matrix for {ticker_symbol}")
numeric_cols = ['Close', 'close_min', 'close_max', 'close_avg', 'price_std', 'price_mu']
available_cols = [c for c in numeric_cols if c in plot_data.columns]
if len(available_cols) > 1:
corr_matrix = plot_data[available_cols].corr()
fig_corr = go.Figure(data=go.Heatmap(
z=corr_matrix.values, x=corr_matrix.columns, y=corr_matrix.index,
colorscale='RdBu_r', zmin=-1, zmax=1,
text=corr_matrix.values.round(2), texttemplate="%{text}", showscale=True
))
fig_corr.update_layout(height=500, paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', title="Pearson Correlation Coefficients")
st.plotly_chart(fig_corr, use_container_width=True)
else:
st.warning("Not enough numeric data available for correlation analysis.")
# ==================================================
# TAB 4: BREACH ANALYSIS (CONDITIONAL)
# ==================================================
if has_breaches:
with tabs[3]:
st.markdown(f"##### ⚠️ Breach Severity Analysis for {ticker_symbol}")
outliers['deviation_amt'] = np.where(
outliers['Close'] > outliers['close_max'],
outliers['Close'] - outliers['close_max'],
outliers['close_min'] - outliers['Close']
)
col_b_plot, col_b_data = st.columns([2, 1])
with col_b_plot:
fig_bar = go.Figure()
fig_bar.add_trace(go.Bar(x=outliers.index, y=outliers['deviation_amt'], marker_color='#EF4444', name='Deviation ($)'))
fig_bar.update_layout(title="Magnitude of Breaches ($)", height=300, paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', yaxis=dict(showgrid=True, gridcolor='#374151'), showlegend=False)
st.plotly_chart(fig_bar, use_container_width=True)
with col_b_data:
st.markdown("###### Breach Log")
export_df = outliers[['Close', 'close_min', 'close_max', 'deviation_amt']].copy()
export_df.columns = ['Price', 'Lower', 'Upper', 'Dev ($)']
st.dataframe(export_df.style.format("{:.2f}"), height=200, use_container_width=True)
csv_breach = export_df.to_csv().encode('utf-8')
st.download_button(
label="📥 Download Breach Report",
data=csv_breach,
file_name=f"{current_ticker}_breaches.csv",
mime="text/csv",
type="primary",
key="breach_dl_btn"
)
# --- General Data Export ---
st.markdown("###")
csv_full = plot_data.to_csv().encode('utf-8')
st.download_button(
label="📥 Download Full Analysis CSV",
data=csv_full,
file_name=f"{current_ticker}_chebyshev_full.csv",
mime="text/csv",
key="full_dl_btn"
)
# ==============================================================
# PAGE 2: MATH FRAMEWORK
# ==============================================================
elif page == "Mathematical Framework":
st.title("📘 Derivation of Chebyshev Price Bounds")
st.markdown("### 1. The Log-Normal Assumption")
st.markdown(
r"""
Financial prices ($P$) are strictly positive and often skewed. We transform them into **log-space** to approximate a symmetric distribution suitable for moment analysis.
Let $L = \ln(P)$, where:
* $\mu_L = E[L]$ (Mean of log-price)
* $\sigma_L = \sqrt{\text{Var}(L)}$ (Std Dev of log-price)
"""
)
st.markdown("### 2. Chebyshev's Inequality")
st.markdown(
r"""
Chebyshev's inequality states that for *any* probability distribution (with finite variance), the probability of a value lying more than $k$ standard deviations from the mean is at most $1/k^2$.
$$
P(|L - \mu_L| \ge k\sigma_L) \le \frac{1}{k^2}
$$
Conversely, the probability of remaining **within** $k$ standard deviations is:
$$
P(|L - \mu_L| \le k\sigma_L) \ge 1 - \frac{1}{k^2}
$$
"""
)
st.markdown("---")
st.markdown("### 3. Algebraic Derivation of Price Bounds")
st.markdown("We solve the inequality for the price $P$ in four steps:")
col_math_L, col_math_R = st.columns([2, 1.5])
with col_math_L:
st.markdown("**Step A: Expand the Modulus**")
st.latex(r"-k\sigma_L \le L - \mu_L \le k\sigma_L")
st.markdown("**Step B: Isolate the Log-Variable ($L$)**")
st.latex(r"\mu_L - k\sigma_L \le L \le \mu_L + k\sigma_L")
st.markdown("**Step C: Substitute Log Definition ($L = \ln P$)**")
st.latex(r"\mu_L - k\sigma_L \le \ln P \le \mu_L + k\sigma_L")
st.markdown("**Step D: Exponentiate to find Price ($P$)**")
st.latex(r"\boxed{e^{\mu_L - k\sigma_L} \le P \le e^{\mu_L + k\sigma_L}}")
with col_math_R:
st.info(
"""
**Why is this useful?**
Unlike Bollinger Bands, the price bounds derived here do not require the data to be Normally distributed.
They provide a "distribution-free" upper and lower bound on the price of the asset.
"""
)
st.markdown(
"""
| k | Confidence Interval ($1 - 1/k^2$) |
| :---: | :--- |
| 2 | 75.00% |
| 5 | 96.00% |
| **10** | **99.00% (Black Swan Safe)** |
"""
)
st.caption("In this model, we fix k=10 to capture extreme market events.")
st.markdown("---")
st.markdown("### 4. KL Divergence (Relative Entropy)")
st.markdown(
r"""
To measure the efficiency of our Forecast Mean against the Actual Price action, we utilize **Kullback-Leibler (KL) Divergence**.
It measures how one probability distribution $P$ (Actual Prices) diverges from a second, expected probability distribution $Q$ (Forecast Model).
$$
D_{KL}(P || Q) = \sum_{x \in X} P(x) \log \left( \frac{P(x)}{Q(x)} \right)
$$
* **Low Divergence ($\approx 0$):** The forecast distribution closely matches the actual market behavior (High Predictability).
* **High Divergence:** The market is behaving erratically compared to the forecast model (Regime Shift or High Volatility).
"""
)