def _odes(self, x,y,args): derivs = [y[1]/x**2] if (x>0) else [0] derivs.append(-9.0*x**2*np.exp(y[0])) u = y[1] phi=y[0] phi0=args[0] eta=args[1] B = args[2] C = 1 - ((1-B)/eta**2) if phi>0: inf = gu(1.5,phi/eta**2)*np.exp(phi/eta**2)*g(1.5) if inf!=inf or inf==np.inf: inf = (phi/eta**2)**0.5 inf2 = gu(1.5,phi0/eta**2)*np.exp(phi0/eta**2)*g(1.5) if inf2!=inf2 or inf2==np.inf: inf2 = (phi0/eta**2)**0.5 dydt =[u/(x**2), -9*(x**2)*(self.mmb*(np.exp(phi)*g(1.5)*gl(1.5,phi)-(2.0/3.0)*phi**(1.5)*B- (4.0/15.0)*C*phi**(2.5)) + self.mmpe* ((1-B)*eta**3 *inf))/ (self.mmb*(np.exp(phi0)*g(1.5)*gl(1.5,phi0)-(2.0/3.0)*phi0**(1.5)*B- (4.0/15.0)*C*phi0**(2.5))+self.mmpe* ((1-B)*eta**3 *inf2))] if phi<0: inf_neg = np.exp(phi/eta**2)*g(1.5) inf2 = gu(1.5,phi0/eta**2)*np.exp(phi0/eta**2)*g(1.5) if inf2!=inf2 or inf2==np.inf: inf2 = (phi0/eta**2)**0.5 dydt =[u/(x**2), -9*(x**2)*(self.mmpe*((1-B)*eta**3 *inf_neg))/(self.mmb*(np.exp(phi0)*g(1.5)*gl(1.5,phi0)-(2.0/3.0)*phi0**(1.5)*B- (4.0/15.0)*C*phi0**(2.5))+self.mmpe* ((1-B)*eta**3 *inf2))] return dydt
def solveNeuralNetworks(): print(" X shape = ", X.shape, "\n Theta1 shape =", Theta1.shape) z2 = np.matmul(X, Theta1.T) a2 = g(z2) print(" a2 shape = ", a2.shape, "\n Theta2 shape =", Theta2.shape) a2 = np.hstack([np.zeros([5000, 1]), a2]) z3 = np.matmul(a2, Theta2.T) h = g(z3) predict = np.argmax(h.T, axis=0)
def anderson_darlin(seq, F, alpha=.05, n=None, verbose=True): """ Statistical test of whether a given sample of data is drawn from a given probability distribution. Inputs: - seq: Array of integers - F: Cumulative distribution function - alpha: Float desirible level of significance - n: Integer number of regions (default is None) - verbose: Boolean; If set to true then print logs Outputs: - Boolean; If hypothesis is ejected """ seq = np.array(seq) if n is None: n = len(seq) else: seq = np.random.choice(seq, n) seq_sorted = sorted(seq) s = 0 for i in range(1, n + 1): f = F(seq_sorted[i - 1]) s += (2*i - 1) * np.log(f) / (2*n) + \ (1 - (2*i - 1) / (2*n)) * np.log(1 - f) s = -n - 2 * s a2 = 0 for j in range(15): a2 += (-1)**j * g(j + .5) * (4*j + 1) / g(.5) * g(j + 1) * \ np.exp((4*j + 1)**2 * np.pi**2 / (-8 * s)) * \ integrate.quad(lambda y: np.exp(s / (8 * (y**2 + 1)) - (4*j + 1)**2 \ * np.pi**2 * y**2 / (8 * s)), 0, np.inf)[0] a2 *= np.sqrt(2 * np.pi) / s p = 1 - a2 is_rejected = p <= alpha if verbose: print(f'Significance level S = {s}') print(f'a2 = {a2}') print(f'p = {p}') return is_rejected
def costFunction(features, target, weights, reg_parameter): hypothesis = np.array(g(features.__matmul__(weights))) I = np.ones((m, 1)) cost = (-1) * (target.T.__matmul__(np.log(hypothesis)) + (I - target).T.__matmul__(np.log(I - hypothesis))) / m reg_func = (reg_parameter / (2 * m)) * (sum(weights**2) - weights[0][0]**2) return cost + reg_func
def costFunction(features, target, weights, reg_parameter): hypothesis = np.array(g(np.matmul(features, weights))) I = np.ones(target.shape) a = np.matmul(target.T, np.log(hypothesis)) b = np.matmul((I - target).T, np.log(I - hypothesis)) cost = (-1) * (sum(a) + sum(b)) / m reg_func = (reg_parameter / (2 * m)) * (sum(weights**2) - weights[0][0]**2) a = max(cost) return a + max(reg_func)
def trainGradientDescent(features,target,num_steps,learning_rate,add_intercept=False): if add_intercept: intercept=np.ones((features.shape[0],1)) features =np.hstack([intercept,features]) weights = np.zeros((features.shape[1],1)) for step in range(int(num_steps)): score = np.matmul(features,weights) hypothesis = g(score) output_error_signal= target - hypothesis gradient = np.matmul(features.T,output_error_signal) weights += (learning_rate)*gradient return weights
def calcGradient(features, target, weights, reg_parameter, add_intercept=False): if add_intercept: intercept = np.ones((features.shape[0], 1)) features = np.hstack([intercept, features]) score = np.matmul(features, weights) hypothesis = g(score) output_error_signal = hypothesis - target gradient = np.matmul(features.T, output_error_signal) weights_zero = copy.deepcopy(weights) weights_zero[0][0] = 0 reg_gradient = gradient / m + (reg_parameter / m) * weights_zero return reg_gradient
def h(x): shape = x.shape d = shape[1] * 1. N = shape[0] e = 0.00001 rho = np.zeros((N, N)) for i in range(N): for j in range(N): if i == j: continue else: rho[i, j] = np.linalg.norm(x[i, :] - x[j, :]) rho = np.sort(rho, axis=0) rho_min = rho[1, :] h = d * np.average(rho) h = h + np.log((N - 1) * np.pi**(d / 2) / g(1 + d / 2)) h = h + 0.577 return h
def normalGamma(m, l, a, b): C = (b**a) * np.sqrt(l) / (g(a) * np.sqrt(2 * np.pi)) return lambda x, tau: C * tau**(a - 0.5) * np.exp(-b * tau - 0.5 * l * tau * (x - m)**2)
def gamma(a, b): C = b**a / g(a) return lambda tau: C * tau**(a - 1) * np.exp(-b * tau)
def lnlike_ian(theta, data): #print theta W0, eta, Mt, rh, B = theta #print theta rho_dat = data[0] sigma_dat = data[1] #discrete_dat = data[2] #try: eta = eta G = np.pi * 4.0 / 9.0 init = W0, 0.0 C = 1 - ((1 - B) / eta**2) params = [W0, eta, Mt, rh] if restart == 0: m = SPES(1000, W0, eta, B, 1, 0, 1) if restart == 1: m = SPES(1000, W0, eta, B, 1, 1, 1) r = m.r phi = m.phi minim = 0 rt = m.rt dispersion = m.dispersion density = m.density count = 0 Mss = [] Mf = (4 * np.pi * np.trapz((r**2) * density, r)) Mcs = [] for l in range(len(phi) - 1): Mc = (4 * np.pi * np.trapz( (r[0:l + 1]**2) * density[0:l + 1], r[0:l + 1])) if (Mc > 0.5 * Mf) & (count == 0): count = 1 rs = rh / r[l] break Ms = Mt / (Mf) vs = np.sqrt((Ms / rs) * (np.pi * 4.0 / 9.0)) density_all = np.array(density) * (Ms) / (rs**3) disp_all = np.array(dispersion) * (vs) r_us = r r = r * rs prob = 0 inf1 = (np.exp(W0 / (eta**2)) * gu(1.5, W0 / (eta**2))) * g(1.5) A = (Ms / ((vs**3) * (rs**3))) * 1. / (4 * np.pi * np.sqrt(2) * (np.exp(W0) * g(1.5) * gl(1.5, W0) - (2.0 / 3.0) * W0**(1.5) * B) - (4.0 / 15.0) * C * W0**(2.5) + ((1 - B) * eta**3 * inf1)) values = np.loadtxt('data/equalmass_32') sigma_mod_interp = np.interp(sigma_dat[0], r, disp_all, right=-np.inf) rho_mod_interp = np.interp(rho_dat[0], r, density_all, right=-np.inf) loglike = 0 for i in range(len(rho_dat[0])): loglike -= (rho_mod_interp[i] - rho_dat[1][i])**2 / (2 * (rho_dat[2][i]**2)) loglike -= log(sqrt(2 * pi * rho_dat[2][i]**2)) # Likelihood: velocity dispersion for i in range(len(sigma_dat[0])): loglike -= (sigma_mod_interp[i] - sigma_dat[1][i])**2 / (2 * (sigma_dat[2][i]**2)) loglike -= log(sqrt(2 * pi * sigma_dat[2][i]**2)) if numpy.isnan(loglike): print "nan" loglike = -np.inf return (loglike)
def costFunction(features,target,weights): prediction =np.array(g(features.__matmul__(weights))) I=np.ones((m,1),order='F') cost_function=(-1)*(target.T.__matmul__(np.log(prediction)) + (I-target).T.__matmul__(np.log(I-prediction)))/m return cost_function
def __init__(self, rmax,phi0,eta,B,delta,res,mm): self.r = np.array([0]) self.C = 1 - ((1-B)/eta**2) self.mmb=1 self.mmpe=mm #print self.C #delta=rhm # The 2 variables in the Poisson equation: phi, U, where U = -GM(<r) self._y = [phi0, 0] self.phi0 = phi0 init = [phi0,0] self.G = 9/(4*pi) # Solve sol = ode(self._odes) #sol.set_integrator('dopri5',nsteps=1e6,max_step=0.1) if res == 0: sol.set_integrator('dopri5')#,nsteps=1e6,atol=1e-10,rtol=1e-10) if res==1: sol.set_integrator('dopri5',max_step=0.1,nsteps=1e6,atol=1e-10,rtol=1e-10) sol.set_f_params([phi0,eta,B]) sol.set_solout(self._logcheck) sol.set_initial_value(numpy.array([phi0,0]),0.0001) sol.integrate(rmax) # Save phi, rho and M from Poisson solver self.phi = self._y[0] #print self.phi phi_in = self.phi[self.phi>0] phi_out = self.phi[self.phi<0] ###Checks for infinitys in density and dispersion inf1 = (np.exp(init[0]/(eta**2))*gu(1.5,init[0]/(eta**2)))*g(1.5) if (inf1==np.inf) or (inf1!=inf1): inf1=(init[0]/eta**2)**0.5 inf = np.exp(phi_in/(eta**2))*gu(1.5,phi_in/(eta**2))*g(1.5) inf2 =np.exp(phi_in/(eta**2))*gu(2.5,phi_in/(eta**2))*g(2.5) for i in range(len(inf)): if (inf[i]==np.inf) or (inf[i]!=inf[i]): inf[i] = (phi_in[i]/eta**2)**0.5 if (inf2[i]==np.inf) or (inf2[i]!=inf2[i]): inf2[i] = (phi_in[i]/eta**2)**1.5 ###Density and Velocity dispersion for r<r_t self.rho0 = ((np.exp(init[0])*g(1.5)*gl(1.5,init[0])-(2.0/3.0)*init[0]**(1.5)*B) - (4.0/15.0)*self.C*init[0]**(2.5)+ ((1-B)*eta**3 *inf1))#*kv(0.25,eta**2/init[0]))) self.rho_b = self.mmb* (np.exp(phi_in)*g(1.5)*gl(1.5,phi_in)-(2.0/3.0)*phi_in**(1.5)*B - (4.0/15.0)*self.C*phi_in**(2.5)) self.rho_pe = self.mmpe* ((1-B)*eta**3 *inf)#*kv(0.25,eta**2/phi_in)) self.rho_in = self.rho_b + self.rho_pe self.pressure_in = (((np.exp(phi_in)*g(2.5)*gl(2.5,(phi_in))-0.4*phi_in**(2.5)*B) - (4.0/35.0)*self.C*phi_in**(3.5)+ ((1-B)*eta**5 *inf2*kv(0.25,eta**2/phi_in)))) self.pressure_b = (((np.exp(phi_in)*g(2.5)*gl(2.5,(phi_in))-0.4*phi_in**(2.5)*B) - (4.0/35.0)*self.C*phi_in**(3.5))) self.pressure_pe=((1-B)*eta**5 *inf2)#*kv(0.25,eta**2/phi_in)) self.sigma_b = np.sqrt( 2*self.pressure_b/self.rho_b) self.sigma_pe = np.sqrt( 2*self.pressure_pe/self.rho_pe) sigma_in = np.sqrt( 2*self.pressure_in/self.rho_in) ###Density and velocity dispersion for r>r_t inf_neg = np.exp(phi_out/eta**2)*g(1.5) inf2_neg = np.exp(phi_out/(eta**2))*g(2.5) self.rho_out = self.mmpe*(((1-B)*eta**3 *inf_neg))#/self.rho0 sigma_out = np.sqrt( 2*eta**2*(inf2_neg/inf_neg)) ###Total rho_hat and sigma #print len(self.rho_in),len(self.rho_out) self.rhohat = np.array(np.concatenate([self.rho_in,self.rho_out]))/np.array(self.rho0) self.sigma = np.concatenate([sigma_in,sigma_out]) ###dM/dE & f(E) self.phii = np.linspace(0,-phi0,100) self.N1=[0]*len(phi_in) self.N2=[0]*len(self.phii) self.fe = [0]*len(phi_in) self.fe_1 = [0]*len(phi_in) self.fe_2 = [0]*len(phi_in) A = 1./(4*np.pi*np.sqrt(2)*(np.exp(init[0])*g(1.5)*gl(1.5,init[0])-(2.0/3.0)*init[0]**(1.5)*B) - (4.0/15.0)*self.C*init[0]**(2.5) + ((1-B)*eta**3 *inf1)) self.fe2=[0]*len(self.phii) self.fe2_1=[0]*len(self.phii) self.fe2_2=[0]*len(self.phii) for i in range(len(phi_in)): self.N1[i] = ((4*np.pi)*(np.exp(phi_in[i])-B-(self.C*phi_in[i]))*simps((self.r[0:(i+1)]**2 * np.sqrt((-phi_in[i] + phi_in[0:(i+1)]))),self.r[0:(i+1)])/self.rho0) self.fe[i] = A*(np.exp(phi_in[i])-B-(self.C*phi_in[i])) self.fe_1[i] = A*(np.exp(phi_in[i])-self.C) self.fe_2[i] = A*(np.exp(phi_in[i])) for i in range(len(self.phii)): self.N2[i] = ((1-B) * (4*np.pi/np.sqrt(2))*np.exp(self.phii[i]/eta**2)*simps(self.r[self.phi>0]**2 * np.sqrt(2*(-self.phii[i] + phi_in)),self.r[self.phi>0])/self.rho0) self.fe2[i] = A*(1-B)*(np.exp(self.phii[i]/eta**2)) self.fe2_1[i] = A*(1-B)*(np.exp(self.phii[i]/eta**2))/eta**2 self.fe2_2[i] = A*(1-B)*(np.exp(self.phii[i]/eta**2))/eta**4 ###Scale values count = 0 Mss = [] self.Mf = (4*np.pi*simps((self.r[0:len(phi_in)]**2)*self.rhohat[0:len(phi_in)],self.r[0:len(phi_in)])) for i in range(len(phi_in)-1): Mc =(4*np.pi*simps((self.r[0:i+1]**2)*self.rhohat[0:i+1],self.r[0:i+1])) if (Mc > 0.5*self.Mf) & (count==0): count = 1 self.rhm = self.r[i] break self.rt = np.interp(0,-self.phi,self.r) self.Mt = (4*np.pi*simps((self.r[self.r<self.rt]**2)*self.rhohat[self.r<self.rt],self.r[self.r<self.rt])) #self.rs = (1.1*rlb)/(delta*self.rt) #self.Ms = Mt/(Mf) #self.vs = np.sqrt( (self.Ms/self.rs)*(0.0043*(np.pi*4.0/9.0))) ###Scaled Parameters #self.rhohat_s = self.rhohat*(self.Ms)/(self.rs**3) #self.rhob_s = (self.rho_b/self.rho0)*(self.Ms)/(self.rs**3) #self.rhope_s = (self.rho_pe/self.rho0)*(self.Ms)/(self.rs**3) self.Mb =(4*np.pi*simps((self.r[self.phi>0]**2)*self.rho_b/self.rho0,self.r[self.phi>0]))#-M_bp# (simps(N,-phi)) self.Mpe = 4*np.pi*simps((self.r[self.phi>0]**2)*self.rho_pe/self.rho0,self.r[self.phi>0]) self.Mfrac = self.Mpe/(self.Mb+self.Mpe) #self.sigma_s = self.sigma*(self.vs) #self.N1_s = np.array(self.N1)*(self.Ms/(self.vs**2)) #self.N2_s = np.array(self.N2)*(self.Ms/(self.vs**2)) #self.phi_s = self.phi*self.vs**2 #self.phii_s = self.phii*self.vs**2 #self.r_s = self.r*self.rs #self.rt = self.rt*self.rs ###Surface Density and projected velocity dispersion R = self.r[self.r<delta*self.rt] self.density = self.rhohat[self.r<delta*self.rt] self.dispersion = self.sigma[self.r<delta*self.rt] self.surface_density = np.zeros(len(self.density)-1) self.surface_dispersion = np.zeros(len(self.density)-1) self.rall = self.r self.phi = self.phi[self.r<delta*self.rt] self.r = self.r[self.r<delta*self.rt] #self.sigma_b = self.sigma_b[self.r<delta*self.rt] #self.rho_b = self.rho_b[self.r<delta*self.rt] #self.sigma_pe = self.sigma_pe[self.r<delta*self.rt] #self.rho_pe = self.rho_pe[self.r<delta*self.rt] for i in range(len(self.r)-1): c = (self.r >= R[i]) r_2 = self.r[c] z = sqrt(abs(r_2**2 - R[i]**2)) # avoid small neg. values self.surface_density[i] = 2.0*abs(simps(self.density[c], x=z)) self.surface_dispersion[i] = np.sqrt((abs(2.0*(simps(self.density[c]*(1.0/3.0)*self.dispersion[c]**2,x=z))))/self.surface_density[i]) #print self.surface_dispersion #self.surface_density = self.surface_density*(self.Ms)/(self.rs**2) #self.surface_dispersion = self.surface_dispersion*(self.vs) self.R = self.r[0:len(self.r)-1]
def acceptF(lambda_all,adv,p): return sum(log(g(1.+adv*lambda_all)/g(adv*lambda_all)*(p**(lambda_all*adv))))