Ejemplo n.º 1
0
def acc(theta, sk1, cj, y):
    y0 = gamma.logpdf(x=theta, a=sk1[0, :], scale=1)
    y1 = gamma.logpdf(x=theta, a=sk1[1, :], scale=1)
    y3 = y1 - y0
    y3 = y3.sum(axis=1)
    y3[y3 <= 0] = 0
    y3[y3 > 0] = 1
    return confusion_matrix(y, y3)
Ejemplo n.º 2
0
def PGM_pred(theta, sk1, cj, y):
    y0 = gamma.logpdf(x=theta, a=sk1[0, :], scale=1)
    y1 = gamma.logpdf(x=theta, a=sk1[1, :], scale=1)
    y3 = y1 - y0
    y3 = y3.sum(axis=1)
    y3[y3 <= 0] = 0
    y3[y3 > 0] = 1
    return y3
Ejemplo n.º 3
0
    def propose(self):
        ret = copy(self)
        ret.value = gamma.rvs(self.value * self.proposal_scale, scale=1./self.proposal_scale)

        fb = gamma.logpdf(ret.value, self.value * self.proposal_scale, scale=1./self.proposal_scale) -\
             gamma.logpdf(self.value, ret.value * self.proposal_scale, scale=1./self.proposal_scale)

        return ret, fb
Ejemplo n.º 4
0
    def propose(self):
        ret = copy(self)
        ret.value = gamma.rvs(self.value * self.proposal_scale,
                              scale=1. / self.proposal_scale)

        fb = gamma.logpdf(ret.value, self.value * self.proposal_scale, scale=1./self.proposal_scale) -\
             gamma.logpdf(self.value, ret.value * self.proposal_scale, scale=1./self.proposal_scale)

        return ret, fb
Ejemplo n.º 5
0
def log_hyperprior(phi):

    lp = norm.logpdf(x=phi['mu_mu'], loc=3, scale=3)
    lp += gamma.logpdf(x=phi['mu_sd'], a=1, scale=1)

    lp += gamma.logpdf(x=phi['sd_a'], a=1, scale=1)
    lp += gamma.logpdf(x=phi['sd_scale'], a=1, scale=1)

    return lp
Ejemplo n.º 6
0
        def f0(x):
            mu, a, th = x[0], x[1], x[2]
            res = uv_exp_ll(t, mu, a, th, T)

            res += gamma.logpdf(mu, mu_hyp[0], scale=mu_hyp[1]) \
                + gamma.logpdf(th, theta_hyp[0], scale=theta_hyp[1]) \
                + beta.logpdf(a, alpha_hyp[0], alpha_hyp[1])

            return res
Ejemplo n.º 7
0
def log_pdf_z_given_dispersion_fcn(z_t, mu_t, dispersion_t):
    r = mu_t / dispersion_t
    p = dispersion_t / (dispersion_t + 1)

    log_pdf_z_dispersion = gamma.logpdf((z_t+r), a = 0.01, scale = 100) - \
           gamma.logpdf(r, a = 0.01, scale = 100) +\
           r * np.log(1-p) + z_t * np.log(p)

    return log_pdf_z_dispersion
 def noise_log_prior(self, x):
     if self.noise_type == "truncnorm":
         return 0
     elif self.noise_type == "gamma":
         return gamma.logpdf(x, 3, scale=3)
     elif self.noise_type == "beta":
         return gamma.logpdf(x, 3, scale=3)
     else:
         print("Unknown noise type for generating noise")
         sys.exit("noise_log_prior: Unknown noise type for generating noise")
Ejemplo n.º 9
0
    def test_dummy_posterior_correct(self):
        A = self.arr
        logpost = self.bhp.log_posterior_with_params(A, 5., .2, .1, A[-1])

        check = self.bhp.log_likelihood_with_params(A, 5, .2, .1, A[-1]) + \
                gamma.logpdf(5, self.bhp.mu_hyp[0], scale=self.bhp.mu_hyp[1]) + \
                gamma.logpdf(.1, self.bhp.theta_hyp[0], scale=self.bhp.theta_hyp[1]) + \
                beta.logpdf(.2, self.bhp.alpha_hyp[0], self.bhp.alpha_hyp[1])

        self.assertAlmostEqual(logpost, check)
Ejemplo n.º 10
0
    def test_diffuse_prior_posterior_correct(self):
        A = self.arr
        bhp2 = BayesianUVExpHawkesProcess((1, 10000), (1, 1), (1, 1e5))

        logpost = bhp2.log_posterior_with_params(A, 5., .2, .1, A[-1])

        check = bhp2.log_likelihood_with_params(A, 5, .2, .1, A[-1]) + \
                gamma.logpdf(5, bhp2.mu_hyp[0], scale=bhp2.mu_hyp[1]) + \
                gamma.logpdf(.1, bhp2.theta_hyp[0], scale=bhp2.theta_hyp[1]) + \
                beta.logpdf(.2, bhp2.alpha_hyp[0], bhp2.alpha_hyp[1])

        self.assertAlmostEqual(logpost, check)
Ejemplo n.º 11
0
    def hyper_param_inf(self, corpus, params, score):
        if self.verbose >= 1:
            print "\n****** HP INFERENCE *******"

        for i in range(params.n_hypermoves):
            if self.verbose > 1:
                print "\n--- current params ---"
                params.show()
                print "hyper param score:" + str(score)
                print "    a_nr: " + str(
                    gamma.logpdf(params.alpha_r, params.alpha_r_hp))
                print "    a_r: " + str(
                    gamma.logpdf(params.alpha_nr, params.alpha_nr_hp))
                print "    empty_i: " + str(
                    beta.logpdf(params.empty_intent, params.intent_hp_a,
                                params.intent_hp_b))

            new_params = Params()
            new_params.propose_hyper_params(params)
            new_score = self.score_full_lex(corpus, new_params)
            # print "* scoring"
            # params.show()

            if self.verbose > 1:
                print "--- new params ---"
                new_params.show()
                print "hyper param score:" + str(new_score)
                print "    a_nr: " + str(
                    gamma.logpdf(new_params.alpha_r, new_params.alpha_r_hp))
                print "    a_r: " + str(
                    gamma.logpdf(new_params.alpha_nr, new_params.alpha_nr_hp))
                print "    empty_i: " + str(
                    beta.logpdf(new_params.empty_intent,
                                new_params.intent_hp_a,
                                new_params.intent_hp_b))

            if new_score - score > 0:
                params = new_params
            elif random() < exp(new_score - score):
                params = new_params

                if self.verbose >= 1:
                    print "    hp change! - old = %2.2f, new = %2.2f" % (
                        score, new_score)

        # now rescore with the new parameters - redundant if you didn't swap, FIXME
        self.score_full_lex(corpus, params)

        return params
def NK_priors(para):
    #([0.8,0.8,1.6000,.8000,.99,1.4,0.016,0.0183,1.6700,0.3,1.01,.117,.032,.9,.85,1.15,.993,1.17])
    para = cp.asnumpy(para)
    shape = np.zeros(11)
    scale = np.zeros(11)
    c = np.zeros(2)
    d = np.zeros(2)
    mu = np.array([.125, .015, 0.031, .2, .3, 1.2, .99, .5, 1.6, .2, .15])
    sig = np.array([.09, .011, .022, .1, .2, .15, .001, .2, .15, .15, .1])
    for i in range(11):
        shape[i] = (mu[i] / sig[i])**2
        scale[i] = sig[i]**2 / mu[i]

    shape[3] = (mu[3] / sig[3])**2 + 2
    scale[3] = (shape[3] - 1) * mu[3]

    c[0] = (mu[6]**2) * ((1 - mu[6]) / ((sig[6])**2) - (1 / mu[6]))
    d[0] = c[0] * ((1 / mu[6]) - 1)
    c[1] = (mu[7]**2) * ((1 - mu[7]) / ((sig[7])**2) - (1 / mu[7]))
    d[1] = c[1] * ((1 / mu[7]) - 1)

    P1 = gamma.logpdf((1 / para[1]), a=shape[0], scale=scale[0])
    P6 = gamma.logpdf(para[6], a=shape[1], scale=scale[1])
    P5 = gamma.logpdf(para[5], a=shape[4], scale=scale[4])
    P10 = gamma.logpdf(para[10], a=shape[5], scale=scale[5])
    P2 = gamma.logpdf(para[2], a=shape[8], scale=scale[8])
    P3 = gamma.logpdf(para[3], a=shape[9], scale=scale[9])

    P7 = gamma.logpdf(para[7], a=shape[2], scale=scale[2])

    P9 = norm.logpdf(para[9], .3, .05**2)

    # P4 = gamma.logpdf(para[4],a=shape[6],scale=.001)

    # muU = .99
    # sigU = .01

    # alphaaz = ((1-muU)/sigU** - 1/muU)*muU**2

    # Betaaz = alphaaz*(1/muU-1)

    # P4 = beta.logpdf(para[4],alphaaz,Betaaz)

    P13 = gamma.logpdf(para[13], a=shape[7], scale=scale[7])

    shape[3] = (mu[3] / sig[3])**2 - 2
    scale[3] = (shape[3] - 1) * (mu[3])

    shape[10] = (mu[10] / sig[10])**2 - 2
    scale[10] = (shape[10] - 1) * (mu[10])

    P8 = invgamma.logpdf(para[8], a=shape[3], scale=scale[3])
    P15 = invgamma.logpdf(para[15], a=shape[3], scale=scale[3])
    P16 = invgamma.logpdf(para[16], a=shape[3], scale=scale[3])
    #np.log(np.exp(np.log(2) - np.log(math.gamma(.5*b)) + .5*b*np.log(b*(a**2)*.5) - ((b+1)*.5)*np.log(x**2) - (b*a**2)*(x**-2)*.5))

    prioc = P1 + P6 + P2 + P3 + P9 + P5 + P10 + P8 + P15 + P16 + P13 + P7  #+P4
    return prioc
Ejemplo n.º 13
0
 def ll_t(self, spec):
     r_loc = self.get_r_t(spec)
     for thing in r_loc:
         if thing < 0:
             return -(10**50)
     if spec in ["SP", "EG"]:
         ret = np.sum(
             nbinom.logpmf(self.data[spec][self.sc],
                           n=np.multiply(
                               r_loc, np.sqrt(self.data[spec][self.area])),
                           p=self.p0_t))
     else:
         ret = np.sum(
             nbinom.logpmf(self.data[spec][self.sc], n=r_loc, p=self.p0_t))
     #ret = 0.
     #i = 0
     #for sc in self.data[spec][self.sc]:
     #    ret += logp0(sc,r_loc[i],self.p0,self.q0)
     #    i += 1
     mylen = len(self.data[spec][self.sc])
     for val in self.al_t[spec]:
         #ret+=mylen*(norm.logpdf(val ,loc=0,scale=10))
         ret += mylen * (gamma.logpdf(val, a=1, scale=300))
     #ret+=mylen*(norm.logpdf(self.al[spec][5] ,loc=0,scale=10))
     return ret
Ejemplo n.º 14
0
    def _post_lnpi(self):
        x_eta = np.sum((self.phi0 * self.gamma0 - 1) * self.eta, 1)
        z_eta = (gammaln(np.sum(self.phi0 * self.gamma0, 1)) -
                 np.sum(gammaln(self.phi0 * self.gamma0), 1))[:, np.newaxis]

        lnp_beta = gamma.logpdf(self.beta,
                                self.a0[:, np.newaxis, np.newaxis],
                                scale=self.b0[:, np.newaxis, np.newaxis])

        cluster_params = (self.eta * self.beta).dot(self.r.T)
        x = np.sum((cluster_params - 1) * self.lnPi, 1)
        z = gammaln(np.sum(cluster_params, 1)) - np.sum(
            gammaln(cluster_params), 1)

        #cluster weights
        weight_prior_params = np.array([1.0, self.conc_prior])[:, np.newaxis]
        w_x = np.sum((weight_prior_params - 1) * self.Elogp_clusters)
        w_z = np.sum(
            gammaln(np.sum(weight_prior_params, axis=0)) -
            np.sum(gammaln(weight_prior_params), axis=0))

        # responsibilities
        logp_membership = np.sum(self.r * self.logw[np.newaxis, :])

        return np.sum(x_eta + z_eta) + np.sum(
            lnp_beta) + w_x + w_z + logp_membership + np.sum(x + z)
