fork download
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. # Function for Gaussian CI
  5. def gaussian_ci(mu, sigma):
  6. lower_bound = mu - 2 * sigma
  7. upper_bound = mu + 2 * sigma
  8. return lower_bound, upper_bound
  9.  
  10. # Function for Lognormal CI
  11. def lognormal_ci(mu, sigma):
  12. lower_bound = np.exp(mu - 2 * sigma)
  13. upper_bound = np.exp(mu + 2 * sigma)
  14. return lower_bound, upper_bound
  15.  
  16. # Example values for Gaussian and Lognormal distributions
  17. mu_gaussian = 10 # Mean of Gaussian
  18. sigma_gaussian = 2 # Standard deviation of Gaussian
  19.  
  20. mu_lognormal = np.log(10) # Mean of lognormal (log-scale)
  21. sigma_lognormal = 0.2 # Standard deviation of lognormal (log-scale)
  22.  
  23. # Calculate CI for Gaussian
  24. gaussian_lower, gaussian_upper = gaussian_ci(mu_gaussian, sigma_gaussian)
  25.  
  26. # Calculate CI for Lognormal
  27. lognormal_lower, lognormal_upper = lognormal_ci(mu_lognormal, sigma_lognormal)
  28.  
  29. # Print results
  30. print(f"Gaussian CI: [{gaussian_lower}, {gaussian_upper}]")
  31. print(f"Lognormal CI: [{lognormal_lower}, {lognormal_upper}]")
  32.  
  33. # Plot the results (for visualization)
  34. x = np.linspace(mu_gaussian - 4 * sigma_gaussian, mu_gaussian + 4 * sigma_gaussian, 1000)
  35. gaussian_pdf = (1 / (sigma_gaussian * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x - mu_gaussian) / sigma_gaussian) ** 2)
  36.  
  37. # Create a figure
  38. plt.figure(figsize=(10, 6))
  39.  
  40. # Plot Gaussian PDF
  41. plt.plot(x, gaussian_pdf, label="Gaussian Distribution", color="blue")
  42.  
  43. # Plot CI for Gaussian
  44. plt.axvline(gaussian_lower, color='red', linestyle='--', label=f'CI Lower (Gaussian): {gaussian_lower}')
  45. plt.axvline(gaussian_upper, color='red', linestyle='--', label=f'CI Upper (Gaussian): {gaussian_upper}')
  46.  
  47. # Lognormal PDF (on the log scale)
  48. x_lognormal = np.linspace(0, 30, 1000)
  49. lognormal_pdf = (1 / (x_lognormal * sigma_lognormal * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((np.log(x_lognormal) - mu_lognormal) / sigma_lognormal) ** 2)
  50.  
  51. # Plot Lognormal PDF
  52. plt.plot(x_lognormal, lognormal_pdf, label="Lognormal Distribution", color="green")
  53.  
  54. # Plot CI for Lognormal
  55. plt.axvline(lognormal_lower, color='orange', linestyle='--', label=f'CI Lower (Lognormal): {lognormal_lower}')
  56. plt.axvline(lognormal_upper, color='orange', linestyle='--', label=f'CI Upper (Lognormal): {lognormal_upper}')
  57.  
  58. # Add labels and legend
  59. plt.title('Gaussian and Lognormal Confidence Intervals')
  60. plt.xlabel('Value')
  61. plt.ylabel('Probability Density')
  62. plt.legend()
  63.  
  64. # Show the plot
  65. plt.show()
Success #stdin #stdout #stderr 1.91s 57044KB
stdin
Standard input is empty
stdout
Gaussian CI: [6, 14]
Lognormal CI: [6.7032004603563955, 14.918246976412705]
stderr
./prog.py:49: RuntimeWarning: divide by zero encountered in true_divide
./prog.py:49: RuntimeWarning: divide by zero encountered in log
./prog.py:49: RuntimeWarning: invalid value encountered in multiply