def test_pos_def_matrix_param(self): k = 2 mat = np.full(k**2, 0.2).reshape(k, k) + np.eye(k) # not symmetric bad_mat = copy.deepcopy(mat) bad_mat[1, 0] += +1 vp = PosDefMatrixParam('test', k) # Check setting. vp_init = PosDefMatrixParam('test', k, val=mat) np_test.assert_array_almost_equal(mat, vp_init.get()) self.assertRaises(ValueError, vp.set, bad_mat) self.assertRaises(ValueError, vp.set, np.eye(k + 1)) vp.set(mat) # Check size. self.assertEqual(k, vp.size()) self.assertEqual(k * (k + 1) / 2, vp.free_size()) # Check getting and free parameters. np_test.assert_array_almost_equal(mat, vp.get()) mat_free = vp.get_free() vp.set(np.full((k, k), 0.)) vp.set_free(mat_free) np_test.assert_array_almost_equal(mat, vp.get()) mat_vectorized = vp.get_vector() vp.set(np.full((k, k), 0.)) vp.set_vector(mat_vectorized) np_test.assert_array_almost_equal(mat, vp.get())
def KP(TempK, Sal, Pbar, RGas, WhichKs, fH): """Calculate phosphoric acid dissociation constants for the given options.""" # Evaluate at atmospheric pressure KP1 = np.full(np.shape(TempK), np.nan) KP2 = np.full(np.shape(TempK), np.nan) KP3 = np.full(np.shape(TempK), np.nan) F = WhichKs == 7 KP1_KP67, KP2_KP67, KP3_KP67 = p1atm.kH3PO4_NBS_KP67(TempK, Sal) KP1 = np.where(F, KP1_KP67, KP1) # already on SWS! KP2 = np.where(F, KP2_KP67 / fH, KP2) # convert NBS to SWS KP3 = np.where(F, KP3_KP67 / fH, KP3) # convert NBS to SWS F = (WhichKs == 6) | (WhichKs == 8) # Note: neither the GEOSECS choice nor the freshwater choice include # contributions from phosphate or silicate. KP1 = np.where(F, 0.0, KP1) KP2 = np.where(F, 0.0, KP2) KP3 = np.where(F, 0.0, KP3) F = (WhichKs != 6) & (WhichKs != 7) & (WhichKs != 8) KP1_YM95, KP2_YM95, KP3_YM95 = p1atm.kH3PO4_SWS_YM95(TempK, Sal) KP1 = np.where(F, KP1_YM95, KP1) KP2 = np.where(F, KP2_YM95, KP2) KP3 = np.where(F, KP3_YM95, KP3) # Now correct for seawater pressure # === CO2SYS.m comments: ======= # These corrections don't matter for the GEOSECS choice (WhichKs = 6) and # the freshwater choice (WhichKs = 8). For the Peng choice I assume that # they are the same as for the other choices (WhichKs = 1 to 5). # The corrections for KP1, KP2, and KP3 are from Millero, 1995, which are # the same as Millero, 1983. KP1 = KP1 * pcx.KP1fac(TempK, Pbar, RGas) KP2 = KP2 * pcx.KP2fac(TempK, Pbar, RGas) KP3 = KP3 * pcx.KP3fac(TempK, Pbar, RGas) return KP1, KP2, KP3
def __init__( self, y, x, dims=3, prior_beta_mu=0, prior_beta_sd=10, sigma_shape=1, sigma_scale=2, inv_T=inv_T, ): self.y = y self.x = x self.dims = x.shape[1] + 2 # one for intercept, one of sigma self.omega = npa.ones(dims) self.mu = npa.ones(dims) # priors self.betas_mu = npa.full(self.dims - 1, prior_beta_mu) self.betas_sd = npa.full(self.dims - 1, prior_beta_sd) self.sigma_shape = sigma_shape self.sigma_scale = sigma_scale # inverse tranform function self.inv_T = inv_T
def KC(TempK, Sal, Pbar, RGas, WhichKs, fH, SWStoTOT0): """Calculate carbonic acid dissociation constants for the given options.""" # Evaluate at atmospheric pressure K1 = np.full(np.shape(TempK), np.nan) K2 = np.full(np.shape(TempK), np.nan) ts = (TempK, Sal) # for convenience K1, K2 = _getKC(WhichKs == 1, p1atm.kH2CO3_TOT_RRV93, SWStoTOT0, K1, K2, ts) K1, K2 = _getKC(WhichKs == 2, p1atm.kH2CO3_SWS_GP89, 1.0, K1, K2, ts) K1, K2 = _getKC(WhichKs == 3, p1atm.kH2CO3_SWS_H73_DM87, 1.0, K1, K2, ts) K1, K2 = _getKC(WhichKs == 4, p1atm.kH2CO3_SWS_MCHP73_DM87, 1.0, K1, K2, ts) K1, K2 = _getKC(WhichKs == 5, p1atm.kH2CO3_SWS_HM_DM87, 1.0, K1, K2, ts) K1, K2 = _getKC((WhichKs == 6) | (WhichKs == 7), p1atm.kH2CO3_NBS_MCHP73, fH, K1, K2, ts) K1, K2 = _getKC(WhichKs == 8, p1atm.kH2CO3_SWS_M79, 1.0, K1, K2, ts) K1, K2 = _getKC(WhichKs == 9, p1atm.kH2CO3_NBS_CW98, fH, K1, K2, ts) K1, K2 = _getKC(WhichKs == 10, p1atm.kH2CO3_TOT_LDK00, SWStoTOT0, K1, K2, ts) K1, K2 = _getKC(WhichKs == 11, p1atm.kH2CO3_SWS_MM02, 1.0, K1, K2, ts) K1, K2 = _getKC(WhichKs == 12, p1atm.kH2CO3_SWS_MPL02, 1.0, K1, K2, ts) K1, K2 = _getKC(WhichKs == 13, p1atm.kH2CO3_SWS_MGH06, 1.0, K1, K2, ts) K1, K2 = _getKC(WhichKs == 14, p1atm.kH2CO3_SWS_M10, 1.0, K1, K2, ts) K1, K2 = _getKC(WhichKs == 15, p1atm.kH2CO3_SWS_WMW14, 1.0, K1, K2, ts) # Added v1.4.1: K1, K2 = _getKC(WhichKs == 16, p1atm.kH2CO3_TOT_SLH20, SWStoTOT0, K1, K2, ts) # Now correct for seawater pressure K1 = K1 * pcx.K1fac(TempK, Pbar, RGas, WhichKs) K2 = K2 * pcx.K2fac(TempK, Pbar, RGas, WhichKs) return K1, K2
def kernelpdf(scale, sigma, dataset, datasetGen): #dataset is binned as eta1,eta2,mass,pt2,pt1 maxR = np.full((100), 3.3) minR = np.full((100), 2.9) valsReco = np.linspace(minR[0], maxR[0], 100) valsGen = valsReco h = np.tensordot( scale, valsGen, axes=0 ) #get a 5D vector with np.newaxis with all possible combos of kinematics and gen mass values h_ext = np.swapaxes(np.swapaxes(h, 2, 4), 3, 4)[:, :, np.newaxis, :, :, :] sigma_ext = sigma[:, :, np.newaxis, np.newaxis, :, :] xscale = np.sqrt(2.) * sigma_ext maxR_ext = maxR[np.newaxis, np.newaxis, :, np.newaxis, np.newaxis, np.newaxis] minR_ext = minR[np.newaxis, np.newaxis, :, np.newaxis, np.newaxis, np.newaxis] maxZ = ((maxR_ext - h_ext.astype('float64')) / xscale) minZ = ((minR_ext - h_ext.astype('float64')) / xscale) arg = np.sqrt(np.pi / 2.) * sigma_ext * (erf(maxZ) - erf(minZ)) #take tensor product between mass and genMass dimensions and sum over gen masses #divide each bin by the sum of gen events in that bin den = np.where( np.sum(datasetGen, axis=2) > 1000., np.sum(datasetGen, axis=2), -1)[:, :, np.newaxis, :, :] I = np.sum(arg * datasetGen[:, :, np.newaxis, :, :, :], axis=3) / den #give vals the right shape -> add dimension for gen mass (axis = 3) vals_ext = valsReco[np.newaxis, np.newaxis, :, np.newaxis, np.newaxis, np.newaxis] gaus = np.exp(-np.power(vals_ext - h_ext.astype('float64'), 2.) / (2 * np.power(sigma_ext, 2.))) #take tensor product between mass and genMass dimensions and sum over gen masses #divide each bin by the sum of gen events in that bin den2 = np.where( np.sum(datasetGen, axis=2) > 1000., np.sum(datasetGen, axis=2), 1)[:, :, np.newaxis, :, :] pdf = np.sum(gaus * datasetGen[:, :, np.newaxis, :, :, :], axis=3) / den2 / np.where(I > 0., I, -1) pdf = np.where(pdf > 0., pdf, 0.) massbinwidth = (maxR[0] - minR[0]) / 100 pdf = pdf * massbinwidth return pdf
def bin_params_GLLVM(y_bin, nj_bin, lambda_bin_old, ps_y, pzl1_ys, zl1_s, AT,\ tol = 1E-5, maxstep = 100): ''' Determine the GLLVM coefficients related to binomial coefficients by optimizing each column coefficients separately. y_bin (numobs x nb_bin nd-array): The binomial data nj_bin (list of int): The number of modalities for each count/binary variable lambda_bin_old (list of nb_ord_j x (nj_ord + r1) elements): The binomial coefficients of the previous iteration ps_y ((numobs, S) nd-array): p(s | y) for all s in Omega pzl1_ys (nd-array): p(z1 | y, s) zl1_s ((M1, r1, s1) nd-array): z1 | s AT ((r1 x r1) nd-array): Var(z1)^{-1/2} tol (int): Control when to stop the optimisation process maxstep (int): The maximum number of optimization step. ---------------------------------------------------------------------- returns (list of nb_bin_j x (nj_ord + r1) elements): The new bin coefficients ''' r0 = zl1_s.shape[1] S0 = zl1_s.shape[2] nb_bin = len(nj_bin) new_lambda_bin = [] for j in range(nb_bin): if j < r0 - 1: # Constrained columns nb_constraints = r0 - j - 1 lcs = np.hstack( [np.zeros((nb_constraints, j + 2)), np.eye(nb_constraints)]) linear_constraint = LinearConstraint(lcs, np.full(nb_constraints, 0), \ np.full(nb_constraints, 0), keep_feasible = True) opt = minimize(binom_loglik_j, lambda_bin_old[j] , \ args = (y_bin[:,j], zl1_s, S0, ps_y, pzl1_ys, nj_bin[j]), tol = tol, method='trust-constr', jac = bin_grad_j, \ constraints = linear_constraint, hess = '2-point', \ options = {'maxiter': maxstep}) else: # Unconstrained columns opt = minimize(binom_loglik_j, lambda_bin_old[j], \ args = (y_bin[:,j], zl1_s, S0, ps_y, pzl1_ys, nj_bin[j]), \ tol = tol, method='BFGS', jac = bin_grad_j, options = {'maxiter': maxstep}) res = opt.x if not (opt.success): res = lambda_bin_old[j] warnings.warn('One of the binomial optimisations has failed', RuntimeWarning) new_lambda_bin.append(deepcopy(res)) # Last identifiability part if nb_bin > 0: new_lambda_bin = np.stack(new_lambda_bin) new_lambda_bin[:, 1:] = new_lambda_bin[:, 1:] @ AT[0] return new_lambda_bin
def setup_weights(self, W_shape, b_shape=None): if "W" not in self._params: self._params["W"] = self.init(shape=W_shape, scale=self.scale) if b_shape is None: self._params["b"] = np.full(W_shape[1], self.initial_bias) else: self._params["b"] = np.full(b_shape, self.initial_bias) self.init_grad()
def get_MFA_params(zl, kl, rl_nextl): ''' Determine clusters with a GMM and then adjust a Factor Model over each cluster zl (ndarray): The lth layer latent variable kl (int): The number of components of the lth layer rl_nextl (1darray): The dimension of the lth layer and (l+1)th layer ----------------------------------------------------- returns (dict): Dict with the parameters of the MFA approximated by GMM + FA. ''' #====================================================== # Fit a GMM in the continuous space #====================================================== numobs = zl.shape[0] not_all_groups = True max_trials = 100 empty_count_counter = 0 while not_all_groups: # If not enough obs per group then the MFA diverge... gmm = GaussianMixture(n_components=kl) s = gmm.fit_predict(zl) clusters_found, count = np.unique(s, return_counts=True) if (len(clusters_found) == kl): # & (count >= 5).all(): not_all_groups = False empty_count_counter += 1 if empty_count_counter >= max_trials: raise RuntimeError( 'Could not find a GMM init that presents the \ proper number of groups:', kl) psi = np.full((kl, rl_nextl[0], rl_nextl[0]), 0).astype(float) psi_inv = np.full((kl, rl_nextl[0], rl_nextl[0]), 0).astype(float) H = np.full((kl, rl_nextl[0], rl_nextl[1]), 0).astype(float) eta = np.full((kl, rl_nextl[0]), 0).astype(float) z_nextl = np.full((numobs, rl_nextl[1]), np.nan).astype(float) #======================================================== # And then a MFA on each of those group #======================================================== for j in range(kl): indices = (s == j) fa = FactorAnalyzer(rotation=None, method='ml', n_factors=rl_nextl[1]) fa.fit(zl[indices]) psi[j] = np.diag(fa.get_uniquenesses()) H[j] = fa.loadings_ psi_inv[j] = np.diag(1 / fa.get_uniquenesses()) z_nextl[indices] = fa.transform(zl[indices]) eta[j] = np.mean(zl[indices], axis=0) params = {'H': H, 'psi': psi, 'z_nextl': z_nextl, 'eta': eta, 'classes': s} return params
def test_parX_TA(): """Compare derivatives from uncertainties module vs direct grads of solve.fill.""" # Define MCS parameters, expand and solve parY = np.array([2100e-6, 8.1, 400e-6, 400e-6, 350e-6, 1800e-6, 10e-6]) parYtype = np.array([2, 3, 4, 5, 6, 7, 8]) npts = np.size(parY) parX = np.full(npts, 2250e-6) parXtype = np.full(npts, 1) TA, TC, PH, PC, FC, CARB, HCO3, CO2, totals, Ks = prep( parX, parY, parXtype, parYtype) # Get uncertainty derivatives with the uncertainty module go = time() uncert = pyco2.uncertainty.automatic.dcore_dparX__parY( parXtype, parYtype, TA, TC, PH, FC, CARB, HCO3, totals, Ks) print(" Uncertainty module runtime = {:.5f} s".format(time() - go)) dTA_uncert = uncert["TAlk"] dTC_uncert = uncert["TCO2"] dPH_uncert = uncert["pH"] dPC_uncert = uncert["pCO2"] dFC_uncert = uncert["fCO2"] dCARB_uncert = uncert["CO3"] dHCO3_uncert = uncert["HCO3"] dCO2_uncert = uncert["CO2"] # Get corresponding uncertainty derivatives directly Icase = pyco2.solve.getIcase(parXtype, parYtype) def _dTA_direct(i): return egrad(lambda TA: pyco2.solve.fill( Icase, TA, TC, PH, PC, FC, CARB, HCO3, CO2, totals, Ks)[i])(TA) go = time() dTA_direct = _dTA_direct(0) dTC_direct = _dTA_direct(1) dPH_direct = _dTA_direct(2) dPC_direct = _dTA_direct(3) dFC_direct = _dTA_direct(4) dCARB_direct = _dTA_direct(5) dHCO3_direct = _dTA_direct(6) dCO2_direct = _dTA_direct(7) print("Direct uncertainties runtime = {:.5f} s".format(time() - go)) # Make sure they agree # print(np.array([dTA_uncert, dTA_direct])) # print(np.array([dTC_uncert, dTC_direct])) # print(np.array([dPH_uncert, dPH_direct])) # print(np.array([dPC_uncert, dPC_direct])) # print(np.array([dFC_uncert, dFC_direct])) # print(np.array([dCARB_uncert, dCARB_direct])) # print(np.array([dHCO3_uncert, dHCO3_direct])) # print(np.array([dCO2_uncert, dCO2_direct])) assert close_enough(dTA_uncert, dTA_direct) assert close_enough(dTC_uncert, dTC_direct) assert close_enough(dPH_uncert, dPH_direct) assert close_enough(dPC_uncert, dPC_direct) assert close_enough(dFC_uncert, dFC_direct) assert close_enough(dCARB_uncert, dCARB_direct) assert close_enough(dHCO3_uncert, dHCO3_direct) assert close_enough(dCO2_uncert, dCO2_direct)
def logsumexp_vjp(g, ans, vs, gvs, x): # If you want to be able to take higher-order derivatives, then all the # code inside this function must be itself differentiable by autograd. # This closure multiplies g with the Jacobian of logsumexp (d_ans/d_x). # Because autograd uses reverse-mode differentiation, g contains # the gradient of the objective w.r.t. ans, the output of logsumexp. # The arguments `vs` and `gvs` contain information about the shapes of # `x` and `g` but using them is optional. return np.full(x.shape, g) * np.exp(x - np.full(x.shape, ans))
def logsumexp_vjp(ans, x): # If you want to be able to take higher-order derivatives, then all the # code inside this function must be itself differentiable by Autograd. # This closure multiplies g with the Jacobian of logsumexp (d_ans/d_x). # Because Autograd uses reverse-mode differentiation, g contains # the gradient of the objective w.r.t. ans, the output of logsumexp. # This returned VJP function doesn't close over `x`, so Python can # garbage-collect `x` if there are no references to it elsewhere. x_shape = x.shape return lambda g: np.full(x_shape, g) * np.exp(x - np.full(x_shape, ans))
def test_masking(self): masks = cg.get_masks(20, 3) self.assertTrue(np.max([np.sum(m) for m in masks]) <= 3) all_m = np.full(20, False) no_m = np.full(20, True) for m in masks: all_m = np.logical_or(all_m, m) no_m = np.logical_xor(no_m, m) self.assertTrue(np.all(all_m)) self.assertTrue(~np.any(no_m))
def run_regressions(y, regressors, w=None): """Get the optimal regression lines in closed form. Parameters ---------------- y : `numpy.ndarray` (N, M) A matrix containing the outcomes of ``N`` regressions with ``M`` observations each. regressors : `numpy.ndarray` (M, D) A matrix of regressors. The regression coefficient will be a ``D``-length vector. w : `numpy.ndarray` (M,), optional A vector of weights on the columns of ``y``. If not set, the vector of ones is used. Returns --------- beta : `numpy.ndarray` (N, D) An array of the ``N`` regression coefficients. beta_infos : `numpy.ndarray` (N, D, D) An array of the "information" matrices, i.e. the inverse covariance matrices, of ``beta``. y_infos : `numpy.ndarray` (N,) An array of the inverse residual variances for each regression. """ if w is None: w = np.ones(y.shape[1]) assert y.shape[1] == regressors.shape[0] num_obs = y.shape[0] x_obs_dim = regressors.shape[1] y_obs_dim = y.shape[1] assert y_obs_dim > x_obs_dim beta = np.full((num_obs, x_obs_dim), float('nan')) beta_infos = np.full((num_obs, x_obs_dim, x_obs_dim), float('nan')) y_infos = np.full(num_obs, float('nan')) rtr = np.matmul(regressors.T, w[:, np.newaxis] * regressors) evs = np.linalg.eigvals(rtr) if np.min(evs < 1e-6): raise ValueError('Regressors are approximately singular.') rtr_inv_rt = np.linalg.solve(rtr, regressors.T) for n in range(num_obs): beta_reg = np.matmul(rtr_inv_rt, w * y[n, :]) beta[n, :] = beta_reg resid = y[n, :] - np.matmul(regressors, beta_reg) # The extra -x_obs_dim comes from the beta variance -- see notes. #y_info = (y_obs_dim - x_obs_dim) / np.sum(resid ** 2) y_info = (sum(w) - x_obs_dim) / np.sum(w * (resid**2)) beta_infos[n, :, :] = rtr * y_info y_infos[n] = y_info return beta, beta_infos, y_infos
def Descent(self, scales, nuggets, nsteps=10, tolerance=1e-5, progress=DefaultOutput): history_para = [] scales = np.atleast_1d(scales) nuggets = np.atleast_1d(nuggets) para = np.concatenate([nuggets, scales]) self.step_size = np.full((1, ), self.step_nuggets_size) self.step_size = np.concatenate( [self.step_size, np.full(scales.shape, self.step_scales_size)]) pCalculator = GradientDescentForEmulator.ProgressCalculator() progress = 0 for i in range(nsteps): new_para, grad = self.StepDescent(para) # stop updating parameters that reaches max values idCap = new_para > self.scaleMax new_para[idCap] = self.scaleMax grad[idCap] = 0 para = new_para history_para.append(new_para) (scales, nuggets) = new_para[1:], new_para[0] mag = np.linalg.norm(grad * self.step_size) progress = pCalculator.Get(nsteps, i, mag, tolerance) pub.sendMessage( "GradientProgress", step=i, progress=progress, mag=mag, nuggets=nuggets, scales=scales, ) # or mag < 0.5*(self.step_scales_size + self.step_nuggets_size): if (mag < tolerance): break if progress < 100: pub.sendMessage( "GradientProgress", step=i, progress=100, mag=mag, nuggets=nuggets, scales=scales, ) pub.sendMessage("GradientEnd") return np.array(history_para)
def initialize_glmm_pars(glmm_par): glmm_par['mu']['mean'].set(0.0) glmm_par['mu']['info'].set(1.0) glmm_par['tau']['shape'].set(2.0) glmm_par['tau']['rate'].set(2.0) beta_dim = glmm_par['beta']['mean'].size() glmm_par['beta']['mean'].set(np.full(beta_dim, 0.0)) glmm_par['beta']['info'].set(np.ones(beta_dim)) num_groups = glmm_par['u']['mean'].size() glmm_par['u']['mean'].set(np.full(num_groups, 0.0)) glmm_par['u']['info'].set(np.full(num_groups, 1.0))
def __init__(self, X, y, prior_beta_mu, prior_beta_std, prior_sigma_shape, prior_sigma_scale): self.x = X self.y = y self.dim = self.x.shape[1] + 2 # (intercept (beta0), betas, sigma) self.N = self.x.shape[0] # Prior hyperparameters # self.betas_mu = prior_beta_mu * np.ones(self.dim - 1) # self.betas_std = prior_beta_std * np.ones(self.dim - 1) self.betas_mu = np.full(self.dim - 1, prior_beta_mu) self.betas_std = np.full(self.dim - 1, prior_beta_std) self.sigma_shape = prior_sigma_shape self.sigma_scale = prior_sigma_scale
def test_e_log_lik(self): n_test_samples = 10000 # Our expected log likelihood should only differ from a sample average # of the generated log likelihood by a constant as the parameters # vary. Check this using num_param different random parameters. num_params = 5 ell_by_param = np.full(num_params, float('nan')) sample_ell_by_param = np.full(num_params, float('nan')) standard_error = 0. for i in range(num_params): tau, nu, phi_mu, phi_var = \ vi.initialize_parameters(num_samples, x_dim, k_approx) phi_var_expanded = np.array([phi_var for d in range(x_dim)]) # set vb parameters vb_params2['phi'].set_vector( np.hstack([np.ravel(phi_mu.T), phi_var])) vb_params2['pi'].set_vector(np.ravel(tau)) vb_params2['nu'].set_vector(np.ravel(nu)) z_sample, a_sample, pi_sample = \ vi.generate_parameter_draws(nu, phi_mu, phi_var_expanded, \ tau, n_test_samples) sample_e_log_lik = [ vi.log_lik(x, z_sample[n, :, :], a_sample[n, :, :], pi_sample[n, :], sigma_eps, sigma_a, alpha, k_approx) \ for n in range(n_test_samples) ] sample_ell_by_param[i] = np.mean(sample_e_log_lik) standard_error = \ np.max([ standard_error, np.std(sample_e_log_lik) / np.sqrt(n_test_samples) ]) # get moments e_log_pi1, e_log_pi2, phi_moment1, phi_moment2, nu_moment =\ vi.get_moments_VB(vb_params2) ell_by_param[i] = vi.exp_log_likelihood(nu_moment, phi_moment1, phi_moment2, e_log_pi1, e_log_pi2, sigma_a, sigma_eps, x, alpha) print('Mean log likelihood standard error: %0.5f' % standard_error) self.assertTrue(np.std(ell_by_param - sample_ell_by_param) < \ 3. * standard_error)
def init_parameter_dict(self, n_users, n_items, train_tuple): ''' Initialize parameter dictionary attribute for this instance. Post Condition -------------- Updates the following attributes of this instance: * param_dict : dict Keys are string names of parameters Values are *numpy arrays* of parameter values ''' # TODO fix the lines below to have right dimensionality & values # TIP: use self.n_factors to access number of hidden dimensions random_state = self.random_state # inherited userN = n_users N = n_items.size itemN = n_items #self.alpha = 0.1 self.n_factors = 2 avg = ag_np.mean(train_tuple[2]) self.param_dict = dict( mu=ag_np.full((N, ), avg), b_per_user=ag_np.ones((userN, )), c_per_item=ag_np.ones((itemN, )), U=random_state.randn(userN, self.n_factors), V=random_state.randn(itemN, self.n_factors), )
def build_params(num_features, num_hidden_nodes, categories, weight_range=[-.1, .1]): ''' num_features <-- (numeric) number of feature in the dataset num_hidden_nodes <-- (numeric) categories <-- (list) list of category labels to use as keys for decode -- output connections weight_range = [-.1,.1] <-- (list of numeric) ''' return { 'input': { 'hidden': { 'weights': np.full([num_features, num_hidden_nodes], 10.0), 'bias': np.random.normal(*weight_range, [num_features, num_hidden_nodes]), }, }, 'hidden': { 'output': { 'weights': np.random.normal( *weight_range, [num_hidden_nodes, len(categories)]), 'bias': np.random.normal(*weight_range, [1, len(categories)]), }, }, 'attn': .5, }
def draw_new_ord(lambda_ord, z_new, nj_ord): ''' A adapter Generates draws from p(y_j | zM, s1 = k1) of the ordinal variables lambda_ord ( (nj_ord_j + r - 1) 1darray): Coefficients of the ordinal distributions in the GLLVM layer z_new (... x r x k ndarray): M Monte Carlo copies of z for each component k1 of the mixture nj_ord (int): The number of possible values values of the jth ordinal variable -------------------------------------------------------------- returns (ndarray): The p(y_j | zM, s1 = k1) for the jth ordinal variable ''' r = z_new.shape[1] nb_ord = len(nj_ord) new_nb_obs = z_new.shape[0] y_ord_new = np.full((new_nb_obs, nb_ord), np.nan) for j in range(nb_ord): lambda0 = lambda_ord[j][:(nj_ord[j] - 1)] Lambda = lambda_ord[j][-r:] broad_lambda0 = lambda0.reshape((1, nj_ord[j] - 1)) eta = broad_lambda0 - (z_new @ Lambda.reshape((r, 1))) gamma = expit(eta) #gamma = np.concatenate([np.zeros((new_nb_obs, 1)), gamma], axis = 1) gamma = np.concatenate([gamma, np.ones((new_nb_obs, 1))], axis=1) # Draw the observations u = np.random.uniform(size=(new_nb_obs, 1)) y_ord_new[:, j] = (gamma > u).argmax(1) return y_ord_new
def __init__( self, glmm_par, prior_par, x_mat, y_vec, y_g_vec, num_gh_points, use_prior=True): self.glmm_par = copy.deepcopy(glmm_par) self.prior_par = copy.deepcopy(prior_par) self.x_mat = np.array(x_mat) self.y_vec = np.array(y_vec) self.y_g_vec = np.array(y_g_vec) self.set_gh_points(num_gh_points) self.use_prior = use_prior self.beta_dim = self.x_mat.shape[1] self.num_groups = np.max(self.y_g_vec) + 1 self.use_weights = False self.weights = np.full(self.x_mat.shape[0], 1.0) assert np.min(y_g_vec) == 0 assert np.max(y_g_vec) == self.glmm_par['u'].size() - 1 self.objective = obj_lib.Objective(self.glmm_par, self.get_kl) self.get_prior_model_grad = \ autograd.grad(self.get_e_log_prior_from_args, argnum=0) self.get_prior_hess = \ autograd.jacobian(self.get_prior_model_grad, argnum=1) self.group_model = SubGroupsModel(self, num_sub_groups=1) self.global_model = GlobalModel(self) self.moment_wrapper = MomentWrapper(self.glmm_par)
def mrf_interpolate(transmission_image, sigma_image, img): """ Interpolate the transmission image with a Markov random field. """ width = constants.patch_size transmission_image[transmission_image < 0.3] = 0.3 transmission_image = dialate(transmission_image, size=(width, width)) sigma_image = dialate(sigma_image, size=(width, width)) sigma_image[sigma_image == 0] = constants.sigma_default interpol_image = np.full(transmission_image.shape, 0.6) interpol_image[transmission_image == 0] = 0 interpol_image = interpol_image + transmission_image grad_data_term = grad(data_term, 0) grad_regularization_term = grad(regularization_term, 0) for _ in range(constants.epochs): data_error_grad = grad_data_term(transmission_image, interpol_image, sigma_image) data_error_grad[transmission_image == 0] = 0 data_error = data_error_grad.sum() regularization_error_grad = grad_regularization_term(interpol_image, img) regularization_error = regularization_error_grad.sum() regularization_error_grad[regularization_error_grad > 2] = 2 regularization_error_grad[regularization_error_grad < -2] = -2 interpol_image += constants.learning_rate * regularization_error_grad interpol_image += constants.learning_rate * data_error_grad print('reg error: {}'.format(abs(regularization_error))) print('data error: {}'.format(abs(data_error))) interpol_image[interpol_image > 1] = 1 interpol_image[interpol_image < 0.3] = 0.3 return interpol_image
def x_star(self): if not hasattr(self, 'x_opt'): try: self.x_opt = np.linalg.solve(self.Q, -self.q) except np.linalg.LinAlgError: self.x_opt = np.full(fill_value=np.nan, shape=self.ndim) return self.x_opt
def draw_new_cont(lambda_cont, z_new): ''' A adapter Generates draws from p(y_j | zM, s1 = k1) of the continuous variables y_cont_j (numobs 1darray): The subset containing only the continuous variables in the dataset zM (M x r x k ndarray): M Monte Carlo copies of z for each component k1 of the mixture k (int): The number of components of the mixture -------------------------------------------------------------- returns (ndarray): p(y_j | zM, s1 = k1) ''' r = z_new.shape[1] nb_cont = lambda_cont.shape[0] new_nb_obs = z_new.shape[0] y_cont_new = np.full((new_nb_obs, nb_cont), np.nan) for j in range(nb_cont): eta = z_new @ lambda_cont[j][1:].reshape(r, 1) eta = eta + lambda_cont[j][0].reshape(1, 1) # Add the constant y_cont_new[:,j] = np.random.multivariate_normal(mean = eta.flatten(),\ cov = np.eye(new_nb_obs).astype(float)) return y_cont_new
def K1fac(TempK, Pbar, RGas, WhichKs): """Calculate pressure correction factor for K1.""" TempC = convert.TempK2C(TempK) K1fac = np.full(np.shape(TempK), np.nan) # because GEOSECS doesn't use _pcxKfac p1atm. F = WhichKs == 8 # freshwater # Pressure effects on K1 in freshwater: this is from Millero, 1983. deltaV = -30.54 + 0.1849 * TempC - 0.0023366 * TempC**2 Kappa = (-6.22 + 0.1368 * TempC - 0.001233 * TempC**2) / 1000 K1fac = np.where(F, Kfac(deltaV, Kappa, Pbar, TempK, RGas), K1fac) F = (WhichKs == 6) | (WhichKs == 7) # GEOSECS Pressure Effects On K1, K2, KB (on the NBS scale) # Takahashi et al, GEOSECS Pacific Expedition v. 3, 1982 quotes # Culberson and Pytkowicz, L and O 13:403-417, 1968: # but the fits are the same as those in # Edmond and Gieskes, GCA, 34:1261-1291, 1970 # who in turn quote Li, personal communication K1fac = np.where(F, np.exp((24.2 - 0.085 * TempC) * Pbar / (RGas * TempK)), K1fac) # This one is handled differently because the equation doesn't fit the # standard deltaV & Kappa form of _pcxKfac. F = (WhichKs != 6) & (WhichKs != 7) & (WhichKs != 8) # These are from Millero, 1995. # They are the same as Millero, 1979 and Millero, 1992. # They are from data of Culberson and Pytkowicz, 1968. deltaV = -25.5 + 0.1271 * TempC # deltaV = deltaV - .151*(Sali - 34.8) # Millero, 1979 Kappa = (-3.08 + 0.0877 * TempC) / 1000 # Kappa = Kappa - .578*(Sali - 34.8)/1000 # Millero, 1979 # The fits given in Millero, 1983 are somewhat different. K1fac = np.where(F, Kfac(deltaV, Kappa, Pbar, TempK, RGas), K1fac) return K1fac
def K2fac(TempK, Pbar, RGas, WhichKs): """Calculate pressure correction factor for K2.""" TempC = convert.TempK2C(TempK) K2fac = np.full(np.shape(TempK), np.nan) # because GEOSECS doesn't use _pcxKfac p1atm. F = WhichKs == 8 # freshwater # Pressure effects on K2 in freshwater: this is from Millero, 1983. deltaV = -29.81 + 0.115 * TempC - 0.001816 * TempC**2 Kappa = (-5.74 + 0.093 * TempC - 0.001896 * TempC**2) / 1000 K2fac = np.where(F, Kfac(deltaV, Kappa, Pbar, TempK, RGas), K2fac) F = (WhichKs == 6) | (WhichKs == 7) # GEOSECS Pressure Effects On K1, K2, KB (on the NBS scale) # Takahashi et al, GEOSECS Pacific Expedition v. 3, 1982 quotes # Culberson and Pytkowicz, L and O 13:403-417, 1968: # but the fits are the same as those in # Edmond and Gieskes, GCA, 34:1261-1291, 1970 # who in turn quote Li, personal communication K2fac = np.where(F, np.exp((16.4 - 0.04 * TempC) * Pbar / (RGas * TempK)), K2fac) # Takahashi et al had 26.4, but 16.4 is from Edmond and Gieskes # and matches the GEOSECS results # This one is handled differently because the equation doesn't fit the # standard deltaV & Kappa form of _pcxKfac. F = (WhichKs != 6) & (WhichKs != 7) & (WhichKs != 8) # These are from Millero, 1995. # They are the same as Millero, 1979 and Millero, 1992. # They are from data of Culberson and Pytkowicz, 1968. deltaV = -15.82 - 0.0219 * TempC # deltaV = deltaV + .321*(Sali - 34.8) # Millero, 1979 Kappa = (1.13 - 0.1475 * TempC) / 1000 # Kappa = Kappa - .314*(Sali - 34.8)/1000 # Millero, 1979 # The fit given in Millero, 1983 is different. # Not by a lot for deltaV, but by much for Kappa. K2fac = np.where(F, Kfac(deltaV, Kappa, Pbar, TempK, RGas), K2fac) return K2fac
def create_support_matrix(patch, point, direction, threshold): """ Returns a matrix encoding the support for the color-line Given a threshold, each pixel in a patch either does or doesn't fit the color-line. This matrix has the same size as a patch and contains `True` if that patch pixel supports the color-line. Args: patch: A square patch of an image. point: A point on the color line. direction: The color-line direction vector. threshold: The maximum distance a point can have from the color-line in order to support the line. Returns: support_matrix: A boolean matrix encoding pixel support. """ height, width, _ = patch.shape support_matrix = np.full((height, width), False, dtype=bool) for idy, row in enumerate(patch): for idx, pixel in enumerate(row): d2 = pixel - point dist = distance(direction, d2) if dist < threshold: support_matrix[idy][idx] = True return support_matrix
def draw_time_weights(regs, w_sum=None): if w_sum is None: w_sum = regs.y_obs_dim w_draw = osp.stats.multinomial.rvs(n=w_sum, p=np.full(regs.y_obs_dim, 1 / regs.y_obs_dim)) return w_draw
def plot_images(images, ax, ims_per_row=5, padding=5, digit_dimensions=(28, 28), cmap=matplotlib.cm.binary, vmin=None, vmax=None): """Images should be a (N_images x pixels) matrix.""" N_images = images.shape[0] N_rows = (N_images - 1) // ims_per_row + 1 pad_value = np.min(images.ravel()) concat_images = np.full( ((digit_dimensions[0] + padding) * N_rows + padding, (digit_dimensions[1] + padding) * ims_per_row + padding), pad_value) for i in range(N_images): cur_image = np.reshape(images[i, :], digit_dimensions) row_ix = i // ims_per_row col_ix = i % ims_per_row row_start = padding + (padding + digit_dimensions[0]) * row_ix col_start = padding + (padding + digit_dimensions[1]) * col_ix concat_images[row_start:row_start + digit_dimensions[0], col_start:col_start + digit_dimensions[1]] = cur_image cax = ax.matshow(concat_images, cmap=cmap, vmin=vmin, vmax=vmax) plt.xticks(np.array([])) plt.yticks(np.array([])) return cax
def _unconstrain_simplex_jacobian(simplex_vec): """Get the unconstraining Jacobian for a single simplex vector. """ return np.hstack([ np.full(len(simplex_vec) - 1, -1 / simplex_vec[0])[:, None], np.diag(1 / simplex_vec[1:]) ])
def draw_new_bin(lambda_bin, z_new, nj_bin): ''' A Adapter Generates draws from p(y_j | zM, s1 = k1) of the binary/count variables lambda_bin ( (r + 1) 1darray): Coefficients of the binomial distributions in the GLLVM layer z_new (M x r x k ndarray): M Monte Carlo copies of z for each component k1 of the mixture nj_bin_j: ... -------------------------------------------------------------- returns (ndarray): p(y_j | zM, s1 = k1) ''' new_nb_obs = z_new.shape[0] r = z_new.shape[1] # Draw one y per new z for the moment nb_bin = len(nj_bin) y_bin_new = np.full((new_nb_obs, nb_bin), np.nan) for j in range(nb_bin): # Compute the probability eta = z_new @ lambda_bin[j][1:][..., n_axis] eta = eta + lambda_bin[j][0].reshape(1, 1) # Add the constant pi = expit(eta) # Draw the observations u = np.random.uniform(size=(new_nb_obs, nj_bin[j])) # To check: work for binomials y_bin_new[:, j] = (pi > u).sum(1) return y_bin_new
def setup(self, x_shape): """ Parameters ---------- x_shape : np.array(batch size, time steps, input shape) """ self.input_dim = x_shape[2] # Input -> Hidden self._params['W'] = self._params.init((self.input_dim, self.hidden_dim)) # Bias self._params['b'] = np.full((self.hidden_dim,), self._params.initial_bias) # Hidden -> Hidden layer self._params['U'] = self.inner_init((self.hidden_dim, self.hidden_dim)) # Init gradient arrays self._params.init_grad() self.hprev = np.zeros((x_shape[0], self.hidden_dim))
def plot_images(images, ax, ims_per_row=5, padding=5, digit_dimensions=(28, 28), cmap=matplotlib.cm.binary, vmin=None, vmax=None): """Images should be a (N_images x pixels) matrix.""" N_images = images.shape[0] N_rows = np.ceil(float(N_images) / ims_per_row) pad_value = np.min(images.ravel()) concat_images = np.full(((digit_dimensions[0] + padding) * N_rows + padding, (digit_dimensions[1] + padding) * ims_per_row + padding), pad_value) for i in range(N_images): cur_image = np.reshape(images[i, :], digit_dimensions) row_ix = i // ims_per_row col_ix = i % ims_per_row row_start = padding + (padding + digit_dimensions[0]) * row_ix col_start = padding + (padding + digit_dimensions[1]) * col_ix concat_images[row_start: row_start + digit_dimensions[0], col_start: col_start + digit_dimensions[1]] = cur_image cax = ax.matshow(concat_images, cmap=cmap, vmin=vmin, vmax=vmax) plt.xticks(np.array([])) plt.yticks(np.array([])) return cax
def setup(self, x_shape): """ Naming convention: i : input gate f : forget gate c : cell o : output gate Parameters ---------- x_shape : np.array(batch size, time steps, input shape) """ self.input_dim = x_shape[2] # Input -> Hidden W_params = ['W_i', 'W_f', 'W_o', 'W_c'] # Hidden -> Hidden U_params = ['U_i', 'U_f', 'U_o', 'U_c'] # Bias terms b_params = ['b_i', 'b_f', 'b_o', 'b_c'] # Initialize params for param in W_params: self._params[param] = self._params.init((self.input_dim, self.hidden_dim)) for param in U_params: self._params[param] = self.inner_init((self.hidden_dim, self.hidden_dim)) for param in b_params: self._params[param] = np.full((self.hidden_dim,), self._params.initial_bias) # Combine weights for simplicity self.W = [self._params[param] for param in W_params] self.U = [self._params[param] for param in U_params] # Init gradient arrays for all weights self._params.init_grad() self.hprev = np.zeros((x_shape[0], self.hidden_dim)) self.oprev = np.zeros((x_shape[0], self.hidden_dim))
def gradient_product(g): # This closure multiplies g with the Jacobian of logsumexp (d_ans/d_x). # Because autograd uses reverse-mode differentiation, g contains # the gradient of the objective w.r.t. ans, the output of logsumexp. return np.full(x.shape, g) * np.exp(x - np.full(x.shape, ans))
def loss_fun(weights, smiles, targets): mean = parser.get(weights, 'mean') return loss_func(np.full(targets.shape, mean), targets)
def pred_fun(weights, smiles): mean = parser.get(weights, 'mean') return np.full((len(smiles),), mean)