def error_2d(f, min_point, axis): '''Returns the error of a function f(x,y) at a given minimum point, (x_min, y_min) for a given axis. Parameters ------------------- f : A function of two variables f. Here f = likelihood_2d min_point (list) : The minimum point [x_min, y_min] of f axis (str) : The direction in which the error to be determined Returns ---------------- std (float) : The standard deviation or the error ''' theta_min = min_point[0] m_min = min_point[1] l_min = f(min_point) l_plus = l_min + 0.5 # similar to the 1D case if axis == 'theta': # if the direction is in theta d = 0.1 criteria = 0.01 x_range = np.linspace(theta_min - d, theta_min + d, 1000) err_list = [] for i in x_range: if (abs(f([i, m_min]) - l_plus) < criteria): err_list.append(theta_min - i) err_plus = max(err_list) err_minus = abs(min(err_list)) if axis == 'm': # if the direction is in theta criteria = 0.01 m_range = np.linspace(0.002, 0.003, 1000) err_list = [] for i in m_range: if (abs(f([theta_min, i]) - l_plus) < criteria): err_list.append(m_min - i) err_plus = max(err_list) err_minus = abs(min(err_list)) std = (err_minus + err_plus) / 2 return std
def error(f, x_min): '''Returns the error of a function at a given minimum point, x_min. Parameters ------------------- f : A function x_min (float) : The minimum x of f(x) Returns ---------------- std (float) : The standard deviation or the error ''' x_range = np.linspace(x_min - 0.1, x_min + 0.1, 1000) l_min = f(x_min) #using likelihood function err_list = [] l_plus = l_min + 0.5 for i in x_range: if (abs(f(i) - l_plus) < 0.01): err_list.append(x_min - i) err_plus = max(err_list) err_minus = abs(min(err_list)) std = (err_minus + err_plus) / 2 # taking the average return std
if k[i] > 0: lamb = sim[i] * p(theta, m_sq, L, x[i]) nll_i = lamb - k[i] + (k[i] * np.log(k[i] / lamb)) nll.append(nll_i) if k[i] == 0: lamb_0 = sim[i] * p(theta, m_sq, L, x[i]) nll.append(lamb_0) return sum(nll) #plotting a surface plot ---> for visualisation purpose, not for report from mpl_toolkits import mplot3d thet_range = np.linspace(0, np.pi / 2, 200) # range for theta m_sq_range = np.linspace(0, 1e-2, 200) # range for m_squared thet_range, m_sq_range = np.meshgrid(thet_range, m_sq_range) # meshgrid of x, y fig = plt.figure(8) ax8 = plt.gca(projection='3d') z_map = likelihood_2d([thet_range, m_sq_range]) # the likelihood in 2D ax8.plot_surface(m_sq_range, thet_range, z_map, cmap='jet') ax8.set(title='NLL against $\Delta {m^2}_{23}$ and $\Theta_{23}$', xlabel=r"$\Delta {m^2}_{23}$ ($eV^2$)", ylabel=r'$\Theta_{23}$ (rad)', zlabel='NLL') plt.show()
if k[i] > 0: # to avoid log(0) lamb = sim[i] * p(theta, m_sq, L, x[i]) nll_i = lamb - k[i] + (k[i] * np.log(k[i] / lamb)) nll.append(nll_i) if k[i] == 0: # ignore the log term lamb_0 = sim[i] * p(theta, m_sq, L, x[i]) nll.append(lamb_0) return sum(nll) # sum of the elements in the nll list # plotting nll with respect to theta thet = np.linspace(0, np.pi / 2, 1000) ax6 = plt.figure(6).add_subplot(111) ax6.set(title='NLL against $\Theta_{23}$ with a fixed $\Delta {m^2}_{23}$', xlabel=r"$\theta_{23}$ (rad)", ylabel='NLL') plt.plot(thet, likelihood(thet), color='red') plt.show() #%% #building a parabolic minimiser def x_min(f, z): '''Returns the minimum point of a parabolic function that contains the three points in the list of z. This is to be used in minimiser() later.
def error_3d(f, x_min, axis): '''Returns the error of a function f(x,y,z) at a given minimum point, (x_min, y_min, z_min) for a given axis. Parameters ------------------- f : A function of three variables f. Here f = likelihood_3d min_point (list) : The minimum point [x_min, y_min, z_min] of f axis (str) : The direction in which the error to be determined Returns ---------------- std (float) : The standard deviation or the error ''' # similarly to 2D case theta_min = x_min[0] m_min = x_min[1] gamma_min = x_min[2] l_min = f(x_min) l_plus = l_min + 0.5 if axis == 'theta': d = 0.1 criteria = 0.1 x_range = np.linspace(theta_min - d, theta_min + d, 200) err_list = [] for i in x_range: if (abs(f([i, m_min, gamma_min]) - l_plus) < criteria): err_list.append(theta_min - i) err_plus = max(err_list) err_minus = abs(min(err_list)) std = (err_minus + err_plus) /2 if axis == 'm': criteria = 0.1 m_range = np.linspace(0.002, 0.003, 200) err_list = [] for i in m_range: if (abs(f([theta_min, i, gamma_min]) - l_plus) < criteria): err_list.append(m_min - i) err_plus = max(err_list) err_minus = abs(min(err_list)) std = (err_minus + err_plus) /2 if axis == 'gamma': criteria = 0.1 gamma_range_e = np.linspace(1.50, 1.70, 200) err_list_g = [] for i in gamma_range_e: if (abs(likelihood_3d([theta_min, m_min, i]) - l_plus) < 0.01): err_list_g.append(min_3d_1[2] - i) err_plus = max(err_list_g) err_minus = abs(min(err_list_g)) std = (err_minus + err_plus) /2 return std
for i in range(200): if k[i] > 0: # similarly to avoid log(0) lamb = lamb_2(u)[i] # modified lambda function nll_i = lamb - k[i] + (k[i] * np.log( k[i] / lamb)) nll.append(nll_i) if k[i] == 0: lamb_0 = lamb_2(u)[i] nll.append(lamb_0) return sum(nll) # plotting nll_3d against gamma --> using guesses for theta and m_squared gamma_range = np.linspace(0.1, 4,100) # raneg for gamma for plotting ax13 = plt.figure(13).add_subplot(111) ax13.set(title='NLL against $\gamma$ with a fixed $\Theta_{23}$ and $\Delta {m^2}_{23}$', xlabel= r"$\gamma$ ($GeV^{-1}$)", ylabel='NLL') plt.plot(gamma_range, likelihood_3d([0.67, 0.00259, gamma_range]), color = 'red') plt.show() #%% # 3D minimisation using gradient descent def grad_3d(f, x, y, z): '''Returns the gradient vector of a function, f at a given point (x,y,z).