Ejemplo n.º 15
0
def truncated_gamma_logpdf(
        a, scale, eta, ts_above_eta, N_above_eta):
    """Calculates the -log(likelihood) of a sample of random numbers
    generated from a gamma pdf truncated from below at x=eta.

    Parameters
    ----------
    a : float
        Shape parameter.
    scale : float
        Scale parameter.
    eta : float
        Test-statistic value at which the gamma function is truncated
        from below.
    ts_above_eta : (n_trials,)-shaped 1D ndarray
        The ndarray holding the test-statistic values falling in
        the truncated gamma pdf.
    N_above_eta : int
        Number of test-statistic values falling in the truncated
        gamma pdf.
        
    Returns
    -------
    -logl : float
    """
    c0 = 1. - gamma.cdf(eta, a=a, scale=scale)
    c0 = 1./c0
    logl = N_above_eta*np.log(c0) + np.sum(gamma.logpdf(ts_above_eta,
                                                        a=a, scale=scale))
    return -logl
def log_prior(logL0, a,b,B_l, sigma, M):

    if any(x<0 for x in (logL0,sigma)):
        return -np.inf

    if np.any(M<0): #masses have to be positive
        return -np.inf

    t1 = np.arctan(B_l)
    t2 = np.arctan(a)
    #if t<0 or t>np.pi/2:
    if any(x< -np.pi/2 or x> np.pi/2 for x in (t1,t2)):
        return -np.inf

    #Hyperparameters
    lambda_logL0 = 1.0
    sigma_a, sigma_b = 1,1

    p = 0
    #Exponential in logL0
    p+= expon.logpdf(logL0, scale = 1/lambda_logL0)
    #Uniform in arctan(B_l) and arctan(a)
    p+=2*np.log(2/np.pi)
    #flat prior for b
    #Have not idea what it would be, start with nothing
    #p+=0

    #Inv Gamma for sigma
    p-= gamma.logpdf(sigma**2,sigma_a, scale = sigma_b)
    return p
 def ln_modelprior(self):
     #Check and initialise the hyper-hyper-parameters if necessary
     if self.gam_scale_alpha==[] or (len(self.gam_scale_alpha.shape) == 3 and self.gam_scale_alpha.shape[2]!=self.alpha0.shape[2]):
         self.gam_shape_alpha = np.float(self.gam_shape_alpha)
         # if the scale was not set, assume current values of alpha0 are the means given by the hyper-prior
         self.gam_scale_alpha = self.alpha0/self.gam_shape_alpha
     if self.gam_scale_nu==[]:
         self.gam_shape_nu = np.float(self.gam_shape_nu)
         # if the scale was not set, assume current values of nu0 are the means given by the hyper-prior
         self.gam_scale_nu = self.nu0/self.gam_shape_nu
     
     #Gamma distribution over each value. Set the parameters of the gammas.
     p_alpha0 = gamma.logpdf(self.alpha0, a=self.gam_shape_alpha, scale=self.gam_scale_alpha)
     p_nu0 = gamma.logpdf(self.nu0, a=self.gam_shape_nu, scale=self.gam_scale_nu)
     
     return np.sum(p_alpha0) + np.sum(p_nu0)
 def ln_modelprior(self):
     #Check and initialise the hyper-hyper-parameters if necessary
     if self.gam_scale_alpha==[] or (len(self.gam_scale_alpha.shape) == 3 and self.gam_scale_alpha.shape[2]!=self.alpha0.shape[2]):
         self.gam_shape_alpha = np.float(self.gam_shape_alpha)
         # if the scale was not set, assume current values of alpha0 are the means given by the hyper-prior
         self.gam_scale_alpha = self.alpha0/self.gam_shape_alpha
     if self.gam_scale_nu==[]:
         self.gam_shape_nu = np.float(self.gam_shape_nu)
         # if the scale was not set, assume current values of nu0 are the means given by the hyper-prior
         self.gam_scale_nu = self.nu0/self.gam_shape_nu
     
     #Gamma distribution over each value. Set the parameters of the gammas.
     p_alpha0 = gamma.logpdf(self.alpha0, a=self.gam_shape_alpha, scale=self.gam_scale_alpha)
     p_nu0 = gamma.logpdf(self.nu0, a=self.gam_shape_nu, scale=self.gam_scale_nu)
     
     return np.sum(p_alpha0) + np.sum(p_nu0)
Ejemplo n.º 19
0
 def loglik(x, y, f, p):
     a, b, c, w0, w1, scale = p
     _mu = logistic(x, f, [a, b, c, w0, w1])
     if noise == 'gamma':
         return -np.sum(gamma.logpdf(y, np.maximum(_mu / scale, 1e-12), scale=scale))
     if noise == 'normal':
         return -np.sum(norm.logpdf(y, loc=_mu, scale=scale))
    def __call__(self, x):
        """
        Evaluates the log-prior in a PINTS framework.

        """
        # Prior contribution for initial R
        log_prior = pints.UniformLogPrior([0], [5])(x[0])

        # Prior contribution for ICs
        # log_prior += pints.UniformLogPrior([0], [1000])(x[-1])

        # Variance for betas
        sigma_b = x[-1]

        log_prior += gamma.logpdf(sigma_b, 1, scale=1 / 100)

        # Prior contriubution for betas
        LEN = len(np.arange(44, len(self._times), 7))
        for r in range(len(self._model.regions)):
            log_prior += norm.logpdf(np.log(x[r * LEN + 1]),
                                     loc=0,
                                     scale=sigma_b)
            for d in range(1, LEN):
                log_prior += norm.logpdf(np.log(x[r * LEN + d + 1]),
                                         loc=np.log(x[r * LEN + d]),
                                         scale=sigma_b)

        return log_prior
Ejemplo n.º 21
0
def test_gamma(X, model):
    """
	Description: Use the Naive Bayes model to test on the given missing features testing data.

	@params:
		X: testing features
		model: gamma models, {'p_spam': float, 'p_nonspam': float, 'gammas': list of gamma}
	@return 
		y: predict labels
	"""
    m, n = X.shape

    y = zeros(m)
    prob_spam = ones(m)
    prob_nonspam = ones(m)

    results_spam = zeros((m, n))
    results_nonspam = zeros((m, n))
    for j in range(n):
        gammas = model["gammas_spam"][j]
        results_spam[:, j] = gamma.logpdf(X[:, j], gammas["a"], loc=gammas["floc"], scale=gammas["scale"])
        # print results_spam[:,j]
        # raw_input()
        if inf in results_spam[:, j]:
            print "inf feature " + j
            # print prod(results_spam[:,j])
        gammas = model["gammas_nonspam"][j]
        results_nonspam[:, j] = gamma.logpdf(X[:, j], gammas["a"], loc=gammas["floc"], scale=gammas["scale"])
        # print prod(results_nonspam[:,j])
        # raw_input()

    for i in range(m):
        prob_spam[i] = sum(results_spam[i, :]) + log(model["p_spam"])
        prob_nonspam[i] = sum(results_nonspam[i, :]) + log(model["p_nonspam"])
        print prob_spam[i]
        raw_input()
        print prob_nonspam[i]
        raw_input()

        # print prob_spam
        # print prob_nonspam
        # raw_input()
    index_spam = prob_spam > prob_nonspam
    index_nonspam = prob_spam <= prob_nonspam
    y[index_spam] = 1
    y[index_nonspam] = 0
    return y
Ejemplo n.º 22
0
def log_pos(alpha0, alpha1, alpha2, alpha12, tau, a=r, b=n):
    y = 0
    prob = calcule_prob(alpha0, alpha1, alpha2, alpha12, tau, x1, x2)
    for i in range(21):
        y = y + r[i] * log(prob[i]) + (n[i] - r[i]) * log(1.0 - prob[i])
    y=y+ norm.logpdf(alpha0, 0 ,1)+norm.logpdf(alpha1, 0 ,  1)+norm.logpdf(alpha2, 0 , 1)+norm.logpdf(alpha12, 0 , 1)+\
    gamma.logpdf(tau, 1, loc=0, scale=1)
    return y
Ejemplo n.º 23
0
    def logPrior(self):
        prior = 0.0
        prior += gamma.logpdf(self.par[0], 2.0, scale=6.0)
        prior += beta.logpdf(0.5 * (self.par[1] + 1.0), 2.0, 2.0)
        prior += beta.logpdf(0.5 * (self.par[2] + 1.0), 2.0, 2.0)
        prior += beta.logpdf(0.5 * (self.par[3] + 1.0), 2.0, 2.0)

        return (prior)
Ejemplo n.º 24
0
    def logPrior(self):
        prior = 0.0
        prior += gamma.logpdf(self.par[0], 2.0, scale=6.0)
        prior += beta.logpdf(0.5 * (self.par[1] + 1.0), 2.0, 2.0)
        prior += beta.logpdf(0.5 * (self.par[2] + 1.0), 2.0, 2.0)
        prior += beta.logpdf(0.5 * (self.par[3] + 1.0), 2.0, 2.0)

        return(prior)
Ejemplo n.º 25
0
 def log_marginal(self, temp_alpha=None, calc_alpha=True):
     ll = super(DirichletDistributionGammaPrior, self).log_marginal()
     if calc_alpha:
         if temp_alpha is not None:
             alpha = temp_alpha
         else:
             alpha = self.alpha
         ll += gamma.logpdf(alpha, self.alpha_a, scale=self.alpha_b)
     return ll
 def ln_modelprior(self):
     #Check and initialise the hyper-hyper-parameters if necessary
     if self.gam_scale_alpha==[]:
         self.gam_shape_alpha = np.float(self.gam_shape_alpha)
         # if the scale was not set, assume current values of alpha0 are the means given by the hyper-prior
         self.gam_scale_alpha = self.alpha0/self.gam_shape_alpha
     #Gamma distribution over each value. Set the parameters of the gammas.
     p_alpha0 = gamma.logpdf(self.alpha0, self.gam_shape_alpha, scale=self.gam_scale_alpha)
     return np.sum(p_alpha0)
Ejemplo n.º 27
0
 def log_marginal(self, temp_alpha=None, calc_alpha=True):
     ll = super(DirichletDistributionGammaPrior, self).log_marginal()
     if calc_alpha:
         if temp_alpha is not None:
             alpha = temp_alpha
         else:
             alpha = self.alpha
         ll += gamma.logpdf(alpha, self.alpha_a, scale=self.alpha_b)
     return ll
Ejemplo n.º 28
0
    def _logposterior(self, i: int):
        '''
        Calculate posterior

        i: iteration index
        '''
        log_w =\
        norm.logpdf(self.ws[i], loc=0, scale=np.sqrt(1/self.alphas[i])).sum()

        log_alphas =\
        gamma.logpdf(self.alphas[i], a=self.c, scale=1/self.d).sum()

        log_betas =\
        gamma.logpdf(self.betas[i], a=self.a, scale=1/self.b).sum()

        log_data =\
        norm.logpdf(self.y, loc=self.ws[i]@self.X.T,
                    scale=np.sqrt(1/self.betas[i])).sum()
        return log_w + log_alphas + log_betas + log_data
