Import NumPy and SciPy (not needed when using --pylab)
In [1]:
%pylab inline
import scipy as sp
Load data from file
In [2]:
zz = np.loadtxt('wiggleZ_DR1_z.dat',dtype='float'); # Load WiggleZ redshifts
In [3]:
np.min(zz) # Check bounds
Out[3]:
In [4]:
np.max(zz)
Out[4]:
Construct histogram from data
There are several histrogram commands: hist() will be fine here, but note the syntax below. Also note that the bin edges are returned, so that there will nbins+1 of these.
In [5]:
nbins = 50; # Is this a good choice?
In [6]:
n, bins, patches = hist(zz,nbins)
In [7]:
x = bins[0:nbins] + (bins[2]-bins[1])/2; # Convert bin edges to centres, chopping the last
Interpolate histogram output -> p(z); n.b. that you can also use numerical quadrature to get $P(z)$ directly.
In [8]:
# Import the function you need
from scipy.interpolate import interp1d
In [9]:
# Build an interpolation function for p(z) that accepts an arbitrary redshift z
zdist = interp1d(x,n, kind='cubic', bounds_error = False, fill_value=0)
In [11]:
z = linspace(0,2,100); plot(z,zdist(z)) # Test your interpolation function out
Out[11]:
Use numerical integration to get $P(z) = \int_0^\infty p(z') dz'$
In [14]:
# Import the function you need
from scipy import integrate
In [15]:
intPz = lambda z0: integrate.quad(zdist,0,z0)[0]
In [16]:
total = intPz(5) # Get normalisation constant by evaluating P(z->\infty)
In [17]:
total # Check that this worked
Out[17]: