Package: pyhalomodel
Halo modelThis repository is home to thepyhalomodelpackage, which was written as part of theAsgari, Mead & Heymans (2023)halo-model review paper. The software is written entirely inPython, with extendability and reusability in mind. The purpose of this software is to take some of the drudgery out of performing basic calculations using the halo model. While the integrals that the halo model requires the researcher to evaluate are simple, in practice the changes of variables required to integrate halo profiles against halo mass functions can be confusing and tedious. In our experience this confusion has led to bugs and misunderstandings over the years, and our hope for this software is to reduce the proliferation of these somewhat. Our software can produce power spectra foranycombinations of tracers, and simply requires halo profiles for the tracers to be specified. These could be matter profiles, galaxy profiles, or anything else, for example electron-pressure or HI profiles.You might also be interested in this purePythonimplementation ofHMcode, which makes use of thepyhalomodelpackage.DependenciesnumpyscipyInstallationFor the general user,pyhalomodelcan be installed usingpip:pip install pyhalomodelIf you you want to modify the source, or use the demo notebooks, then simply clone the repository. You can then create an environment with all necessary dependencies usingpoetry. From the clonedpyhalomodeldirectory:poetry installThe demo notebooks require some additional dependencies:camb;dark-emulator;ipykernel;matplotlib;halomod. These will be installed in the environment automatically. You can also install withoutpoetry, either system wide or using another environment manager; we include arequirements.txt.UsageStart a script withimport numpy as np
import pyhalomodel as haloImporting viaimport pyhalomodel as halois nice because the functions and classes then have readable names (e.g.,halo.model,halo.profile). To make non-linear power spectrum predictions using the halo model requires a linear power spectrum. In our demonstration notebooks we always take this fromCAMB, but it could come from any source. Calculations also require the variance in the linear density field when smoothed on comoving scale $R$: $\sigma^2(R)$. Once again, this function could come from any source, but we take it fromCAMB.A typical call to create an instance of amodelobject looks likemodel = halo.model(z, Omega_m, name='Tinker et al. (2010)', Dv=330., dc=1.686, verbose=True)where:zis the redshiftOmega_mis the cosmological matter density parameter (at $z=0$)nameis the name of the halo mass function/linear halo bias pair to useDvis the halo overdensity definitiondcis the linear collapse thresholdCurrently supportednamechoices are:Press & Schecter (1974)Sheth & Tormen (1999)Sheth, Mo & Tormen (2001)Tinker et al. (2010)Despali et al. (2016)When themodelinstance is created the desired mass function is initialised.To make a power-spectrum calculation one simply calls:Pk_2h, Pk_1h, Pk_hm = model.power_spectrum(k, Pk_lin, M, sigmaM, profiles)where:kis an array of comoving Fourier wavenumbers (units: $h\mathrm{Mpc}^{-1}$)Pk_linis an array of linear power spectrum values at a givenk(units: $(h^{-1}\mathrm{Mpc})^3$)Mis an array of halo masses (units: $h^{-1}M_\odot$)sigmaMis an array of root-variance linear density values at Lagrangian scales corresponding toMprofilesis a dictionary of haloprofiles (which could contain a single entry)The function returns a tuple ofPk_2h(two-halo),Pk_1h(one-halo), andPk_hm(halo model) power at the chosenkvalues. Thepower_spectrummethod computes all possible auto- and cross-spectra given the dictionary of halo profiles. For example, if three profiles were in the dictionary this would compute the three autospectra and the three unique cross spectra. The returnedPkare then dictionaries containing all possible spectra. For example, ifprofiles={'a':profile_a, 'b':profile_b, 'c':profile_c}then thePkdictionaries will contain the keys:a-a;a-b;a-c;b-b;b-c,c-c. It will also contain symmetric combinations (e.g.,b-aas well asa-b) but the values will be identical. Each value in thePkdictionary is an array of the power at allk.Halo profiles are instances of theprofileclass. These are initialised in Fourier space like:profile = halo.profile.Fourier(k, M, Uk, amplitude=None, normalisation=1., variance=None, mass_tracer=False, discrete_tracer=False)wherekis an array of comoving Fourier wavenumbers (units: $h\mathrm{Mpc}^{-1}$)Mis an array of halo masses (units: $h^{-1}M_\odot$)Ukis a 2D array of the Fourier halo profile at eachk(first index) andM(second index) valueamplitudeis an array of (mean) profile amplitudes, corresponding to eachMnormalisationis a float containing the field normalisationvarianceis an array containing the variance in the profile amplitude at eachMmass_traceris a boolean telling the code if the profile corresponds to mass densitydiscrete_traceris a boolean telling the code if it dealing with a discrete tracer or notThe arrayskandMbe identical to those in the subsequentmodel.power_spectrumcall. Ifamplitude=Nonethe Fourier profile is assumed to be normalised such that $U(k\to0, M)$ gives the total contribution of the halo to the field. Otherwise the profile is renormalised by theamplitude, and $U(k\to0, M)=1$ is assumed.Some examples best illustrate how to create your own halo profiles:matter_profile = halo.profile.Fourier(k, M, Uk_matter, amplitude=M, normalisation=rho_matter, mass_tracer=True)would create a matter profile. HereUk_matterwould be the normalised Fourier transform of a matter profile (e.g., an NFW profile), the amplitude of each profile is exactlyM(because the haloes are the mass), but the field normalisation isrho_matter(which can be accessed viamodel.rhom) because the field we are usually interested in is matteroverdensity. We usemass_tracer=Trueto tell the code that the profile corresponds to mass. Note that in this case we would get identical behaviour if we fixed the profile amplitude asamplitude=M/rho_matterand the field normalisation asnormalisation=1..galaxy_profile = halo.profile.Fourier(k, M, Uk_galaxy, amplitude=N_galaxy, normalisation=rho_galaxy, variance=var_galaxy, discrete_tracer=True)would create a galaxy profile. HereUk_galaxywould be the normalised Fourier transform of a galaxy profile (e.g., an isothermal profile). The amplitude of the profile is the mean galaxy-occupation number at eachM:N_galaxy. The field is normalised by the mean galaxy density:rho_galaxy. For a given assumption about the mean galaxy-occupation number and halo model this can be calculated using theaveragemethod of themodelclass:rho_galaxy = hmod.average(M, sigmaM, N_galaxy)The variance in galaxy number at eachMisvar_galaxy. If one is assuming Poisson statistics thenvariance=N_galaxyis appropriate, but any value can be used in principle, includingvariance=None, which ignores the contribution of tracer-occupation variance to the power. We tell the code thatdiscrete_tracer=Truebecause in the discrete-tracer case it is essential to split the profile amplitude from the field normalisation if the discreteness of the tracer is to be accounted for properly.Halo profiles can also be specified in configuration (real) space, via a function of radius from the halo centre. This is slower than specifying the Fourier profiles since the conversion of the profile to Fourier space will need to be performed internally.halo.profile.configuration(k, M, rv, c, differential_profile, amplitude=None, normalisation=1., variance=None, mass_tracer=False, discrete_tracer=False):the arguments are similar to those for Fourier-space profiles, except thatdifferential_profileis a the halo profile multiplied by $4\pi r^2$ with call signaturedifferential_profile(r, M, rv, c)rvis the halo virial radius (units: $h^{-1}\mathrm{Mpc}$)cis the halo concentrationthe differential halo profile is the function defined such that integrating in radius between $0$ and $r_\mathrm{v}$ gives the total contribution of an individual halo to the field. It is usually the standard density profile multiplied $4\pi r^2$. This convention is used so as to avoid singularities that often occur in halo profiles at $r=0$. Again, some examples best illustrate how to use thisdef differential_profile_matter(r, M, rv, c):
    # This is NFW (1./((r/rs)*(1.+r/rs)**2)) multiplied by 4pir^2 with constant factors ignored
    rs = rv/c
    return r/(1.+r/rs)**2.

