Here is my code to fit the data to bimodal normal distribution. but I could not find the optimum parameters.I would appreciate if you could help me with it.
import numpy as np
import matplotlib.pyplot as plt
# Data
hours = [i/2 for i in range(49)]
frequency = [79, 77, 68, 72, 72, 73, 97, 126, 179, 278, 436, 529, 684, 641, 532,
460, 275, 206, 136, 120, 108, 104, 88, 114, 129, 122, 142, 143, 142,
139, 145, 149, 162, 270, 371, 418, 495, 421, 334, 274, 144, 126, 97,
99, 90, 78, 77, 94, 79]
# Bimodal Distribution Function
def bimodal(x, m, s, w, delta):
phi = 0.5 + np.arctan(w)/np.pi
return (phi * np.exp(-(x - m + delta/2)**2 / (2. * s**2)) / np.sqrt(2. * np.pi * s**2) +
(1-phi) * np.exp(-(x - m - delta/2)**2 / (2. * s**2)) / np.sqrt(2. * np.pi * s**2))
# Optimal parameters obtained from previous step
m_opt = 12.07
s_opt =2.2#2.62#1.7
w_opt =0.2#0.3
delta_opt = 11.52#11.52
# Calculate the bimodal distribution values
x = np.linspace(min(hours), max(hours), 400)
y = bimodal(x, m_opt, s_opt, w_opt, delta_opt)
# Plotting
plt.figure(figsize=(10, 6))
plt.hist(hours, bins=len(hours), weights=frequency, density=True, alpha=0.7, label="Density Histogram Data", align='mid')
plt.plot(x, y, 'r-', label="Bimodal Fit", linewidth=2)
plt.xlabel('Hour')
plt.ylabel('Density')
plt.title('Supply Timing Density Histogram with Bimodal Distribution Fit')
plt.legend()
plt.grid(True, which='both', linestyle='--', linewidth=0.5)
plt.tight_layout()
plt.show()
I tried some optimisation but it did not work well.