Prob_H = 0.08 Prob_E_given_H = 0.95 Prob_E_given_Hc = 0.07 Prob_Hc = 1 - Prob_H Prob_E = Prob_E_given_H * Prob_H + Prob_E_given_Hc * Prob_Hc Prob_H_given_E = Prob_E_given_H * Prob_H / Prob_E print(Prob_H_given_E) import numpy as np import matplotlib.pyplot as plt plt.figure(figsize=(10,10)) size = 0.35 inner_vals = np.array([Prob_H, 1-Prob_H]) vals1 = np.array([Prob_E_given_H, 1-Prob_E_given_H])*Prob_H vals2 = np.array([Prob_E_given_Hc, 1-Prob_E_given_Hc])*Prob_Hc outer_vals = np.hstack((vals1, vals2)) cmap = plt.get_cmap("tab20c") inner_colors = cmap([4, 0]) outer_colors = cmap([5, 6, 1, 2]) outer_labels = 'positive | ill', 'negative | ill', \ 'positive | well', 'negative | well' plt.pie(outer_vals, radius=1, colors=outer_colors, wedgeprops=dict(width=size, edgecolor='w'), labels=outer_labels, explode=(0.05, 0, 0.05, 0)) plt.legend() inner_labels = r'$H$', r'$H^c$' plt.pie(inner_vals, radius=1-size, colors=inner_colors, wedgeprops=dict(width=size, edgecolor='w'), autopct='%1.1f%%' ) plt.show() from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import numpy as np import matplotlib.pyplot as plt # Define the mean and covariance matrix mean = np.array([0, 0]) cov = np.array([[2, 0.8], [0.8, 1]]) invcov = np.linalg.inv(cov) # Create a meshgrid for plotting x1 = np.linspace(-4, 4, 100) x2 = np.linspace(-4, 4, 100) X1, X2 = np.meshgrid(x1, x2) # Calculate the Gaussian density function def gaussian_density(x, mean, covariance): x_minus_mean = x - mean exponent = -0.5*np.dot(np.dot(x_minus_mean.T, invcov), x_minus_mean) coefficient = 1/((2*np.pi)**(len(mean)/2)*np.sqrt(np.linalg.det(cov))) return coefficient * np.exp(exponent) Z = np.zeros((len(x1), len(x2))) for i in range(len(x1)): for j in range(len(x2)): x = np.array([X1[i, j], X2[i, j]]) Z[i, j] = gaussian_density(x, mean, cov) # Create the figure and subplots fig = plt.figure(figsize=(11, 5), dpi=100) ax1 = fig.add_subplot(121, projection='3d') ax2 = fig.add_subplot(122) # Surface plot ax1.plot_surface(X1, X2, Z, cmap=cm.viridis) ax1.set_xlabel('$x_1$') ax1.set_ylabel('$x_2$') ax1.set_zlabel('$p(x)$') ax1.set_title('Gaussian density function') # Contour plot contour = ax2.contour(X1, X2, Z, levels=10) ax2.set_xlabel('$x_1$') ax2.set_ylabel('$x_2$') ax2.set_title('Contour of Gaussian density function') ax2.axis('square') ax2.grid() fig.tight_layout() plt.show() fig = plt.figure(figsize=(11, 5), dpi=100) ax1 = fig.add_subplot(121, projection='3d') ax2 = fig.add_subplot(122) # Surface plot ax1.plot_surface(X1, X2, Z, cmap=cm.viridis, alpha=0.8) ax1.plot(X1[25,:], X2[25,:], Z[25,:], color='red', linewidth=3, alpha=1) ax1.set_xlabel('$x_1$') ax1.set_ylabel('$x_2$') ax1.set_zlabel('$p(x)$') ax1.set_title('Gaussian density function') # Contour plot contour = ax2.contour(X1, X2, Z, levels=10) ax2.axhline(-2, color='red', linewidth=4, alpha=0.5) ax2.plot(x2*0.8, x2, ':') ax2.set_xlabel('$x_1$') ax2.set_ylabel('$x_2$') ax2.set_title('Contour of Gaussian density function') ax2.axis('square') ax2.grid() fig.tight_layout() plt.show() # Conditional pdf plot plt.figure(figsize=(5, 3), dpi=100) plt.plot(X1[25,:], Z[25,:], color='red', linewidth=4, alpha=0.3) plt.xlabel('$x_1$') plt.ylabel('$p(x_1|x_2=-2)$') plt.title('Conditional density function (scaled)') plt.grid() plt.show()