Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALInference 4.1.9.1-3a66518
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
bayestar_cosmology.py
Go to the documentation of this file.
2# Copyright (C) 2017 Leo Singer
3#
4# This program is free software; you can redistribute it and/or modify it
5# under the terms of the GNU General Public License as published by the
6# Free Software Foundation; either version 2 of the License, or (at your
7# option) any later version.
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12# Public License for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc.,
16# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17#
18from __future__ import print_function
19"""
20Generate Chebyshev coefficients for BAYESTAR uniform-in-comoving-volume prior.
21"""
22import argparse
23import os
24import astropy.cosmology
25import astropy.units as u
26import numpy as np
27import scipy.misc
28
29parser = argparse.ArgumentParser()
30parser.add_argument(
31 'cosmology', choices=astropy.cosmology.parameters.available,
32 default='WMAP9', nargs='?', help='Cosmological model')
33args = parser.parse_args()
34
35cosmo = astropy.cosmology.default_cosmology.get_cosmology_from_string(
36 args.cosmology)
37
38
39def dVC_dVL_for_z(z):
40 """Ratio between the comoving volume element dVC and a
41 DL**2 prior at redshift z."""
42 Ok0 = cosmo.Ok0
43 DH = cosmo.hubble_distance
44 DM_by_DH = (cosmo.comoving_transverse_distance(z) / DH).value
45 DC_by_DH = (cosmo.comoving_distance(z) / DH).value
46 zplus1 = z + 1.0
47 if Ok0 == 0.0:
48 ret = 1.0
49 elif Ok0 > 0.0:
50 ret = np.cosh(np.sqrt(Ok0) * DC_by_DH)
51 else: # Ok0 < 0.0 or Ok0 is nan
52 ret = np.cos(np.sqrt(-Ok0) * DC_by_DH)
53 ret *= zplus1
54 ret += DM_by_DH * cosmo.efunc(z)
55 ret *= np.square(zplus1)
56 return 1.0 / ret
57
58
59@np.vectorize
60def z_for_DL(DL):
61 return astropy.cosmology.z_at_value(
62 cosmo.luminosity_distance, DL * u.Mpc)
63
64
66 return dVC_dVL_for_z(z_for_DL(DL))
67
68
69DL = np.logspace(0, 6, 32)
70log_DL = np.log(DL)
71dVC_dVL = dVC_dVL_for_DL(DL)
72log_dVC_dVL = np.log(dVC_dVL)
73
74
75def func(x):
76 return np.log(dVC_dVL_for_DL(np.exp(x)))
77
78
79high_z_x0 = np.log(1e6)
80high_z_y0 = func(high_z_x0)
81high_z_slope = scipy.misc.derivative(func, high_z_x0)
82high_z_intercept = high_z_y0 - high_z_slope * high_z_x0
83
84filename = os.path.basename(__file__)
85print('/* DO NOT EDIT. Automatically generated by', filename, '*/')
86print('static const double dVC_dVL_data[] = {')
87print(*('\t{:+.8e}'.format(c) for c in log_dVC_dVL), sep=',\n')
88print('};')
89print('static const double dVC_dVL_tmin = {:.15f};'.format(log_DL[0]))
90print('static const double dVC_dVL_tmax = {:.15f};'.format(log_DL[-1]))
91print('static const double dVC_dVL_dt = {:.15f};'.format(
92 np.diff(log_DL)[0]))
93print('static const double dVC_dVL_high_z_slope = {:.15f};'.format(
94 high_z_slope))
95print('static const double dVC_dVL_high_z_intercept = {:.15f};'.format(
96 high_z_intercept))
97
98
99def exact_dVC_dVL(DL):
100 """An alternate expression of dVC_dVL_for_DL."""
101 z = z_for_DL(DL)
102 DL = DL * u.Mpc
103 DH = cosmo.hubble_distance
104 DC = cosmo.comoving_distance(z)
105 DM = cosmo.comoving_transverse_distance(z)
106 dVC_dz = cosmo.differential_comoving_volume(z)
107
108 Ok0 = cosmo.Ok0
109 if Ok0 == 0.0:
110 dDM_dDC = 1.0
111 elif Ok0 > 0.0:
112 dDM_dDC = np.cosh(np.sqrt(Ok0) * DC / DH)
113 else: # Ok0 < 0.0 or Ok0 is nan
114 dDM_dDC = np.cos(np.sqrt(-Ok0) * DC / DH)
115
116 dDC_dz = DH * cosmo.inv_efunc(z)
117 dDL_dz = DM + (1 + z) * dDM_dDC * dDC_dz
118 dVL_dz = np.square(DL) * dDL_dz / u.sr
119
120 return dVC_dz / dVL_dz
121
122
123DL = np.logspace(-2, 6, 1000)
124dVC_dVL = exact_dVC_dVL(DL)
125
126print('static const double dVC_dVL_test_x[] = {')
127print(*('\t{:+.8e}'.format(c) for c in DL), sep=',\n')
128print('};')
129print('static const double dVC_dVL_test_y[] = {')
130print(*('\t{:+.8e}'.format(c) for c in dVC_dVL), sep=',\n')
131print('};')
def dVC_dVL_for_z(z)
Ratio between the comoving volume element dVC and a DL**2 prior at redshift z.
def exact_dVC_dVL(DL)
An alternate expression of dVC_dVL_for_DL.