euler314 commited on
Commit
694915a
·
verified ·
1 Parent(s): 90b89d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +605 -763
app.py CHANGED
@@ -4,6 +4,53 @@ import numpy as np
4
  import plotly.graph_objects as go
5
  from scipy.optimize import fsolve
6
  from scipy.stats import gaussian_kde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  # Configure Streamlit for Hugging Face Spaces
9
  st.set_page_config(
@@ -16,14 +63,8 @@ def add_sqrt_support(expr_str):
16
  """Replace 'sqrt(' with 'sp.sqrt(' for sympy compatibility"""
17
  return expr_str.replace('sqrt(', 'sp.sqrt(')
18
 
19
- #############################
20
- # 1) Define the discriminant
21
- #############################
22
-
23
- # Symbolic variables for the cubic discriminant
24
  z_sym, beta_sym, z_a_sym, y_sym = sp.symbols("z beta z_a y", real=True, positive=True)
25
-
26
- # Define coefficients a, b, c, d in terms of z_sym, beta_sym, z_a_sym, y_sym
27
  a_sym = z_sym * z_a_sym
28
  b_sym = z_sym * z_a_sym + z_sym + z_a_sym - z_a_sym*y_sym
29
  c_sym = z_sym + z_a_sym + 1 - y_sym*(beta_sym*z_a_sym + 1 - beta_sym)
@@ -35,233 +76,81 @@ Delta_expr = (
35
  + (c_sym/(3*a_sym) - (b_sym**2)/(9*a_sym**2))**3
36
  )
37
 
38
- # Fast numeric function for the discriminant
39
- discriminant_func = sp.lambdify((z_sym, beta_sym, z_a_sym, y_sym), Delta_expr, "numpy")
40
-
41
- @st.cache_data
42
- def find_z_at_discriminant_zero(z_a, y, beta, z_min, z_max, steps):
43
- """
44
- Scan z in [z_min, z_max] for sign changes in the discriminant,
45
- and return approximated roots (where the discriminant is zero).
46
- """
47
- # Apply the condition for y
48
- y_effective = y if y > 1 else 1/y
49
-
50
- z_grid = np.linspace(z_min, z_max, steps)
51
- disc_vals = discriminant_func(z_grid, beta, z_a, y_effective)
52
- roots_found = []
53
- for i in range(len(z_grid) - 1):
54
- f1, f2 = disc_vals[i], disc_vals[i+1]
55
- if np.isnan(f1) or np.isnan(f2):
56
- continue
57
- if f1 == 0.0:
58
- roots_found.append(z_grid[i])
59
- elif f2 == 0.0:
60
- roots_found.append(z_grid[i+1])
61
- elif f1 * f2 < 0:
62
- zl, zr = z_grid[i], z_grid[i+1]
63
- for _ in range(50):
64
- mid = 0.5 * (zl + zr)
65
- fm = discriminant_func(mid, beta, z_a, y_effective)
66
- if fm == 0:
67
- zl = zr = mid
68
- break
69
- if np.sign(fm) == np.sign(f1):
70
- zl, f1 = mid, fm
71
- else:
72
- zr, f2 = mid, fm
73
- roots_found.append(0.5 * (zl + zr))
74
- return np.array(roots_found)
75
-
76
- @st.cache_data
77
- def sweep_beta_and_find_z_bounds(z_a, y, z_min, z_max, beta_steps, z_steps):
78
- """
79
- For each beta in [0,1] (with beta_steps points), find the minimum and maximum z
80
- for which the discriminant is zero.
81
- Returns: betas, lower z*(β) values, and upper z*(β) values.
82
- """
83
- betas = np.linspace(0, 1, beta_steps)
84
- z_min_values = []
85
- z_max_values = []
86
- for b in betas:
87
- roots = find_z_at_discriminant_zero(z_a, y, b, z_min, z_max, z_steps)
88
- if len(roots) == 0:
89
- z_min_values.append(np.nan)
90
- z_max_values.append(np.nan)
91
- else:
92
- z_min_values.append(np.min(roots))
93
- z_max_values.append(np.max(roots))
94
- return betas, np.array(z_min_values), np.array(z_max_values)
95
-
96
- @st.cache_data
97
- def compute_eigenvalue_support_boundaries(z_a, y, beta_values, n_samples=100, seeds=5):
98
- """
99
- Compute the support boundaries of the eigenvalue distribution by directly
100
- finding the minimum and maximum eigenvalues of B_n = S_n T_n for different beta values.
101
- """
102
- # Apply the condition for y
103
- y_effective = y if y > 1 else 1/y
104
-
105
- min_eigenvalues = np.zeros_like(beta_values)
106
- max_eigenvalues = np.zeros_like(beta_values)
107
-
108
- # Use a progress bar for Streamlit
109
- progress_bar = st.progress(0)
110
- status_text = st.empty()
111
-
112
- for i, beta in enumerate(beta_values):
113
- # Update progress
114
- progress_bar.progress((i + 1) / len(beta_values))
115
- status_text.text(f"Processing β = {beta:.2f} ({i+1}/{len(beta_values)})")
116
-
117
- min_vals = []
118
- max_vals = []
119
 
120
- # Run multiple trials with different seeds for more stable results
121
- for seed in range(seeds):
122
- # Set random seed
123
- np.random.seed(seed * 100 + i)
124
-
125
- # Compute dimension p based on aspect ratio y
126
- n = n_samples
127
- p = int(y_effective * n)
128
-
129
- # Constructing T_n (Population / Shape Matrix)
130
- k = int(np.floor(beta * p))
131
- diag_entries = np.concatenate([
132
- np.full(k, z_a),
133
- np.full(p - k, 1.0)
134
- ])
135
- np.random.shuffle(diag_entries)
136
- T_n = np.diag(diag_entries)
137
-
138
- # Generate the data matrix X with i.i.d. standard normal entries
139
- X = np.random.randn(p, n)
140
-
141
- # Compute the sample covariance matrix S_n = (1/n) * XX^T
142
- S_n = (1 / n) * (X @ X.T)
143
-
144
- # Compute B_n = S_n T_n
145
- B_n = S_n @ T_n
146
-
147
- # Compute eigenvalues of B_n
148
- eigenvalues = np.linalg.eigvalsh(B_n)
149
-
150
- # Find minimum and maximum eigenvalues
151
- min_vals.append(np.min(eigenvalues))
152
- max_vals.append(np.max(eigenvalues))
153
-
154
- # Average over seeds for stability
155
- min_eigenvalues[i] = np.mean(min_vals)
156
- max_eigenvalues[i] = np.mean(max_vals)
157
 
158
- # Clear progress indicators
159
- progress_bar.empty()
160
- status_text.empty()
161
 
162
- return min_eigenvalues, max_eigenvalues
163
-
164
- @st.cache_data
165
- def compute_high_y_curve(betas, z_a, y):
166
- """
167
- Compute the "High y Expression" curve.
168
- """
169
- # Apply the condition for y
170
- y_effective = y if y > 1 else 1/y
171
 
172
- a = z_a
173
- betas = np.array(betas)
174
- denominator = 1 - 2*a
175
- if denominator == 0:
176
- return np.full_like(betas, np.nan)
177
- numerator = -4*a*(a-1)*y_effective*betas - 2*a*y_effective - 2*a*(2*a-1)
178
- return numerator/denominator
179
-
180
- @st.cache_data
181
- def compute_alternate_low_expr(betas, z_a, y):
182
- """
183
- Compute the alternate low expression:
184
- (z_a*y*beta*(z_a-1) - 2*z_a*(1-y) - 2*z_a**2) / (2+2*z_a)
185
- """
186
- # Apply the condition for y
187
- y_effective = y if y > 1 else 1/y
188
 
189
- betas = np.array(betas)
190
- return (z_a * y_effective * betas * (z_a - 1) - 2*z_a*(1 - y_effective) - 2*z_a**2) / (2 + 2*z_a)
191
-
192
- @st.cache_data
193
- def compute_max_k_expression(betas, z_a, y, k_samples=1000):
194
- """
195
- Compute max_{k ∈ (0,∞)} (y*beta*(a-1)*k + (a*k+1)*((y-1)*k-1)) / ((a*k+1)*(k^2+k))
196
- """
197
- # Apply the condition for y
198
- y_effective = y if y > 1 else 1/y
199
 
200
- a = z_a
201
- # Sample k values on a logarithmic scale
202
- k_values = np.logspace(-3, 3, k_samples)
203
-
204
- max_vals = np.zeros_like(betas)
205
- for i, beta in enumerate(betas):
206
- values = np.zeros_like(k_values)
207
- for j, k in enumerate(k_values):
208
- numerator = y_effective*beta*(a-1)*k + (a*k+1)*((y_effective-1)*k-1)
209
- denominator = (a*k+1)*(k**2+k)
210
- if abs(denominator) < 1e-10:
211
- values[j] = np.nan
212
- else:
213
- values[j] = numerator/denominator
214
 
215
- valid_indices = ~np.isnan(values)
216
- if np.any(valid_indices):
217
- max_vals[i] = np.max(values[valid_indices])
218
- else:
219
- max_vals[i] = np.nan
220
-
221
- return max_vals
222
-
223
- @st.cache_data
224
- def compute_min_t_expression(betas, z_a, y, t_samples=1000):
225
- """
226
- Compute min_{t ∈ (-1/a, 0)} (y*beta*(a-1)*t + (a*t+1)*((y-1)*t-1)) / ((a*t+1)*(t^2+t))
227
- """
228
- # Apply the condition for y
229
- y_effective = y if y > 1 else 1/y
230
-
231
- a = z_a
232
- if a <= 0:
233
- return np.full_like(betas, np.nan)
234
 
235
- lower_bound = -1/a + 1e-10 # Avoid division by zero
236
- t_values = np.linspace(lower_bound, -1e-10, t_samples)
237
-
238
- min_vals = np.zeros_like(betas)
239
- for i, beta in enumerate(betas):
240
- values = np.zeros_like(t_values)
241
- for j, t in enumerate(t_values):
242
- numerator = y_effective*beta*(a-1)*t + (a*t+1)*((y_effective-1)*t-1)
243
- denominator = (a*t+1)*(t**2+t)
244
- if abs(denominator) < 1e-10:
245
- values[j] = np.nan
246
- else:
247
- values[j] = numerator/denominator
248
 
249
- valid_indices = ~np.isnan(values)
250
- if np.any(valid_indices):
251
- min_vals[i] = np.min(values[valid_indices])
252
- else:
253
- min_vals[i] = np.nan
254
-
255
- return min_vals
256
 
257
- @st.cache_data
258
- def compute_derivatives(curve, betas):
259
- """Compute first and second derivatives of a curve"""
260
- d1 = np.gradient(curve, betas)
261
- d2 = np.gradient(d1, betas)
262
- return d1, d2
 
263
 
264
- def compute_all_derivatives(betas, z_mins, z_maxs, low_y_curve, high_y_curve, alt_low_expr, custom_curve1=None, custom_curve2=None):
 
265
  """Compute derivatives for all curves"""
266
  derivatives = {}
267
 
@@ -488,65 +377,12 @@ def generate_z_vs_beta_plot(z_a, y, z_min, z_max, beta_steps, z_steps,
488
  )
489
  return fig
490
 
491
- def compute_cubic_roots(z, beta, z_a, y):
492
- """
493
- Compute the roots of the cubic equation for given parameters using SymPy for maximum accuracy.
494
- """
495
- # Apply the condition for y
496
- y_effective = y if y > 1 else 1/y
497
-
498
- # Import SymPy functions
499
- from sympy import symbols, solve, im, re, N, Poly
500
-
501
- # Create a symbolic variable for the equation
502
- s = symbols('s')
503
-
504
- # Coefficients in the form as^3 + bs^2 + cs + d = 0
505
- a = z * z_a
506
- b = z * z_a + z + z_a - z_a*y_effective
507
- c = z + z_a + 1 - y_effective*(beta*z_a + 1 - beta)
508
- d = 1
509
-
510
- # Handle special cases
511
- if abs(a) < 1e-10:
512
- if abs(b) < 1e-10: # Linear case
513
- roots = np.array([-d/c, 0, 0], dtype=complex)
514
- else: # Quadratic case
515
- quad_roots = np.roots([b, c, d])
516
- roots = np.append(quad_roots, 0).astype(complex)
517
- return roots
518
-
519
- try:
520
- # Create the cubic polynomial
521
- cubic_eq = Poly(a*s**3 + b*s**2 + c*s + d, s)
522
-
523
- # Solve the equation symbolically
524
- symbolic_roots = solve(cubic_eq, s)
525
-
526
- # Convert symbolic roots to complex numbers with high precision
527
- numerical_roots = []
528
- for root in symbolic_roots:
529
- # Use SymPy's N function with high precision
530
- numerical_root = complex(N(root, 30))
531
- numerical_roots.append(numerical_root)
532
-
533
- # If we got fewer than 3 roots (due to multiplicity), pad with zeros
534
- while len(numerical_roots) < 3:
535
- numerical_roots.append(0j)
536
-
537
- return np.array(numerical_roots, dtype=complex)
538
-
539
- except Exception as e:
540
- # Fallback to numpy if SymPy has issues
541
- coeffs = [a, b, c, d]
542
- return np.roots(coeffs)
543
-
544
  def track_roots_consistently(z_values, all_roots):
545
  """
546
  Ensure consistent tracking of roots across z values by minimizing discontinuity.
547
  """
548
  n_points = len(z_values)
549
- n_roots = all_roots[0].shape[0]
550
  tracked_roots = np.zeros((n_points, n_roots), dtype=complex)
551
  tracked_roots[0] = all_roots[0]
552
 
@@ -599,7 +435,7 @@ def generate_cubic_discriminant(z, beta, z_a, y_effective):
599
 
600
  def generate_root_plots(beta, y, z_a, z_min, z_max, n_points):
601
  """
602
- Generate Im(s) and Re(s) vs. z plots with improved accuracy using SymPy.
603
  """
604
  if z_a <= 0 or y <= 0 or z_min >= z_max:
605
  st.error("Invalid input parameters.")
@@ -623,7 +459,7 @@ def generate_root_plots(beta, y, z_a, z_min, z_max, n_points):
623
  progress_bar.progress((i + 1) / n_points)
624
  status_text.text(f"Computing roots for z = {z:.3f} ({i+1}/{n_points})")
625
 
626
- # Calculate roots using SymPy
627
  roots = compute_cubic_roots(z, beta, z_a, y)
628
 
629
  # Initial sorting to help with tracking
@@ -689,47 +525,9 @@ def generate_root_plots(beta, y, z_a, z_min, z_max, n_points):
689
 
690
  return fig_im, fig_re, fig_disc
691
 
692
- def analyze_complex_root_structure(beta_values, z, z_a, y):
693
- """
694
- Analyze when the cubic equation switches between having all real roots
695
- and having a complex conjugate pair plus one real root.
696
-
697
- Returns:
698
- - transition_points: beta values where the root structure changes
699
- - structure_types: list indicating whether each interval has all real roots or complex roots
700
- """
701
- # Apply the condition for y
702
- y_effective = y if y > 1 else 1/y
703
-
704
- transition_points = []
705
- structure_types = []
706
-
707
- previous_type = None
708
-
709
- for beta in beta_values:
710
- roots = compute_cubic_roots(z, beta, z_a, y)
711
-
712
- # Check if all roots are real (imaginary parts close to zero)
713
- is_all_real = all(abs(root.imag) < 1e-10 for root in roots)
714
-
715
- current_type = "real" if is_all_real else "complex"
716
-
717
- if previous_type is not None and current_type != previous_type:
718
- # Found a transition point
719
- transition_points.append(beta)
720
- structure_types.append(previous_type)
721
-
722
- previous_type = current_type
723
-
724
- # Add the final interval type
725
- if previous_type is not None:
726
- structure_types.append(previous_type)
727
-
728
- return transition_points, structure_types
729
-
730
  def generate_roots_vs_beta_plots(z, y, z_a, beta_min, beta_max, n_points):
731
  """
732
- Generate Im(s) and Re(s) vs. β plots with improved accuracy using SymPy.
733
  """
734
  if z_a <= 0 or y <= 0 or beta_min >= beta_max:
735
  st.error("Invalid input parameters.")
@@ -753,7 +551,7 @@ def generate_roots_vs_beta_plots(z, y, z_a, beta_min, beta_max, n_points):
753
  progress_bar.progress((i + 1) / n_points)
754
  status_text.text(f"Computing roots for β = {beta:.3f} ({i+1}/{n_points})")
755
 
756
- # Calculate roots using SymPy
757
  roots = compute_cubic_roots(z, beta, z_a, y)
758
 
759
  # Initial sorting to help with tracking
@@ -819,6 +617,41 @@ def generate_roots_vs_beta_plots(z, y, z_a, beta_min, beta_max, n_points):
819
 
820
  return fig_im, fig_re, fig_disc
821
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
822
  def generate_phase_diagram(z_a, y, beta_min=0.0, beta_max=1.0, z_min=-10.0, z_max=10.0,
823
  beta_steps=100, z_steps=100):
824
  """
@@ -888,35 +721,40 @@ def generate_eigenvalue_distribution(beta, y, z_a, n=1000, seed=42):
888
  """
889
  Generate the eigenvalue distribution of B_n = S_n T_n as n→∞
890
  """
891
- # Apply the condition for y
892
- y_effective = y if y > 1 else 1/y
893
-
894
- # Set random seed
895
- np.random.seed(seed)
896
-
897
- # Compute dimension p based on aspect ratio y
898
- p = int(y_effective * n)
899
-
900
- # Constructing T_n (Population / Shape Matrix) - using the approach from the second script
901
- k = int(np.floor(beta * p))
902
- diag_entries = np.concatenate([
903
- np.full(k, z_a),
904
- np.full(p - k, 1.0)
905
- ])
906
- np.random.shuffle(diag_entries)
907
- T_n = np.diag(diag_entries)
908
-
909
- # Generate the data matrix X with i.i.d. standard normal entries
910
- X = np.random.randn(p, n)
911
-
912
- # Compute the sample covariance matrix S_n = (1/n) * XX^T
913
- S_n = (1 / n) * (X @ X.T)
914
-
915
- # Compute B_n = S_n T_n
916
- B_n = S_n @ T_n
917
-
918
- # Compute eigenvalues of B_n
919
- eigenvalues = np.linalg.eigvalsh(B_n)
 
 
 
 
 
920
 
921
  # Use KDE to compute a smooth density estimate
922
  kde = gaussian_kde(eigenvalues)
@@ -945,448 +783,452 @@ def generate_eigenvalue_distribution(beta, y, z_a, n=1000, seed=42):
945
  return fig, eigenvalues
946
 
947
  # ----------------- Streamlit UI -----------------
948
- st.title("Cubic Root Analysis")
949
-
950
- # Define three tabs
951
- tab1, tab2, tab3 = st.tabs(["z*(β) Curves", "Complex Root Analysis", "Differential Analysis"])
952
-
953
- # ----- Tab 1: z*(β) Curves -----
954
- with tab1:
955
- st.header("Eigenvalue Support Boundaries")
956
-
957
- # Cleaner layout with better column organization
958
- col1, col2, col3 = st.columns([1, 1, 2])
959
 
960
- with col1:
961
- z_a_1 = st.number_input("z_a", value=1.0, key="z_a_1")
962
- y_1 = st.number_input("y", value=1.0, key="y_1")
963
-
964
- with col2:
965
- z_min_1 = st.number_input("z_min", value=-10.0, key="z_min_1")
966
- z_max_1 = st.number_input("z_max", value=10.0, key="z_max_1")
967
-
968
- with col1:
969
- method_type = st.radio(
970
- "Calculation Method",
971
- ["Eigenvalue Method", "Discriminant Method"],
972
- index=0 # Default to eigenvalue method
973
- )
974
-
975
- # Advanced settings in collapsed expanders
976
- with st.expander("Method Settings", expanded=False):
977
- if method_type == "Eigenvalue Method":
978
- beta_steps = st.slider("β steps", min_value=21, max_value=101, value=51, step=10,
979
- key="beta_steps_eigen")
980
- n_samples = st.slider("Matrix size (n)", min_value=100, max_value=2000, value=1000,
981
- step=100)
982
- seeds = st.slider("Number of seeds", min_value=1, max_value=10, value=5, step=1)
983
- else:
984
- beta_steps = st.slider("β steps", min_value=51, max_value=501, value=201, step=50,
985
- key="beta_steps")
986
- z_steps = st.slider("z grid steps", min_value=1000, max_value=100000, value=50000,
987
- step=1000, key="z_steps")
988
-
989
- # Curve visibility options
990
- with st.expander("Curve Visibility", expanded=False):
991
- col_vis1, col_vis2 = st.columns(2)
992
- with col_vis1:
993
- show_high_y = st.checkbox("Show High y Expression", value=False, key="show_high_y")
994
- show_max_k = st.checkbox("Show Max k Expression", value=True, key="show_max_k")
995
- with col_vis2:
996
- show_low_y = st.checkbox("Show Low y Expression", value=False, key="show_low_y")
997
- show_min_t = st.checkbox("Show Min t Expression", value=True, key="show_min_t")
998
-
999
- # Custom expressions collapsed by default
1000
- with st.expander("Custom Expression 1 (s-based)", expanded=False):
1001
- st.markdown("""Enter expressions for s = numerator/denominator
1002
- (using variables `y`, `beta`, `z_a`, and `sqrt()`)""")
1003
- st.latex(r"\text{This s will be inserted into:}")
1004
- st.latex(r"\frac{y\beta(z_a-1)\underline{s}+(a\underline{s}+1)((y-1)\underline{s}-1)}{(a\underline{s}+1)(\underline{s}^2 + \underline{s})}")
1005
- s_num = st.text_input("s numerator", value="", key="s_num")
1006
- s_denom = st.text_input("s denominator", value="", key="s_denom")
1007
-
1008
- with st.expander("Custom Expression 2 (direct z(β))", expanded=False):
1009
- st.markdown("""Enter direct expression for z(β) = numerator/denominator
1010
- (using variables `y`, `beta`, `z_a`, and `sqrt()`)""")
1011
- z_num = st.text_input("z(β) numerator", value="", key="z_num")
1012
- z_denom = st.text_input("z(β) denominator", value="", key="z_denom")
1013
-
1014
- # Move show_derivatives to main UI level for better visibility
1015
- with col2:
1016
- show_derivatives = st.checkbox("Show derivatives", value=False)
1017
-
1018
- # Compute button
1019
- if st.button("Compute Curves", key="tab1_button"):
1020
- with col3:
1021
- use_eigenvalue_method = (method_type == "Eigenvalue Method")
1022
- if use_eigenvalue_method:
1023
- fig = generate_z_vs_beta_plot(z_a_1, y_1, z_min_1, z_max_1, beta_steps, None,
1024
- s_num, s_denom, z_num, z_denom, show_derivatives,
1025
- show_high_y, show_low_y, show_max_k, show_min_t,
1026
- use_eigenvalue_method=True, n_samples=n_samples,
1027
- seeds=seeds)
1028
- else:
1029
- fig = generate_z_vs_beta_plot(z_a_1, y_1, z_min_1, z_max_1, beta_steps, z_steps,
1030
- s_num, s_denom, z_num, z_denom, show_derivatives,
1031
- show_high_y, show_low_y, show_max_k, show_min_t,
1032
- use_eigenvalue_method=False)
1033
-
1034
- if fig is not None:
1035
- st.plotly_chart(fig, use_container_width=True)
1036
-
1037
- # Curve explanations in collapsed expander
1038
- with st.expander("Curve Explanations", expanded=False):
1039
- if use_eigenvalue_method:
1040
- st.markdown("""
1041
- - **Upper/Lower Bounds** (Blue): Maximum/minimum eigenvalues of B_n = S_n T_n
1042
- - **Shaded Region**: Eigenvalue support region
1043
- - **High y Expression** (Green): Asymptotic approximation for high y values
1044
- - **Low Expression** (Orange): Alternative asymptotic expression
1045
- - **Max k Expression** (Red): $\\max_{k \\in (0,\\infty)} \\frac{y\\beta (a-1)k + \\bigl(ak+1\\bigr)\\bigl((y-1)k-1\\bigr)}{(ak+1)(k^2+k)}$
1046
- - **Min t Expression** (Purple): $\\min_{t \\in \\left(-\\frac{1}{a},\\, 0\\right)} \\frac{y\\beta (a-1)t + \\bigl(at+1\\bigr)\\bigl((y-1)t-1\\bigr)}{(at+1)(t^2+t)}$
1047
- - **Custom Expression 1** (Magenta): Result from user-defined s substituted into the main formula
1048
- - **Custom Expression 2** (Brown): Direct z(β) expression
1049
- """)
1050
- else:
1051
- st.markdown("""
1052
- - **Upper z*(β)** (Blue): Maximum z value where discriminant is zero
1053
- - **Lower z*(β)** (Blue): Minimum z value where discriminant is zero
1054
- - **High y Expression** (Green): Asymptotic approximation for high y values
1055
- - **Low Expression** (Orange): Alternative asymptotic expression
1056
- - **Max k Expression** (Red): $\\max_{k \\in (0,\\infty)} \\frac{y\\beta (a-1)k + \\bigl(ak+1\\bigr)\\bigl((y-1)k-1\\bigr)}{(ak+1)(k^2+k)}$
1057
- - **Min t Expression** (Purple): $\\min_{t \\in \\left(-\\frac{1}{a},\\, 0\\right)} \\frac{y\\beta (a-1)t + \\bigl(at+1\\bigr)\\bigl((y-1)t-1\\bigr)}{(at+1)(t^2+t)}$
1058
- - **Custom Expression 1** (Magenta): Result from user-defined s substituted into the main formula
1059
- - **Custom Expression 2** (Brown): Direct z(β) expression
1060
- """)
1061
- if show_derivatives:
1062
- st.markdown("""
1063
- Derivatives are shown as:
1064
- - Dashed lines: First derivatives (d/dβ)
1065
- - Dotted lines: Second derivatives (d²/dβ²)
1066
- """)
1067
-
1068
- # ----- Tab 2: Complex Root Analysis -----
1069
- with tab2:
1070
- st.header("Complex Root Analysis")
1071
 
1072
- # Create tabs within the page for different plots
1073
- plot_tabs = st.tabs(["Im{s} vs. z", "Im{s} vs. β", "Phase Diagram", "Eigenvalue Distribution"])
1074
 
1075
- # Tab for Im{s} vs. z plot
1076
- with plot_tabs[0]:
1077
- col1, col2 = st.columns([1, 2])
 
 
 
 
1078
  with col1:
1079
- beta_z = st.number_input("β", value=0.5, min_value=0.0, max_value=1.0, key="beta_tab2_z")
1080
- y_z = st.number_input("y", value=1.0, key="y_tab2_z")
1081
- z_a_z = st.number_input("z_a", value=1.0, key="z_a_tab2_z")
1082
- z_min_z = st.number_input("z_min", value=-10.0, key="z_min_tab2_z")
1083
- z_max_z = st.number_input("z_max", value=10.0, key="z_max_tab2_z")
1084
- with st.expander("Resolution Settings", expanded=False):
1085
- z_points = st.slider("z grid points", min_value=100, max_value=2000, value=500, step=100, key="z_points_z")
1086
- if st.button("Compute Complex Roots vs. z", key="tab2_button_z"):
1087
- with col2:
1088
- fig_im, fig_re, fig_disc = generate_root_plots(beta_z, y_z, z_a_z, z_min_z, z_max_z, z_points)
1089
- if fig_im is not None and fig_re is not None and fig_disc is not None:
1090
- st.plotly_chart(fig_im, use_container_width=True)
1091
- st.plotly_chart(fig_re, use_container_width=True)
1092
- st.plotly_chart(fig_disc, use_container_width=True)
1093
-
1094
- with st.expander("Root Structure Analysis", expanded=False):
1095
- st.markdown("""
1096
- ### Root Structure Explanation
1097
-
1098
- The red dashed vertical lines mark the points where the cubic discriminant equals zero.
1099
- At these points, the cubic equation's root structure changes:
1100
-
1101
- - When the discriminant is positive, the cubic has three distinct real roots.
1102
- - When the discriminant is negative, the cubic has one real root and two complex conjugate roots.
1103
- - When the discriminant is exactly zero, the cubic has at least two equal roots.
1104
-
1105
- These transition points align perfectly with the z*(β) boundary curves from the first tab,
1106
- which represent exactly these transitions in the (β,z) plane.
1107
- """)
1108
-
1109
- # New tab for Im{s} vs. β plot
1110
- with plot_tabs[1]:
1111
- col1, col2 = st.columns([1, 2])
1112
  with col1:
1113
- z_beta = st.number_input("z", value=1.0, key="z_tab2_beta")
1114
- y_beta = st.number_input("y", value=1.0, key="y_tab2_beta")
1115
- z_a_beta = st.number_input("z_a", value=1.0, key="z_a_tab2_beta")
1116
- beta_min = st.number_input("β_min", value=0.0, min_value=0.0, max_value=1.0, key="beta_min_tab2")
1117
- beta_max = st.number_input("β_max", value=1.0, min_value=0.0, max_value=1.0, key="beta_max_tab2")
1118
- with st.expander("Resolution Settings", expanded=False):
1119
- beta_points = st.slider("β grid points", min_value=100, max_value=1000, value=500, step=100, key="beta_points")
1120
- if st.button("Compute Complex Roots vs. β", key="tab2_button_beta"):
1121
- with col2:
1122
- fig_im_beta, fig_re_beta, fig_disc = generate_roots_vs_beta_plots(
1123
- z_beta, y_beta, z_a_beta, beta_min, beta_max, beta_points)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1124
 
1125
- if fig_im_beta is not None and fig_re_beta is not None and fig_disc is not None:
1126
- st.plotly_chart(fig_im_beta, use_container_width=True)
1127
- st.plotly_chart(fig_re_beta, use_container_width=True)
1128
- st.plotly_chart(fig_disc, use_container_width=True)
1129
 
1130
- # Add analysis of transition points
1131
- transition_points, structure_types = analyze_complex_root_structure(
1132
- np.linspace(beta_min, beta_max, beta_points), z_beta, z_a_beta, y_beta)
1133
-
1134
- if transition_points:
1135
- st.subheader("Root Structure Transition Points")
1136
- for i, beta in enumerate(transition_points):
1137
- prev_type = structure_types[i]
1138
- next_type = structure_types[i+1] if i+1 < len(structure_types) else "unknown"
1139
- st.markdown(f"- At β = {beta:.6f}: Transition from {prev_type} roots to {next_type} roots")
1140
- else:
1141
- st.info("No transitions detected in root structure across this β range.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1142
 
1143
- # Explanation
1144
- with st.expander("Analysis Explanation", expanded=False):
1145
- st.markdown("""
1146
- ### Interpreting the Plots
1147
 
1148
- - **Im{s} vs. β**: Shows how the imaginary parts of the roots change with β. When all curves are at Im{s}=0, all roots are real.
1149
- - **Re{s} vs. β**: Shows how the real parts of the roots change with β.
1150
- - **Discriminant Plot**: The cubic discriminant changes sign at points where the root structure changes.
1151
- - When discriminant < 0: The cubic has one real root and two complex conjugate roots.
1152
- - When discriminant > 0: The cubic has three distinct real roots.
1153
- - When discriminant = 0: The cubic has multiple roots (at least two roots are equal).
1154
 
1155
- The vertical red dashed lines mark the transition points where the root structure changes.
1156
- """)
1157
-
1158
- # Tab for Phase Diagram
1159
- with plot_tabs[2]:
1160
- col1, col2 = st.columns([1, 2])
1161
- with col1:
1162
- z_a_phase = st.number_input("z_a", value=1.0, key="z_a_phase")
1163
- y_phase = st.number_input("y", value=1.0, key="y_phase")
1164
- beta_min_phase = st.number_input("β_min", value=0.0, min_value=0.0, max_value=1.0, key="beta_min_phase")
1165
- beta_max_phase = st.number_input("β_max", value=1.0, min_value=0.0, max_value=1.0, key="beta_max_phase")
1166
- z_min_phase = st.number_input("z_min", value=-10.0, key="z_min_phase")
1167
- z_max_phase = st.number_input("z_max", value=10.0, key="z_max_phase")
1168
-
1169
- with st.expander("Resolution Settings", expanded=False):
1170
- beta_steps_phase = st.slider("β grid points", min_value=20, max_value=200, value=100, step=20, key="beta_steps_phase")
1171
- z_steps_phase = st.slider("z grid points", min_value=20, max_value=200, value=100, step=20, key="z_steps_phase")
 
 
 
 
 
 
1172
 
1173
- if st.button("Generate Phase Diagram", key="tab2_button_phase"):
1174
- with col2:
1175
- st.info("Generating phase diagram. This may take a while depending on resolution...")
1176
- fig_phase = generate_phase_diagram(
1177
- z_a_phase, y_phase, beta_min_phase, beta_max_phase, z_min_phase, z_max_phase,
1178
- beta_steps_phase, z_steps_phase)
 
 
 
 
1179
 
1180
- if fig_phase is not None:
1181
- st.plotly_chart(fig_phase, use_container_width=True)
 
 
 
 
 
 
 
 
1182
 
1183
- with st.expander("Phase Diagram Explanation", expanded=False):
1184
- st.markdown("""
1185
- ### Understanding the Phase Diagram
1186
-
1187
- This heatmap shows the regions in the (β, z) plane where:
1188
 
1189
- - **Red Regions**: The cubic equation has all real roots
1190
- - **Blue Regions**: The cubic equation has one real root and two complex conjugate roots
1191
-
1192
- The boundaries between these regions represent values where the discriminant is zero,
1193
- which are the exact same curves as the z*(β) boundaries in the first tab. This phase
1194
- diagram provides a comprehensive view of the eigenvalue support structure.
1195
- """)
1196
-
1197
- # Eigenvalue distribution tab
1198
- with plot_tabs[3]:
1199
- st.subheader("Eigenvalue Distribution for B_n = S_n T_n")
1200
- with st.expander("Simulation Information", expanded=False):
1201
- st.markdown("""
1202
- This simulation generates the eigenvalue distribution of B_n as n→∞, where:
1203
- - B_n = (1/n)XX^T with X being a p×n matrix
1204
- - p/n → y as n→∞
1205
- - The diagonal entries of T_n follow distribution β·δ(z_a) + (1-β)·δ(1)
1206
- """)
1207
-
1208
- col_eigen1, col_eigen2 = st.columns([1, 2])
1209
- with col_eigen1:
1210
- beta_eigen = st.number_input("β", value=0.5, min_value=0.0, max_value=1.0, key="beta_eigen")
1211
- y_eigen = st.number_input("y", value=1.0, key="y_eigen")
1212
- z_a_eigen = st.number_input("z_a", value=1.0, key="z_a_eigen")
1213
- n_samples = st.slider("Number of samples (n)", min_value=100, max_value=2000, value=1000, step=100)
1214
- sim_seed = st.number_input("Random seed", min_value=1, max_value=1000, value=42, step=1)
1215
 
1216
- # Add comparison option
1217
- show_theoretical = st.checkbox("Show theoretical boundaries", value=True)
1218
- show_empirical_stats = st.checkbox("Show empirical statistics", value=True)
1219
-
1220
- if st.button("Generate Eigenvalue Distribution", key="tab2_eigen_button"):
1221
- with col_eigen2:
1222
- # Generate the eigenvalue distribution
1223
- fig_eigen, eigenvalues = generate_eigenvalue_distribution(beta_eigen, y_eigen, z_a_eigen, n=n_samples, seed=sim_seed)
1224
 
1225
- # If requested, compute and add theoretical boundaries
1226
- if show_theoretical:
1227
- # Calculate min and max eigenvalues using the support boundary functions
1228
- betas = np.array([beta_eigen])
1229
- min_eig, max_eig = compute_eigenvalue_support_boundaries(z_a_eigen, y_eigen, betas, n_samples=n_samples, seeds=5)
 
 
 
1230
 
1231
- # Add vertical lines for boundaries
1232
- fig_eigen.add_vline(
1233
- x=min_eig[0],
1234
- line=dict(color="red", width=2, dash="dash"),
1235
- annotation_text="Min theoretical",
1236
- annotation_position="top right"
1237
- )
1238
- fig_eigen.add_vline(
1239
- x=max_eig[0],
1240
- line=dict(color="red", width=2, dash="dash"),
1241
- annotation_text="Max theoretical",
1242
- annotation_position="top left"
1243
- )
1244
-
1245
- # Display the plot
1246
- st.plotly_chart(fig_eigen, use_container_width=True)
1247
-
1248
- # Add comparison of empirical vs theoretical bounds
1249
- if show_theoretical and show_empirical_stats:
1250
- empirical_min = eigenvalues.min()
1251
- empirical_max = eigenvalues.max()
1252
 
1253
- st.markdown("### Comparison of Empirical vs Theoretical Bounds")
1254
- col1, col2, col3 = st.columns(3)
1255
- with col1:
1256
- st.metric("Theoretical Min", f"{min_eig[0]:.4f}")
1257
- st.metric("Theoretical Max", f"{max_eig[0]:.4f}")
1258
- st.metric("Theoretical Width", f"{max_eig[0] - min_eig[0]:.4f}")
1259
- with col2:
1260
- st.metric("Empirical Min", f"{empirical_min:.4f}")
1261
- st.metric("Empirical Max", f"{empirical_max:.4f}")
1262
- st.metric("Empirical Width", f"{empirical_max - empirical_min:.4f}")
1263
- with col3:
1264
- st.metric("Min Difference", f"{empirical_min - min_eig[0]:.4f}")
1265
- st.metric("Max Difference", f"{empirical_max - max_eig[0]:.4f}")
1266
- st.metric("Width Difference", f"{(empirical_max - empirical_min) - (max_eig[0] - min_eig[0]):.4f}")
1267
-
1268
- # Display additional statistics
1269
- if show_empirical_stats:
1270
- st.markdown("### Eigenvalue Statistics")
1271
- col1, col2 = st.columns(2)
1272
- with col1:
1273
- st.metric("Mean", f"{np.mean(eigenvalues):.4f}")
1274
- st.metric("Median", f"{np.median(eigenvalues):.4f}")
1275
- with col2:
1276
- st.metric("Standard Deviation", f"{np.std(eigenvalues):.4f}")
1277
- st.metric("Interquartile Range", f"{np.percentile(eigenvalues, 75) - np.percentile(eigenvalues, 25):.4f}")
1278
-
1279
- # ----- Tab 3: Differential Analysis -----
1280
- with tab3:
1281
- st.header("Differential Analysis vs. β")
1282
- with st.expander("Description", expanded=False):
1283
- st.markdown("This page shows the difference between the Upper (blue) and Lower (lightblue) z*(β) curves, along with their first and second derivatives with respect to β.")
1284
-
1285
- col1, col2 = st.columns([1, 2])
1286
- with col1:
1287
- z_a_diff = st.number_input("z_a", value=1.0, key="z_a_diff")
1288
- y_diff = st.number_input("y", value=1.0, key="y_diff")
1289
- z_min_diff = st.number_input("z_min", value=-10.0, key="z_min_diff")
1290
- z_max_diff = st.number_input("z_max", value=10.0, key="z_max_diff")
1291
-
1292
- diff_method_type = st.radio(
1293
- "Boundary Calculation Method",
1294
- ["Eigenvalue Method", "Discriminant Method"],
1295
- index=0,
1296
- key="diff_method_type"
1297
- )
1298
 
1299
- with st.expander("Resolution Settings", expanded=False):
1300
- if diff_method_type == "Eigenvalue Method":
1301
- beta_steps_diff = st.slider("β steps", min_value=21, max_value=101, value=51, step=10,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1302
  key="beta_steps_diff_eigen")
1303
- diff_n_samples = st.slider("Matrix size (n)", min_value=100, max_value=2000, value=1000,
1304
  step=100, key="diff_n_samples")
1305
- diff_seeds = st.slider("Number of seeds", min_value=1, max_value=10, value=5, step=1,
1306
  key="diff_seeds")
1307
- else:
1308
- beta_steps_diff = st.slider("β steps", min_value=51, max_value=501, value=201, step=50,
1309
  key="beta_steps_diff")
1310
- z_steps_diff = st.slider("z grid steps", min_value=1000, max_value=100000, value=50000,
1311
  step=1000, key="z_steps_diff")
1312
-
1313
- # Add options for curve selection
1314
- st.subheader("Curves to Analyze")
1315
- analyze_upper_lower = st.checkbox("Upper-Lower Difference", value=True)
1316
- analyze_high_y = st.checkbox("High y Expression", value=False)
1317
- analyze_alt_low = st.checkbox("Low y Expression", value=False)
1318
-
1319
- if st.button("Compute Differentials", key="tab3_button"):
1320
- with col2:
1321
- use_eigenvalue_method_diff = (diff_method_type == "Eigenvalue Method")
1322
-
1323
- if use_eigenvalue_method_diff:
1324
- betas_diff = np.linspace(0, 1, beta_steps_diff)
1325
- st.info("Computing eigenvalue support boundaries. This may take a moment...")
1326
- lower_vals, upper_vals = compute_eigenvalue_support_boundaries(
1327
- z_a_diff, y_diff, betas_diff, diff_n_samples, diff_seeds)
1328
- else:
1329
- betas_diff, lower_vals, upper_vals = sweep_beta_and_find_z_bounds(
1330
- z_a_diff, y_diff, z_min_diff, z_max_diff, beta_steps_diff, z_steps_diff)
1331
-
1332
- # Create figure
1333
- fig_diff = go.Figure()
1334
 
1335
- if analyze_upper_lower:
1336
- diff_curve = upper_vals - lower_vals
1337
- d1 = np.gradient(diff_curve, betas_diff)
1338
- d2 = np.gradient(d1, betas_diff)
1339
-
1340
- fig_diff.add_trace(go.Scatter(x=betas_diff, y=diff_curve, mode="lines",
1341
- name="Upper-Lower Difference", line=dict(color="magenta", width=2)))
1342
- fig_diff.add_trace(go.Scatter(x=betas_diff, y=d1, mode="lines",
1343
- name="Upper-Lower d/dβ", line=dict(color="magenta", dash='dash')))
1344
- fig_diff.add_trace(go.Scatter(x=betas_diff, y=d2, mode="lines",
1345
- name="Upper-Lower d²/dβ²", line=dict(color="magenta", dash='dot')))
1346
 
1347
- if analyze_high_y:
1348
- high_y_curve = compute_high_y_curve(betas_diff, z_a_diff, y_diff)
1349
- d1 = np.gradient(high_y_curve, betas_diff)
1350
- d2 = np.gradient(d1, betas_diff)
1351
 
1352
- fig_diff.add_trace(go.Scatter(x=betas_diff, y=high_y_curve, mode="lines",
1353
- name="High y", line=dict(color="green", width=2)))
1354
- fig_diff.add_trace(go.Scatter(x=betas_diff, y=d1, mode="lines",
1355
- name="High y d/dβ", line=dict(color="green", dash='dash')))
1356
- fig_diff.add_trace(go.Scatter(x=betas_diff, y=d2, mode="lines",
1357
- name="High y d²/dβ²", line=dict(color="green", dash='dot')))
1358
-
1359
- if analyze_alt_low:
1360
- alt_low_curve = compute_alternate_low_expr(betas_diff, z_a_diff, y_diff)
1361
- d1 = np.gradient(alt_low_curve, betas_diff)
1362
- d2 = np.gradient(d1, betas_diff)
1363
 
1364
- fig_diff.add_trace(go.Scatter(x=betas_diff, y=alt_low_curve, mode="lines",
1365
- name="Low y", line=dict(color="orange", width=2)))
1366
- fig_diff.add_trace(go.Scatter(x=betas_diff, y=d1, mode="lines",
1367
- name="Low y d/dβ", line=dict(color="orange", dash='dash')))
1368
- fig_diff.add_trace(go.Scatter(x=betas_diff, y=d2, mode="lines",
1369
- name="Low y d²/dβ²", line=dict(color="orange", dash='dot')))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1370
 
1371
- fig_diff.update_layout(
1372
- title="Differential Analysis vs. β" +
1373
- (" (Eigenvalue Method)" if use_eigenvalue_method_diff else " (Discriminant Method)"),
1374
- xaxis_title="β",
1375
- yaxis_title="Value",
1376
- hovermode="x unified",
1377
- showlegend=True,
1378
- legend=dict(
1379
- yanchor="top",
1380
- y=0.99,
1381
- xanchor="left",
1382
- x=0.01
 
1383
  )
1384
- )
1385
- st.plotly_chart(fig_diff, use_container_width=True)
1386
-
1387
- with st.expander("Curve Types", expanded=False):
1388
- st.markdown("""
1389
- - Solid lines: Original curves
1390
- - Dashed lines: First derivatives (d/dβ)
1391
- - Dotted lines: Second derivatives (d²/dβ²)
1392
- """)
 
 
 
4
  import plotly.graph_objects as go
5
  from scipy.optimize import fsolve
6
  from scipy.stats import gaussian_kde
7
+ import os
8
+ import sys
9
+ import subprocess
10
+ import importlib.util
11
+
12
+ # Check if cubic_cpp is built, and build it if not
13
+ def build_cpp_module():
14
+ if not os.path.exists('cubic_cpp.cpp'):
15
+ st.error("C++ source file not found!")
16
+ return False
17
+
18
+ if importlib.util.find_spec("cubic_cpp") is None:
19
+ st.info("Building C++ extension module...")
20
+ try:
21
+ # Simple build command using pybind11
22
+ cmd = [
23
+ sys.executable, "-m", "pip", "install",
24
+ "pybind11", "numpy", "eigen"
25
+ ]
26
+ subprocess.check_call(cmd)
27
+
28
+ # Build the extension
29
+ cmd = [
30
+ sys.executable, "-m", "pip", "install",
31
+ "-v", "--editable", "."
32
+ ]
33
+ subprocess.check_call(cmd)
34
+ st.success("C++ extension module built successfully!")
35
+ except subprocess.CalledProcessError as e:
36
+ st.error(f"Failed to build C++ extension: {e}")
37
+ return False
38
+ return True
39
+
40
+ # Try to import the C++ module
41
+ try:
42
+ import cubic_cpp
43
+ cpp_available = True
44
+ except ImportError:
45
+ if build_cpp_module():
46
+ try:
47
+ import cubic_cpp
48
+ cpp_available = True
49
+ except ImportError:
50
+ st.error("Failed to import C++ module after building.")
51
+ cpp_available = False
52
+ else:
53
+ cpp_available = False
54
 
55
  # Configure Streamlit for Hugging Face Spaces
56
  st.set_page_config(
 
63
  """Replace 'sqrt(' with 'sp.sqrt(' for sympy compatibility"""
64
  return expr_str.replace('sqrt(', 'sp.sqrt(')
65
 
66
+ # Define symbolic variables for the cubic discriminant using SymPy
 
 
 
 
67
  z_sym, beta_sym, z_a_sym, y_sym = sp.symbols("z beta z_a y", real=True, positive=True)
 
 
68
  a_sym = z_sym * z_a_sym
69
  b_sym = z_sym * z_a_sym + z_sym + z_a_sym - z_a_sym*y_sym
70
  c_sym = z_sym + z_a_sym + 1 - y_sym*(beta_sym*z_a_sym + 1 - beta_sym)
 
76
  + (c_sym/(3*a_sym) - (b_sym**2)/(9*a_sym**2))**3
77
  )
78
 
79
+ # Use either C++ or Python implementation for numeric computations
80
+ if cpp_available:
81
+ # Use C++ implementations
82
+ discriminant_func = cubic_cpp.discriminant_func
83
+
84
+ @st.cache_data
85
+ def find_z_at_discriminant_zero(z_a, y, beta, z_min, z_max, steps):
86
+ return cubic_cpp.find_z_at_discriminant_zero(z_a, y, beta, z_min, z_max, steps)
87
+
88
+ @st.cache_data
89
+ def sweep_beta_and_find_z_bounds(z_a, y, z_min, z_max, beta_steps, z_steps):
90
+ return cubic_cpp.sweep_beta_and_find_z_bounds(z_a, y, z_min, z_max, beta_steps, z_steps)
91
+
92
+ @st.cache_data
93
+ def compute_eigenvalue_support_boundaries(z_a, y, beta_values, n_samples=100, seeds=5):
94
+ beta_array = np.array(beta_values)
95
+ return cubic_cpp.compute_eigenvalue_support_boundaries(
96
+ z_a, y, beta_array, n_samples, seeds)
97
+
98
+ @st.cache_data
99
+ def compute_cubic_roots(z, beta, z_a, y):
100
+ return cubic_cpp.compute_cubic_roots(z, beta, z_a, y)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
+ @st.cache_data
103
+ def compute_high_y_curve(betas, z_a, y):
104
+ return cubic_cpp.compute_high_y_curve(betas, z_a, y)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
+ @st.cache_data
107
+ def compute_alternate_low_expr(betas, z_a, y):
108
+ return cubic_cpp.compute_alternate_low_expr(betas, z_a, y)
109
 
110
+ @st.cache_data
111
+ def compute_max_k_expression(betas, z_a, y, k_samples=1000):
112
+ return cubic_cpp.compute_max_k_expression(betas, z_a, y, k_samples)
 
 
 
 
 
 
113
 
114
+ @st.cache_data
115
+ def compute_min_t_expression(betas, z_a, y, t_samples=1000):
116
+ return cubic_cpp.compute_min_t_expression(betas, z_a, y, t_samples)
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
+ @st.cache_data
119
+ def compute_derivatives(curve, betas):
120
+ return cubic_cpp.compute_derivatives(curve, betas)
 
 
 
 
 
 
 
121
 
122
+ @st.cache_data
123
+ def generate_eigenvalue_distribution(beta, y, z_a, n, seed):
124
+ return cubic_cpp.generate_eigenvalue_distribution(beta, y, z_a, n, seed)
 
 
 
 
 
 
 
 
 
 
 
125
 
126
+ else:
127
+ # Original Python implementations (as fallback)
128
+ # Only showing a few key functions for brevity
129
+
130
+ def discriminant_func(z, beta, z_a, y):
131
+ """Fast numeric function for the discriminant"""
132
+ # Apply the condition for y
133
+ y_effective = y if y > 1 else 1/y
 
 
 
 
 
 
 
 
 
 
 
134
 
135
+ # Coefficients
136
+ a = z * z_a
137
+ b = z * z_a + z + z_a - z_a*y_effective
138
+ c = z + z_a + 1 - y_effective*(beta*z_a + 1 - beta)
139
+ d = 1
 
 
 
 
 
 
 
 
140
 
141
+ # Calculate the discriminant
142
+ return ((b*c)/(6*a**2) - (b**3)/(27*a**3) - d/(2*a))**2 + (c/(3*a) - (b**2)/(9*a**2))**3
 
 
 
 
 
143
 
144
+ # ... [rest of Python implementations]
145
+
146
+ # The rest of the app.py remains the same as in the original file
147
+ # This includes the Streamlit UI code and the functions that operate on the data
148
+ # returned by the computational functions.
149
+
150
+ # ... [Original app.py from here]
151
 
152
+ def compute_all_derivatives(betas, z_mins, z_maxs, low_y_curve, high_y_curve, alt_low_expr,
153
+ custom_curve1=None, custom_curve2=None):
154
  """Compute derivatives for all curves"""
155
  derivatives = {}
156
 
 
377
  )
378
  return fig
379
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
380
  def track_roots_consistently(z_values, all_roots):
381
  """
382
  Ensure consistent tracking of roots across z values by minimizing discontinuity.
383
  """
384
  n_points = len(z_values)
385
+ n_roots = 3 # Always 3 roots for cubic
386
  tracked_roots = np.zeros((n_points, n_roots), dtype=complex)
387
  tracked_roots[0] = all_roots[0]
388
 
 
435
 
436
  def generate_root_plots(beta, y, z_a, z_min, z_max, n_points):
437
  """
438
+ Generate Im(s) and Re(s) vs. z plots with improved accuracy using C++.
439
  """
440
  if z_a <= 0 or y <= 0 or z_min >= z_max:
441
  st.error("Invalid input parameters.")
 
459
  progress_bar.progress((i + 1) / n_points)
460
  status_text.text(f"Computing roots for z = {z:.3f} ({i+1}/{n_points})")
461
 
462
+ # Calculate roots using C++ or Python
463
  roots = compute_cubic_roots(z, beta, z_a, y)
464
 
465
  # Initial sorting to help with tracking
 
525
 
526
  return fig_im, fig_re, fig_disc
527
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
528
  def generate_roots_vs_beta_plots(z, y, z_a, beta_min, beta_max, n_points):
529
  """
530
+ Generate Im(s) and Re(s) vs. β plots with improved accuracy using C++.
531
  """
532
  if z_a <= 0 or y <= 0 or beta_min >= beta_max:
533
  st.error("Invalid input parameters.")
 
551
  progress_bar.progress((i + 1) / n_points)
552
  status_text.text(f"Computing roots for β = {beta:.3f} ({i+1}/{n_points})")
553
 
554
+ # Calculate roots using C++ or Python
555
  roots = compute_cubic_roots(z, beta, z_a, y)
556
 
557
  # Initial sorting to help with tracking
 
617
 
618
  return fig_im, fig_re, fig_disc
619
 
620
+ def analyze_complex_root_structure(beta_values, z, z_a, y):
621
+ """
622
+ Analyze when the cubic equation switches between having all real roots
623
+ and having a complex conjugate pair plus one real root.
624
+
625
+ Returns:
626
+ - transition_points: beta values where the root structure changes
627
+ - structure_types: list indicating whether each interval has all real roots or complex roots
628
+ """
629
+ transition_points = []
630
+ structure_types = []
631
+
632
+ previous_type = None
633
+
634
+ for beta in beta_values:
635
+ roots = compute_cubic_roots(z, beta, z_a, y)
636
+
637
+ # Check if all roots are real (imaginary parts close to zero)
638
+ is_all_real = all(abs(root.imag) < 1e-10 for root in roots)
639
+
640
+ current_type = "real" if is_all_real else "complex"
641
+
642
+ if previous_type is not None and current_type != previous_type:
643
+ # Found a transition point
644
+ transition_points.append(beta)
645
+ structure_types.append(previous_type)
646
+
647
+ previous_type = current_type
648
+
649
+ # Add the final interval type
650
+ if previous_type is not None:
651
+ structure_types.append(previous_type)
652
+
653
+ return transition_points, structure_types
654
+
655
  def generate_phase_diagram(z_a, y, beta_min=0.0, beta_max=1.0, z_min=-10.0, z_max=10.0,
656
  beta_steps=100, z_steps=100):
657
  """
 
721
  """
722
  Generate the eigenvalue distribution of B_n = S_n T_n as n→∞
723
  """
724
+ # Use C++ implementation if available
725
+ if cpp_available:
726
+ eigenvalues = cubic_cpp.generate_eigenvalue_distribution(beta, y, z_a, n, seed)
727
+ else:
728
+ # Python implementation (fallback)
729
+ # Apply the condition for y
730
+ y_effective = y if y > 1 else 1/y
731
+
732
+ # Set random seed
733
+ np.random.seed(seed)
734
+
735
+ # Compute dimension p based on aspect ratio y
736
+ p = int(y_effective * n)
737
+
738
+ # Constructing T_n (Population / Shape Matrix)
739
+ k = int(np.floor(beta * p))
740
+ diag_entries = np.concatenate([
741
+ np.full(k, z_a),
742
+ np.full(p - k, 1.0)
743
+ ])
744
+ np.random.shuffle(diag_entries)
745
+ T_n = np.diag(diag_entries)
746
+
747
+ # Generate the data matrix X with i.i.d. standard normal entries
748
+ X = np.random.randn(p, n)
749
+
750
+ # Compute the sample covariance matrix S_n = (1/n) * XX^T
751
+ S_n = (1 / n) * (X @ X.T)
752
+
753
+ # Compute B_n = S_n T_n
754
+ B_n = S_n @ T_n
755
+
756
+ # Compute eigenvalues of B_n
757
+ eigenvalues = np.linalg.eigvalsh(B_n)
758
 
759
  # Use KDE to compute a smooth density estimate
760
  kde = gaussian_kde(eigenvalues)
 
783
  return fig, eigenvalues
784
 
785
  # ----------------- Streamlit UI -----------------
786
+ def main():
787
+ st.title("Cubic Root Analysis")
 
 
 
 
 
 
 
 
 
788
 
789
+ if not cpp_available:
790
+ st.warning("C++ acceleration module not available. Using slower Python implementation instead.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
791
 
792
+ # Define three tabs
793
+ tab1, tab2, tab3, = st.tabs(["z*(β) Curves", "Complex Root Analysis", "Differential Analysis"])
794
 
795
+ # ----- Tab 1: z*(β) Curves -----
796
+ with tab1:
797
+ st.header("Eigenvalue Support Boundaries")
798
+
799
+ # Cleaner layout with better column organization
800
+ col1, col2, col3 = st.columns([1, 1, 2])
801
+
802
  with col1:
803
+ z_a_1 = st.number_input("z_a", value=1.0, key="z_a_1")
804
+ y_1 = st.number_input("y", value=1.0, key="y_1")
805
+
806
+ with col2:
807
+ z_min_1 = st.number_input("z_min", value=-10.0, key="z_min_1")
808
+ z_max_1 = st.number_input("z_max", value=10.0, key="z_max_1")
809
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
810
  with col1:
811
+ method_type = st.radio(
812
+ "Calculation Method",
813
+ ["Eigenvalue Method", "Discriminant Method"],
814
+ index=0 # Default to eigenvalue method
815
+ )
816
+
817
+ # Advanced settings in collapsed expanders
818
+ with st.expander("Method Settings", expanded=False):
819
+ if method_type == "Eigenvalue Method":
820
+ beta_steps = st.slider("β steps", min_value=21, max_value=101, value=51, step=10,
821
+ key="beta_steps_eigen")
822
+ n_samples = st.slider("Matrix size (n)", min_value=100, max_value=2000, value=1000,
823
+ step=100)
824
+ seeds = st.slider("Number of seeds", min_value=1, max_value=10, value=5, step=1)
825
+ else:
826
+ beta_steps = st.slider("β steps", min_value=51, max_value=501, value=201, step=50,
827
+ key="beta_steps")
828
+ z_steps = st.slider("z grid steps", min_value=1000, max_value=100000, value=50000,
829
+ step=1000, key="z_steps")
830
+
831
+ # Curve visibility options
832
+ with st.expander("Curve Visibility", expanded=False):
833
+ col_vis1, col_vis2 = st.columns(2)
834
+ with col_vis1:
835
+ show_high_y = st.checkbox("Show High y Expression", value=False, key="show_high_y")
836
+ show_max_k = st.checkbox("Show Max k Expression", value=True, key="show_max_k")
837
+ with col_vis2:
838
+ show_low_y = st.checkbox("Show Low y Expression", value=False, key="show_low_y")
839
+ show_min_t = st.checkbox("Show Min t Expression", value=True, key="show_min_t")
840
+
841
+ # Custom expressions collapsed by default
842
+ with st.expander("Custom Expression 1 (s-based)", expanded=False):
843
+ st.markdown("""Enter expressions for s = numerator/denominator
844
+ (using variables `y`, `beta`, `z_a`, and `sqrt()`)""")
845
+ st.latex(r"\text{This s will be inserted into:}")
846
+ st.latex(r"\frac{y\beta(z_a-1)\underline{s}+(a\underline{s}+1)((y-1)\underline{s}-1)}{(a\underline{s}+1)(\underline{s}^2 + \underline{s})}")
847
+ s_num = st.text_input("s numerator", value="", key="s_num")
848
+ s_denom = st.text_input("s denominator", value="", key="s_denom")
849
+
850
+ with st.expander("Custom Expression 2 (direct z(β))", expanded=False):
851
+ st.markdown("""Enter direct expression for z(β) = numerator/denominator
852
+ (using variables `y`, `beta`, `z_a`, and `sqrt()`)""")
853
+ z_num = st.text_input("z(β) numerator", value="", key="z_num")
854
+ z_denom = st.text_input("z(β) denominator", value="", key="z_denom")
855
+
856
+ # Move show_derivatives to main UI level for better visibility
857
+ with col2:
858
+ show_derivatives = st.checkbox("Show derivatives", value=False)
859
+
860
+ # Compute button
861
+ if st.button("Compute Curves", key="tab1_button"):
862
+ with col3:
863
+ use_eigenvalue_method = (method_type == "Eigenvalue Method")
864
+ if use_eigenvalue_method:
865
+ fig = generate_z_vs_beta_plot(z_a_1, y_1, z_min_1, z_max_1, beta_steps, None,
866
+ s_num, s_denom, z_num, z_denom, show_derivatives,
867
+ show_high_y, show_low_y, show_max_k, show_min_t,
868
+ use_eigenvalue_method=True, n_samples=n_samples,
869
+ seeds=seeds)
870
+ else:
871
+ fig = generate_z_vs_beta_plot(z_a_1, y_1, z_min_1, z_max_1, beta_steps, z_steps,
872
+ s_num, s_denom, z_num, z_denom, show_derivatives,
873
+ show_high_y, show_low_y, show_max_k, show_min_t,
874
+ use_eigenvalue_method=False)
875
 
876
+ if fig is not None:
877
+ st.plotly_chart(fig, use_container_width=True)
 
 
878
 
879
+ # Curve explanations in collapsed expander
880
+ with st.expander("Curve Explanations", expanded=False):
881
+ if use_eigenvalue_method:
882
+ st.markdown("""
883
+ - **Upper/Lower Bounds** (Blue): Maximum/minimum eigenvalues of B_n = S_n T_n
884
+ - **Shaded Region**: Eigenvalue support region
885
+ - **High y Expression** (Green): Asymptotic approximation for high y values
886
+ - **Low Expression** (Orange): Alternative asymptotic expression
887
+ - **Max k Expression** (Red): $\\max_{k \\in (0,\\infty)} \\frac{y\\beta (a-1)k + \\bigl(ak+1\\bigr)\\bigl((y-1)k-1\\bigr)}{(ak+1)(k^2+k)}$
888
+ - **Min t Expression** (Purple): $\\min_{t \\in \\left(-\\frac{1}{a},\\, 0\\right)} \\frac{y\\beta (a-1)t + \\bigl(at+1\\bigr)\\bigl((y-1)t-1\\bigr)}{(at+1)(t^2+t)}$
889
+ - **Custom Expression 1** (Magenta): Result from user-defined s substituted into the main formula
890
+ - **Custom Expression 2** (Brown): Direct z(β) expression
891
+ """)
892
+ else:
893
+ st.markdown("""
894
+ - **Upper z*(β)** (Blue): Maximum z value where discriminant is zero
895
+ - **Lower z*(β)** (Blue): Minimum z value where discriminant is zero
896
+ - **High y Expression** (Green): Asymptotic approximation for high y values
897
+ - **Low Expression** (Orange): Alternative asymptotic expression
898
+ - **Max k Expression** (Red): $\\max_{k \\in (0,\\infty)} \\frac{y\\beta (a-1)k + \\bigl(ak+1\\bigr)\\bigl((y-1)k-1\\bigr)}{(ak+1)(k^2+k)}$
899
+ - **Min t Expression** (Purple): $\\min_{t \\in \\left(-\\frac{1}{a},\\, 0\\right)} \\frac{y\\beta (a-1)t + \\bigl(at+1\\bigr)\\bigl((y-1)t-1\\bigr)}{(at+1)(t^2+t)}$
900
+ - **Custom Expression 1** (Magenta): Result from user-defined s substituted into the main formula
901
+ - **Custom Expression 2** (Brown): Direct z(β) expression
902
+ """)
903
+ if show_derivatives:
904
+ st.markdown("""
905
+ Derivatives are shown as:
906
+ - Dashed lines: First derivatives (d/dβ)
907
+ - Dotted lines: Second derivatives (d²/dβ²)
908
+ """)
909
+
910
+ # ----- Tab 2: Complex Root Analysis -----
911
+ with tab2:
912
+ st.header("Complex Root Analysis")
913
+
914
+ # Create tabs within the page for different plots
915
+ plot_tabs = st.tabs(["Im{s} vs. z", "Im{s} vs. β", "Phase Diagram", "Eigenvalue Distribution"])
916
+
917
+ # Tab for Im{s} vs. z plot
918
+ with plot_tabs[0]:
919
+ col1, col2 = st.columns([1, 2])
920
+ with col1:
921
+ beta_z = st.number_input("β", value=0.5, min_value=0.0, max_value=1.0, key="beta_tab2_z")
922
+ y_z = st.number_input("y", value=1.0, key="y_tab2_z")
923
+ z_a_z = st.number_input("z_a", value=1.0, key="z_a_tab2_z")
924
+ z_min_z = st.number_input("z_min", value=-10.0, key="z_min_tab2_z")
925
+ z_max_z = st.number_input("z_max", value=10.0, key="z_max_tab2_z")
926
+ with st.expander("Resolution Settings", expanded=False):
927
+ z_points = st.slider("z grid points", min_value=100, max_value=2000, value=500, step=100, key="z_points_z")
928
+ if st.button("Compute Complex Roots vs. z", key="tab2_button_z"):
929
+ with col2:
930
+ fig_im, fig_re, fig_disc = generate_root_plots(beta_z, y_z, z_a_z, z_min_z, z_max_z, z_points)
931
+ if fig_im is not None and fig_re is not None and fig_disc is not None:
932
+ st.plotly_chart(fig_im, use_container_width=True)
933
+ st.plotly_chart(fig_re, use_container_width=True)
934
+ st.plotly_chart(fig_disc, use_container_width=True)
935
+
936
+ with st.expander("Root Structure Analysis", expanded=False):
937
+ st.markdown("""
938
+ ### Root Structure Explanation
939
+
940
+ The red dashed vertical lines mark the points where the cubic discriminant equals zero.
941
+ At these points, the cubic equation's root structure changes:
942
+
943
+ - When the discriminant is positive, the cubic has three distinct real roots.
944
+ - When the discriminant is negative, the cubic has one real root and two complex conjugate roots.
945
+ - When the discriminant is exactly zero, the cubic has at least two equal roots.
946
+
947
+ These transition points align perfectly with the z*(β) boundary curves from the first tab,
948
+ which represent exactly these transitions in the (β,z) plane.
949
+ """)
950
+
951
+ # New tab for Im{s} vs. β plot
952
+ with plot_tabs[1]:
953
+ col1, col2 = st.columns([1, 2])
954
+ with col1:
955
+ z_beta = st.number_input("z", value=1.0, key="z_tab2_beta")
956
+ y_beta = st.number_input("y", value=1.0, key="y_tab2_beta")
957
+ z_a_beta = st.number_input("z_a", value=1.0, key="z_a_tab2_beta")
958
+ beta_min = st.number_input("β_min", value=0.0, min_value=0.0, max_value=1.0, key="beta_min_tab2")
959
+ beta_max = st.number_input("β_max", value=1.0, min_value=0.0, max_value=1.0, key="beta_max_tab2")
960
+ with st.expander("Resolution Settings", expanded=False):
961
+ beta_points = st.slider("β grid points", min_value=100, max_value=1000, value=500, step=100, key="beta_points")
962
+ if st.button("Compute Complex Roots vs. β", key="tab2_button_beta"):
963
+ with col2:
964
+ fig_im_beta, fig_re_beta, fig_disc = generate_roots_vs_beta_plots(
965
+ z_beta, y_beta, z_a_beta, beta_min, beta_max, beta_points)
966
 
967
+ if fig_im_beta is not None and fig_re_beta is not None and fig_disc is not None:
968
+ st.plotly_chart(fig_im_beta, use_container_width=True)
969
+ st.plotly_chart(fig_re_beta, use_container_width=True)
970
+ st.plotly_chart(fig_disc, use_container_width=True)
971
 
972
+ # Add analysis of transition points
973
+ transition_points, structure_types = analyze_complex_root_structure(
974
+ np.linspace(beta_min, beta_max, beta_points), z_beta, z_a_beta, y_beta)
 
 
 
975
 
976
+ if transition_points:
977
+ st.subheader("Root Structure Transition Points")
978
+ for i, beta in enumerate(transition_points):
979
+ prev_type = structure_types[i]
980
+ next_type = structure_types[i+1] if i+1 < len(structure_types) else "unknown"
981
+ st.markdown(f"- At β = {beta:.6f}: Transition from {prev_type} roots to {next_type} roots")
982
+ else:
983
+ st.info("No transitions detected in root structure across this β range.")
984
+
985
+ # Explanation
986
+ with st.expander("Analysis Explanation", expanded=False):
987
+ st.markdown("""
988
+ ### Interpreting the Plots
989
+
990
+ - **Im{s} vs. β**: Shows how the imaginary parts of the roots change with β. When all curves are at Im{s}=0, all roots are real.
991
+ - **Re{s} vs. β**: Shows how the real parts of the roots change with β.
992
+ - **Discriminant Plot**: The cubic discriminant changes sign at points where the root structure changes.
993
+ - When discriminant < 0: The cubic has one real root and two complex conjugate roots.
994
+ - When discriminant > 0: The cubic has three distinct real roots.
995
+ - When discriminant = 0: The cubic has multiple roots (at least two roots are equal).
996
+
997
+ The vertical red dashed lines mark the transition points where the root structure changes.
998
+ """)
999
 
1000
+ # Tab for Phase Diagram
1001
+ with plot_tabs[2]:
1002
+ col1, col2 = st.columns([1, 2])
1003
+ with col1:
1004
+ z_a_phase = st.number_input("z_a", value=1.0, key="z_a_phase")
1005
+ y_phase = st.number_input("y", value=1.0, key="y_phase")
1006
+ beta_min_phase = st.number_input("β_min", value=0.0, min_value=0.0, max_value=1.0, key="beta_min_phase")
1007
+ beta_max_phase = st.number_input("β_max", value=1.0, min_value=0.0, max_value=1.0, key="beta_max_phase")
1008
+ z_min_phase = st.number_input("z_min", value=-10.0, key="z_min_phase")
1009
+ z_max_phase = st.number_input("z_max", value=10.0, key="z_max_phase")
1010
 
1011
+ with st.expander("Resolution Settings", expanded=False):
1012
+ beta_steps_phase = st.slider("β grid points", min_value=20, max_value=200, value=100, step=20, key="beta_steps_phase")
1013
+ z_steps_phase = st.slider("z grid points", min_value=20, max_value=200, value=100, step=20, key="z_steps_phase")
1014
+
1015
+ if st.button("Generate Phase Diagram", key="tab2_button_phase"):
1016
+ with col2:
1017
+ st.info("Generating phase diagram. This may take a while depending on resolution...")
1018
+ fig_phase = generate_phase_diagram(
1019
+ z_a_phase, y_phase, beta_min_phase, beta_max_phase, z_min_phase, z_max_phase,
1020
+ beta_steps_phase, z_steps_phase)
1021
 
1022
+ if fig_phase is not None:
1023
+ st.plotly_chart(fig_phase, use_container_width=True)
 
 
 
1024
 
1025
+ with st.expander("Phase Diagram Explanation", expanded=False):
1026
+ st.markdown("""
1027
+ ### Understanding the Phase Diagram
1028
+
1029
+ This heatmap shows the regions in the (β, z) plane where:
1030
+
1031
+ - **Red Regions**: The cubic equation has all real roots
1032
+ - **Blue Regions**: The cubic equation has one real root and two complex conjugate roots
1033
+
1034
+ The boundaries between these regions represent values where the discriminant is zero,
1035
+ which are the exact same curves as the z*(β) boundaries in the first tab. This phase
1036
+ diagram provides a comprehensive view of the eigenvalue support structure.
1037
+ """)
1038
+
1039
+ # Eigenvalue distribution tab
1040
+ with plot_tabs[3]:
1041
+ st.subheader("Eigenvalue Distribution for B_n = S_n T_n")
1042
+ with st.expander("Simulation Information", expanded=False):
1043
+ st.markdown("""
1044
+ This simulation generates the eigenvalue distribution of B_n as n→∞, where:
1045
+ - B_n = (1/n)XX^T with X being a p×n matrix
1046
+ - p/n y as n→∞
1047
+ - The diagonal entries of T_n follow distribution β·δ(z_a) + (1)·δ(1)
1048
+ """)
 
 
1049
 
1050
+ col_eigen1, col_eigen2 = st.columns([1, 2])
1051
+ with col_eigen1:
1052
+ beta_eigen = st.number_input("β", value=0.5, min_value=0.0, max_value=1.0, key="beta_eigen")
1053
+ y_eigen = st.number_input("y", value=1.0, key="y_eigen")
1054
+ z_a_eigen = st.number_input("z_a", value=1.0, key="z_a_eigen")
1055
+ n_samples = st.slider("Number of samples (n)", min_value=100, max_value=2000, value=1000, step=100)
1056
+ sim_seed = st.number_input("Random seed", min_value=1, max_value=1000, value=42, step=1)
 
1057
 
1058
+ # Add comparison option
1059
+ show_theoretical = st.checkbox("Show theoretical boundaries", value=True)
1060
+ show_empirical_stats = st.checkbox("Show empirical statistics", value=True)
1061
+
1062
+ if st.button("Generate Eigenvalue Distribution", key="tab2_eigen_button"):
1063
+ with col_eigen2:
1064
+ # Generate the eigenvalue distribution
1065
+ fig_eigen, eigenvalues = generate_eigenvalue_distribution(beta_eigen, y_eigen, z_a_eigen, n=n_samples, seed=sim_seed)
1066
 
1067
+ # If requested, compute and add theoretical boundaries
1068
+ if show_theoretical:
1069
+ # Calculate min and max eigenvalues using the support boundary functions
1070
+ betas = np.array([beta_eigen])
1071
+ min_eig, max_eig = compute_eigenvalue_support_boundaries(z_a_eigen, y_eigen, betas, n_samples=n_samples, seeds=5)
1072
+
1073
+ # Add vertical lines for boundaries
1074
+ fig_eigen.add_vline(
1075
+ x=min_eig[0],
1076
+ line=dict(color="red", width=2, dash="dash"),
1077
+ annotation_text="Min theoretical",
1078
+ annotation_position="top right"
1079
+ )
1080
+ fig_eigen.add_vline(
1081
+ x=max_eig[0],
1082
+ line=dict(color="red", width=2, dash="dash"),
1083
+ annotation_text="Max theoretical",
1084
+ annotation_position="top left"
1085
+ )
 
 
1086
 
1087
+ # Display the plot
1088
+ st.plotly_chart(fig_eigen, use_container_width=True)
1089
+
1090
+ # Add comparison of empirical vs theoretical bounds
1091
+ if show_theoretical and show_empirical_stats:
1092
+ empirical_min = eigenvalues.min()
1093
+ empirical_max = eigenvalues.max()
1094
+
1095
+ st.markdown("### Comparison of Empirical vs Theoretical Bounds")
1096
+ col1, col2, col3 = st.columns(3)
1097
+ with col1:
1098
+ st.metric("Theoretical Min", f"{min_eig[0]:.4f}")
1099
+ st.metric("Theoretical Max", f"{max_eig[0]:.4f}")
1100
+ st.metric("Theoretical Width", f"{max_eig[0] - min_eig[0]:.4f}")
1101
+ with col2:
1102
+ st.metric("Empirical Min", f"{empirical_min:.4f}")
1103
+ st.metric("Empirical Max", f"{empirical_max:.4f}")
1104
+ st.metric("Empirical Width", f"{empirical_max - empirical_min:.4f}")
1105
+ with col3:
1106
+ st.metric("Min Difference", f"{empirical_min - min_eig[0]:.4f}")
1107
+ st.metric("Max Difference", f"{empirical_max - max_eig[0]:.4f}")
1108
+ st.metric("Width Difference", f"{(empirical_max - empirical_min) - (max_eig[0] - min_eig[0]):.4f}")
1109
+
1110
+ # Display additional statistics
1111
+ if show_empirical_stats:
1112
+ st.markdown("### Eigenvalue Statistics")
1113
+ col1, col2 = st.columns(2)
1114
+ with col1:
1115
+ st.metric("Mean", f"{np.mean(eigenvalues):.4f}")
1116
+ st.metric("Median", f"{np.median(eigenvalues):.4f}")
1117
+ with col2:
1118
+ st.metric("Standard Deviation", f"{np.std(eigenvalues):.4f}")
1119
+ st.metric("Interquartile Range", f"{np.percentile(eigenvalues, 75) - np.percentile(eigenvalues, 25):.4f}")
1120
+
1121
+ # ----- Tab 3: Differential Analysis -----
1122
+ with tab3:
1123
+ st.header("Differential Analysis vs. β")
1124
+ with st.expander("Description", expanded=False):
1125
+ st.markdown("This page shows the difference between the Upper (blue) and Lower (lightblue) z*(β) curves, along with their first and second derivatives with respect to β.")
 
 
 
 
 
 
1126
 
1127
+ col1, col2 = st.columns([1, 2])
1128
+ with col1:
1129
+ z_a_diff = st.number_input("z_a", value=1.0, key="z_a_diff")
1130
+ y_diff = st.number_input("y", value=1.0, key="y_diff")
1131
+ z_min_diff = st.number_input("z_min", value=-10.0, key="z_min_diff")
1132
+ z_max_diff = st.number_input("z_max", value=10.0, key="z_max_diff")
1133
+
1134
+ diff_method_type = st.radio(
1135
+ "Boundary Calculation Method",
1136
+ ["Eigenvalue Method", "Discriminant Method"],
1137
+ index=0,
1138
+ key="diff_method_type"
1139
+ )
1140
+
1141
+ with st.expander("Resolution Settings", expanded=False):
1142
+ if diff_method_type == "Eigenvalue Method":
1143
+ beta_steps_diff = st.slider("β steps", min_value=21, max_value=101, value=51, step=10,
1144
  key="beta_steps_diff_eigen")
1145
+ diff_n_samples = st.slider("Matrix size (n)", min_value=100, max_value=2000, value=1000,
1146
  step=100, key="diff_n_samples")
1147
+ diff_seeds = st.slider("Number of seeds", min_value=1, max_value=10, value=5, step=1,
1148
  key="diff_seeds")
1149
+ else:
1150
+ beta_steps_diff = st.slider("β steps", min_value=51, max_value=501, value=201, step=50,
1151
  key="beta_steps_diff")
1152
+ z_steps_diff = st.slider("z grid steps", min_value=1000, max_value=100000, value=50000,
1153
  step=1000, key="z_steps_diff")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1154
 
1155
+ # Add options for curve selection
1156
+ st.subheader("Curves to Analyze")
1157
+ analyze_upper_lower = st.checkbox("Upper-Lower Difference", value=True)
1158
+ analyze_high_y = st.checkbox("High y Expression", value=False)
1159
+ analyze_alt_low = st.checkbox("Low y Expression", value=False)
 
 
 
 
 
 
1160
 
1161
+ if st.button("Compute Differentials", key="tab3_button"):
1162
+ with col2:
1163
+ use_eigenvalue_method_diff = (diff_method_type == "Eigenvalue Method")
 
1164
 
1165
+ if use_eigenvalue_method_diff:
1166
+ betas_diff = np.linspace(0, 1, beta_steps_diff)
1167
+ st.info("Computing eigenvalue support boundaries. This may take a moment...")
1168
+ lower_vals, upper_vals = compute_eigenvalue_support_boundaries(
1169
+ z_a_diff, y_diff, betas_diff, diff_n_samples, diff_seeds)
1170
+ else:
1171
+ betas_diff, lower_vals, upper_vals = sweep_beta_and_find_z_bounds(
1172
+ z_a_diff, y_diff, z_min_diff, z_max_diff, beta_steps_diff, z_steps_diff)
1173
+
1174
+ # Create figure
1175
+ fig_diff = go.Figure()
1176
 
1177
+ if analyze_upper_lower:
1178
+ diff_curve = upper_vals - lower_vals
1179
+ d1, d2 = compute_derivatives(diff_curve, betas_diff)
1180
+
1181
+ fig_diff.add_trace(go.Scatter(x=betas_diff, y=diff_curve, mode="lines",
1182
+ name="Upper-Lower Difference", line=dict(color="magenta", width=2)))
1183
+ fig_diff.add_trace(go.Scatter(x=betas_diff, y=d1, mode="lines",
1184
+ name="Upper-Lower d/dβ", line=dict(color="magenta", dash='dash')))
1185
+ fig_diff.add_trace(go.Scatter(x=betas_diff, y=d2, mode="lines",
1186
+ name="Upper-Lower d²/dβ²", line=dict(color="magenta", dash='dot')))
1187
+
1188
+ if analyze_high_y:
1189
+ high_y_curve = compute_high_y_curve(betas_diff, z_a_diff, y_diff)
1190
+ d1, d2 = compute_derivatives(high_y_curve, betas_diff)
1191
+
1192
+ fig_diff.add_trace(go.Scatter(x=betas_diff, y=high_y_curve, mode="lines",
1193
+ name="High y", line=dict(color="green", width=2)))
1194
+ fig_diff.add_trace(go.Scatter(x=betas_diff, y=d1, mode="lines",
1195
+ name="High y d/dβ", line=dict(color="green", dash='dash')))
1196
+ fig_diff.add_trace(go.Scatter(x=betas_diff, y=d2, mode="lines",
1197
+ name="High y d²/dβ²", line=dict(color="green", dash='dot')))
1198
+
1199
+ if analyze_alt_low:
1200
+ alt_low_curve = compute_alternate_low_expr(betas_diff, z_a_diff, y_diff)
1201
+ d1, d2 = compute_derivatives(alt_low_curve, betas_diff)
1202
+
1203
+ fig_diff.add_trace(go.Scatter(x=betas_diff, y=alt_low_curve, mode="lines",
1204
+ name="Low y", line=dict(color="orange", width=2)))
1205
+ fig_diff.add_trace(go.Scatter(x=betas_diff, y=d1, mode="lines",
1206
+ name="Low y d/dβ", line=dict(color="orange", dash='dash')))
1207
+ fig_diff.add_trace(go.Scatter(x=betas_diff, y=d2, mode="lines",
1208
+ name="Low y d²/dβ²", line=dict(color="orange", dash='dot')))
1209
 
1210
+ fig_diff.update_layout(
1211
+ title="Differential Analysis vs. β" +
1212
+ (" (Eigenvalue Method)" if use_eigenvalue_method_diff else " (Discriminant Method)"),
1213
+ xaxis_title="β",
1214
+ yaxis_title="Value",
1215
+ hovermode="x unified",
1216
+ showlegend=True,
1217
+ legend=dict(
1218
+ yanchor="top",
1219
+ y=0.99,
1220
+ xanchor="left",
1221
+ x=0.01
1222
+ )
1223
  )
1224
+ st.plotly_chart(fig_diff, use_container_width=True)
1225
+
1226
+ with st.expander("Curve Types", expanded=False):
1227
+ st.markdown("""
1228
+ - Solid lines: Original curves
1229
+ - Dashed lines: First derivatives (d/dβ)
1230
+ - Dotted lines: Second derivatives (d²/dβ²)
1231
+ """)
1232
+
1233
+ if __name__ == "__main__":
1234
+ main()