Guest User

Untitled

a guest
May 30th, 2025
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. #BUG DEMONSTRATION
  2.  
  3. import numpy as np
  4. from scipy.optimize import curve_fit
  5.  
  6. def swap_model(x_in, X_eff, Y_eff):
  7.     denominator = X_eff + x_in
  8.     # Avoid division by zero, though bounds should help
  9.     if np.isscalar(denominator):
  10.         return (Y_eff * x_in) / (denominator if not np.isclose(denominator, 0) else 1e-9)
  11.     else: # It's an array
  12.         denominator_copy = np.copy(denominator) # Work on a copy to avoid modifying input array if x_in is array-like
  13.         denominator_copy[np.isclose(denominator_copy, 0)] = 1e-9
  14.         return (Y_eff * x_in) / denominator_copy
  15.  
  16. bounds = ([1e-6, 1e-6], [np.inf, np.inf]) # Parameters must be positive
  17.  
  18. # Test input for prediction
  19. x_test_value = 6507.3125
  20.  
  21. print("--- Case 1: Poor Fit ---")
  22. x_data_bad = np.array([1, 2, 39000])
  23. y_data_bad = np.array([8423.724335, 16846.531751, 319765062.090722])
  24. # Actual value for x_test_value corresponding to this "bad" scenario's universe
  25. y_actual_for_bad_case_test = 54311422.15982
  26.  
  27. try:
  28.     popt_bad, _ = curve_fit(swap_model, x_data_bad, y_data_bad, bounds=bounds, maxfev=5000)
  29.     X_eff_bad, Y_eff_bad = popt_bad
  30.     print(f"Fitted (Bad): X_eff = {X_eff_bad:.4f}, Y_eff = {Y_eff_bad:.4f}")
  31.     y_pred_bad = swap_model(x_test_value, X_eff_bad, Y_eff_bad)
  32.     error_bad = abs(y_actual_for_bad_case_test - y_pred_bad) / y_actual_for_bad_case_test * 100
  33.     print(f"Predicted for {x_test_value:.4f}: {y_pred_bad:.4f}, Actual: {y_actual_for_bad_case_test:.4f}, Error: {error_bad:.2f}%")
  34. except RuntimeError as e: print(f"RuntimeError: {e}")
  35. except Exception as e: print(f"Other exception in bad fit: {e}")
  36.  
  37.  
  38. print("\n--- Case 2: Better Fit ---")
  39. x_data_good = np.array([1, 2, 32000])
  40. y_data_good = np.array([8430.169107, 16859.464909, 264727159.854379])
  41. # Actual value for x_test_value corresponding to this "good" scenario's universe
  42. y_actual_for_good_case_test = 54549399.392518
  43.  
  44. try:
  45.     popt_good, _ = curve_fit(swap_model, x_data_good, y_data_good, bounds=bounds, maxfev=5000)
  46.     X_eff_good, Y_eff_good = popt_good
  47.     print(f"Fitted (Good): X_eff = {X_eff_good:,.4f}, Y_eff = {Y_eff_good:,.4f}") # Added comma for readability
  48.     y_pred_good = swap_model(x_test_value, X_eff_good, Y_eff_good)
  49.     error_good = abs(y_actual_for_good_case_test - y_pred_good) / y_actual_for_good_case_test * 100
  50.     print(f"Predicted for {x_test_value:.4f}: {y_pred_good:.4f}, Actual: {y_actual_for_good_case_test:.4f}, Error: {error_good:.2f}%")
  51. except RuntimeError as e: print(f"RuntimeError: {e}")
  52. except Exception as e: print(f"Other exception in good fit: {e}")
  53.  
Advertisement
Add Comment
Please, Sign In to add comment