1515# limitations under the License.
1616
1717from math import lgamma
18+ from math import log
19+ from math import pi
1820
1921import numpy as np
2022
2123from cgpm .primitives .distribution import DistributionGpm
2224from cgpm .utils import general as gu
2325
2426
27+ LOG2 = log (2 )
28+ LOGPI = log (pi )
29+ LOG2PI = LOG2 + LOGPI
30+
31+
2532class Normal (DistributionGpm ):
2633 """Normal distribution with normal prior on mean and gamma prior on
2734 precision. Collapsed.
@@ -163,15 +170,15 @@ def calc_predictive_logp(x, N, sum_x, sum_x_sq, m, r, s, nu):
163170 N + 1 , sum_x + x , sum_x_sq + x * x , m , r , s , nu )
164171 ZN = Normal .calc_log_Z (rn , sn , nun )
165172 ZM = Normal .calc_log_Z (rm , sm , num )
166- return - .5 * np . log ( 2 * np . pi ) + ZM - ZN
173+ return - .5 * LOG2PI + ZM - ZN
167174
168175 @staticmethod
169176 def calc_logpdf_marginal (N , sum_x , sum_x_sq , m , r , s , nu ):
170177 mn , rn , sn , nun = Normal .posterior_hypers (
171178 N , sum_x , sum_x_sq , m , r , s , nu )
172179 Z0 = Normal .calc_log_Z (r , s , nu )
173180 ZN = Normal .calc_log_Z (rn , sn , nun )
174- return - (N / 2. ) * np . log ( 2 * np . pi ) + ZN - Z0
181+ return - (N / 2. ) * LOG2PI + ZN - Z0
175182
176183 @staticmethod
177184 def posterior_hypers (N , sum_x , sum_x_sq , m , r , s , nu ):
@@ -185,8 +192,8 @@ def posterior_hypers(N, sum_x, sum_x_sq, m, r, s, nu):
185192 @staticmethod
186193 def calc_log_Z (r , s , nu ):
187194 return (
188- ((nu + 1. ) / 2. ) * np . log ( 2 )
189- + .5 * np . log ( np . pi )
195+ ((nu + 1. ) / 2. ) * LOG2
196+ + .5 * LOGPI
190197 - .5 * np .log (r )
191198 - (nu / 2. ) * np .log (s )
192199 + lgamma (nu / 2.0 ))
0 commit comments