def _tmeancost(mu, sigma, bound): ''' additive constant for the mean of the truncated normal ''' l, u = bound l = (l - mu) / sigma u = (u - mu) / sigma n = norm_pdf(u) - norm_pdf(l) d = norm_cdf(u) - norm_cdf(l) c = sigma * n / d return c
def tnorm_pdf(x, mu, sigma, bound): ''' truncated normal density function ''' x = np.asarray(x) x = (x - mu) / sigma u = (bound[1] - mu) / sigma l = (bound[0] - mu) / sigma c = norm_cdf(u) - norm_cdf(l) d = norm_pdf(x) / (c * sigma) return np.where((x >= l) & (x <= u), d, 0.)
def tnorm_cdf(x, mu, sigma, bound): ''' truncated normal distribution function ''' x = np.asarray(x) x = (x - mu) / sigma u = (bound[1] - mu) / sigma l = (bound[0] - mu) / sigma c = norm_cdf(u) - norm_cdf(l) p = (norm_cdf(x) - norm_cdf(l)) / c p[x < l] = 0 p[x > u] = 1 return p
def _tvarcost(mu, sigma, bound): ''' multiplicative factor for the variance of the truncated normal ''' l,u = bound l = (l - mu) / sigma u = (u - mu) / sigma # as x --> +/- Inf, x * f(x) --> 0 for the gaussian density f, but 0. * # Inf would normally give NaN n1_1 = 0. if np.isinf(u) else u * norm_pdf(u) n1_2 = 0. if np.isinf(l) else l * norm_pdf(l) n1 = n1_1 - n1_2 n2 = norm_pdf(u) - norm_pdf(l) d = norm_cdf(u) - norm_cdf(l) c = 1 + n1 / d - (n2 /d )**2 assert c > 0, "c = %g " % c return c