Notebook on nbviewer (original) (raw)

plt.figure(figsize=(5,5)) for outcome in df2.Y.cat.categories: plt.scatter(df2[df2.Y == outcome].X1, df2[df2.Y == outcome].X2, s=100, marker='${}$'.format(outcome))

Take 20 values from the posterior distribution and plot the lines

tr_len = len(trace2) n_curves = 20 stepIdxVec = np.arange(500, tr_len, tr_len//n_curves) X1_span = np.linspace(df2.X1.min(), df2.X1.max()) X2_span = np.linspace(df2.X2.min(), df2.X2.max())

Prepare grid for contour plot

xx, yy = np.meshgrid(X1_span, X2_span, indexing='xy') Z0 = np.zeros((X2_span.size,X1_span.size)) Z1 = Z0.copy() Z2 = Z0.copy()

Calculate p based on grid of X1 and X2

for idx in stepIdxVec: for (i,j) in np.ndindex(Z0.shape): Z0[i,j] = expit(estimates2[idx,0,0] + np.dot(estimates2[idx,1,0],xx[i,j]) + estimates2[idx,2,0]*yy[i,j]) Z1[i,j] = expit(estimates2[idx,0,1] + np.dot(estimates2[idx,1,1],xx[i,j]) + estimates2[idx,2,1]*yy[i,j]) Z2[i,j] = expit(estimates2[idx,0,2] + np.dot(estimates2[idx,1,2],xx[i,j]) + estimates2[idx,2,2]*yy[i,j]) plt.contour(xx, yy, Z0, colors=color, alpha=0.6, levels=[0.5]) plt.contour(xx, yy, Z1, colors=color, alpha=0.6, levels=[0.5]) plt.contour(xx, yy, Z2, colors=color, alpha=0.6, levels=[0.5])

plt.xlabel('$X_1$') plt.ylabel('$X_2$') plt.title('$\lambda_{{1}|{1,2,3,4}}$ \n λ{2}∣{2,3,4}\lambda_{{2}|{2,3,4}}λ{2}{2,3,4} \n λ{3}∣{3,4}\lambda_{{3}|{3,4}}λ{3}{3,4}') plt.gca().set_aspect('equal')