matter_profile = halo.profile.configuration(k, M, rv, c, differential_profile_matter, amplitude=M/rho_matter, mass_tracer=True)note that because we specify the amplitude here we do not need to worry about constant factors in thedifferential_profiledefinition, since the profile normalisation will be calculated self consistently. Note also that because we setamplitude=M/rho_matter(matteroverdensity) we can omit thenormalisationargument, which defaults to1..# Isothermal profile: 1/r^2, multiplied by 4pir^2 with constant factors ignored
differential_profile_gal = lambda r, M, rv, c: 1. 

galaxy_profile = halo.profile.configuration(k, M, rv, c, differential_profile_gal, amplitude=N_galaxy, normalisation=rho_galaxy, discrete_tracer=True)in the discrete tracer case it is important to split upnormalisationandamplitudeso thatamplitudeis something that can be interpreted as the mean of a discrete probability distribution. In this example we have also decided to ignore the contribution of the variance in the number of galaxies at fixed halo mass to the eventual power spectrum calculation.Note that thecovariancein the mean profile amplitude between two different tracers is not currently supported. This can be important in halo-occupation models where galaxies are split into centrals and satellites and the presence of a satellite galaxy is conditional on the halo first containing a central galaxy. We hope to include this in future. Also anyspatialvariance or covariance in halo profiles at fixed mass is not currently supported; we have no plans to include this in future.NotebooksThere are severaljupyternotebooks in thenotebooks/directory giving examples of how to use the code. The first one to try isdemo-basic.ipynb, which gives an overview of the main features ofpyhalomodel. As a bonus, we include notebooks that produce (almost) all of the plots from the review paper.CitationPlease add a citation toAsgari, Mead & Heymans (2023)if you use this code.