import numpy as np
import matplotlib.pyplot as plt
# Function for Gaussian CI
def gaussian_ci( mu, sigma) :
lower_bound = mu - 2 * sigma
upper_bound = mu + 2 * sigma
return lower_bound, upper_bound
# Function for Lognormal CI
def lognormal_ci( mu, sigma) :
lower_bound = np.exp ( mu - 2 * sigma)
upper_bound = np.exp ( mu + 2 * sigma)
return lower_bound, upper_bound
# Example values for Gaussian and Lognormal distributions
mu_gaussian = 10 # Mean of Gaussian
sigma_gaussian = 2 # Standard deviation of Gaussian
mu_lognormal = np.log ( 10 ) # Mean of lognormal (log-scale)
sigma_lognormal = 0.2 # Standard deviation of lognormal (log-scale)
# Calculate CI for Gaussian
gaussian_lower, gaussian_upper = gaussian_ci( mu_gaussian, sigma_gaussian)
# Calculate CI for Lognormal
lognormal_lower, lognormal_upper = lognormal_ci( mu_lognormal, sigma_lognormal)
# Print results
print ( f"Gaussian CI: [{gaussian_lower}, {gaussian_upper}]" )
print ( f"Lognormal CI: [{lognormal_lower}, {lognormal_upper}]" )
# Plot the results (for visualization)
x = np.linspace ( mu_gaussian - 4 * sigma_gaussian, mu_gaussian + 4 * sigma_gaussian, 1000 )
gaussian_pdf = ( 1 / ( sigma_gaussian * np.sqrt ( 2 * np.pi ) ) ) * np.exp ( -0.5 * ( ( x - mu_gaussian) / sigma_gaussian) ** 2 )
# Create a figure
plt.figure ( figsize= ( 10 , 6 ) )
# Plot Gaussian PDF
plt.plot ( x, gaussian_pdf, label= "Gaussian Distribution" , color= "blue" )
# Plot CI for Gaussian
plt.axvline ( gaussian_lower, color= 'red' , linestyle= '--' , label= f'CI Lower (Gaussian): {gaussian_lower}' )
plt.axvline ( gaussian_upper, color= 'red' , linestyle= '--' , label= f'CI Upper (Gaussian): {gaussian_upper}' )
# Lognormal PDF (on the log scale)
x_lognormal = np.linspace ( 0 , 30 , 1000 )
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 )
# Plot Lognormal PDF
plt.plot ( x_lognormal, lognormal_pdf, label= "Lognormal Distribution" , color= "green" )
# Plot CI for Lognormal
plt.axvline ( lognormal_lower, color= 'orange' , linestyle= '--' , label= f'CI Lower (Lognormal): {lognormal_lower}' )
plt.axvline ( lognormal_upper, color= 'orange' , linestyle= '--' , label= f'CI Upper (Lognormal): {lognormal_upper}' )
# Add labels and legend
plt.title ( 'Gaussian and Lognormal Confidence Intervals' )
plt.xlabel ( 'Value' )
plt.ylabel ( 'Probability Density' )
plt.legend ( )
# Show the plot
plt.show ( )
aW1wb3J0IG51bXB5IGFzIG5wCmltcG9ydCBtYXRwbG90bGliLnB5cGxvdCBhcyBwbHQKCiMgRnVuY3Rpb24gZm9yIEdhdXNzaWFuIENJCmRlZiBnYXVzc2lhbl9jaShtdSwgc2lnbWEpOgogICAgbG93ZXJfYm91bmQgPSBtdSAtIDIgKiBzaWdtYQogICAgdXBwZXJfYm91bmQgPSBtdSArIDIgKiBzaWdtYQogICAgcmV0dXJuIGxvd2VyX2JvdW5kLCB1cHBlcl9ib3VuZAoKIyBGdW5jdGlvbiBmb3IgTG9nbm9ybWFsIENJCmRlZiBsb2dub3JtYWxfY2kobXUsIHNpZ21hKToKICAgIGxvd2VyX2JvdW5kID0gbnAuZXhwKG11IC0gMiAqIHNpZ21hKQogICAgdXBwZXJfYm91bmQgPSBucC5leHAobXUgKyAyICogc2lnbWEpCiAgICByZXR1cm4gbG93ZXJfYm91bmQsIHVwcGVyX2JvdW5kCgojIEV4YW1wbGUgdmFsdWVzIGZvciBHYXVzc2lhbiBhbmQgTG9nbm9ybWFsIGRpc3RyaWJ1dGlvbnMKbXVfZ2F1c3NpYW4gPSAxMCAgIyBNZWFuIG9mIEdhdXNzaWFuCnNpZ21hX2dhdXNzaWFuID0gMiAgIyBTdGFuZGFyZCBkZXZpYXRpb24gb2YgR2F1c3NpYW4KCm11X2xvZ25vcm1hbCA9IG5wLmxvZygxM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