Ejemplo n.º 29
0
def conditional_gamma_logpdf(value, mean, shape, upper_limit):
    shape, scale = transform_to_shape_scale(mean, shape=shape)
    logprob_conditional = gamma.logcdf(upper_limit, a=shape, scale=scale)
    if False:  #not isfinite(logprob_conditional):
        print 'logprob_conditional', logprob_conditional
        print 'upperlimit', upper_limit
        print 'shape', shape
        print 'scale', scale
        print value
    return gamma.logpdf(value, a=shape, scale=scale) - logprob_conditional
Ejemplo n.º 30
0
def get_outliers(data, filter, plotting):
    if plotting:
        for x, r in [("x1", (0, 1)), ("x2", (0, 30)), ("x3", (0, 1))]:
            plt.violinplot(data[x], vert=False)
            plt.xlim(r)
            plt.savefig("plots/violin/%s.png" % x)
            plt.clf()

    if filter:
        data_fl = data[data["class"] == 0]
    else:
        data_fl = data

    pdf = pd.DataFrame({})

    a, b, loc, scale = beta.fit(data_fl["x1"])
    pdf["x1"] = beta.logpdf(data["x1"], a, b, loc=loc, scale=scale)

    a, loc, scale = gamma.fit(data_fl["x2"])
    pdf["x2"] = gamma.logpdf(data["x2"], a, loc=loc, scale=scale)

    a, b, loc, scale = beta.fit(data_fl["x3"])
    pdf["x3"] = beta.logpdf(data["x3"], a, b, loc=loc, scale=scale)

    pdfs = pdf["x1"] + pdf["x2"] + pdf["x3"]

    if plotting:
        sns.boxplot(y=pdfs, x="class", data=data)
        plt.savefig("plots/boxplot.png")
        plt.clf()

    if plotting:
        plt.plot(np.sort(pdfs))
        splits = [40, 45, 50, 60]
        for split in splits:
            split = np.sort(pdfs)[60]
            plt.plot((0, 1000), (split, split), 'k-', lw=0.5)
            split = np.sort(pdfs)[50]
            plt.plot((0, 1000), (split, split), 'k.', lw=0.5)
            split = np.sort(pdfs)[45]
            plt.plot((0, 1000), (split, split), 'k--', lw=0.5)
            split = np.sort(pdfs)[40]
            plt.plot((0, 1000), (split, split), 'k--', lw=0.5)

        plt.savefig("plots/thresholds.png")
        plt.clf()

    outliers = np.argsort(pdfs)

    final = []
    for outlier in outliers:
        if data["class"][outlier] == -1:
            final.append(outlier)

    return np.array(final[:100])
Ejemplo n.º 31
0
        def hyper_param_inf(self,
                            corpus,
                            params,
                            score):
            if self.verbose >= 1:
                print "\n****** HP INFERENCE *******"

            for i in range(params.n_hypermoves):
                if self.verbose > 1:
                    print "\n--- current params ---"
                    params.show()
                    print "hyper param score:" + str(score)
                    print "    a_nr: " + str(gamma.logpdf(params.alpha_r, params.alpha_r_hp))
                    print "    a_r: " + str(gamma.logpdf(params.alpha_nr, params.alpha_nr_hp))
                    print "    empty_i: " + str(beta.logpdf(params.empty_intent, params.intent_hp_a, params.intent_hp_b))

                new_params = Params()
                new_params.propose_hyper_params(params)
                new_score = self.score_full_lex(corpus, new_params)
                # print "* scoring"
                # params.show()

                if self.verbose > 1:
                    print "--- new params ---"
                    new_params.show()
                    print "hyper param score:" + str(new_score)
                    print "    a_nr: " + str(gamma.logpdf(new_params.alpha_r, new_params.alpha_r_hp))
                    print "    a_r: " + str(gamma.logpdf(new_params.alpha_nr, new_params.alpha_nr_hp))
                    print "    empty_i: " + str(beta.logpdf(new_params.empty_intent, new_params.intent_hp_a, new_params.intent_hp_b))

                if new_score - score > 0:
                    params = new_params
                elif random() < exp(new_score - score):
                    params = new_params

                    if self.verbose >= 1:
                        print "    hp change! - old = %2.2f, new = %2.2f" % (score, new_score)

            # now rescore with the new parameters - redundant if you didn't swap, FIXME
            self.score_full_lex(corpus, params)

            return params
Ejemplo n.º 32
0
def evaluate_exponential(x, hyperparameters):

    a = hyperparameters

    if x > 0:

        return gamma.logpdf(x, a)

    else:

        return -np.inf
Ejemplo n.º 33
0
    def propose(self):
        ret = copy(self)

        if len(ret.value) == 1: return ret, 0.0 # handle singleton rules

        inx = sample1(range(0,self.alpha.shape[0]))
        ret.value[inx] = numpy.random.beta(self.value[inx]*self.proposal_scale,
                                           self.proposal_scale - self.value[inx] * self.proposal_scale)

        # add a tiny bit of smoothing away from 0/1
        ret.value[inx] = (1.0 - DirichletDistribution.SMOOTHING) * ret.value[inx] + DirichletDistribution.SMOOTHING / 2.0
        v = sum(ret.value)

        fb = sum(gamma.logpdf(ret.value, self.value)) + gamma.logpdf(v, 1) -\
             sum(gamma.logpdf(self.value, ret.value)) - gamma.logpdf(1, v)


        # and renormalize it, slightly breaking MCMC
        ret.value = ret.value / sum(ret.value)

        return ret, fb
Ejemplo n.º 34
0
    def propose(self):
        ret = copy(self)

        if len(ret.value) == 1: return ret, 0.0  # handle singleton rules

        inx = sample1(list(range(0, self.alpha.shape[0])))
        ret.value[inx] = numpy.random.beta(
            self.value[inx] * self.proposal_scale,
            self.proposal_scale - self.value[inx] * self.proposal_scale)

        # add a tiny bit of smoothing away from 0/1
        ret.value[inx] = (1.0 - DirichletDistribution.SMOOTHING) * ret.value[
            inx] + DirichletDistribution.SMOOTHING / 2.0
        v = sum(ret.value)

        fb = sum(gamma.logpdf(ret.value, self.value)) + gamma.logpdf(v, 1) -\
             sum(gamma.logpdf(self.value, ret.value)) - gamma.logpdf(1, v)

        # and renormalize it, slightly breaking MCMC
        ret.value = ret.value / sum(ret.value)

        return ret, fb
