def stddev(a, weights=None, axis=None): """ Compute the standard deviation of the array a along a given axis Parameters ---------- a: array like contains the values whose standard deviations are desired weights: array like, optional array containg the values the weights of the elements in the columns of 'a'. Must have the same dimention as the 0th of 'a' or the same dimention of 'a'. axis: integer, optional Axis over which the sum is taken. By default axis is None, and all elements are summed. output: array like standard deviations """ # if the weights are given used hand made recipe (appending the weights can # create an array too large for numpy.std) if (weights != None): if (a.shape != weights.shape): if (a.shape[0] != weights.shape[0] and len(weights.shape) == 1): print( "ERROR: the code needs either weights.shape=a.shape or weights.shape[0] = a.shape[0]" ) sys.exit(110) else: weights = np.column_stack([ weights, ] * a.shape[1]) #so weights.shape=a.shape swx2 = np.sum(weights * a * a, axis=axis) sw = np.sum(weights, axis=axis) sw2 = np.sum(weights * weights, axis=axis) swx = np.sum(weights * a, axis=axis) std = (swx2 * sw - swx * swx) / (sw * sw - sw2) if ( std.ndim == 0 ): #if std is a single element convert to a 1d array in order to continue std = np.array([ std, ]) for i, s, w, x in zip(it.count(), mf.convert2array(std), mf.convert2array(sw), mf.convert2array(swx)): if (abs(s * w / x) > 1e-09): std[i] = np.sqrt(s) else: std[i] = 0. else: std = np.std(a, axis=axis) #if no weights used return std
def effective_volume_sr_ah(self, a): """Compute the effective volume in [Mpc/h]^3/deg^2 \int_af^ai dV(a') da' with dV(a) = c*d_a^2/(a^4*H(a)) if len(a)==1 the effective volume the integration is done in the interval a'=[a,1]; otherwise the values of a given are interpreted as the extrema of integration interval and the effective volume of the len(a)-1 intervals is returned Parameters ---------- a: float, list, tuble, numpy array scale factor normalised to 1 at present time. output: float effective distance evaluated in a """ a = mf.convert2array(a) if (a.size == 1): #if only one z given, add 0 a = np.r_[a, 0.] eff_vol = np.empty(a.size - 1) #initialise the output array integrand = lambda t: self.angular_distance_ah( t) * self.angular_distance_ah(t) / (t * t * t * t * np.sqrt( self.c.E_a(t))) #define the integrand as a local function. for i, ai, af in it.izip( it.count(), a[:-1], a[1:] ): #do the integration in each redshift bin between z[i] and z[i+1] eff_vol[i] = spi.quad(integrand, ai, af)[0] return ckms * eff_vol / 100.
def angular_distance_a(self, a): """Compute the angular diameter distance d_a=a*r(xi(a)) in Mpc with r(xi) = xi, if Omega_k=0 r(xi) = sin(sqrt(-Omega_k)H_0*xi)/(H_0*sqrt(|Omega_k|)) if Omega_k<0 r(xi) = sinh(sqrt(Omega_k)H_0*xi)/(H_0*sqrt(|Omega_k|)) if Omega_k>0 Parameters ---------- a: float scale factor normalised to 1 at present time. output: float angular diameter distance evaluated in a """ a = mf.convert2array(a) xi = self.comoving_distance_a( a) / ckms #compute the comoving distance without c if (self.c.ok == 0.): #if flat r = xi elif (self.c.ok < 0.): #if closed r = np.sin(np.sqrt(-self.c.ok) * self.c.H0 * xi) / (self.c.H0 * np.sqrt(np.abs(self.c.ok))) else: #if open r = np.sinh(np.sqrt(self.c.ok) * self.c.H0 * xi) / (self.c.H0 * np.sqrt(np.abs(self.c.ok))) return a * ckms * r #angular diameter distance
def effective_volume_sr_zh(self, z): """Compute the effective volume in [Mpc/h]^3/deg^2 \int_zi^zf dV(z') dz' with dV(z) = c*(1+z)^2*d_a^2/H(z) if len(z)==1 the effective volume the integration is done in the interval z'=[0,z]; otherwise the values of z given are interpreted as the extrema of integration of each redshift interval and the effective volume of the len(z)-1 intervals is returned Parameters ---------- z: float, list, tuble, numpy array redshift output: float effective distance evaluated in z """ z = mf.convert2array(z) if (z.size == 1): #if only one z given, add 0 z = np.r_[0., z] eff_vol = np.empty(z.size - 1) #initialise the output array #define the integrand as a local function. integrand = lambda t: (1 + t) * (1 + t) * self.angular_distance_zh( t) * self.angular_distance_zh(t) / np.sqrt(self.c.E_z(t)) for i, zi, zf in it.izip( it.count(), z[:-1], z[1:] ): #do the integration in each redshift bin between z[i] and z[i+1] eff_vol[i] = spi.quad(integrand, zi, zf)[0] return ckms * eff_vol / 100.
def H_a(self, a): """Given a set of cosmological parameters returns H(a) Parameters ---------- a: float, list, tuble, numpy array scale factor normalised to 1 at present time output: np.array with a.shape H(a) """ a = mf.convert2array(a) return self.H0 * np.sqrt(self.E_a(a))
def H_z(self, z): """Given a set of cosmological parameters returns H(z) Parameters ---------- z: float, list, tuble, numpy array redshift output: np.array with a.shape H(z) """ z = mf.convert2array(z) return self.H0*np.sqrt( self.E_a(1./(1.+z)) )
def E_z(self, z): """Given a set of cosmological parameters computes H^2(z)/H_0^2 Parameters ---------- z: float, list, tuble, numpy array redshift output: np.array with a.shape E(z) """ z = mf.convert2array(z) return self.E_a(1./(1.+z))
def E_a(self, a): """Given a set of cosmological parameters computes H^2(a)/H_0^2 Parameters ---------- a: float, list, tuble, numpy array scale factor normalised to 1 at present time output: np.array with a.shape E(a) """ a = mf.convert2array(a) return self.om/(a*a*a) + self.ora/(a*a*a*a) + self.ode*self._de_ev(a) + self.ok/(a*a)
def effective_distance_zh(self, z): """Compute the effective distance d_V(z) = [d_a^2(z) *c*z/ (H(z))]^(1/3) in Mpc/h Parameters ---------- z: float, list, tuble, numpy array redshift output: float effective distance evaluated in z """ z = mf.convert2array(z) d_a = self.angular_distance_zh(z) #get the angular diameter distance dv3 = d_a * d_a * z * ckms / (np.sqrt(self.c.E_z(z)) * 100. ) #cube of the effective distance return np.power(dv3, 1. / 3.) #return the effective distance
def luminosity_distance_z(self, z): """Compute the luminosity distance d_z=r(xi(z))*(1+z) in Mpc with r(xi) = xi, if Omega_k=0 r(xi) = sin(sqrt(-Omega_k)H_0*xi)/(H_0*sqrt(|Omega_k|)) if Omega_k<0 r(xi) = sinh(sqrt(Omega_k)H_0*xi)/(H_0*sqrt(|Omega_k|)) if Omega_k>0 Parameters ---------- z: float, list, tuble, numpy array redshift output: float luminosity distance evaluated in z """ a = 1. / (1 + mf.convert2array(z)) return self.angular_distance_a(a) / (a * a)
def luminosity_distance_a(self, a): """Compute the luminosity distance d_a=r(xi(a))/a in Mpc with r(xi) = xi, if Omega_k=0 r(xi) = sin(sqrt(-Omega_k)H_0*xi)/(H_0*sqrt(|Omega_k|)) if Omega_k<0 r(xi) = sinh(sqrt(Omega_k)H_0*xi)/(H_0*sqrt(|Omega_k|)) if Omega_k>0 Parameters ---------- a: float scale factor normalised to 1 at present time. output: float luminosity distance evaluated in a """ a = mf.convert2array(a) return self.angular_distance_a(a) / (a * a)
def effective_distance_ah(self, a): """Compute the effective distance d_V(a) = [d_a^2(a) *c*(1./a-1)/ (H(a))]^(1/3) in Mpc/h Parameters ---------- a: float scale factor normalised to 1 at present time. output: float effective distance evaluated in a """ a = mf.convert2array(a) z = 1. / a - 1. #get the redshift d_a = self.angular_distance_ah(a) #get the angular diameter distance dv3 = d_a * d_a * z * ckms / (np.sqrt(self.c.E_a(a)) * 100. ) #cube of the effective distance return np.power(dv3, 1. / 3.) #return the effective distance
def comoving_distance_z(self, z): """Compute the comoving distance xi(z) = c*\int_0^z dz'/(H(z')) in Mpc Parameters ---------- z: float, list, tuble, numpy array redshift output: float comoving distance evaluated in z """ z = mf.convert2array(z) cdis = np.empty_like( z) #initialise the array containing the comoving distance oneoH = lambda t: 1. / self.c.H_z( t ) #define the integrand as a local function. Integration in logarith for i, zz in enumerate(z): cdis[i] = spi.quad(oneoH, 0., zz)[0] return ckms * cdis #return the comoving distance
def comoving_distance_a(self, a): """Compute the comoving distance xi(a) = c*\int_a^1 da'/(a'^2H(a')) in Mpc Parameters ---------- a: float, list, tuble, numpy array scale factor normalised to 1 at present time. output: float comoving distance evaluated in a """ a = mf.convert2array(a) cdis = np.empty_like( a) #initialise the array containing the comoving distance a2H = lambda t: 1. / ( np.power(t, 2.) * self.c.H_a(t) ) #define the integrand as a local function. Integration in logarith for i, aa in enumerate(a): cdis[i] = spi.quad(a2H, aa, 1.)[0] return ckms * cdis #return the comoving distance