def test_special_cases(): assert_equal(sc.owens_t(5, 0), 0) assert_allclose(sc.owens_t(0, 5), 0.5*np.arctan(5)/np.pi, rtol=5e-14) # Target value is 0.5*Phi(5)*(1 - Phi(5)) for Phi the CDF of the # standard normal distribution assert_allclose(sc.owens_t(5, 1), 1.4332574485503512543e-07, rtol=5e-14)
def test_special_cases(): assert_equal(sc.owens_t(5, 0), 0) assert_allclose(sc.owens_t(0, 5), 0.5*np.arctan(5)/np.pi, rtol=5e-14) # Target value is 0.5*Phi(5)*(1 - Phi(5)) for Phi the CDF of the # standard normal distribution assert_allclose(sc.owens_t(5, 1), 1.4332574485503512543e-07, rtol=5e-14)
def Cov2S2(tau, g, strategy=0): vf = tau2vf(tau, strategy=strategy) x = np.sqrt((1 - g) / (1 + g)) if strategy: # |u|<tau x1 = np.where(np.abs(x) < 1.e-6, 1.e-6, x) S2 = 2 * vf - 4 * owens_t(tau, x) - 4 * owens_t(tau, 1 / x1) else: # u<tau S2 = vf - 2 * owens_t(tau, x) return S2
def change_of_var_Phi_withGradients(m, s, dmdx, dsdx): """ from arrays of mean and std of NORMAL variables X, produce the mean and std of Phi(X) where Phi is the normal cumulative distribution E[Phi(X)] = Phi(m/sqrt(1 + s^2)) Var[Phi(X)] = E - E^2 - T (m/sqrt(1 + s^2), m/sqrt(1 + 2*s^2)) RETURNS VARIANCE (and not STD) """ assert np.ndim( dmdx) == np.ndim(m) + 1, 'dim of m: {}, dim of dm: {}'.formate( np.ndim(m), np.ndim(dmdx)) assert np.ndim( dsdx) == np.ndim(s) + 1, 'dim of s: {}, dim of ds: {}'.formate( np.ndim(s), np.ndim(dsdx)) v = np.square(s) a = m / np.sqrt(1 + v) h = 1 / np.sqrt(1 + 2 * v) mcdf = stats.norm.cdf(a) vcdf = mcdf - np.square(mcdf) - 2 * special.owens_t(a, h) # Carefull dadx = dmdx / np.sqrt( 1 + v[:, :, None]) - a[:, :, None] * s[:, :, None] * dsdx / ( 1 + v[:, :, None]) #dhdx = - 2 * s * dsdx * h / (1+2*v) A = np.exp(-np.square(a[:, :, None]) / 2) dmcdfdx = 1 / np.sqrt(2 * np.pi) * A * dadx dTda = -A / (2 * np.sqrt(2 * np.pi)) * erf(a * h / np.sqrt(2))[:, :, None] dTdhdhdx = -(np.exp(-np.square(m * h))[:, :, None] * h[:, :, None] * s[:, :, None] * dsdx) / (2 * np.pi * (1 + v[:, :, None])) dTdx = dTda * dadx + dTdhdhdx dvcdfdx = dmcdfdx - 2 * mcdf[:, :, None] * dmcdfdx - 2 * dTdx return mcdf, vcdf, dmcdfdx, dvcdfdx
def test_infs(): h = 1 res = 0.5*sc.erfc(h/np.sqrt(2)) assert_allclose(sc.owens_t(h, np.inf), res, rtol=5e-14) assert_allclose(sc.owens_t(h, -np.inf), -res, rtol=5e-14) assert_equal(sc.owens_t(np.inf, 1), 0) assert_equal(sc.owens_t(-np.inf, 1), 0) assert_equal(sc.owens_t(np.inf, np.inf), 0) assert_equal(sc.owens_t(-np.inf, np.inf), 0) assert_equal(sc.owens_t(np.inf, -np.inf), -0.0) assert_equal(sc.owens_t(-np.inf, -np.inf), -0.0)
def test_infs(): h = 1 res = 0.5 * sc.erfc(h / np.sqrt(2)) assert_allclose(sc.owens_t(h, np.inf), res, rtol=5e-14) assert_allclose(sc.owens_t(h, -np.inf), -res, rtol=5e-14) assert_equal(sc.owens_t(np.inf, 1), 0) assert_equal(sc.owens_t(-np.inf, 1), 0) assert_equal(sc.owens_t(np.inf, np.inf), 0) assert_equal(sc.owens_t(-np.inf, np.inf), 0) assert_equal(sc.owens_t(np.inf, -np.inf), -0.0) assert_equal(sc.owens_t(-np.inf, -np.inf), -0.0)
def change_of_var_Phi(m, s): """ from arrays of mean and std of NORMAL variables X, produce the mean and std of Phi(X) where Phi is the normal cumulative distribution E[Phi(X)] = Phi(m/sqrt(1 + s^2)) Var[Phi(X)] = E - E^2 - T (m/sqrt(1 + s^2), m/sqrt(1 + 2*s^2)) """ a = m / np.sqrt(1 + np.square(s)) h = 1 / np.sqrt(1 + 2 * np.square(s)) m_cdf = stats.norm.cdf(a) s_cdf = np.sqrt(m_cdf - np.square(m_cdf) - 2 * special.owens_t(a, h)) return m_cdf, s_cdf
def margin(self, eps=0.0001): _t = list(np.linspace(-3, 5, 101)) _phi = np.asarray([norm.cdf(t) for t in _t]) _owens_t = np.asarray([owens_t(t, self.alpha) for t in _t]) _cdf = _phi - 2 * _owens_t idx_l = next(x for x in range(len(_cdf)) if _cdf[x] > eps) idx_h = next(x for x in range(len(_cdf)) if _cdf[x] > 1 - eps) low = _t[max(idx_l - 1, 0)] high = _t[min(idx_h + 1, 100)] low = (low * self.omega) + self.zeta high = (high * self.omega) + self.zeta return low, high
def testOwensTLarge(self, dtype): seed_stream = test_util.test_seed_stream() a = tf.random.uniform(shape=[int(1e4)], minval=100., maxval=1000., dtype=dtype, seed=seed_stream()) h = tf.random.uniform(shape=[int(1e4)], minval=100., maxval=1000., dtype=dtype, seed=seed_stream()) a_, h_, owens_t_ = self.evaluate([a, h, tfp.math.owens_t(h, a)]) self.assertAllClose(scipy_special.owens_t(h_, a_), owens_t_)
def test_infs(): h, a = 0, np.inf # T(0, a) = 1/2π * arctan(a) res = 1 / (2 * np.pi) * np.arctan(a) assert_allclose(sc.owens_t(h, a), res, rtol=5e-14) assert_allclose(sc.owens_t(h, -a), -res, rtol=5e-14) h = 1 # Refer Owens T function definition in Wikipedia # https://en.wikipedia.org/wiki/Owen%27s_T_function # Value approximated through Numerical Integration # using scipy.integrate.quad # quad(lambda x: 1/(2*pi)*(exp(-0.5*(1*1)*(1+x*x))/(1+x*x)), 0, inf) res = 0.07932762696572854 assert_allclose(sc.owens_t(h, np.inf), res, rtol=5e-14) assert_allclose(sc.owens_t(h, -np.inf), -res, rtol=5e-14) assert_equal(sc.owens_t(np.inf, 1), 0) assert_equal(sc.owens_t(-np.inf, 1), 0) assert_equal(sc.owens_t(np.inf, np.inf), 0) assert_equal(sc.owens_t(-np.inf, np.inf), 0) assert_equal(sc.owens_t(np.inf, -np.inf), -0.0) assert_equal(sc.owens_t(-np.inf, -np.inf), -0.0)
def predict( self, x: Union[torch.Tensor, np.ndarray], probability_space: bool = False ) -> Tuple[torch.Tensor, torch.Tensor]: """Query the model for posterior mean and variance. Args: x (torch.Tensor): Points at which to predict from the model. probability_space (bool, optional): Return outputs in units of response probability instead of latent function value. Defaults to False. Returns: Tuple[np.ndarray, np.ndarray]: Posterior mean and variance at queries points. """ with torch.no_grad(): post = self.posterior(x) fmean = post.mean.squeeze() fvar = post.variance.squeeze() if probability_space: if isinstance(self.likelihood, BernoulliLikelihood): # Probability-space mean and variance for Bernoulli-probit models is # available in closed form, Proposition 1 in Letham et al. 2022 (AISTATS). a_star = fmean / torch.sqrt(1 + fvar) pmean = Normal(0, 1).cdf(a_star) t_term = torch.tensor( owens_t(a_star.numpy(), 1 / np.sqrt(1 + 2 * fvar.numpy())), dtype=a_star.dtype, ) pvar = pmean - 2 * t_term - pmean.square() return promote_0d(pmean), promote_0d(pvar) else: fsamps = post.sample(torch.Size([10000])) if hasattr(self.likelihood, "objective"): psamps = self.likelihood.objective(fsamps) else: psamps = norm.cdf(fsamps) pmean, pvar = psamps.mean(0), psamps.var(0) return promote_0d(pmean), promote_0d(pvar) else: return promote_0d(fmean), promote_0d(fvar)
def variance(u, sigma_0, kappa): global J0, J1, sigma_1 return J0*J0 * mean_rate(u, sigma_0, kappa) - 2 * special.owens_t(u/np.sqrt(1.0 + sigma_0 + sigma_1 * kappa * kappa), 1/np.sqrt(1.0 + 2*sigma_0 + 2*sigma_1 * kappa * kappa))
def variance(u, sigma): var = np.maximum(0, gv.J0**2 * ( mean_rate(u, sigma) - 2 * special.owens_t(u/np.sqrt(1.0 + sigma), 1/np.sqrt(1.0 + 2*sigma) ) ) ) return var
def cdf1D(self, x): if self._check1D(): x = np.squeeze( (x-self.loc) / self.scale ) s = np.squeeze(self.alpha) return stats.norm.cdf(x) - 2 * special.owens_t(x, s)
def test_symmetries(): np.random.seed(1234) a, h = np.random.rand(100), np.random.rand(100) assert_equal(sc.owens_t(h, a), sc.owens_t(-h, a)) assert_equal(sc.owens_t(h, a), -sc.owens_t(h, -a))
def test_nans(): assert_equal(sc.owens_t(20, np.nan), np.nan) assert_equal(sc.owens_t(np.nan, 20), np.nan) assert_equal(sc.owens_t(np.nan, np.nan), np.nan)
def test_nans(): assert_equal(sc.owens_t(20, np.nan), np.nan) assert_equal(sc.owens_t(np.nan, 20), np.nan) assert_equal(sc.owens_t(np.nan, np.nan), np.nan)
def test_symmetries(): np.random.seed(1234) a, h = np.random.rand(100), np.random.rand(100) assert_equal(sc.owens_t(h, a), sc.owens_t(-h, a)) assert_equal(sc.owens_t(h, a), -sc.owens_t(h, -a))