Ejemplo n.º 35
0
    def compute_prior(self):
        shape = self.prior_shape
        scale = self.prior_scale
        rule_priors = [gamma.logpdf(v, shape, scale=scale) for v in self.value]

        # If there are any negative values in our vector, prior is 0
        if [v for v in self.value if v < 0.0]:
            prior = -Infinity
        else:
            prior = sum([r for r in rule_priors])
        self.prior = prior
        self.update_posterior()
        return prior
Ejemplo n.º 36
0
 def ll(self, spec):
     r_loc = self.get_r(spec)
     for thing in r_loc:
         if thing < 0:
             return -(10**50)
     ret = np.sum(
         nbinom.logpmf(self.data[spec][self.sc], n=r_loc, p=self.p0))
     mylen = len(self.data[spec][self.sc])
     for val in self.al[spec]:
         #ret+=mylen*(norm.logpdf(val ,loc=0,scale=10))
         ret += mylen * (gamma.logpdf(val, a=1, scale=300))
     #ret+=mylen*(norm.logpdf(self.al[spec][5] ,loc=0,scale=10))
     return ret
Ejemplo n.º 37
0
    def posterior(self, theta):
        epsilon = 1e-16
        I = np.eye(2)
        origo = np.zeros(2)

        # First two elements are for linreg weights
        w = theta[:2]
        beta = theta[-1]

        logw = multivariate_normal.logpdf(w, origo,
                                          self.alpha * I).clip(epsilon)
        logbeta = gamma.logpdf(beta, self.a, self.b).clip(epsilon)
        logdata =\
            norm.logpdf([email protected], loc=0, scale=1/(beta+epsilon)).clip(epsilon).sum()

        return logw + logbeta + logdata
Ejemplo n.º 38
0
def generate(max_time, n_sequences, filename='nonstationary_renewal'):
    times, nll = [], []

    for _ in range(n_sequences):
        L = max_time
        amp = 0.99
        l_t = lambda t: np.sin(2 * np.pi * t / L) * amp + 1
        l_int = lambda t1, t2: -L / (2 * np.pi) * (np.cos(
            2 * np.pi * t2 / L) - np.cos(2 * np.pi * t1 / L)) * amp + (t2 - t1)

        T = []
        lpdf = []
        x = 0

        k = 4
        rs = gamma.rvs(k, size=max_time)
        lpdfs = gamma.logpdf(rs, k)
        rs = rs / k
        lpdfs = lpdfs + np.log(k)

        for i in range(max_time):
            x_next = brentq(lambda t: l_int(x, t) - rs[i], x, x + max_time)
            l = l_t(x_next)
            T.append(x_next)
            lpdf.append(lpdfs[i] + np.log(l))
            x = x_next

        T = np.array(T)

        T = T[T < max_time]
        lpdf = np.array(lpdf)[:len(T)]
        score = -lpdf.sum()

        times.append(T)
        nll.append(score)

    if filename is not None:
        mean_number_items = sum(len(t) for t in times) / len(times)
        nll = [n / mean_number_items for n in nll]
        np.savez(f'{dataset_dir}/{filename}.npz',
                 arrival_times=times,
                 nll=nll,
                 t_max=max_time,
                 mean_number_items=mean_number_items)
    else:
        return times
Ejemplo n.º 39
0
    def test_log_pdf_mean_variance(self, dtype, mean, mean_isSamples, variance,
                                   variance_isSamples, rv, rv_isSamples,
                                   num_samples):
        isSamples_any = any([mean_isSamples, variance_isSamples, rv_isSamples])
        rv_shape = rv.shape[1:] if rv_isSamples else rv.shape
        n_dim = 1 + len(
            rv.shape) if isSamples_any and not rv_isSamples else len(rv.shape)
        mean_np = numpy_array_reshape(mean, mean_isSamples, n_dim)
        variance_np = numpy_array_reshape(variance, variance_isSamples, n_dim)
        rv_np = numpy_array_reshape(rv, rv_isSamples, n_dim)
        beta_np = mean_np / variance_np
        alpha_np = mean_np * beta_np
        log_pdf_np = gamma.logpdf(rv_np, a=alpha_np, loc=0, scale=1. / beta_np)

        mean_mx = mx.nd.array(mean, dtype=dtype)
        if not mean_isSamples:
            mean_mx = add_sample_dimension(mx.nd, mean_mx)
        variance_mx = mx.nd.array(variance, dtype=dtype)
        if not variance_isSamples:
            variance_mx = add_sample_dimension(mx.nd, variance_mx)
        rv_mx = mx.nd.array(rv, dtype=dtype)
        if not rv_isSamples:
            rv_mx = add_sample_dimension(mx.nd, rv_mx)
        var = GammaMeanVariance.define_variable(mean=mean_mx,
                                                variance=variance_mx,
                                                shape=rv_shape,
                                                dtype=dtype).factor
        variables = {
            var.mean.uuid: mean_mx,
            var.variance.uuid: variance_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) == isSamples_any
        if isSamples_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)
Ejemplo n.º 40
0
def prior_gamma(x, gamma_params):
    """ Gamma prior distribution

    Parameters
    ----------
    x: float
        Parameter value whos likelihood we want to test.
    gamma_params: tuple
        Tuple containg gamma parameters alpha and beta.

    Returns
    -------
    Log likelihood of sample x in light of a gamma prior distribution.

    """
    a, b = gamma_params
    return gamma.logpdf(x, a=a, scale=1 / b)
Ejemplo n.º 41
0
def prior_weibull(x, weibull_params):
    """ Weibull prior distribution

    Parameters
    ----------
    x: float
        Parameter value whos likelihood we want to test.
    weibull_params: tuple
        Tuple containg weibull parameters k and lambda.

    Returns
    -------
    Log likelihood of sample x in light of a weibull prior distribution.

    """
    k, lam = weibull_params
    return gamma.logpdf(x, k, shape=lam, loc=0)
Ejemplo n.º 42
0
    def compute_prior(self):
        """
        Compute priors, according only to values that are proposed to. Priors computed according to gamma dist.

        """
        shape = self.prior_shape
        scale = self.prior_scale
        propose_values = [self.value[i] for i in self.get_propose_idxs()]
        rule_priors = [gamma.logpdf(v, shape, scale=scale) for v in propose_values]

        # If there are any negative values in our vector, prior is 0
        if [v for v in self.value if v < 0.0]:
            prior = -Infinity
        else:
            prior = sum([r for r in rule_priors])
        self.update_posterior()
        return prior
def log_prior(a,b,sigma):

    if any(x<0 for x in (a,sigma)):
    #if sigma<0:
        return - np.inf
    t = np.arctan(b)
    #if t<0 or t>np.pi/2:
    if t<-np.pi/2 or t>np.pi/2:
        return -np.inf

    #Hyperparameters
    lambda_a = 1.0
    sigma_a, sigma_b = 1,1

    p = 0
    #Exponential in log a
    #p+= np.log(lambda_a)-lambda_a*np.log(a)
    #p+= np.log(lambda_a)-lambda_a*a #changed a => logA TODO Change variable name?
    p+=expon.logpdf(np.log(a), scale = 1/lambda_a)
    #Uniform in arctan(b)
    p+=np.log(2/np.pi)
    #Inv Gamma for sigma
    p-= gamma.logpdf(sigma,sigma_a, scale = sigma_b)
    return p
def UniformSpaceDensityGaussianLFemcee(posWalker, posteriorDict, observations, observationalErrors):
  """
  A model for luminosity classification of stars. The stars are all assumed to be of a single class
  characterized by a mean absolute magnitude <M> and the variance on this magnitude sigma^2_M.
  Furthermore the stars are assumed to be distributed uniformly in the space around the sun. The upper
  and lower distance limits are fixed and the survey is assumed to be volume complete.

  The prior on the mean absolute magnitude is flat and the prior on the variance var of the
  absolute magnitude distribution is f(var)=Gamma(var|shape=k,scale=theta).
 
  **** This model is for the emcee python package. ****

  Parameters
  ----------

  posWalker - The Emcee walker position. The first two entries are the hyper-parameters, mu = Mean
              Absolute Magnitude and var = Variance, the next set of parameters are the true parallaxes
              and absolute magnitudes, respectively, of the stars in the survey (so 2 times the number of
              stars in the survey.)
  posteriorDict - Dictionary with posterior probability density function parameters:
    minParallax : Lower limit on parallax distribution
    maxParallax : Upper limit on parallax distribution
    muLow       : Lower limit of uniform prior on mu.
    muHigh      : Upper limit of uniform prior on mu.
    varShape    : Shape parameter for Gamma prior on the variance.
    varScale    : Scale parameter for Gamma prior on the variance.
  observations - Vector of observed parallaxes and apparent magnitudes (in that order).
  observationalErrors - Vector of errors (as inverse variances) on observed parallaxes and apparent
                        magnitudes (in that order).

  Returns
  -------

  Natural logarithm of the posterior probability density at the position of the walker.
  """
  lnPosterior=0.0
  numStarsInSurvey=(len(posWalker)-2)/2
  meanAbsMag=posWalker[0]
  if (meanAbsMag<posteriorDict['muLow'] or meanAbsMag>posteriorDict['muHigh']):
    return -np.inf
  lnPosterior=lnPosterior-np.log(posteriorDict['muHigh']-posteriorDict['muLow'])

  variance=posWalker[1]
  if (variance<0.0):
    return -np.inf
  lnPosterior=lnPosterior + gamma.logpdf(variance, posteriorDict['varShape'],
      scale=posteriorDict['varScale'])

  parallaxes=posWalker[2:numStarsInSurvey+2]
  if (np.any(parallaxes < posteriorDict['minParallax']) or np.any(parallaxes >
    posteriorDict['maxParallax'])):
    return -np.inf
  lnPosterior = lnPosterior - 4.0*np.sum(np.log(parallaxes))

  absoluteMagnitudes=posWalker[numStarsInSurvey+2:]
  apparentMagnitudes=absoluteMagnitudes-5.0*np.log10(parallaxes)+10.0

  y=np.concatenate((parallaxes,apparentMagnitudes,absoluteMagnitudes))
  inv_var=np.diagflat(np.concatenate((observationalErrors, 1.0/np.repeat(variance,numStarsInSurvey))))
  means=np.concatenate((observations, np.repeat(meanAbsMag,numStarsInSurvey)))
  diff=y-means
  lnPosterior = lnPosterior - 0.5*np.dot(diff,np.dot(inv_var,diff))
  lnPosterior = lnPosterior - 0.5*numStarsInSurvey*np.log(variance)
  return lnPosterior
