pynkowski.theory.p2
1import numpy as np 2import scipy.stats 3 4from .utils import get_μ, define_mu, define_us_for_V 5 6norm = scipy.stats.norm() 7 8 9 10def V0_th_P2(u): 11 """Compute the expected value of the normalised first MF v0 at threshold u for the sum of two squared Gaussian isotropic fields normalised for their standard deviations. 12 13 Parameters 14 ---------- 15 u : np.array 16 Thresholds at which the first MF is computed. 17 18 Returns 19 ------- 20 v0 : np.array 21 The expected value of the normalised first MF at thresholds u. 22 23 """ 24 return 1-((1-np.exp(-u/2.)) * (u>=0.)) 25 26def V1_th_P2(u, μ): 27 """Compute the expected value of the normalised second MF v1 at threshold u for the sum of two squared Gaussian isotropic fields normalised for their standard deviations. 28 29 Parameters 30 ---------- 31 u : np.array 32 Thresholds at which the second MF is computed. 33 34 μ : float 35 The derivative of the covariance function at the origin for the Gaussian isotropic scalar field. 36 37 Returns 38 ------- 39 v1 : np.array 40 The expected value of the normalised second MF at thresholds u. 41 42 """ 43 return np.sqrt(μ * (u) / 4.) * np.exp(-u/2.) * (u>=0.) * (np.sqrt(np.pi/8.)) 44 45def V2_th_P2(u, μ): 46 """Compute the expected value of the normalised third MF v2 at threshold u for the sum of two squared Gaussian isotropic fields normalised for their standard deviations. 47 48 Parameters 49 ---------- 50 u : np.array 51 Thresholds at which the third MF is computed. 52 53 μ : float 54 The derivative of the covariance function at the origin for the Gaussian isotropic scalar field. 55 56 Returns 57 ------- 58 v2 : np.array 59 The expected value of the normalised third MF at thresholds u. 60 61 """ 62 return (((μ * (u-1.) * np.exp(-u/2.)) / (2.*np.pi) ) * (u>=0.)) 63 64 65 66 67 68class TheoryP2(): 69 """Class to compute the expected values of Minkowski functionals (MFs) for the sum of two squared Gaussian isotropic fields normalised for their standard deviations defined on the sphere 70 like the polarised intensity of the CMB ($P^2 = Q^2 + U^2$). 71 72 Parameters 73 ---------- 74 us : np.array, optional 75 The thresholds at which the theoretical MFs will be computed. 76 If not given, a range between 0 and 5σ with steps of 0.1σ is considered, 77 with σ=1 the expected standard deviation of the fields U and Q. 78 79 cls : np.array, optional 80 The angular power spectrum associated to the Gaussian isotropic fields. 81 Shape '(..., lmax+1)'. '...' can be 2 (EE, BB) or absent (assumed to be EE+BB). 82 Default : None 83 84 μ : float, optional 85 The derivative of the covariance function at the origin for each of the two independent Gaussian isotropic fields (i.e., U and Q in the cosmological case). 86 If both μ and cls are given, an error will be raised. 87 If only cls is given, μ will be computed from input cls. 88 If neither μ nor cls are given, μ is assumed to be 1. 89 Default : None 90 91 average_bin : bool, optional 92 If True, the results of V1 and V2 are the average on each bin, to be compared with binned computations on maps. 93 If False, the results are the evaluation on the center of each bin. 94 The value is always exactly computed for V0, as the computation on maps does not imply binning. 95 Defaul : True 96 97 edges : bool, optional 98 If False, the given 'us' is considered as an array of uniformly distributed thresholds. 99 If True, input 'us' is considered as a monotonically increasing array of bin edges, including the rightmost edge, allowing for non-uniform distributions of thresholds. 100 In this last case, the thresholds are the central value of the given bins. 101 Neglected if 'us' is not given. 102 Default : False. 103 104 Attributes 105 ---------- 106 us : np.array 107 The thresholds at which the theoretical MFs are computed. 108 109 μ : float 110 The derivative of the covariance function at the origin for the sum of two squared Gaussian isotropic fields. 111 112 """ 113 def __init__(self, us=None, cls=None, μ=None, average_bin=True, edges=False): 114 """Class to compute the expected values of Minkowski functionals (MFs) for a Gaussian isotropic scalar field defined on the sphere. 115 116 """ 117 if (us is None): 118 Δu = 0.05 119 self.us = np.arange(Δu/2., 5.+Δu/2., Δu) 120 self.dus = Δu*np.ones(self.us.shape[0]) 121 else: 122 us = np.array(us) 123 if us.shape == (1,): 124 self.us = us 125 self.dus = 0. 126 else: 127 if edges: 128 self.dus = (us[1:]-us[:-1]) 129 self.us = (us[1:]+us[:-1])/2. 130 else: 131 self.us = us 132 self.dus = (us[1]-us[0])*np.ones(us.shape[0]) 133 134 if (cls is not None) and (μ is None): 135 if (cls.ndim == 2) and (cls.shape[0]==2): 136 clss = (cls[0]+cls[1])/2. 137 elif (cls.ndim == 1): 138 clss = cls/2. 139 else: 140 raise ValueError(r"Cls dimension has to be either (2,lmax+1) or (lmax+1)") 141 else: 142 clss=cls 143 144 self.μ = define_mu(clss,μ) 145 if not average_bin: 146 self.dus = 0.*self.dus 147 148 149 def V0(self): 150 """Compute the expected values of the normalised first MF v0 at the different thresholds us. 151 152 $$\mathbb{E}[{v_{0}(u)}] = \exp (-u/2)$$ 153 """ 154 return (V0_th_P2(self.us)) 155 156 def V1(self): 157 """Compute the expected values of the normalised second MF v1 at the different thresholds us. 158 159 $$\mathbb{E}[{v_{1}(u)}] = {\sqrt{2\pi } \over 8} \sqrt{\mu u}\exp (-{u \over 2})$$ 160 """ 161 us_ = define_us_for_V(self.us,self.dus) 162 v1_ = V1_th_P2(us_, self.μ) 163 164 return np.mean(v1_, axis=1) 165 166 def V2(self): 167 """Compute the expected values of the normalised third MF v2 at the different thresholds us. 168 169 $$\mathbb{E}[{v_{2}(u)}] = \mu {(u-1)\exp (-u/2) \over 2\pi }$$ 170 """ 171 us_ = define_us_for_V(self.us,self.dus) 172 v2_ = V2_th_P2(us_, self.μ) 173 174 return np.mean(v2_, axis=1) 175 176 177__all__ = ["TheoryP2"] 178 179__docformat__ = "numpy"
69class TheoryP2(): 70 """Class to compute the expected values of Minkowski functionals (MFs) for the sum of two squared Gaussian isotropic fields normalised for their standard deviations defined on the sphere 71 like the polarised intensity of the CMB ($P^2 = Q^2 + U^2$). 72 73 Parameters 74 ---------- 75 us : np.array, optional 76 The thresholds at which the theoretical MFs will be computed. 77 If not given, a range between 0 and 5σ with steps of 0.1σ is considered, 78 with σ=1 the expected standard deviation of the fields U and Q. 79 80 cls : np.array, optional 81 The angular power spectrum associated to the Gaussian isotropic fields. 82 Shape '(..., lmax+1)'. '...' can be 2 (EE, BB) or absent (assumed to be EE+BB). 83 Default : None 84 85 μ : float, optional 86 The derivative of the covariance function at the origin for each of the two independent Gaussian isotropic fields (i.e., U and Q in the cosmological case). 87 If both μ and cls are given, an error will be raised. 88 If only cls is given, μ will be computed from input cls. 89 If neither μ nor cls are given, μ is assumed to be 1. 90 Default : None 91 92 average_bin : bool, optional 93 If True, the results of V1 and V2 are the average on each bin, to be compared with binned computations on maps. 94 If False, the results are the evaluation on the center of each bin. 95 The value is always exactly computed for V0, as the computation on maps does not imply binning. 96 Defaul : True 97 98 edges : bool, optional 99 If False, the given 'us' is considered as an array of uniformly distributed thresholds. 100 If True, input 'us' is considered as a monotonically increasing array of bin edges, including the rightmost edge, allowing for non-uniform distributions of thresholds. 101 In this last case, the thresholds are the central value of the given bins. 102 Neglected if 'us' is not given. 103 Default : False. 104 105 Attributes 106 ---------- 107 us : np.array 108 The thresholds at which the theoretical MFs are computed. 109 110 μ : float 111 The derivative of the covariance function at the origin for the sum of two squared Gaussian isotropic fields. 112 113 """ 114 def __init__(self, us=None, cls=None, μ=None, average_bin=True, edges=False): 115 """Class to compute the expected values of Minkowski functionals (MFs) for a Gaussian isotropic scalar field defined on the sphere. 116 117 """ 118 if (us is None): 119 Δu = 0.05 120 self.us = np.arange(Δu/2., 5.+Δu/2., Δu) 121 self.dus = Δu*np.ones(self.us.shape[0]) 122 else: 123 us = np.array(us) 124 if us.shape == (1,): 125 self.us = us 126 self.dus = 0. 127 else: 128 if edges: 129 self.dus = (us[1:]-us[:-1]) 130 self.us = (us[1:]+us[:-1])/2. 131 else: 132 self.us = us 133 self.dus = (us[1]-us[0])*np.ones(us.shape[0]) 134 135 if (cls is not None) and (μ is None): 136 if (cls.ndim == 2) and (cls.shape[0]==2): 137 clss = (cls[0]+cls[1])/2. 138 elif (cls.ndim == 1): 139 clss = cls/2. 140 else: 141 raise ValueError(r"Cls dimension has to be either (2,lmax+1) or (lmax+1)") 142 else: 143 clss=cls 144 145 self.μ = define_mu(clss,μ) 146 if not average_bin: 147 self.dus = 0.*self.dus 148 149 150 def V0(self): 151 """Compute the expected values of the normalised first MF v0 at the different thresholds us. 152 153 $$\mathbb{E}[{v_{0}(u)}] = \exp (-u/2)$$ 154 """ 155 return (V0_th_P2(self.us)) 156 157 def V1(self): 158 """Compute the expected values of the normalised second MF v1 at the different thresholds us. 159 160 $$\mathbb{E}[{v_{1}(u)}] = {\sqrt{2\pi } \over 8} \sqrt{\mu u}\exp (-{u \over 2})$$ 161 """ 162 us_ = define_us_for_V(self.us,self.dus) 163 v1_ = V1_th_P2(us_, self.μ) 164 165 return np.mean(v1_, axis=1) 166 167 def V2(self): 168 """Compute the expected values of the normalised third MF v2 at the different thresholds us. 169 170 $$\mathbb{E}[{v_{2}(u)}] = \mu {(u-1)\exp (-u/2) \over 2\pi }$$ 171 """ 172 us_ = define_us_for_V(self.us,self.dus) 173 v2_ = V2_th_P2(us_, self.μ) 174 175 return np.mean(v2_, axis=1)
Class to compute the expected values of Minkowski functionals (MFs) for the sum of two squared Gaussian isotropic fields normalised for their standard deviations defined on the sphere like the polarised intensity of the CMB ($P^2 = Q^2 + U^2$).
Parameters
- us (np.array, optional): The thresholds at which the theoretical MFs will be computed. If not given, a range between 0 and 5σ with steps of 0.1σ is considered, with σ=1 the expected standard deviation of the fields U and Q.
- cls (np.array, optional): The angular power spectrum associated to the Gaussian isotropic fields. Shape '(..., lmax+1)'. '...' can be 2 (EE, BB) or absent (assumed to be EE+BB). Default : None
- μ (float, optional): The derivative of the covariance function at the origin for each of the two independent Gaussian isotropic fields (i.e., U and Q in the cosmological case). If both μ and cls are given, an error will be raised. If only cls is given, μ will be computed from input cls. If neither μ nor cls are given, μ is assumed to be 1. Default : None
- average_bin (bool, optional): If True, the results of V1 and V2 are the average on each bin, to be compared with binned computations on maps. If False, the results are the evaluation on the center of each bin. The value is always exactly computed for V0, as the computation on maps does not imply binning. Defaul : True
- edges (bool, optional): If False, the given 'us' is considered as an array of uniformly distributed thresholds. If True, input 'us' is considered as a monotonically increasing array of bin edges, including the rightmost edge, allowing for non-uniform distributions of thresholds. In this last case, the thresholds are the central value of the given bins. Neglected if 'us' is not given. Default : False.
Attributes
- us (np.array): The thresholds at which the theoretical MFs are computed.
- μ (float): The derivative of the covariance function at the origin for the sum of two squared Gaussian isotropic fields.
114 def __init__(self, us=None, cls=None, μ=None, average_bin=True, edges=False): 115 """Class to compute the expected values of Minkowski functionals (MFs) for a Gaussian isotropic scalar field defined on the sphere. 116 117 """ 118 if (us is None): 119 Δu = 0.05 120 self.us = np.arange(Δu/2., 5.+Δu/2., Δu) 121 self.dus = Δu*np.ones(self.us.shape[0]) 122 else: 123 us = np.array(us) 124 if us.shape == (1,): 125 self.us = us 126 self.dus = 0. 127 else: 128 if edges: 129 self.dus = (us[1:]-us[:-1]) 130 self.us = (us[1:]+us[:-1])/2. 131 else: 132 self.us = us 133 self.dus = (us[1]-us[0])*np.ones(us.shape[0]) 134 135 if (cls is not None) and (μ is None): 136 if (cls.ndim == 2) and (cls.shape[0]==2): 137 clss = (cls[0]+cls[1])/2. 138 elif (cls.ndim == 1): 139 clss = cls/2. 140 else: 141 raise ValueError(r"Cls dimension has to be either (2,lmax+1) or (lmax+1)") 142 else: 143 clss=cls 144 145 self.μ = define_mu(clss,μ) 146 if not average_bin: 147 self.dus = 0.*self.dus
Class to compute the expected values of Minkowski functionals (MFs) for a Gaussian isotropic scalar field defined on the sphere.
150 def V0(self): 151 """Compute the expected values of the normalised first MF v0 at the different thresholds us. 152 153 $$\mathbb{E}[{v_{0}(u)}] = \exp (-u/2)$$ 154 """ 155 return (V0_th_P2(self.us))
Compute the expected values of the normalised first MF v0 at the different thresholds us.
$$\mathbb{E}[{v_{0}(u)}] = \exp (-u/2)$$
157 def V1(self): 158 """Compute the expected values of the normalised second MF v1 at the different thresholds us. 159 160 $$\mathbb{E}[{v_{1}(u)}] = {\sqrt{2\pi } \over 8} \sqrt{\mu u}\exp (-{u \over 2})$$ 161 """ 162 us_ = define_us_for_V(self.us,self.dus) 163 v1_ = V1_th_P2(us_, self.μ) 164 165 return np.mean(v1_, axis=1)
Compute the expected values of the normalised second MF v1 at the different thresholds us.
$$\mathbb{E}[{v_{1}(u)}] = {\sqrt{2\pi } \over 8} \sqrt{\mu u}\exp (-{u \over 2})$$
167 def V2(self): 168 """Compute the expected values of the normalised third MF v2 at the different thresholds us. 169 170 $$\mathbb{E}[{v_{2}(u)}] = \mu {(u-1)\exp (-u/2) \over 2\pi }$$ 171 """ 172 us_ = define_us_for_V(self.us,self.dus) 173 v2_ = V2_th_P2(us_, self.μ) 174 175 return np.mean(v2_, axis=1)
Compute the expected values of the normalised third MF v2 at the different thresholds us.
$$\mathbb{E}[{v_{2}(u)}] = \mu {(u-1)\exp (-u/2) \over 2\pi }$$