euler314 commited on
Commit
dec1797
·
verified ·
1 Parent(s): 245a98b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -41
app.py CHANGED
@@ -96,7 +96,9 @@ def compute_eigenvalue_support_boundaries(z_a, y, beta_values, n_samples=100, se
96
  Compute the support boundaries of the eigenvalue distribution by directly
97
  finding the minimum and maximum eigenvalues of B_n = S_n T_n for different beta values.
98
  """
99
- y_effective =y
 
 
100
  min_eigenvalues = np.zeros_like(beta_values)
101
  max_eigenvalues = np.zeros_like(beta_values)
102
 
@@ -137,10 +139,10 @@ def compute_eigenvalue_support_boundaries(z_a, y, beta_values, n_samples=100, se
137
  S_n = (1 / n) * (X @ X.T)
138
 
139
  # Compute B_n = S_n T_n
140
- B_n = S_n @ T_n /y_effective
141
 
142
  # Compute eigenvalues of B_n
143
- eigenvalues = np.linalg.eigvalsh(B_n)
144
 
145
  # Find minimum and maximum eigenvalues
146
  min_vals.append(np.min(eigenvalues))
@@ -270,10 +272,12 @@ def compute_all_derivatives(betas, z_mins, z_maxs, low_y_curve, high_y_curve, al
270
  derivatives['low_y'] = compute_derivatives(low_y_curve, betas)
271
 
272
  # High y Expression
273
- derivatives['high_y'] = compute_derivatives(high_y_curve, betas)
 
274
 
275
  # Alternate Low Expression
276
- derivatives['alt_low'] = compute_derivatives(alt_low_expr, betas)
 
277
 
278
  # Custom Expression 1 (if provided)
279
  if custom_curve1 is not None:
@@ -330,6 +334,10 @@ def generate_z_vs_beta_plot(z_a, y, z_min, z_max, beta_steps, z_steps,
330
  s_num_expr=None, s_denom_expr=None,
331
  z_num_expr=None, z_denom_expr=None,
332
  show_derivatives=False,
 
 
 
 
333
  use_eigenvalue_method=True,
334
  n_samples=1000,
335
  seeds=5):
@@ -348,12 +356,12 @@ def generate_z_vs_beta_plot(z_a, y, z_min, z_max, beta_steps, z_steps,
348
  # Use the original discriminant method
349
  betas, z_mins, z_maxs = sweep_beta_and_find_z_bounds(z_a, y, z_min, z_max, beta_steps, z_steps)
350
 
351
- high_y_curve = compute_high_y_curve(betas, z_a, y)
352
- alt_low_expr = compute_alternate_low_expr(betas, z_a, y)
353
 
354
  # Compute the max/min expressions
355
- max_k_curve = compute_max_k_expression(betas, z_a, y)
356
- min_t_curve = compute_min_t_expression(betas, z_a, y)
357
 
358
  # Compute both custom curves
359
  custom_curve1 = None
@@ -367,9 +375,11 @@ def generate_z_vs_beta_plot(z_a, y, z_min, z_max, beta_steps, z_steps,
367
  if show_derivatives:
368
  derivatives = compute_all_derivatives(betas, z_mins, z_maxs, None, high_y_curve,
369
  alt_low_expr, custom_curve1, custom_curve2)
370
- # Calculate derivatives for max_k and min_t curves
371
- max_k_derivatives = compute_derivatives(max_k_curve, betas)
372
- min_t_derivatives = compute_derivatives(min_t_curve, betas)
 
 
373
 
374
  fig = go.Figure()
375
 
@@ -395,17 +405,24 @@ def generate_z_vs_beta_plot(z_a, y, z_min, z_max, beta_steps, z_steps,
395
  fig.add_trace(go.Scatter(x=betas, y=z_mins, mode="markers+lines",
396
  name="Lower z*(β)", line=dict(color='blue')))
397
 
398
- # Removed the Low y Expression trace
399
- fig.add_trace(go.Scatter(x=betas, y=high_y_curve, mode="markers+lines",
400
- name="High y Expression", line=dict(color='green')))
401
- fig.add_trace(go.Scatter(x=betas, y=alt_low_expr, mode="markers+lines",
402
- name="Low Expression", line=dict(color='green')))
 
 
 
 
 
 
 
 
 
403
 
404
- # Add the new max/min curves
405
- fig.add_trace(go.Scatter(x=betas, y=max_k_curve, mode="lines",
406
- name="Max k Expression", line=dict(color='red', width=2)))
407
- fig.add_trace(go.Scatter(x=betas, y=min_t_curve, mode="lines",
408
- name="Min t Expression", line=dict(color='orange', width=2)))
409
 
410
  if custom_curve1 is not None:
411
  fig.add_trace(go.Scatter(x=betas, y=custom_curve1, mode="markers+lines",
@@ -419,31 +436,37 @@ def generate_z_vs_beta_plot(z_a, y, z_min, z_max, beta_steps, z_steps,
419
  curve_info = [
420
  ('upper', 'Upper Bound' if use_eigenvalue_method else 'Upper z*(β)', 'blue'),
421
  ('lower', 'Lower Bound' if use_eigenvalue_method else 'Lower z*(β)', 'lightblue'),
422
- # Removed low_y curve
423
- ('high_y', 'High y', 'green'),
424
- ('alt_low', 'Alt Low', 'orange')
425
  ]
426
 
 
 
 
 
 
427
  if custom_curve1 is not None:
428
  curve_info.append(('custom1', 'Custom 1', 'purple'))
429
  if custom_curve2 is not None:
430
  curve_info.append(('custom2', 'Custom 2', 'magenta'))
431
 
432
  for key, name, color in curve_info:
433
- fig.add_trace(go.Scatter(x=betas, y=derivatives[key][0], mode="lines",
434
- name=f"{name} d/dβ", line=dict(color=color, dash='dash')))
435
- fig.add_trace(go.Scatter(x=betas, y=derivatives[key][1], mode="lines",
436
- name=f"{name} d²/dβ²", line=dict(color=color, dash='dot')))
 
 
 
 
 
 
 
 
437
 
438
- # Add derivatives for max_k and min_t curves
439
- fig.add_trace(go.Scatter(x=betas, y=max_k_derivatives[0], mode="lines",
440
- name="Max k d/dβ", line=dict(color='red', dash='dash')))
441
- fig.add_trace(go.Scatter(x=betas, y=max_k_derivatives[1], mode="lines",
442
- name="Max k d²/dβ²", line=dict(color='red', dash='dot')))
443
- fig.add_trace(go.Scatter(x=betas, y=min_t_derivatives[0], mode="lines",
444
- name="Min t d/dβ", line=dict(color='orange', dash='dash')))
445
- fig.add_trace(go.Scatter(x=betas, y=min_t_derivatives[1], mode="lines",
446
- name="Min t d²/dβ²", line=dict(color='orange', dash='dot')))
447
 
448
  fig.update_layout(
449
  title="Curves vs β: Eigenvalue Support Boundaries and Asymptotic Expressions" if use_eigenvalue_method
@@ -601,9 +624,9 @@ with tab1:
601
  # Advanced settings in collapsed expanders
602
  with st.expander("Method Settings", expanded=False):
603
  if method_type == "Eigenvalue Method":
604
- beta_steps = st.slider("β steps", min_value=21, max_value=10001, value=51, step=10,
605
  key="beta_steps_eigen")
606
- n_samples = st.slider("Matrix size (n)", min_value=100, max_value=200000, value=1000,
607
  step=100)
608
  seeds = st.slider("Number of seeds", min_value=1, max_value=10, value=5, step=1)
609
  else:
@@ -612,6 +635,16 @@ with tab1:
612
  z_steps = st.slider("z grid steps", min_value=1000, max_value=100000, value=50000,
613
  step=1000, key="z_steps")
614
 
 
 
 
 
 
 
 
 
 
 
615
  # Custom expressions collapsed by default
616
  with st.expander("Custom Expression 1 (s-based)", expanded=False):
617
  st.markdown("""Enter expressions for s = numerator/denominator
@@ -637,12 +670,14 @@ with tab1:
637
  use_eigenvalue_method = (method_type == "Eigenvalue Method")
638
  if use_eigenvalue_method:
639
  fig = generate_z_vs_beta_plot(z_a_1, y_1, z_min_1, z_max_1, beta_steps, None,
640
- s_num, s_denom, z_num, z_denom, show_derivatives,
 
641
  use_eigenvalue_method=True, n_samples=n_samples,
642
  seeds=seeds)
643
  else:
644
  fig = generate_z_vs_beta_plot(z_a_1, y_1, z_min_1, z_max_1, beta_steps, z_steps,
645
  s_num, s_denom, z_num, z_denom, show_derivatives,
 
646
  use_eigenvalue_method=False)
647
 
648
  if fig is not None:
 
96
  Compute the support boundaries of the eigenvalue distribution by directly
97
  finding the minimum and maximum eigenvalues of B_n = S_n T_n for different beta values.
98
  """
99
+ # Apply the condition for y
100
+ y_effective = y if y > 1 else 1/y
101
+
102
  min_eigenvalues = np.zeros_like(beta_values)
103
  max_eigenvalues = np.zeros_like(beta_values)
104
 
 
139
  S_n = (1 / n) * (X @ X.T)
140
 
141
  # Compute B_n = S_n T_n
142
+ B_n = S_n @ T_n
143
 
144
  # Compute eigenvalues of B_n
145
+ eigenvalues = np.linalg.eigvalsh(B_n)
146
 
147
  # Find minimum and maximum eigenvalues
148
  min_vals.append(np.min(eigenvalues))
 
272
  derivatives['low_y'] = compute_derivatives(low_y_curve, betas)
273
 
274
  # High y Expression
275
+ if high_y_curve is not None:
276
+ derivatives['high_y'] = compute_derivatives(high_y_curve, betas)
277
 
278
  # Alternate Low Expression
279
+ if alt_low_expr is not None:
280
+ derivatives['alt_low'] = compute_derivatives(alt_low_expr, betas)
281
 
282
  # Custom Expression 1 (if provided)
283
  if custom_curve1 is not None:
 
334
  s_num_expr=None, s_denom_expr=None,
335
  z_num_expr=None, z_denom_expr=None,
336
  show_derivatives=False,
337
+ show_high_y=False,
338
+ show_low_y=False,
339
+ show_max_k=True,
340
+ show_min_t=True,
341
  use_eigenvalue_method=True,
342
  n_samples=1000,
343
  seeds=5):
 
356
  # Use the original discriminant method
357
  betas, z_mins, z_maxs = sweep_beta_and_find_z_bounds(z_a, y, z_min, z_max, beta_steps, z_steps)
358
 
359
+ high_y_curve = compute_high_y_curve(betas, z_a, y) if show_high_y else None
360
+ alt_low_expr = compute_alternate_low_expr(betas, z_a, y) if show_low_y else None
361
 
362
  # Compute the max/min expressions
363
+ max_k_curve = compute_max_k_expression(betas, z_a, y) if show_max_k else None
364
+ min_t_curve = compute_min_t_expression(betas, z_a, y) if show_min_t else None
365
 
366
  # Compute both custom curves
367
  custom_curve1 = None
 
375
  if show_derivatives:
376
  derivatives = compute_all_derivatives(betas, z_mins, z_maxs, None, high_y_curve,
377
  alt_low_expr, custom_curve1, custom_curve2)
378
+ # Calculate derivatives for max_k and min_t curves if they exist
379
+ if show_max_k:
380
+ max_k_derivatives = compute_derivatives(max_k_curve, betas)
381
+ if show_min_t:
382
+ min_t_derivatives = compute_derivatives(min_t_curve, betas)
383
 
384
  fig = go.Figure()
385
 
 
405
  fig.add_trace(go.Scatter(x=betas, y=z_mins, mode="markers+lines",
406
  name="Lower z*(β)", line=dict(color='blue')))
407
 
408
+ # Add High y Expression only if selected
409
+ if show_high_y and high_y_curve is not None:
410
+ fig.add_trace(go.Scatter(x=betas, y=high_y_curve, mode="markers+lines",
411
+ name="High y Expression", line=dict(color='green')))
412
+
413
+ # Add Low Expression only if selected
414
+ if show_low_y and alt_low_expr is not None:
415
+ fig.add_trace(go.Scatter(x=betas, y=alt_low_expr, mode="markers+lines",
416
+ name="Low Expression", line=dict(color='green')))
417
+
418
+ # Add the max/min curves if selected
419
+ if show_max_k and max_k_curve is not None:
420
+ fig.add_trace(go.Scatter(x=betas, y=max_k_curve, mode="lines",
421
+ name="Max k Expression", line=dict(color='red', width=2)))
422
 
423
+ if show_min_t and min_t_curve is not None:
424
+ fig.add_trace(go.Scatter(x=betas, y=min_t_curve, mode="lines",
425
+ name="Min t Expression", line=dict(color='orange', width=2)))
 
 
426
 
427
  if custom_curve1 is not None:
428
  fig.add_trace(go.Scatter(x=betas, y=custom_curve1, mode="markers+lines",
 
436
  curve_info = [
437
  ('upper', 'Upper Bound' if use_eigenvalue_method else 'Upper z*(β)', 'blue'),
438
  ('lower', 'Lower Bound' if use_eigenvalue_method else 'Lower z*(β)', 'lightblue'),
 
 
 
439
  ]
440
 
441
+ if show_high_y and high_y_curve is not None:
442
+ curve_info.append(('high_y', 'High y', 'green'))
443
+ if show_low_y and alt_low_expr is not None:
444
+ curve_info.append(('alt_low', 'Alt Low', 'orange'))
445
+
446
  if custom_curve1 is not None:
447
  curve_info.append(('custom1', 'Custom 1', 'purple'))
448
  if custom_curve2 is not None:
449
  curve_info.append(('custom2', 'Custom 2', 'magenta'))
450
 
451
  for key, name, color in curve_info:
452
+ if key in derivatives:
453
+ fig.add_trace(go.Scatter(x=betas, y=derivatives[key][0], mode="lines",
454
+ name=f"{name} d/dβ", line=dict(color=color, dash='dash')))
455
+ fig.add_trace(go.Scatter(x=betas, y=derivatives[key][1], mode="lines",
456
+ name=f"{name} d²/dβ²", line=dict(color=color, dash='dot')))
457
+
458
+ # Add derivatives for max_k and min_t curves if they exist
459
+ if show_max_k and max_k_curve is not None:
460
+ fig.add_trace(go.Scatter(x=betas, y=max_k_derivatives[0], mode="lines",
461
+ name="Max k d/dβ", line=dict(color='red', dash='dash')))
462
+ fig.add_trace(go.Scatter(x=betas, y=max_k_derivatives[1], mode="lines",
463
+ name="Max k d²/dβ²", line=dict(color='red', dash='dot')))
464
 
465
+ if show_min_t and min_t_curve is not None:
466
+ fig.add_trace(go.Scatter(x=betas, y=min_t_derivatives[0], mode="lines",
467
+ name="Min t d/dβ", line=dict(color='orange', dash='dash')))
468
+ fig.add_trace(go.Scatter(x=betas, y=min_t_derivatives[1], mode="lines",
469
+ name="Min t d²/dβ²", line=dict(color='orange', dash='dot')))
 
 
 
 
470
 
471
  fig.update_layout(
472
  title="Curves vs β: Eigenvalue Support Boundaries and Asymptotic Expressions" if use_eigenvalue_method
 
624
  # Advanced settings in collapsed expanders
625
  with st.expander("Method Settings", expanded=False):
626
  if method_type == "Eigenvalue Method":
627
+ beta_steps = st.slider("β steps", min_value=21, max_value=101, value=51, step=10,
628
  key="beta_steps_eigen")
629
+ n_samples = st.slider("Matrix size (n)", min_value=100, max_value=2000, value=1000,
630
  step=100)
631
  seeds = st.slider("Number of seeds", min_value=1, max_value=10, value=5, step=1)
632
  else:
 
635
  z_steps = st.slider("z grid steps", min_value=1000, max_value=100000, value=50000,
636
  step=1000, key="z_steps")
637
 
638
+ # Curve visibility options
639
+ with st.expander("Curve Visibility", expanded=False):
640
+ col_vis1, col_vis2 = st.columns(2)
641
+ with col_vis1:
642
+ show_high_y = st.checkbox("Show High y Expression", value=False, key="show_high_y")
643
+ show_max_k = st.checkbox("Show Max k Expression", value=True, key="show_max_k")
644
+ with col_vis2:
645
+ show_low_y = st.checkbox("Show Low y Expression", value=False, key="show_low_y")
646
+ show_min_t = st.checkbox("Show Min t Expression", value=True, key="show_min_t")
647
+
648
  # Custom expressions collapsed by default
649
  with st.expander("Custom Expression 1 (s-based)", expanded=False):
650
  st.markdown("""Enter expressions for s = numerator/denominator
 
670
  use_eigenvalue_method = (method_type == "Eigenvalue Method")
671
  if use_eigenvalue_method:
672
  fig = generate_z_vs_beta_plot(z_a_1, y_1, z_min_1, z_max_1, beta_steps, None,
673
+ s_num, s_denom, z_num, z_denom, show_derivatives,
674
+ show_high_y, show_low_y, show_max_k, show_min_t,
675
  use_eigenvalue_method=True, n_samples=n_samples,
676
  seeds=seeds)
677
  else:
678
  fig = generate_z_vs_beta_plot(z_a_1, y_1, z_min_1, z_max_1, beta_steps, z_steps,
679
  s_num, s_denom, z_num, z_denom, show_derivatives,
680
+ show_high_y, show_low_y, show_max_k, show_min_t,
681
  use_eigenvalue_method=False)
682
 
683
  if fig is not None: