Example #1
0
    def prior(self, params):
        mp = np.array([0.] * self.l)
        qp = mp.copy()
        if self.fixp:
            pp = np.array([0.])
        else:
            pp = mp.copy()
        sd = np.array([0.])
        for i in range(self.l):
            mp[i] = norm.logpdf(params['m'][i], self.pr['mp'][i][0],
                                self.pr['mp'][i][1])
            #qp[i] = norm.logpdf(params['q'][i], self.pr['qp'][i][0],self.pr['qp'][i][1])
            qp[i] = uniform.logpdf(params['q'][i], 0, 10)
            if self.fixp is False:
                pp[i] = uniform.logpdf(params['p'][i], 0, 10)
        #pp[0] = norm.logpdf(params['p'][0], self.pr['pp'][0], self.pr['pp'][1])
        if self.fixp:
            pp[0] = uniform.logpdf(params["p"][0], 0, 5)
        sd[0] = norm.logpdf(params['sd'][0], self.pr['sd'][0],
                            self.pr['sd'][1])
        sumll = np.sum(mp) + np.sum(qp) + np.sum(pp) + sd

        ##  Prior For price
        if self.price is not None:
            if self.hete:
                ap = norm.logpdf(params["a"][0], self.pr["a"][0],
                                 self.pr["a"][1])
            else:
                ap = 0
                for i in params["a"]:
                    ap += norm.logpdf(params["a"][i], self.pr["a"][0],
                                      self.pr["a"][1])
            sumll += ap

        return sumll[0]
Example #2
0
def logprior(sig_r=None, sig_s=None, sig_i=None, rho1=None, rho2=None, rho3=None,
             rbar=None, sbar=None, ibar=None,
	     sigma80=None, Om0=None, Om_a=0.01, Om_b=0.99, sig_star=None, **kwargs):

	ret = 0.0
	#   Uniform priors (location parameters)
	if rho1 is not None:
		ret += uniform.logpdf(rho1, loc=-0.999, scale=1.998)
	if rho2 is not None:
		ret += uniform.logpdf(rho2, loc=-0.999, scale=1.998)
	if rho3 is not None:
		ret += uniform.logpdf(rho3, loc=-0.999, scale=1.998)

	if rbar is not None:
		ret += uniform.logpdf(rbar, loc=0.05, scale=0.25)
	if sbar is not None:
		ret += 0.0 # uniform.logpdf(sbar, loc=0.5, scale=5.0)
	if ibar is not None:
		ret += 0.0 # uniform.logpdf(ibar, loc=1.0, scale=6.0)

	if Om0 is not None:
		ret += uniform.logpdf(Om0, loc=Om_a, scale=Om_b-Om_a)

	#   Log Uniform priors (scale parameters)
	if sig_r is not None:
		ret += reciprocal.logpdf(sig_r, 1e-05, 1e+01)
	if sig_s is not None:
		ret += reciprocal.logpdf(sig_s, 1e-05, 1e+01)
	if sig_i is not None:
		ret += reciprocal.logpdf(sig_i, 1e-05, 1e+01)
	if sigma80 is not None:
		ret += reciprocal.logpdf(sigma80, 1e-05, 1e+01)
	if sig_star is not None:
		ret += reciprocal.logpdf(sig_star, 1e-02, 600.)
	return ret
Example #3
0
    def compute_log_prior(self, params: ndarray) -> float:
        """Calcuales the the natural log of the prior p(params).  

        This implements the the fixed emperically determined priors from the
        paper.

        Args:
            params : array of model parameters [ln_tE,ln_A0,ln_deltaT,fbl,mb],
                or [ln(Einstien time),ln(Max Amplication),ln(time since alert),
                blending paramerter,baseline magnitude],
                shape = (5,).
        Returns:
            ln_prior : natural logarithm of the prior.
        """
        ln_tE = params[0]
        ln_A0 = params[1]
        ln_deltaT = params[2]
        fbl = params[3]
        mb = params[4]

        # Equation (16,15,17) (note that Albrow uses "log" for log10)
        log10e = np.log10(np.exp(1))
        ln_pr_ln_tE = np.log(0.476) - (
            (log10e * ln_tE - 1.333)**2 / 0.330) + np.log(log10e)
        ln_pr_ln_A0 = np.log(0.660) - (1.289 * log10e * ln_A0) + np.log(log10e)
        ln_pr_ln_deltaT = np.log(0.156) - ((log10e*ln_deltaT - 1.432)**2 / 0.458) +\
                np.log(log10e)

        # Paper doesnt mention the prior used, but I assume it to be uniform
        ln_pr_fbl = uniform.logpdf(fbl, 0.0, 1.0)

        # Paper doesnr mention the prior used but I will asuumed it to be uniform
        ln_pr_mb = uniform.logpdf(mb, self.mag_min - 1.0, self.mag_max + 1.0)

        return ln_pr_fbl + ln_pr_ln_A0 + ln_pr_ln_deltaT + ln_pr_ln_tE + ln_pr_mb
Example #4
0
def noise_density(params, x):
    xy = x[['x', 'y']].values
    dens_x = uniform.logpdf(xy[:, 0],
                            loc=xy[:, 0].min(),
                            scale=xy[:, 0].max() - xy[:, 0].min())
    dens_y = uniform.logpdf(xy[:, 1],
                            loc=xy[:, 1].min(),
                            scale=xy[:, 1].max() - xy[:, 1].min())
    return np.log(params['w']) + dens_x + dens_y
Example #5
0
def prior(a, b, sd):
    # 'uninformative' paramters for a_prior and b_prior
    # (uniform distribution and normal distribution)
    a_prior = uniform.logpdf(a, loc=0, scale=10)  # MIN=loc, MAX=loc+scale
    b_prior = norm.logpdf(b, scale=5)  # loc=mean(default:0),scale=var
    # 1/sigma is applied for denying standard deviation's informativeness.
    # (check 'Jeffreys Prior' for more detail.)
    # (add epsilon constant to avoid division by zero.)
    sd_prior = 1 / (sd + eps) * uniform.logpdf(
        sd, loc=0, scale=30)  # MIN=loc, MAX=loc+scale
    return a_prior + b_prior + sd_prior
Example #6
0
 def test_gaussian(self):
     samples = []
     for i in range(5000):
         s = rejection_sample(
             lambda x: norm.logpdf(x, 0, 1) - norm.logpdf(0, 0, 1),
             lambda x: uniform.logpdf(x, -10, 20) - uniform.logpdf(
                 0, -10, 20),
             lambda: uniform.rvs(-10, 20),
             using_logs=True).send(None)
         samples.append(s)
     self.assertAlmostEqual(np.mean(samples), 0, places=3)
     self.assertAlmostEqual(np.std(samples), 1, places=3)
Example #7
0
    def test_log_pdf(self, dtype, low, low_is_samples, high, high_is_samples, rv, rv_is_samples,
                     num_samples):
        is_samples_any = any([low_is_samples, high_is_samples, rv_is_samples])
        rv_shape = rv.shape[1:] if rv_is_samples else rv.shape
        n_dim = 1 + len(rv.shape) if is_samples_any and not rv_is_samples else len(rv.shape)
        low_np = numpy_array_reshape(low, low_is_samples, n_dim)
        high_np = numpy_array_reshape(high, high_is_samples, n_dim)
        scale_np = high_np - low_np
        rv_np = numpy_array_reshape(rv, rv_is_samples, n_dim)

        # Note uniform.logpdf takes loc and scale, where loc=a and scale=b-a
        log_pdf_np = uniform.logpdf(rv_np, low_np, scale_np)
        var = Uniform.define_variable(shape=rv_shape, dtype=dtype).factor

        low_mx = mx.nd.array(low, dtype=dtype)
        if not low_is_samples:
            low_mx = add_sample_dimension(mx.nd, low_mx)
        high_mx = mx.nd.array(high, dtype=dtype)
        if not high_is_samples:
            high_mx = add_sample_dimension(mx.nd, high_mx)
        rv_mx = mx.nd.array(rv, dtype=dtype)
        if not rv_is_samples:
            rv_mx = add_sample_dimension(mx.nd, rv_mx)
        variables = {var.low.uuid: low_mx, var.high.uuid: high_mx, var.random_variable.uuid: rv_mx}
        log_pdf_rt = var.log_pdf(F=mx.nd, variables=variables)

        assert np.issubdtype(log_pdf_rt.dtype, dtype)
        assert array_has_samples(mx.nd, log_pdf_rt) == is_samples_any
        if is_samples_any:
            assert get_num_samples(mx.nd, log_pdf_rt) == num_samples
        if np.issubdtype(dtype, np.float64):
            rtol, atol = 1e-7, 1e-10
        else:
            rtol, atol = 1e-4, 1e-5
        assert np.allclose(log_pdf_np, log_pdf_rt.asnumpy(), rtol=rtol, atol=atol)
Example #8
0
    def probability(self, x, log):

        if self.log:
            x = np.log(x)

        if log:
            out = np.squeeze(uniform.logpdf(x, self._min, self.scale))
            return np.sum(out) if self.multivariate else out
        else:
            out = np.squeeze(uniform.pdf(x, self._min, self.scale))
            return np.prod(out) if self.multivariate else out
Example #9
0
    def test_parameter_bounds(self):
        x = Parameter(4, bounds=Interval(-4, 4))
        assert_equal(x.logp(), uniform.logpdf(0, -4, 8))

        x.bounds = None
        assert_(isinstance(x._bounds, Interval))
        assert_equal(x.bounds.lb, -np.inf)
        assert_equal(x.bounds.ub, np.inf)
        assert_equal(x.logp(), 0)

        x.setp(bounds=norm(0, 1))
        assert_almost_equal(x.logp(1), norm.logpdf(1, 0, 1))

        # all created parameters were mistakenly being given the same
        # default bounds instance!
        x = Parameter(4)
        y = Parameter(5)
        assert_(id(x.bounds) != id(y.bounds))
Example #10
0
def negPost(x, *args):
    nWater = args[0]
    nTosses = args[1]
    p = -1 * (binom.logpmf(k=nWater, n=nTosses, p=x) + uniform.logpdf(x, 0, 1))
    return p
    #print(theta_draw)
    params_m = [theta_draw[0], theta_draw[1], sigma_R]
    params_t = [theta_draw[2], theta_draw[3], delta_2, sigma_Q]

    z_t = z(params_m, params_t, x_t, y_t, M, c_init, r_star)

    # Prior for gamma_0, gamma_1, delta_0 and delta_1
    prior = multivariate_normal.logpdf(theta_draw[:4],
                                       mean=np.array([2.74, -1.19, 0.5, 0.8]),
                                       cov=2 * np.eye(4))

    prior -= multivariate_normal.logpdf(theta[i][:4],
                                        mean=np.array([2.74, -1.19, 0.5, 0.8]),
                                        cov=2 * np.eye(4))
    # Prior for delta_1
    prior += uniform.logpdf(delta_2, -1, 2)
    prior -= uniform.logpdf(np.tanh(theta[i][4]), -1, 2)

    # Prior Sigma_R
    prior += gamma.logpdf(sigma_R, a=5, scale=1 / 5)
    prior -= gamma.logpdf(np.exp(theta[i][5]), a=5, scale=1 / 5)

    # Prior Sigma_Q
    prior += gamma.logpdf(sigma_Q, a=5, scale=1 / 5)
    prior -= gamma.logpdf(np.exp(theta[i][6]), a=5, scale=1 / 5)

    # Correct for Jacobian
    logJacob = np.log(np.abs(1 - delta_2**2)) - np.log(
        np.abs(1 - np.tanh(theta[i][4])**2))
    logJacob += np.log(np.abs(sigma_R)) - np.log(np.abs(np.exp(theta[i][5])))
    logJacob += np.log(np.abs(sigma_Q)) - np.log(np.abs(np.exp(theta[i][6])))
 def evaluate_log_prior(self, parameters):
     parameters = parameters.reshape((-1, self.parameter_dim()))
     return np.sum(uniform.logpdf(parameters, loc=-1.0, scale=2.0), axis=1)
Example #13
0
def uniform_adj_log_pdf(x, m, M):
    return np.sum(uniform.logpdf(x, m, M))
Example #14
0
 def calc_log_prior(mu, sigma, alpha, beta, l, h):
     log_sigma = gamma.logpdf(sigma, alpha, scale=beta)
     log_mu = uniform.logpdf(mu, loc=l, scale=l + h)
     return log_mu + log_sigma
Example #15
0
 def __call__(self, u: np.ndarray, y: float) -> np.ndarray:
     loc = y - self.scale / 2
     return uniform.logpdf(x=u, loc=loc, scale=self.scale)
Example #16
0
d = pd.read_csv('rethinking-Experimental/data/Howell1.csv',
                delimiter=';')

d2 = d.loc[d.age >= 18]
mu = np.linspace(140, 160, num=200)
sigma = np.linspace(4, 9, num=200)

post = pd.DataFrame([[x, y] for y in sigma for x in mu],
                    columns=['mu', 'sigma']
                    )
post['LL'] = np.sum(norm.logpdf(d2.height.values.reshape(1, -1),
                                loc=post.mu.values.reshape(-1, 1),
                                scale=post.sigma.values.reshape(-1, 1)),
                    axis=1)
post['logprod'] = post.LL + norm.logpdf(post.mu, 178, 20) +\
                        uniform.logpdf(post.sigma, 0, 50)
post['prob'] = np.exp(post.logprod - post.logprod.max())
post.head()
ax=post.plot.hexbin(x='mu', y='sigma', C='prob', cmap=cm.thermal)
ax.set_xlabel('mu')
f = pl.gcf()

samples = post[['mu', 'sigma', 'prob']].sample(n=10000, replace=True, weights=post.prob)
samples.plot.scatter(x='mu', y='sigma', alpha=0.1)
samples.plot.hexbin(x='mu', y='sigma', C='prob', cmap=cm.thermal);
samples.mu.plot.density()
samples.sigma.plot.density(color='orange')

d3 = d2.height.sample(n=20)

mu = np.linspace(150, 170, num=200)
Example #17
0
prior_h = norm.rvs(sample_mu, sample_sigma, 1000)
sns.set_theme(style='darkgrid')
ax = sns.kdeplot(prior_h, bw=2)
ax.set(xlabel='height', title='Prior')
plt.show()

# code chunk 4.14 (grid estimation)
mu_grid = np.linspace(140, 160, 200)
sigma_grid = np.linspace(4, 9, 200)
post_list = [sum(norm.logpdf(d2, m, s)) for m in mu_grid for s in sigma_grid]
post_ll = np.concatenate(post_list, axis=0)

mu_grid_rep = np.repeat(mu_grid, 200)
sigma_grid_rep = np.tile(sigma_grid, 200)
len(post_ll) == len(mu_grid_rep) and len(post_ll) == len(sigma_grid_rep)
post_log_prob = post_ll + norm.logpdf(mu_grid_rep, 178, 20) + uniform.logpdf(
    sigma_grid_rep, 0, 50)
post_prob = np.exp(post_log_prob - max(post_log_prob))
X, Y = np.meshgrid(mu_grid, sigma_grid)
Z = post_prob.reshape(200, 200)
plt.contour(X, Y, Z)
plt.show()

post_prob_std = post_prob / sum(post_prob)
sample_rows = np.random.choice(range(len(post_prob)),
                               size=1000,
                               replace=True,
                               p=post_prob_std)
sample_mu = mu_grid_rep[sample_rows]
sample_sigma = sigma_grid_rep[sample_rows]
plt.scatter(sample_mu, sample_sigma)
plt.show()
Example #18
0
def negPost(x, *args):
    y = args[0]
    p = -1 * sum(norm.logpdf(y, x[0], x[1]))[0] + norm.logpdf(
        x[0], 178, 20) + uniform.logpdf(x[1], 0, 50)
    return p
Example #19
0
def prior(a, b, sd):
    aprior = uniform.logpdf(a, 0.0, 10.0)
    bprior = norm.logpdf(b, 0.0, 5.0)
    sdprior = uniform.logpdf(sd, 0.0, 30.0)
    return (aprior + bprior + sdprior)
Example #20
0
    def _logp(self, value, lower, upper):
        
        if value < lower or value > upper:
            raise ValueError("Domain Error.") 

        return np.sum(uniform.logpdf(value, loc=lower, scale=upper-lower)) 
Example #21
0
def joint_logprior(Om0=None,
                   Ob0=None,
                   Ol0=None,
                   q=None,
                   sigma80=None,
                   A=None,
                   X0=None,
                   B=None,
                   C0=None,
                   M0=None,
                   VX=None,
                   VC=None,
                   VM=None,
                   Om_a=0.01,
                   Om_b=0.99,
                   Ob_a=0.001,
                   Ob_b=0.4,
                   Ol_a=0.01,
                   Ol_b=0.99,
                   q_a=-1.5,
                   q_b=4.5,
                   Nq=None,
                   zbins=None,
                   Nscale=4,
                   bin_type='zunif',
                   xi0_lims=[5e-2, 2.0],
                   xi0=0.1,
                   **kwargs):

    lp = 0.0

    # Cosmological parameters

    if Om0 is not None:
        lp += uniform.logpdf(Om0, loc=Om_a, scale=Om_b - Om_a)
    if Ob0 is not None:
        lp += uniform.logpdf(Ob0, loc=Ob_a, scale=Ob_b - Ob_a)
    if Ol0 is not None:
        lp += uniform.logpdf(Ol0, loc=Ol_a, scale=Ol_b - Ol_a)
    if sigma80 is not None:
        lp += reciprocal.logpdf(sigma80, 1e-5, 1e+02)

    # Nuisance parameters

    if A is not None:
        lp += uniform.logpdf(A, loc=0.0, scale=1.0)
    if B is not None:
        lp += uniform.logpdf(B, loc=0.0, scale=4.0)
    if X0 is not None:
        lp += norm.logpdf(X0, loc=0.0, scale=10.0)
    if C0 is not None:
        lp += norm.logpdf(C0, loc=0.0, scale=1.0)
    if M0 is not None:
        lp += norm.logpdf(M0, loc=-19.3, scale=2.0)
    if VX is not None:
        lp += reciprocal.logpdf(VX, 1e-10, 1e+04)
    if VC is not None:
        lp += reciprocal.logpdf(VC, 1e-10, 1e+04)
    if VM is not None:
        lp += reciprocal.logpdf(VM, 1e-10, 1e+04)

    if q is not None:  # one q par only
        lp += uniform.logpdf(q, loc=q_a, scale=q_b - q_a)
        # lp += norm.logpdf(q, loc=0.0, scale=0.3)

    # Multiple correlated q pars from multivariate gaussian

    if Nq is not None:  # means multiple q pars
        if Nq == 1:
            lp += uniform.logpdf(kwargs['q1'], loc=q_a, scale=q_b - q_a)
            return lp

        qs = np.array([kwargs['q{}'.format(i + 1)]
                       for i in range(Nq)])  # extract q pars

        # Pick one:
        qmeans = smooth(qs, Nw=5)  # fiducial
        # qmeans = 0.0*qs

        # Option 1: Hyperparameter dependent prior (unmarginalised)
        # cov = cov_q(Nq, zbins, Nscale=Nscale, bin_type=bin_type, xi0=xi0_lims[0])
        # lp += multivariate_normal.logpdf(qs, mean=qmeans, cov=cov)

        # Option 2: Hyperparameter-marginalised prior
        lp += np.log(
            prior_q(qs,
                    zbins,
                    Nscale=Nscale,
                    bin_type=bin_type,
                    xi0_lims=xi0_lims,
                    qmeans=qmeans))

    return lp