Ejemplo n.º 45
0
        def score_full_lex(self,
                           corpus,
                           params,
                           init=False):

            # set up the intent caching
            for i in range(corpus.n_sents):

                # cache word and object probabilities uniformly
                # 1 x o matrix with [uniform ... empty]
                # and 1 x w matrix again with [uniform ... empty]
                n_os = len(corpus.sents[i][0])
                if n_os > 0:
                    unif_o = log((1 - params.empty_intent) / n_os)
                else:
                    unif_o = [None] # protects against zero objects

                self.intent_obj_probs[i] = [unif_o] * n_os + [log(params.empty_intent)]

                if init:
                    # update lexicon dirichlets based on random init
                    io = self.oi[i] == self.intent_obj[i]
                    rw = self.wi[i] == self.ref_word[i]

                    if io.any():  # protect against nulls
                        self.ref[corpus.sents[i][0][io],corpus.sents[i][1][rw]] += 1

                    # includes all words that are not the referential word
                    self.non_ref[corpus.sents[i][1][self.wi[i] != self.ref_word[i]]] += 1

                    # now add the referential words for null objects
                    if not io.any():
                        self.non_ref[corpus.sents[i][1][self.wi[i] == self.ref_word[i]]] += 1


                # now set up the quick scoring probability caches
                self.intent_obj_prob[i] = self.intent_obj_probs[i][self.intent_obj[i]]

            # cache DM scores for lexicon
            for i in range(corpus.world.n_objs):
                self.ref_score[i] = score_dm(self.ref[i, :], params.alpha_r)

            # cache non-ref DM score also
            self.nr_score = score_dm(self.non_ref, params.alpha_nr)

            # score hyperparameters (via hyper-hyperparameters)
            empty_intent_score = beta.logpdf(params.empty_intent, params.intent_hp_a, params.intent_hp_b)
            alpha_score = gamma.logpdf(params.alpha_r, params.alpha_r_hp) + gamma.logpdf(params.alpha_nr,
                                                                                         params.alpha_nr_hp)
            self.param_score = empty_intent_score + alpha_score
            score = self.update_score(corpus.n_sents)

            # debugging stuff
            if self.verbose >= 1:
                print "\n--- score full lex ---"
                print self.ref
                print " " + str(self.non_ref)

                if self.verbose > 1:
                    print "counts: %d" % (sum(self.non_ref) + sum(self.ref))
                    print "    intent obj: " + str(self.intent_obj)
                    print "    ref word: " + str(self.ref_word)
                    print "    intent obj prob: " + str(self.intent_obj_prob.round(1))

                print "full score: r %2.1f, nr %2.1f, i %2.1f, " \
                          "p %2.1f,  total: %2.1f" % (sum(self.ref_score),
                                                      self.nr_score,
                                                      sum(self.intent_obj_prob),
                                                      self.param_score,
                                                      score)


            return score
def pmh_sv(y,initPar,nPart,T,sm,nIter,stepSize):

    # Initalise variables
    th     = np.zeros((nIter,3));
    thp    = np.zeros((nIter,3));
    ll     = np.zeros(nIter);
    llp    = np.zeros(nIter);
    x      = np.zeros((nIter,T));
    xp     = np.zeros((nIter,T));
    accept = np.zeros(nIter);

    # Set the initial parameter and estimate the initial log-likelihood
    th[0,:]         = initPar;
    ( x[0,:], ll[0] ) = sm(y,th[0,:],nPart,T);

    #=====================================================================
    # Run main loop
    #=====================================================================
    for kk in range(1, nIter):

        # Propose a new parameter
        thp[kk,:] = th[kk-1,:] + np.random.multivariate_normal( mean=np.zeros(3), cov=stepSize );

        # Estimate the log-likelihood
        # Dont run if system is unstable
        if ( ( np.abs( thp[kk,1] ) < 1.0 ) & ( thp[kk,2] > 0.0 ) ):
            ( xp[kk,:], llp[kk] ) = sm(y,thp[kk,:],nPart,T);

        # Compute the ratio between the prior distributions (in log-form)
        prior =  norm.logpdf(  thp[kk,0], 0, 1)       - norm.logpdf(  th[kk-1,0], 0, 1);
        prior += norm.logpdf(  thp[kk,1], 0.95, 0.05) - norm.logpdf(  th[kk-1,1], 0.95, 0.05)
        prior += gamma.logpdf( thp[kk,2], 2, 1.0/10.0)- gamma.logpdf( th[kk-1,2], 2, 1.0/10.0);

        # Compute the acceptance probability
        aprob = np.min( (1.0, np.exp( prior + llp[kk] - ll[kk-1] ) ) );

        # Generate uniform random variable in U[0,1]
        u = np.random.uniform()

        # Check if | par[0] | > 1.0, in that case set u = 1.0;
        # Check if par[1] < 0.0, in that case set u = 1.0;
        # Check if par[2] < 0.0, in that case set u = 1.0;
        # So that the parameter is rejected. This is due to that the model
        # is only stable when | par[0] | is smaller than 1
        if ( ( np.abs( thp[kk,1] ) > 1.0 ) | ( thp[kk,2] < 0.0 ) ):
            u = 1.0;

        # Accept / reject step
        if ( u < aprob ):
            # Accept the parameter
            th[kk,:]   = thp[kk,:]
            x[kk,:]    = xp[kk,:]
            ll[kk]     = llp[kk]
            accept[kk] = 1.0;
        else:
            # Reject the parameter
            th[kk,:]   = th[kk-1,:]
            x[kk,:]    = x[kk-1,:]
            ll[kk]     = ll[kk-1]
            accept[kk] = 0.0;

        # Write out progress
        if np.remainder(kk,100) == 0:
            print("##################################################################### ");
            print(" Iteration: " + str(kk) + " of : " + str(nIter) + " completed.")
            print("");
            print(" Current state of the Markov chain:       " + "%.4f" % th[kk,0] + " " + "%.4f" % th[kk,1] + " " + "%.4f" % th[kk,2] + "." )
            print(" Proposed next state of the Markov chain: " + "%.4f" % thp[kk,0] + " " + "%.4f" % thp[kk,1] + " " + "%.4f" % thp[kk,2] + "." )
            print(" Current posterior mean:                  " + "%.4f" % np.mean(th[0:kk,0]) + " " + "%.4f" % np.mean(th[0:kk,1]) + " " + "%.4f" % np.mean(th[0:kk,2]) + "." )
            print(" Current acceptance rate:                 " + "%.4f" % np.mean(accept[0:kk]) + "." )
            print("##################################################################### ");
    #=====================================================================
    # Return traces of the parameters
    #=====================================================================
    return ( x, th );
Ejemplo n.º 47
0
 def compute_prior(self):
     return gamma.logpdf(self.value, self.a, scale=self.scale)