def test_matrix_functions(n): dim = 3 + int(4 * np.random.rand()) print(dim) matrix = [] for i in range(dim): row = [] for j in range(dim): row.append( pe.pseudo_Obs(np.random.rand(), 0.2 + 0.1 * np.random.rand(), 'e1')) matrix.append(row) matrix = np.array(matrix) @ np.identity(dim) # Check inverse of matrix inv = pe.linalg.mat_mat_op(np.linalg.inv, matrix) check_inv = matrix @ inv for (i, j), entry in np.ndenumerate(check_inv): entry.gamma_method() if (i == j): assert math.isclose( entry.value, 1.0, abs_tol=1e-9), 'value ' + str(i) + ',' + str(j) + ' ' + str( entry.value) else: assert math.isclose( entry.value, 0.0, abs_tol=1e-9), 'value ' + str(i) + ',' + str(j) + ' ' + str( entry.value) assert math.isclose( entry.dvalue, 0.0, abs_tol=1e-9), 'dvalue ' + str(i) + ',' + str(j) + ' ' + str( entry.dvalue) # Check Cholesky decomposition sym = np.dot(matrix, matrix.T) cholesky = pe.linalg.mat_mat_op(np.linalg.cholesky, sym) check = cholesky @ cholesky.T for (i, j), entry in np.ndenumerate(check): diff = entry - sym[i, j] diff.gamma_method() assert math.isclose(diff.value, 0.0, abs_tol=1e-9), 'value ' + str(i) + ',' + str(j) assert math.isclose(diff.dvalue, 0.0, abs_tol=1e-9), 'dvalue ' + str(i) + ',' + str(j) # Check eigh e, v = pe.linalg.eigh(sym) for i in range(dim): tmp = sym @ v[:, i] - v[:, i] * e[i] for j in range(dim): tmp[j].gamma_method() assert math.isclose(tmp[j].value, 0.0, abs_tol=1e-9), 'value ' + str(i) + ',' + str(j) assert math.isclose( tmp[j].dvalue, 0.0, abs_tol=1e-9), 'dvalue ' + str(i) + ',' + str(j)
def transition_matrix(self): if self._transition_matrix is not None: return self._transition_matrix As, rs, ps = self.Ps, self.rs, self.ps # Fill in the transition matrix one block at a time K_total = self.total_num_states P = np.zeros((K_total, K_total)) starts = np.concatenate(([0], np.cumsum(rs)[:-1])) ends = np.cumsum(rs) for (i, j), Aij in np.ndenumerate(As): block = P[starts[i]:ends[i], starts[j]:ends[j]] # Diagonal blocks (stay in sub-state or advance to next sub-state) if i == j: for k in range(rs[i]): # p(z_{t+1} = (.,i+k) | z_t = (.,i)) = (1-p)^k p # for 0 <= k <= r - i block += (1 - ps[i])**k * ps[i] * np.diag(np.ones(rs[i]-k), k=k) # Off-diagonal blocks (exit to a new super state) else: # p(z_{t+1} = (j,1) | z_t = (k,i)) = (1-p_k)^{r_k-i+1} * A[k, j] block[:,0] = (1-ps[i]) ** np.arange(rs[i], 0, -1) * Aij assert np.allclose(P.sum(1),1) assert (0 <= P).all() and (P <= 1.).all() # Cache the transition matrix self._transition_matrix = P return P
def hess_lnpost(ws,fdensity,alpha,sig): print('hess') #print(ws); mo = np.exp(-4.); #hval = hfunc(ws); ws = ws.reshape((n_grid,n_grid)); #calc l1 lsis = np.array([-1*np.sum(psi(index)**2)/sig_noise**2 for (index,w) in np.ndenumerate(ws)]); lsis = lsis.reshape((n_grid,n_grid)); l1 = lsis#*np.sum((Psi(ws)-data)/2/sig_noise**2); xsi = (1.-fdensity ) * gaussian(np.log(ws),loc=np.log(mo), scale=sig)/ws + fdensity*(ws**alpha /w_norm) dxsi = -1*gaussian(np.log(ws),loc=np.log(mo), scale=sig)*(1.-fdensity)/ws**2 - (1.-fdensity)*np.log(ws/mo)*np.exp(-np.log(ws/mo)**2 /2/sig**2)/np.sqrt(2*np.pi)/ws**2 /sig**3 + fdensity*alpha*ws**(alpha-1) /w_norm; dxsi_st = -1*gaussian(np.log(ws),loc=np.log(mo), scale=sig)*(1.-fdensity)/ws**2 - (1.-fdensity)*np.log(ws/mo)*np.exp(-np.log(ws/mo)**2 /2/sig**2)/np.sqrt(2*np.pi)/ws**2 /sig**3; ddxsi_st = -1*dxsi_st/ws - dxsi_st*np.log(ws/mo)/ws /sig**2 -(1.-fdensity)*(1/np.sqrt(2*np.pi)/sig)*np.exp(-np.log(ws/mo)**2 /2/sig**2)*(1/sig**2 - np.log(ws/mo)/sig**2 -1)/ ws**3; ddxsi = ddxsi_st + fdensity*alpha*(alpha-1)*ws**(alpha-2) /w_norm ; l2 = -1*(dxsi/xsi)**2 + ddxsi/np.absolute(xsi); l_tot = l1+l2; #those are the diagonal terms, now need to build off diagonal hess_m = np.zeros((n_grid**2,n_grid**2)); np.fill_diagonal(hess_m,l_tot); ''' for i in range(0,n_grid**2): for j in range(i+1,n_grid**2): ind1 = (int(i/n_grid),i%n_grid); ind2 = (int(j/n_grid),j%n_grid); hess_m[i,j] = -1*np.sum(psi(ind1)*psi(ind2))/sig_noise**2; hess_m = symmetrize(hess_m); ''' print('hess fin'); #print(l_tot); #print('new it'); #print(np.average(hval[0][:][:]-hess_m)); return -1*hess_m;
def Psi(self,ws): ''' "forward operator" i.e. forward model Psi = int psi(theta) dmu(theta) where mu is the signal parameter ''' return np.sum(np.array([w*self.psi(index) for (index,w) in np.ndenumerate(ws)]),0)
def init(): offset = 2.0 #if optimum[0] < np.inf: # xmin = min(results['ADAM'][0][0], optimum[0]) - offset # xmax = max(results['ADAM'][0][0], optimum[0]) + offset #else: xmin = domain[0, 0] xmax = domain[0, 1] #if optimum[1] < np.inf: # ymin = min(results['ADAM'][1][0], optimum[1]) - offset # ymax = max(results['ADAM'][1][0], optimum[1]) + offset #else: ymin = domain[1, 0] ymax = domain[1, 1] x = np.arange(xmin, xmax, 0.01) y = np.arange(ymin, ymax, 0.01) X, Y = np.meshgrid(x, y) Z = np.zeros(np.shape(Y)) for a, _ in np.ndenumerate(Y): Z[a] = func(X[a], Y[a]) level = fdict['level'] if level is None: level = np.linspace(Z.min(), Z.max(), 20) else: if level[0] == 'normal': level = np.linspace(Z.min(), Z.max(), level[1]) if level[0] == 'log': level = np.logspace(np.log(Z.min()), np.log(Z.max()), level[1]) CF = ax[0].contour(X,Y,Z, levels=level) #plt.colorbar(CF, orientation='horizontal', format='%.2f') ax[0].grid() ax[0].plot(results['ADAM'][0][0], results['ADAM'][1][0], 'h', markersize=15, color = '0.75') if optimum[0] < np.inf and optimum[1] < np.inf: ax[0].plot(optimum[0], optimum[1], '*', markersize=40, markeredgewidth = 2, alpha = 0.5, color = '0.75') ax[0].legend(loc='upper center', ncol=3, bbox_to_anchor=(0.5, 1.15)) ax[1].plot(0, results['ADAM'][2][0], 'o') ax[1].axis([0, T, -0.5, max_err + 0.5]) ax[1].set_xlabel('num. iteration') ax[1].set_ylabel('loss') line1.set_data([], []) line2.set_data([], []) line3.set_data([], []) line4.set_data([], []) line5.set_data([], []) err1.set_data([], []) err2.set_data([], []) err3.set_data([], []) err4.set_data([], []) err5.set_data([], []) return line1, line2, line3, line4, line5, \ err1, err2, err3, err4, err5,
def _data_frame(self): df = [] for index, value in np.ndenumerate(self.components): mode, component, population = index df.append((component + 1, self.weights[component], "Pop{}".format(mode + 1), self.populations[population], value)) return pd.DataFrame(df, columns = ( "Component", "ComponentWeight", "Leaf", "Population", "PopulationWeight"))
def plot_confusion_matrix(y, y_model, cost_func, weighted, alpha): if weighted: wstr = ", Weighted" wstr2 = "_weighted" else: wstr = "" wstr2 = "" N_class = int(np.max(y) + 1) fname = cost_func + wstr2 + f"_{N_class}class" # create zeros matrix of correct size mat = np.zeros((N_class, N_class)) # construct confusion matrix for i in range(N_class): for j in range(N_class): mat[i, j] = np.sum((y == i) & (y_model == j)) mat_norm = mat / np.sum(mat, axis=1).reshape(N_class, 1) fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 8)) fig.suptitle( f'Confusion Matrix: Multiclass {cost_func.capitalize()}{wstr}\n' + r'$\alpha =$' + f'{alpha:.4f}') ax1.matshow(mat) ax2.matshow(mat_norm) # add text to each square for (i, j), z in np.ndenumerate(mat): ax1.text(j, i, f'{int(z)}', ha='center', va='center') for (i, j), z in np.ndenumerate(mat_norm): ax2.text(j, i, f'{z:.3f}', ha='center', va='center') ax1.set_xlabel('Number of Bubbles (Predicted)') ax1.set_ylabel('Number of Bubbles (Actual)') ax2.set_xlabel('Number of Bubbles (Predicted)') ax2.set_ylabel('Number of Bubbles (Actual)') ax1.set_title('Number') ax2.set_title('Percent') fig.tight_layout(rect=[0, 0, 1, 0.9]) fig.savefig(plotdir + "confusion_matrix_plots_" + fname + ".pdf") fig.savefig(plotdir + "confusion_matrix_plots_" + fname + ".png") return fig, ax1, ax2, mat, mat_norm
def grad_lnpost(ws,fdensity,alpha,sig): #calculate gradient of the ln posterior print('grad'); mo = np.exp(-4.); ws = ws.reshape((n_grid,n_grid)); #calc l1 bsis = (Psi(ws)-data)/sig_noise**2; lsis = ws*0; for (index,w) in np.ndenumerate(ws): lsis[index] = np.sum(psi(index)*bsis); l1 = lsis#*np.sum((Psi(ws)-data)/2/sig_noise**2); xsi = (1.-fdensity ) * gaussian(np.log(ws),loc=np.log(mo), scale=sig)/ws + fdensity*(ws**alpha /w_norm) l2 = -1*gaussian(np.log(ws),loc=np.log(mo), scale=sig)*(1.-fdensity)/ws**2 - (1.-fdensity)*np.log(ws/mo)*np.exp(-np.log(ws/mo)**2 /2/sig**2)/np.sqrt(2*np.pi)/ws**2 /sig**3 + fdensity*alpha*ws**(alpha-1) /w_norm; l2 = l2/np.absolute(xsi); l_tot = l1-l2; return l_tot.flatten();
def grad_lnpost(self,ws,fdensity,alpha,sig): #calculate gradient of the ln posterior print('grad'); w_norm = (self.wlim[1]**(alpha+1) - self.wlim[0]**(alpha+1))/(alpha+1); #normalization from integrating mo = np.exp(self.norm_mean); ws = ws.reshape((self.n_grid,self.n_grid)); #calc l1 bsis = (self.Psi(ws)-self.data)/self.sig_noise**2; lsis = ws*0; for (index,w) in np.ndenumerate(ws): lsis[index] = np.sum(self.psi(index)*bsis); l1 = lsis#*np.sum((Psi(ws)-data)/2/sig_noise**2); xsi = (1.-fdensity ) * self.gaussian(np.log(ws),loc=np.log(mo), scale=sig)/ws + fdensity*(ws**alpha /w_norm) l2 = -1*self.gaussian(np.log(ws),loc=np.log(mo), scale=sig)*(1.-fdensity)/ws**2 - (1.-fdensity)*np.log(ws/mo)*np.exp(-np.log(ws/mo)**2 /2/sig**2)/np.sqrt(2*np.pi)/ws**2 /sig**3 + fdensity*alpha*ws**(alpha-1) /w_norm; l2 = l2/np.absolute(xsi); l_tot = l1-l2; return l_tot.flatten();
def grad_lnpost(ws, fdensity, alpha, sig): #calculate gradient of the ln posterior mo = np.exp(-4.) ws = ws.reshape((n_grid, n_grid)) #calc l1 bsis = -(Psi(ws) - data) / sig_noise**2 lsis = np.array( [np.sum(bsis * psi(index)) for (index, w) in np.ndenumerate(ws)]) lsis = lsis.reshape((n_grid, n_grid)) l1 = lsis #*np.sum((Psi(ws)-data)/2/sig_noise**2); xsi = (1. - fdensity) * gaussian(np.log(ws), loc=np.log( mo), scale=sig) / ws + fdensity * (ws**alpha / w_norm) l2 = -1 * gaussian(np.log(ws), loc=np.log(mo), scale=sig) * ( 1. - fdensity) / ws**2 - (1. - fdensity) * np.log(ws / mo) * np.exp( -np.log(ws / mo)**2 / 2 / sig**2) / np.sqrt( 2 * np.pi) / ws**2 / sig**3 + fdensity * alpha * ws**( alpha - 1) / w_norm l2 = l2 / np.absolute(xsi) #l2 = fdensity*alpha*ws**(alpha-1) /w_norm /(fdensity*(ws**alpha /w_norm)) l_tot = l1 + l2 return -1 * l_tot.flatten()
print "opt result for band", bi, ":", res final_fluxes[bi] = np.exp(res.x[bi]) print "fluxes:", src.fluxes, final_fluxes final_star = SrcParams(src.u, a=0, fluxes=final_fluxes) # add noise and calculate per-band likelihoods ZERO_CONST = 0.1 gal_likelihoods = np.zeros(len(BANDS)) orig_star_likelihoods = np.zeros(len(BANDS)) final_star_likelihoods = np.zeros(len(BANDS)) for bi, b in enumerate(BANDS): galaxy_im = celeste.gen_src_image(src, imgs[bi], return_patch=False) galaxy_im_noise = np.zeros(galaxy_im.shape) for (i,j),value in np.ndenumerate(galaxy_im): galaxy_im_noise[i,j] = np.random.poisson(galaxy_im[i,j]) # calculate galaxy likelihood gal_likelihoods[bi] = np.sum(galaxy_im_noise * np.log(galaxy_im + ZERO_CONST) - galaxy_im) # calculate star likelihood orig_star_im = celeste.gen_src_image(star, imgs[bi], return_patch=False) final_star_im = celeste.gen_src_image(final_star, imgs[bi], return_patch=False) orig_star_likelihoods[bi] = np.sum(np.sum(galaxy_im_noise * np.log(orig_star_im + ZERO_CONST) - orig_star_im)) + ZERO_CONST) - final_star_im)) print "galaxy likelihoods:", gal_likelihoods print "orig star likelihoods:", orig_star_likelihoods print "final star likelihoods:", final_star_likelihoods # show the new star