Example #1
0
    def gibbs_init_step(self,
                        nb_days,
                        nb_particles,
                        sigma2_s_param=None,
                        sigma2_g_param=None):
        """	Initialize all the variables required for the bootstrap filter init using gibbs sampling
        And perform the gibbs initialization step """

        #Gibbs step 0
        if sigma2_s_param is None:
            self.sigma_s_star_2[0, 0] = invgamma.rvs(a=10**(-2),
                                                     scale=10**(2),
                                                     size=1)[0]
        else:
            self.sigma_s_star_2[0, 0] = sigma2_s_param
        if sigma2_g_param is None:
            self.sigma_g_star_2[0, 0] = invgamma.rvs(a=10**(-2),
                                                     scale=10**(2),
                                                     size=1)[0]
        else:
            self.sigma_g_star_2[0, 0] = sigma2_g_param

        self.s[0, 0] = self.truncated_norm(0, inf, 0, 10**8)
        self.g_heat[0, 0] = self.truncated_norm(-inf, 0, 0, 10**8)
        for i in range(1, nb_days):
            self.s[i, 0] = self.s[i - 1, 0] + self.truncated_norm(
                -self.s[i - 1, 0], inf, 0, self.sigma_s_star_2[0, 0])
            self.g_heat[i, 0] = self.g_heat[i - 1, 0] + self.truncated_norm(
                -inf, -self.g_heat[i - 1, 0], 0, self.sigma_g_star_2[0, 0])
def calculate_difference((hh, Xs)):
    # step3の階差の計算--------------------------------------
    # step3の階差計算の和の計算で使用する変数の初期化
    dift = 0.
    difw = 0.
    difbeta = np.array([np.float(0)] * nz)
    #
    dift = sum((Xs[hh, 0, 1:TIMES] - Xs[hh, 0, 0:TIMES - 1])**2)
    difw = sum((Xs[hh, k, 1:TIMES] +
                sum(Xs[hh, k:(k - 1) + SEASONALYTY - 1, 0:TIMES - 1]))**2)
    for d in xrange(nz):
        difbeta[d] = sum((Xs[hh, (k - 1) + SEASONALYTY + d, 1:TIMES] -
                          Xs[hh, (k - 1) + SEASONALYTY + d, 0:TIMES - 1])**2)
    # step3--------------------------------------
    Sita_sys[hh, 0, 0] = invgamma.rvs((nu0 + TIMES) / 2,
                                      scale=(s0 + dift) / 2,
                                      size=1)[0]
    Sita_sys[hh, 1, 1] = invgamma.rvs((nu0 + TIMES) / 2,
                                      scale=(s0 + difw) / 2,
                                      size=1)[0]
    for d in xrange(nz):
        Sita_sys[hh, 2 + d, 2 + d] = invgamma.rvs((nu0 + TIMES) / 2,
                                                  scale=(s0 + difbeta[d]) / 2,
                                                  size=1)[0]
    return Sita_sys[hh, :, :]
Example #3
0
def sample_g(c_value, V):
    n = len(V)
    V = np.array(V)
    #print(n)
    index = V**2 < 0
    para = c_value/2
    sho = 0
    while(sho<20 and np.sum(index)<n):
        V_new = invgamma.rvs(a = 1, loc=0, scale=para).reshape(-1)
        u = np.random.rand(n)
        index_up = (u< V_new/(1+V_new)).reshape(-1)
        #print(index_up)
        V[index_up] = V_new[index_up]
        index = (index+index_up)>0
        sho = sho + 1
    left = np.where(index == False)[0]
    for those in left:
        V_new = invgamma.rvs(a = 1, loc = 0, scale = para[those], size = 100)
        u = np.random.rand(100) 
        index_up = (u<V_new/(1+V_new)).reshape(-1)
              #print("judge done")
        if np.sum(index_up) != 0:                  
            V[those] = V_new[index_up][0]
            index[those] = True
    if n!=sum(index):
        print(n, sum(index))
    return V
def sv_parameter_generator(s=None,phi_mode=0.97,phi_a_shape=300,sqrt_sigma_mode=0.20,sigma_shape=2,sqrt_beta_mode=0.20,beta_shape=2,saveOpt=False,displayOpt=True,filename='sv_parameters.csv'):
    if s is None:
        pass
    else:
        seed(s)

    phi_b_shape=2+(phi_a_shape-1)/float(phi_mode)-phi_a_shape
    phi=beta(1+phi_a_shape,1+phi_b_shape,1)[0]

    sigma_mode=sqrt_sigma_mode**2
    sigma_scale=sigma_mode*(sigma_shape+1)
    sigma=sqrt(invgamma.rvs(sigma_shape,scale=sigma_mode))

    beta_mode=sqrt_beta_mode**2
    beta_scale=beta_mode*(beta_shape+1)
    beta_=sqrt(invgamma.rvs(beta_shape,scale=beta_mode))

    if saveOpt:
        np.savetxt(filename,np.array([phi,sigma,beta_]),fmt='%.17f')
    if displayOpt:
        print 'Generated parameters:'
        print ' phi = ', phi
        print ' sigma = ',sigma
        print ' beta = ', beta_
        if saveOpt:
            print 'Saved to ',filename

    return phi,sigma,beta_
Example #5
0
 def updatePriorVals(self,weights,biases): 
     new_sW = np.zeros(self.sW.shape)
     new_mean = np.zeros(self.mean.shape)
     weights_cpu = weights.get()
     n_w = np.float32(weights_cpu.shape[1])
     shape_new = self.shape + n_w/2.0
     for i in range(0,len(weights_cpu)):
         scale_new =  self.scale + ((weights_cpu[i])**2).sum()/2.0
         new_val = invgamma.rvs(shape_new,scale=scale_new,size=1)
         new_sW[0,i] = np.float32(new_val)
         new_mean[0,i] = np.float32(scale_new/(shape_new-1.0))
         #print 'New shape for feature ' + str(i+1) + ': ' + str(shape_new)
         #print 'New scale for feature ' + str(i+1) + ': ' + str(1.0/rate_new)
         #print 'New standard deviation for feature ' + str(i+1) + ': ' + str(new_val)
     
     self.sW = gpuarray.to_gpu(new_sW.astype(self.precision))
     self.mean = gpuarray.to_gpu(new_mean.astype(self.precision))
     ## Biases have common variance
     biases_cpu = biases.get()
     n_b = np.float32(biases.shape[1])
     shape_new = self.shape + n_b/2.0
     scale_new =  self.scale  + ((biases_cpu)**2).sum()/2.0
     new_val = invgamma.rvs(shape_new,scale=scale_new,size=1)
     new_sB = np.float32(new_val)
     self.sB = gpuarray.to_gpu(new_sB)
def calculate_difference_m(Xs):  # 人間すべてに対応
    # step3の階差の計算--------------------------------------
    # step3の階差計算の和の計算で使用する変数の初期化
    dift = np.zeros(nhh, dtype=np.float)
    difw = np.zeros(nhh, dtype=np.float)
    difbeta = np.zeros((nhh, nz), dtype=np.float)
    #
    dift = np.sum((Xs[:, 0, 1:TIMES] - Xs[:, 0, 0:TIMES - 1])**2, axis=1)
    difw = np.sum(
        (Xs[:, k, 1:TIMES] +
         np.sum(Xs[:, k:(k - 1) + SEASONALYTY - 1, 0:TIMES - 1], axis=1))**2,
        axis=1)
    for d in xrange(nz):
        difbeta[:, d] = np.sum((Xs[:, (k - 1) + SEASONALYTY + d, 1:TIMES] -
                                Xs[:,
                                   (k - 1) + SEASONALYTY + d, 0:TIMES - 1])**2,
                               axis=1)
    # step3--------------------------------------
    Sita_sys[:, 0, 0] = invgamma.rvs((nu0 + TIMES) / 2,
                                     scale=(s0 + dift) / 2,
                                     size=nhh)
    Sita_sys[:, 1, 1] = invgamma.rvs((nu0 + TIMES) / 2,
                                     scale=(s0 + difw) / 2,
                                     size=nhh)
    for d in xrange(nz):
        Sita_sys[:, 2 + d, 2 + d] = invgamma.rvs(
            (nu0 + TIMES) / 2, scale=(s0 + difbeta[:, d]) / 2, size=nhh)
    return Sita_sys[:, :, :]
Example #7
0
 def __init__(self,shape,scale,layer,precision=np.float32):
     self.precision = precision
     self.shape = shape
     self.scale = scale
     
     init_var = invgamma.rvs(1.0,scale=1.0,size=(1,layer.weights.shape[1])).astype(precision)
     self.sW = gpuarray.to_gpu(init_var)
     
     init_var = invgamma.rvs(1.0,scale=1.0,size=(1,1)).astype(precision)
     self.sB = gpuarray.to_gpu(init_var)
     kernels = SourceModule(open(path+'/kernels.cu', "r").read())        
     self.add_prior_w_kernel = kernels.get_function("add_gaussian_unit_grad")
     self.add_prior_b_kernel = kernels.get_function("add_bias_grad")
     self.scale_momentum_kernel = kernels.get_function("scale_momentum_normal_unit")
Example #8
0
    def draw_expected_value(self, x, num_samples=1):
        # draw a sample from normal innvgamma posterior which is
        # same as expected reward given this model.
        # TODO: check if it is actually the same with sampling from a student's t distribution

        if num_samples > 1:
            mu_tile = self.mu * np.ones(num_samples)
            sigma_sqr_tile = []
            for i in range(num_samples):
                sigma_sqr_tile[i] = invgamma.rvs(self.alpha, scale=self.beta)
            return np.random.normal(mu_tile, sigma_sqr_tile)

        # first sample sigma^2 with inverse gamma
        sigma_sqr = invgamma.rvs(self.alpha, scale=self.beta)
        # then sample x from normal with self.mean and sigma^2/nu
        return np.random.normal(self.mu, sigma_sqr / self.v)
Example #9
0
 def __init__(self, path=None, image_size=128, K_potts=10, beta_potts=1, mu_mean=1, mu_std=1, alpha=1, beta=10):
     """
     :param path: Path of the image we want to compress. If no path is selected, a random gray scale image is
                     generated
     :param image_size: the passed image will be resized to this size
     :param K_potts: number of values possible for each component of the Potts model
     :param beta_potts: The Beta parameter of the Potts model
     :param mu_mean: parameter to compute the initial means of the mu vector
     :param mu_std: parameter to compute the initial std of the mu vector
     :param alpha: initial alpha parameter for the InvGamma law
     :param beta: initial beta parameter for the InvGamma law
     :var self.mu : initial value of the mu vector
     :var self.sigma: initial value of the sigma vector
     :var self.potts_values: initial value of each pixel in the Potts model
     """
     if path is None:
         self.image = np.random.randint(0, 256, size=(image_size, image_size))
     else:
         image = Image.open(path)
         image.thumbnail((image_size, image_size), Image.ANTIALIAS)
         gray_image = ImageOps.grayscale(image)
         self.image = np.array(gray_image)
     self.rows, self.columns = self.image.shape
     self.k_potts = K_potts
     self.beta_potts = beta_potts
     self.mu_mean = np.arange(0, 255, 255 * mu_mean / self.k_potts)
     self.mu_std = [mu_std] * self.k_potts
     self.alpha = np.array([alpha] * self.k_potts)
     self.beta = np.array([beta] * self.k_potts)
     self.mu = np.random.normal(self.mu_mean, self.mu_std, self.k_potts)
     self.sigma = invgamma.rvs(self.alpha, scale=self.beta, size=self.k_potts)
     self.potts_values = np.zeros((self.rows, self.columns))
Example #10
0
def linear(seq_length=30, num_samples=28*5*100, a0=10, b0=0.01, k=2, **kwargs):
    """
    Generate data from linear trend from probabilistic model.

    The invgamma function in scipy corresponds to wiki defn. of inverse gamma:
        scipy a = wiki alpha = a0
        scipy scale = wiki beta = b0

    k is the number of regression coefficients (just 2 here, slope and intercept)
    """
    T = np.zeros(shape=(seq_length, 2))
    T[:, 0] = np.arange(seq_length)
    T[:, 1] = 1                         # equivalent to X
    lambda0 = 0.01*np.eye(k)     # diagonal covariance for beta
    y = np.zeros(shape=(num_samples, seq_length, 1))
    sigmasq = invgamma.rvs(a=a0, scale=b0, size=num_samples)
    increasing = np.random.choice([-1, 1], num_samples)     # flip slope
    for n in range(num_samples):
        sigmasq_n = sigmasq[n]
        offset = np.random.uniform(low=-0.5, high=0.5)     # todo limits
        mu0 = np.array([increasing[n]*(1.0-offset)/seq_length, offset])
        beta = multivariate_normal.rvs(mean=mu0, cov=sigmasq_n*lambda0)
        epsilon = np.random.normal(loc=0, scale=np.sqrt(sigmasq_n), size=seq_length)
        y[n, :, :] = (np.dot(T, beta) + epsilon).reshape(seq_length, 1)
    marginal = partial(linear_marginal_likelihood, X=T, a0=a0, b0=b0, mu0=mu0, lambda0=lambda0)
    samples = y
    pdf = marginal
    return samples, pdf
Example #11
0
def update_sigma2(alpha, beta, gamma, b, G, n_i, uni_diet, id_g, uni_id, N_g, p, W, X, Z, y):
    temp1 = (np.sum(N_g.values()) + np.sum(gamma))/2.0
    temp3 = 0.0
    for g_index in xrange(G):
        g = uni_diet[g_index]
        temp2 = 0.0
        if np.all(gamma[g_index] == 0):  #check if all gamma's are 0
            for i in id_g[g]:
                i_index = np.where(uni_id == i)[0][0]
                temp2 += (y[i] - np.dot(W[i],alpha) - np.dot(Z[i], b[i_index].reshape(p, 1))).T.dot(y[i] - np.dot(W[i],alpha) - np.dot(Z[i], b[i_index].reshape(p, 1)))
            temp3 += temp2
        else:  #if not all gamma's are 0
            temp6 = XXsum(g_index, uni_diet, id_g, gamma, X)
            temp5 = np.zeros([np.sum(gamma[g_index]),1])
            for i in id_g[g]:
                i_index = np.where(uni_id == i)[0][0]
                temp2 += (y[i] - np.dot(W[i],alpha) - np.dot(X[i][:,gamma[g_index]!=0], beta[g_index][gamma[g_index]!=0].reshape(np.sum(gamma[g_index]),1)) - np.dot(Z[i], b[i_index].reshape(p, 1))).T.dot(y[i] - np.dot(W[i],alpha) - np.dot(X[i][:,gamma[g_index]!=0], beta[g_index][gamma[g_index]!=0].reshape(np.sum(gamma[g_index]),1)) - np.dot(Z[i], b[i_index].reshape(p, 1)))
                temp5 += np.dot(X[i][:,gamma[g_index]!=0].T, y[i] - np.dot(W[i], alpha) - np.dot(Z[i], b[i_index].reshape(p, 1)))
            temp4 = np.dot((beta[g_index][gamma[g_index]!=0].reshape(np.sum(gamma[g_index]),1) - pinv(temp6).dot(temp5)).T, temp6).dot(beta[g_index][gamma[g_index]!=0].reshape(np.sum(gamma[g_index]),1) - pinv(temp6).dot(temp5))
            temp3 += temp2 + temp4/N_g[g]
    temp3 = temp3/2.0

    #update
    sigma2_new = invgamma.rvs(temp1, scale = temp3, size = 1)
    return sigma2_new
Example #12
0
def init_params(y, N_data, K_clusters, D_data, tol,
                max_iter):  #initialize parameters for FI-NPL by picking MLE
    R_restarts = 10

    pi_bb = np.zeros((R_restarts, K_clusters))  #mixing weights
    mu_bb = np.zeros((R_restarts, K_clusters, D_data))  #means
    sigma_bb = np.zeros((R_restarts, K_clusters, D_data))  #covariances
    ll_bb = np.zeros(R_restarts)

    #Initialize parameters randomly
    pi_init = np.random.dirichlet(np.ones(K_clusters), R_restarts)
    mu_init = 8 * np.random.rand(R_restarts, K_clusters, D_data) - 2
    sigma_init = invgamma.rvs(1, size=(R_restarts, K_clusters, D_data))

    for i in range(R_restarts):
        model = skgm.GaussianMixture(K_clusters,
                                     covariance_type='diag',
                                     means_init=mu_init[i],
                                     weights_init=pi_init[i],
                                     precisions_init=1 / sigma_init[i],
                                     tol=tol,
                                     max_iter=max_iter)
        model.fit(y, np.ones(N_data))
        pi_bb[i] = model.weights_
        mu_bb[i] = model.means_
        sigma_bb[i] = np.sqrt(model.covariances_)
        ll_bb[i] = model.score(y, np.ones(N_data)) * N_data
    ind = np.argmax(ll_bb)
    return pi_bb[ind], mu_bb[ind], sigma_bb[ind]
Example #13
0
    def _sample_posterior_predictive(self, x_t, n_samples=1):
        # p(sigma^2)
        sigma_sq_t_list = [
            invgamma.rvs(self._a_list[j], scale=self._b_list[j])
            for j in range(self._n_actions)
        ]

        try:
            # p(w|sigma^2) = N(mu, sigam^2 * cov)
            W_t = [
                np.random.multivariate_normal(
                    self._mu_list[j], sigma_sq_t_list[j] * self._cov_list[j])
                for j in range(self._n_actions)
            ]
        except np.linalg.LinAlgError as e:
            print("Error in {}".format(type(self).__name__))
            print('Errors: {}.'.format(e.args[0]))
            W_t = [
                np.random.multivariate_normal(np.zeros(self._d),
                                              np.eye(self._d))
                for i in range(self._n_actions)
            ]

        # p(r_new | params)
        mean_t_predictive = np.dot(W_t, x_t)
        cov_t_predictive = sigma_sq_t_list * np.eye(self._n_actions)
        r_t_estimates = np.random.multivariate_normal(mean_t_predictive,
                                                      cov=cov_t_predictive,
                                                      size=1)
        r_t_estimates = r_t_estimates.squeeze()

        assert r_t_estimates.shape[0] == self._n_actions

        return r_t_estimates
Example #14
0
    def mixture_model(self):
        self.assignmentIteration = 0
        self.parameterIteration = 0
        if self.Util.alpha_initial_sample:
            # concentration parameter prior: inverse gamma function with scale parameter: 1
            self.alpha = invgamma.rvs(a=1, size=1)
        else:
            self.alpha = self.Util.alpha_initial
        self.n = 1
        self.K = 1
        self.z.append(0)
        # draw a new pattern for the first frame
        wx, wy, pwx, pwy = self.Util.draw_w()
        frame_first = []
        frame_first.append(self.frames[0])
        ux, uy, sigmax, sigmay, sigman = self.cov_mean(frame_first)
        pattern_0 = MotionPattern(ux, uy, sigmax, sigmay, sigman, wx, wy)
        pattern_0.update_para(self.frames[0])
        self.b.append(pattern_0)
        # update partition
        self.partition = self.z2partition(self.z, self.K)

        # from the second frame, do update assignment
        for i in range(1, len(self.frames)):
            self.n = i + 1
            # first assign unseen frame to cluster 0
            self.z.append(0)
            # update assignment of frame i
            self.update_assignment(i)
            print('Initializing Frame: ', i, 'total frames: ',
                  len(self.frames))
Example #15
0
def simulate(copula, n):
    """
	Generates random variables with selected copula's structure.

	Parameters
	----------
	copula : Copula
		The Copula to sample.
	n : integer
		The size of the sample.
	"""
    d = copula.getDimension()

    X = []
    if type(copula).__name__ == "GaussianCopula":
        # We get correlation matrix from covariance matrix
        Sigma = copula.getCovariance()
        D = sqrtm(np.diag(np.diag(Sigma)))
        Dinv = inv(D)
        P = np.dot(np.dot(Dinv, Sigma), Dinv)
        A = cholesky(P)

        for i in range(n):
            Z = np.random.normal(size=d)
            V = np.dot(A, Z)
            U = stats.norm.cdf(V)
            X.append(U)
    elif type(copula).__name__ == "ArchimedeanCopula":
        U = np.random.rand(n, d)

        # Laplace–Stieltjes invert transform
        LSinv = {
            'clayton':
            lambda theta: np.random.gamma(shape=1. / theta),
            'gumbel':
            lambda theta: stats.levy_stable.rvs(
                1. / theta, 1., 0,
                math.cos(math.pi / (2 * theta))**theta),
            'frank':
            lambda theta: stats.logser.rvs(1. - math.exp(-theta)),
            'amh':
            lambda theta: stats.geom.rvs(theta)
        }

        for i in range(n):
            V = LSinv[copula.getFamily()](copula.getParameter())
            X_i = [copula.inverseGenerator(-np.log(u) / V) for u in U[i, :]]
            X.append(X_i)
    elif type(copula).__name__ == "StudentCopula":
        nu = copula.getFreedomDegrees()
        Sigma = copula.getCovariance()

        for i in range(n):
            Z = multivariate_normal.rvs(size=1, cov=Sigma)
            W = invgamma.rvs(nu / 2., size=1)
            U = np.sqrt(W) * Z
            X_i = [student.cdf(u, nu) for u in U]
            X.append(X_i)

    return X
Example #16
0
    def _sample_posterior_predictive(self, x_t, n_samples=1):
        # 1. p(sigma^2)
        sigma_sq_t = invgamma.rvs(self._a, scale=self._b)

        try:
            # p(w|sigma^2) = N(mu, sigam^2 * cov)
            w_t = np.random.multivariate_normal(self._mu,
                                                sigma_sq_t * self._cov)
        except np.linalg.LinAlgError as e:
            logger.debug("Error in {}".format(type(self).__name__))
            logger.debug('Errors: {}.'.format(e.args[0]))
            w_t = np.random.multivariate_normal(np.zeros(self._d),
                                                np.eye(self._d))

        # modify context
        u_t, S_t = x_t
        n_actions = S_t.shape[0]

        x_ta = [
            np.concatenate((u_t, S_t[j, :], [1])) for j in range(n_actions)
        ]
        assert len(x_ta[0]) == self._d

        # 2. p(r_new | params)
        mean_t_predictive = [np.dot(w_t, x_ta[j]) for j in range(n_actions)]

        cov_t_predictive = sigma_sq_t * np.eye(n_actions)
        r_t_estimates = np.random.multivariate_normal(mean_t_predictive,
                                                      cov=cov_t_predictive,
                                                      size=1)
        r_t_estimates = r_t_estimates.squeeze()

        assert r_t_estimates.shape[0] == n_actions

        return r_t_estimates
Example #17
0
def sweep(sigma2, mu_0, sigma2_0, alpha, beta, data):
    posterior_mean, posterior_variance = mean_posterior(
        data, mu_0, sigma2_0, sigma2)
    mu = norm.rvs(posterior_mean, scale=np.sqrt(posterior_variance))
    posterior_alpha, posterior_beta = variance_posterior(data, alpha, beta, mu)
    sigma2 = invgamma.rvs(posterior_alpha, scale=posterior_beta)
    return mu, sigma2
def gibbs_sampler(selective_posterior,
                  nsample=2000,
                  nburnin=100,
                  proposal_scale=None,
                  step=1.):
    state = selective_posterior.initial_estimate
    stepsize = 1. / (step * selective_posterior.ntarget)

    if proposal_scale is None:
        proposal_scale = selective_posterior.inverse_info

    sampler = langevin(state, selective_posterior.log_posterior,
                       proposal_scale, stepsize,
                       np.sqrt(selective_posterior.dispersion))
    samples = np.zeros((nsample, selective_posterior.ntarget))
    scale_samples = np.zeros(nsample)
    scale_update = np.sqrt(selective_posterior.dispersion)
    for i in range(nsample):
        sample = sampler.__next__()
        samples[i, :] = sample

        scale_update_sq = invgamma.rvs(
            a=(0.1 + selective_posterior.ntarget +
               selective_posterior.ntarget / 2),
            scale=0.1 - ((scale_update**2) * sampler.posterior_[0]),
            size=1)
        scale_samples[i] = np.sqrt(scale_update_sq)
        sampler.scaling = np.sqrt(scale_update_sq)

    return samples[nburnin:, :], scale_samples[nburnin:]
    def action(self):
        """Samples beta's from posterior, and chooses best action accordingly.

        Returns:
          action: Selected action for the context.
        """

        if len(self.data.positive_actions) == 0:
            # Return fake positive action if run out of positives
            return -1, np.zeros(self.data.actions_dim), self.data.positive_reward, self.data.positive_reward

        # Sample sigma2, and beta conditional on sigma2
        sigma2_s = self.b * invgamma.rvs(self.a)

        try:
            beta_s = np.random.multivariate_normal(self.mu, sigma2_s * self.cov)
        except np.linalg.LinAlgError as e:
            # Sampling could fail if covariance is not positive definite
            print('Exception when sampling from {}.'.format(self.hparams.name))
            print('Details: {} | {}.'.format(str(e), e.args))
            d = self.hparams.actions_dim + 1
            beta_s = np.random.multivariate_normal(np.zeros(d), np.eye(d))

        # Compute sampled expected rewards, intercept is last component of beta
        pred_rs = [
            np.dot(beta_s[:-1], action.T) + beta_s[-1]
            for action in self.data.actions
        ]

        action_i = np.argmax(pred_rs)
        return action_i, self.data.actions[action_i], pred_rs[action_i], self.data.rewards[action_i]
Example #20
0
    def updates(self, particleNum, maxStage, xdata, ydata, dimNum,
                hyper_sigma_1, hyper_sigma_2):
        total_mu_mat = np.zeros((self.mTree, len(ydata)))
        for mi in range(self.mTree):
            total_mu_mat[mi] = self.add_dts[mi].assign_to_data(
                len(self.add_dts[mi].z_label) - 1)

        for mi in range(self.mTree):
            add_dts_other_mu = np.sum(total_mu_mat, axis=0) - total_mu_mat[mi]
            dts_mi = copy.deepcopy(self.add_dts[mi])

            dts_mi.dts_update(particleNum, dts_mi, maxStage, xdata, ydata,
                              add_dts_other_mu)
            total_mu_mat[mi] = dts_mi.assign_to_data(len(dts_mi.z_label) - 1)
            self.add_dts[mi] = dts_mi

        # update the hyper-parameters of the variance

        posterior_hyper_sigma_1 = hyper_sigma_1 + len(
            xdata) / 2  # the number refers to the number of terminal nodes
        square_of_errors = np.sum((np.sum(total_mu_mat, axis=0) - ydata)**2)
        posterior_hyper_sigma_2 = hyper_sigma_2 + square_of_errors / 2
        self.true_var = invgamma.rvs(a=posterior_hyper_sigma_1,
                                     loc=0,
                                     scale=posterior_hyper_sigma_2)

        # pass the self.true_var to each tree of the addtree
        for add_dts_i in self.add_dts:
            add_dts_i.true_var = self.true_var
Example #21
0
    def _sample(self, context, parallelize=False, n_threads=-1):
        """
        Samples beta's from posterior, and samples from expected values.
        Args:
          context: Context for which the action need to be chosen.
        Returns:
          action: sampled reward vector."""

        # Sample sigma2, and beta conditional on sigma2
        context = context.reshape(-1, self.hparams['context_dim'])
        n_rows = len(context)
        a_projected = np.repeat(np.array(self.a)[np.newaxis, :],
                                n_rows,
                                axis=0)
        sigma2_s = self.b * invgamma.rvs(a_projected)
        if n_rows == 1:
            sigma2_s = sigma2_s.reshape(1, -1)
        beta_s = []
        try:
            for i in range(self.hparams['num_actions']):
                global mus
                global covs
                mus = np.repeat(self.mu[i][np.newaxis, :], n_rows, axis=0)
                s2s = sigma2_s[:, i]
                rep = np.repeat(s2s[:, np.newaxis],
                                self.hparams['context_dim'] + 1,
                                axis=1)
                rep = np.repeat(rep[:, :, np.newaxis],
                                self.hparams['context_dim'] + 1,
                                axis=2)
                covs = np.repeat(self.cov[i][np.newaxis, :, :], n_rows, axis=0)
                covs = rep * covs
                if parallelize:
                    multivariates = parallelize_multivar(mus,
                                                         covs,
                                                         n_threads=n_threads)
                else:
                    multivariates = [
                        np.random.multivariate_normal(mus[j], covs[j])
                        for j in range(n_rows)
                    ]
                beta_s.append(multivariates)
        except np.linalg.LinAlgError as e:
            # Sampling could fail if covariance is not positive definite
            # Todo: Fix This
            print('Exception when sampling from {}.'.format(self.name))
            print('Details: {} | {}.'.format(e.message, e.args))
            d = self.hparams['context_dim'] + 1
            for i in range(self.hparams['num_actions']):
                multivariates = [
                    np.random.multivariate_normal(np.zeros((d)), np.eye(d))
                    for j in range(n_rows)
                ]
                beta_s.append(multivariates)
        beta_s = np.array(beta_s)

        # Compute sampled expected values, intercept is last component of beta
        vals = [(beta_s[i, :, :-1] * context).sum(axis=-1) + beta_s[i, :, -1]
                for i in range(self.hparams['num_actions'])]
        return np.array(vals)
Example #22
0
def draw_mus_and_sigmas(data, m0, k0, s_sq0, v0,n_samples=10000):
    # number of samples
    N = size(data)
    # find the mean of the data
    the_mean = mean(data)
    # sum of squared differences between data and mean
    SSD = sum( (data - the_mean)**2 )

    # combining the prior with the data - page 79 of Gelman et al.
    # to make sense of this note that
    # inv-chi-sq(v,s^2) = inv-gamma(v/2,(v*s^2)/2)
    kN = float(k0 + N)
    mN = (k0/kN)*m0 + (N/kN)*the_mean
    vN = v0 + N
    vN_times_s_sqN = v0*s_sq0 + SSD + (N*k0*(m0-the_mean)**2)/kN

    # 1) draw the variances from an inverse gamma
    # (params: alpha, beta)
    alpha = vN/2
    beta = vN_times_s_sqN/2
    # thanks to wikipedia, we know that:
    # if X ~ inv-gamma(a,1) then b*X ~ inv-gamma(a,b)
    sig_sq_samples = beta*invgamma.rvs(alpha,size=n_samples)

    # 2) draw means from a normal conditioned on the drawn sigmas
    # (params: mean_norm, var_norm)
    mean_norm = mN
    var_norm = sqrt(sig_sq_samples/kN)
    mu_samples = norm.rvs(mean_norm,scale=var_norm,size=n_samples)

    # 3) return the mu_samples and sig_sq_samples
    return mu_samples, sig_sq_samples
Example #23
0
    def _sampling_s2_ep(self):

        A_ep = self.A_ep
        B_ep = self.B_ep

        y = self.y
        X = self.X
        XTX = np.dot(X.T, X)
        k = self.k

        beta0 = self.params['beta0'][-1]
        beta1 = self.params['beta1'][-1]
        beta = np.array([beta0, beta1])

        # parameters of full conditionals
        shape = self.n / 2
        scale = np.dot(
            np.dot(X, beta).reshape(self.n) - y,
            np.dot(X, beta).reshape(self.n) - y) / 2

        # sampling from full conditionals
        s2_ep = invgamma.rvs(a=shape, scale=scale, size=1)[0]

        # update posterior samples
        self.params['s2_ep'].append(s2_ep)
    def action(self):
        """Samples beta's from posterior, and chooses best action accordingly."""

        if len(self.data.positive_actions) == 0:
            # Return fake positive action if run out of positives
            return -1, np.zeros(
                self.data.actions_dim
            ), self.data.positive_reward, self.data.positive_reward

        # Sample sigma2, and beta conditional on sigma2
        sigma2_s = self.b * invgamma.rvs(self.a)

        try:
            beta_s = np.random.multivariate_normal(self.mu,
                                                   sigma2_s * self.cov)
        except np.linalg.LinAlgError as e:
            # Sampling could fail if covariance is not positive definite
            print('Exception when sampling for {}.'.format(self.hparams.name))
            print('Details: {} | {}.'.format(str(e), e.args))
            d = self.latent_dim
            beta_s = np.random.multivariate_normal(np.zeros(d), np.eye(d))

        # Compute last-layer representation for the current context
        with self.bnn.graph.as_default():
            z_actions = self.bnn.sess.run(
                self.bnn.nn, feed_dict={self.bnn.x: self.data.actions})

        # Apply Thompson Sampling to last-layer representation
        pred_rs = np.dot(beta_s, z_actions.T)

        action_i = np.argmax(pred_rs)
        return action_i, self.data.actions[action_i], pred_rs[
            action_i], self.data.rewards[action_i]
Example #25
0
    def choose_arm(self, x: np.ndarray, step) -> np.ndarray:
        """腕の中から1つ選択肢し、インデックスを返す"""
        if True in (self.counts < self.warmup):
            result = np.argmax(np.array(self.counts < self.warmup))
        else:
            sigma2 = [
                self._ig_b[i] * invgamma.rvs(self._ig_a[i])
                for i in range(self.n_arms)
            ]
            try:
                beta = [
                    np.random.multivariate_normal(self._mu[i],
                                                  sigma2[i] * self._cov[i])
                    for i in range(self.n_arms)
                ]
            except np.linalg.LinAlgError as e:
                print('except')
                d = self.n_features + 1
                beta = [
                    np.random.multivariate_normal(np.zeros(d), np.identity(d))
                    for _ in range(self.n_arms)
                ]
            vals = [
                beta[i][:-1] @ x.T + beta[i][-1] for i in range(self.n_arms)
            ]

            result = np.argmax(vals)

        return result
Example #26
0
def draw_mus(N, xbar, SSD, m0, k0, s_sq0, v0, n_samples=10000):
    # SSD = variance
    # combining the prior with the datva - page 79 of Gelman et al.
    # to make sense of this note that
    # inv-chi-sq(v,s^2) = inv-gamma(v/2,(v*s^2)/2)
    kN = float(k0 + N)
    mN = (k0 / kN) * m0 + (N / kN) * xbar
    vN = v0 + N
    vN_times_s_sqN = v0 * s_sq0 + SSD + (N * k0 * (m0 - xbar)**2) / kN

    # 1) draw the variances from an inverse gamma
    # (params: alpha, beta)
    alpha = vN / 2
    beta = vN_times_s_sqN / 2
    # thanks to wikipedia, we know that:
    # if X ~ inv-gamma(a,1) then b*X ~ inv-gamma(a,b)
    sig_sq_samples = beta * invgamma.rvs(alpha, size=n_samples)

    # 2) draw means from a normal conditioned on the drawn sigmas
    # (params: mean_norm, var_norm)
    mean_norm = mN
    var_norm = sqrt(sig_sq_samples / kN)
    mu_samples = norm.rvs(mean_norm, scale=var_norm, size=n_samples)

    # 3) return the mu_samples and sig_sq_samples
    return mu_samples
Example #27
0
def gibbs_sampler(X, y, beta0, sigma_squared0, B0inv, alpha0, delta0, burn_in, sample_size):
    '''Gibbs sampler for Bayesian normal linear regression model
    
    See Greenberg p. 116, adapted for better numerical stability
    '''
    n = len(X)
    k = len(beta0)
    assert X.shape == (n, k)
    alpha1 = alpha0 + n
    # set up beta and sigma_squared chains
    beta = np.zeros((burn_in + sample_size, k))
    beta[0] = beta0
    sigma_squared = np.zeros(burn_in + sample_size)
    sigma_squared[0] = sigma_squared0
    # compute some operations to save time later
    XT = X.T
    XTX = XT.dot(X)
    XTy = XT.dot(y)
    for i in range(1, burn_in + sample_size):
        if i % 1000 == 0 or i == burn_in + sample_size - 1:
            print(f'Sampling {i} / {burn_in + sample_size}')
        B1inv = XTX / sigma_squared[i-1] + B0inv
        # using cholesky decomp (since we need cholesky anyway), compute beta_bar
        L = np.linalg.cholesky(B1inv)
        beta_bar = solve_triangular(L.T, solve_triangular(L, XTy / sigma_squared[i-1] + B0inv.dot(beta0), lower=True))
        beta[i] = multnorm_sampler(beta_bar, L=L)
        Z = y - X.dot(beta[i])
        delta = delta0 + Z.T.dot(Z)
        sigma_squared[i] = invgamma.rvs(a=alpha1/2, scale=delta/2)
    return beta, sigma_squared
Example #28
0
    def _sampling_s2_d(self):

        A_d = self.A_d
        B_d = self.B_d
        n = self.n
        z = self.z
        one_vec = np.ones(n)

        if self.current_iteration == 0:
            x = self.params['x']
        else:
            x = self.params['x'][:, -1]

        X = np.c_[one_vec, x]
        gam0 = self.params['gam0'][-1]
        gam1 = self.params['gam1'][-1]
        gam = np.array([gam0, gam1])

        # parameters of full conditionals
        shape = A_d + n / 2
        scale = B_d + np.dot(
            np.dot(X, gam).reshape(self.n) - z,
            np.dot(X, gam).reshape(self.n) - z) / 2

        # sampling from full conditionals
        s2_d = invgamma.rvs(a=shape, scale=scale, size=1)[0]

        # update posterior samples
        self.params['s2_d'].append(s2_d)
Example #29
0
    def _sampling_s2_ep(self):

        A_ep = self.A_ep
        B_ep = self.B_ep

        if self.current_iteration == 0:
            x = self.params['x']
        else:
            x = self.params['x'][:, -1]

        y = self.y
        one_vec = np.ones(self.n)
        X = np.c_[one_vec, x]

        beta0 = self.params['beta0'][-1]
        beta1 = self.params['beta1'][-1]
        beta = np.array([beta0, beta1])

        # parameters of full conditionals
        shape = A_ep + self.n / 2
        scale = B_ep + np.dot(
            np.dot(X, beta).reshape(self.n) - y,
            np.dot(X, beta).reshape(self.n) - y) / 2

        # sampling from full conditionals
        s2_ep = invgamma.rvs(a=shape, scale=scale, size=1)[0]

        # update posterior samples
        self.params['s2_ep'].append(s2_ep)
Example #30
0
def draw_mus_and_sigmas(data):
    # number of samples
    N = size(data)
    # find the mean of the data
    the_mean = mean(data)
    # sum of squared differences between data and mean
    SSD = sum((data - the_mean)**2)

    # combining the prior with the data - page 79 of Gelman et al.
    # to make sense of this note that
    # inv-chi-sq(v,s^2) = inv-gamma(v/2,(v*s^2)/2)
    kN = float(k0 + N)
    mN = (k0 / kN) * m0 + (N / kN) * the_mean
    vN = v0 + N
    vN_times_s_sqN = v0 * s_sq0 + SSD + (N * k0 * (m0 - the_mean)**2) / kN

    # 1) draw the variances from an inverse gamma
    # (params: alpha, beta)
    alpha = vN / 2
    beta = vN_times_s_sqN / 2
    # thanks to wikipedia, we know that:
    # if X ~ inv-gamma(a,1) then b*X ~ inv-gamma(a,b)
    sig_sq_samples = beta * invgamma.rvs(alpha)

    # 2) draw means from a normal conditioned on the drawn sigmas
    # (params: mean_norm, var_norm)
    mean_norm = mN
    var_norm = sqrt(sig_sq_samples / kN)
    mu_samples = np.random.normal(mean_norm, var_norm)

    # 3) return the mu_samples and std_devs
    return mu_samples, sqrt(sig_sq_samples)
def sampleMeanAndVarianceForGaussianDataAndGelmansPriori(data,m0=0,k0=1,s_sq0=1,v0=1,samples=10000):
    # actual observations
    N = size(data)
    data_mean = mean(data) 
    sum_sd = sum( (data - data_mean)**2 ) 

    # combining the prior with the data - page 79 of Gelman et al.
    # to make sense of this note that 
    # inv-chi-sq(v,s^2) = inv-gamma(v/2,(v*s^2)/2)
    kN = float(k0 + N)
    mN = (k0/kN)*m0 + (N/kN)*data_mean
    vN = v0 + N
    vN_times_s_sqN = v0*s_sq0 + sum_sd + (N*k0*(m0-data_mean)**2)/kN

    # Sample \sigma^2 from the Inverse Gamma
    # (params: alpha, beta)
    # Note: if X ~ inv-gamma(a,1) then b*X ~ inv-gamma(a,b)
    alpha = vN/2
    beta = vN_times_s_sqN/2
    variance_samples = beta*invgamma.rvs(alpha,size=samples)

    # Sample \mu from a normal conditioned on the sampled \sigma^2
    # (params: mean_norm, var_norm)
    mean_norm = mN
    var_norm = sqrt(variance_samples/kN)
    mean_samples = norm.rvs(mean_norm,scale=var_norm,size=samples)

    return mean_samples, variance_samples
    def select_action(self, context: torch.Tensor) -> int:
        """Select an action based on given context.

        Selects an action by computing a forward pass through network to output
        a representation of the context on which bayesian linear regression is
        performed to select an action.

        Args:
            context (torch.Tensor): The context vector to select action for.

        Returns:
            int: The action to take.
        """
        self.model.use_dropout = self.eval_with_dropout
        self.t += 1
        if self.t < self.n_actions * self.init_pulls:
            return torch.tensor(
                self.t % self.n_actions, device=self.device, dtype=torch.int
            )
        var = torch.tensor(
            [self.b[i] * invgamma.rvs(self.a[i]) for i in range(self.n_actions)],
            device=self.device,
            dtype=torch.float,
        )
        try:
            beta = (
                torch.tensor(
                    np.stack(
                        [
                            np.random.multivariate_normal(
                                self.mu[i], var[i] * self.cov[i]
                            )
                            for i in range(self.n_actions)
                        ]
                    )
                )
                .to(self.device)
                .to(torch.float)
            )
        except np.linalg.LinAlgError as e:  # noqa F841
            beta = (
                (
                    torch.stack(
                        [
                            torch.distributions.MultivariateNormal(
                                torch.zeros(self.context_dim + 1),
                                torch.eye(self.context_dim + 1),
                            ).sample()
                            for i in range(self.n_actions)
                        ]
                    )
                )
                .to(self.device)
                .to(torch.float)
            )
        results = self.model(context)
        latent_context = results["x"]
        values = torch.mv(beta, torch.cat([latent_context.squeeze(0), torch.ones(1)]))
        action = torch.argmax(values).to(torch.int)
        return action
def calculate_difference((hh, Xs)):
    # step3の階差の計算--------------------------------------
    # step3の階差計算の和の計算で使用する変数の初期化
    dift = 0.
    difw = 0.
    difbeta = np.array([np.float(0)]*nz)
#
    dift = sum( (Xs[hh,0,1:TIMES] - Xs[hh,0,0:TIMES-1])**2 )
    difw = sum( (Xs[hh,k,1:TIMES]+sum(Xs[hh, k:(k-1)+SEASONALYTY-1, 0:TIMES-1]))**2 )
    for d in xrange(nz):
        difbeta[d] = sum( (Xs[hh, (k-1)+SEASONALYTY+d, 1:TIMES] 
                                    - Xs[hh, (k-1)+ SEASONALYTY+d, 0:TIMES-1])**2 )      
    # step3--------------------------------------
    Sita_sys[hh,0,0] = invgamma.rvs((nu0+TIMES)/2, scale=(s0+dift)/2, size=1)[0]
    Sita_sys[hh,1,1] = invgamma.rvs((nu0+TIMES)/2, scale=(s0+difw)/2, size=1)[0]
    for d in xrange(nz):
        Sita_sys[hh,2+d,2+d] = invgamma.rvs((nu0+TIMES)/2, scale=(s0+difbeta[d])/2, size=1)[0]
    return Sita_sys[hh,:,:]
Example #34
0
def draw_posterior_sigma2(model, states, phi, mu, prior_params=(5, 0.05)):
    sigma_r, S_sigma = prior_params

    v1 = sigma_r + model.nobs
    tmp1 = (states[0, 0] - mu)**2 * (1 - phi**2)
    tmp = np.sum(((states[0, 1:] - mu) - phi * (states[0, :-1] - mu))**2)
    delta1 = S_sigma + tmp1 + tmp

    return invgamma.rvs(v1, scale=delta1)
Example #35
0
def calculate_difference_m(Xs): # 人間すべてに対応
    # step3の階差の計算--------------------------------------
    # step3の階差計算の和の計算で使用する変数の初期化
    dift = np.zeros(nhh, dtype=np.float)
    difw = np.zeros(nhh, dtype=np.float)
    difbeta = np.zeros((nhh,nz), dtype=np.float)

    dift = np.sum( (Xs[:,0,1:TIMES] - Xs[:,0,0:TIMES-1])**2, axis=1 )
    difw = np.sum( (Xs[:,k,1:TIMES]+np.sum(Xs[:, k:(k-1)+SEASONALYTY-1, 0:TIMES-1], axis=1))**2, axis=1 )
    for d in xrange(nz):
        difbeta[:,d] = np.sum( (Xs[:, (k-1)+SEASONALYTY+d, 1:TIMES] 
                                    - Xs[:, (k-1)+ SEASONALYTY+d, 0:TIMES-1])**2, axis=1 )      
    # step3--------------------------------------
    Sita_sys[:,0,0] = invgamma.rvs((nu0+TIMES)/2, scale=(s0+dift)/2, size=nhh)
    Sita_sys[:,1,1] = invgamma.rvs((nu0+TIMES)/2, scale=(s0+difw)/2, size=nhh)
    for d in xrange(nz):
        Sita_sys[:,2+d,2+d] = invgamma.rvs((nu0+TIMES)/2, scale=(s0+difbeta[:,d])/2, size=nhh)
    return Sita_sys[:,:,:]
Example #36
0
 def updatePriorVals(self,weights,biases): 
     new_sW = np.zeros(self.sW.shape)
     weights_cpu = weights.get()
     n_w = np.float32(weights_cpu.shape[0])
     shape_new = self.shape + n_w/2.0
     for i in range(0,weights_cpu.shape[1]):
         scale_new =  self.scale + ((weights_cpu[:,i])**2).sum()/2.0
         new_val = invgamma.rvs(shape_new,scale=scale_new,size=1)
         new_sW[0,i] = np.float32(new_val)
     self.sW = gpuarray.to_gpu(new_sW.astype(self.precision))
     
      ## Biases have common variance
     biases_cpu = biases.get()
     n_b = np.float32(biases.shape[1])
     shape_new = self.shape + n_b/2.0
     scale_new =  self.scale + ((biases_cpu)**2).sum()/2.0
     new_val = invgamma.rvs(shape_new,scale=scale_new,size=1)
     new_sB = np.float32(new_val)
     self.sB = gpuarray.to_gpu(new_sB)
Example #37
0
 def updatePriorVals(self,weights,biases): 
     new_sW = np.zeros(self.sW.shape)
     weights_cpu = weights.get()
     n_w = np.float32(weights_cpu.shape[0]*weights_cpu.shape[1])
     shape_new = self.shape + n_w/2.0
     scale_new =  self.scale + (weights_cpu**2).sum()/2.0
     new_val = invgamma.rvs(shape_new,scale=scale_new,size=1)
     #print 'New standard deviation for feature ' + ': ' + str(new_val)
     new_sW = np.float32(new_val)
     self.sW = gpuarray.to_gpu(new_sW.astype(self.precision))
     
      ## Biases have common variance
     biases_cpu = biases.get()
     n_b = np.float32(biases.shape[1])
     shape_new = self.shape + n_b/2.0
     scale_new =  self.scale + ((biases_cpu)**2).sum()/2.0
     new_val = invgamma.rvs(shape_new,scale=scale_new,size=1)
     new_sB = np.float32(new_val)
     self.sB = gpuarray.to_gpu(new_sB)
Example #38
0
def simple_model_sample_prior():
    vals = np.zeros(3 * components)
    vals[:components] = \
        np.exp(multivariate_normal.rvs(cov=COV_ALPHA*np.eye(components)))
    for i in range(components, 2*components):
        vals[i] = random.random() * (END - START) + START

    vals[(2*components):(3*components)] = \
        invgamma.rvs(A, LOC, SCALE, size=components)

    return vals
Example #39
0
    def _resample_eta(self):
        """
        Resample sigma under an inverse gamma prior, sigma ~ IG(1,1)
        :return:
        """
        L = self.L

        a_prior = 1.0
        b_prior = 1.0

        a_post = a_prior + L.size / 2.0
        b_post = b_prior + (L**2).sum() / 2.0

        from scipy.stats import invgamma
        self.eta = invgamma.rvs(a=a_post, scale=b_post)
Example #40
0
 def __init__(self,shape,scale,layer,precision=np.float32,init=100):
     self.precision = precision
     self.shape = shape
     self.scale = scale
     
     ##initialize with random draw
     #init_var = invgamma.rvs(shape,scale=scale,size=(1,layer.weights.shape[0])).astype(precision)
     init_var = (np.tile(init,reps=layer.weights.shape[0]).reshape(1,layer.weights.shape[0])).astype(precision)
     self.sW = gpuarray.to_gpu(init_var)
     init_mean = (np.tile(self.scale/(self.shape-1),reps=layer.weights.shape[0]).reshape(1,layer.weights.shape[0])).astype(precision)
     self.mean = gpuarray.to_gpu(init_mean)
     
     init_var = invgamma.rvs(1.0,scale=1.0,size=(1,1)).astype(precision)
     self.sB = gpuarray.to_gpu(init_var)
     kernels = SourceModule(open(path+'/kernels.cu', "r").read())
     self.add_prior_kernel = kernels.get_function("add_ARD_grad")
     self.add_prior_b_kernel = kernels.get_function("add_bias_grad")
     self.scale_momentum_kernel = kernels.get_function("scale_momentum_ARD")
     self.scale_stepsize_kernel = kernels.get_function("scale_stepsize_ARD")
  def action(self, context):
    """Samples beta's from posterior, and chooses best action accordingly.

    Args:
      context: Context for which the action need to be chosen.

    Returns:
      action: Selected action for the context.
    """

    # Round robin until each action has been selected "initial_pulls" times
    if self.t < self.hparams.num_actions * self.hparams.initial_pulls:
      return self.t % self.hparams.num_actions

    # Sample sigma2, and beta conditional on sigma2
    sigma2_s = [
        self.b[i] * invgamma.rvs(self.a[i])
        for i in range(self.hparams.num_actions)
    ]

    try:
      beta_s = [
          np.random.multivariate_normal(self.mu[i], sigma2_s[i] * self.cov[i])
          for i in range(self.hparams.num_actions)
      ]
    except np.linalg.LinAlgError as e:
      # Sampling could fail if covariance is not positive definite
      print('Exception when sampling from {}.'.format(self.name))
      print('Details: {} | {}.'.format(e.message, e.args))
      d = self.hparams.context_dim + 1
      beta_s = [
          np.random.multivariate_normal(np.zeros((d)), np.eye(d))
          for i in range(self.hparams.num_actions)
      ]

    # Compute sampled expected values, intercept is last component of beta
    vals = [
        np.dot(beta_s[i][:-1], context.T) + beta_s[i][-1]
        for i in range(self.hparams.num_actions)
    ]

    return np.argmax(vals)
  def action(self, context):
    """Samples beta's from posterior, and chooses best action accordingly."""

    # Round robin until each action has been selected "initial_pulls" times
    if self.t < self.hparams.num_actions * self.hparams.initial_pulls:
      return self.t % self.hparams.num_actions

    # Sample sigma2, and beta conditional on sigma2
    sigma2_s = [
        self.b[i] * invgamma.rvs(self.a[i])
        for i in range(self.hparams.num_actions)
    ]

    try:
      beta_s = [
          np.random.multivariate_normal(self.mu[i], sigma2_s[i] * self.cov[i])
          for i in range(self.hparams.num_actions)
      ]
    except np.linalg.LinAlgError as e:
      # Sampling could fail if covariance is not positive definite
      print('Exception when sampling for {}.'.format(self.name))
      print('Details: {} | {}.'.format(e.message, e.args))
      d = self.latent_dim
      beta_s = [
          np.random.multivariate_normal(np.zeros((d)), np.eye(d))
          for i in range(self.hparams.num_actions)
      ]

    # Compute last-layer representation for the current context
    with self.bnn.graph.as_default():
      c = context.reshape((1, self.hparams.context_dim))
      z_context = self.bnn.sess.run(self.bnn.nn, feed_dict={self.bnn.x: c})

    # Apply Thompson Sampling to last-layer representation
    vals = [
        np.dot(beta_s[i], z_context.T) for i in range(self.hparams.num_actions)
    ]
    return np.argmax(vals)
Example #43
0
    def resample(self,data=[]):

        return
        if self.bayesian:
            # Compute the states matrix of the whole sequence given the other chains' states
            global_state = self.l + self.others

            # Correct the overflow in the bias term
            global_state[-1, :] = 1.

            # Sanity check
            assert global_state[global_state > 1].any() == False, "There is a problem rebuilding the global state matrix"
            w = self.W*global_state #These should be numpy's matrix objects so this works

            # Reconstruct the indices of the data
            data = data[0]

            idx = np.zeros(data.shape[0], dtype=int)
            for i in xrange(data.shape[0]):
                idx[i] = np.where(np.all(self.global_data == data[i, :], axis=1))[0]

            # Compute the sum of squared errors
            centered = np.power(data - w.T[idx, :], 2)

            sse = np.sum(centered, axis=0)

            variances = np.ones(sse.shape[1])
            # Resample the variances
            for i in xrange(sse.shape[1]):
                alpha = self.ah + (self.global_data.shape[0]/2.)
                beta = 1. /(self.bh + (sse[0, i]/2.))

                sample = invgamma.rvs(alpha, beta)
                variances[i] = sample

            self.variances = variances
 #
 # step3の階差の計算--------------------------------------
 dift = sum((Xs[hh, 0, 1:TIMES] - Xs[hh, 0, 0 : TIMES - 1]) ** 2)
 difw = sum((Xs[hh, k, 1:TIMES] + sum(Xs[hh, k : (k - 1) + SEASONALYTY - 1, 0 : TIMES - 1])) ** 2)
 for d in range(nz):
     difbeta[d] = sum(
         (Xs[hh, (k - 1) + SEASONALYTY + d, 1:TIMES] - Xs[hh, (k - 1) + SEASONALYTY + d, 0 : TIMES - 1]) ** 2
     )
 # --------------------------------------------------------
 #
 # step4の効用値の誤差計算(step4のθの尤度計算)------------
 Lsita = sum((u[hh, :] - np.diag(np.dot(Ztld[:, :, hh].T, Xs[hh, :, :]))) ** 2)
 # --------------------------------------------------------
 #
 # step3--------------------------------------
 Sita_sys[hh, 0, 0] = invgamma.rvs((nu0 + TIMES) / 2, scale=(s0 + dift) / 2, size=1)[0]
 Sita_sys[hh, 1, 1] = invgamma.rvs((nu0 + TIMES) / 2, scale=(s0 + difw) / 2, size=1)[0]
 for d in range(nz):
     Sita_sys[hh, 2 + d, 2 + d] = invgamma.rvs((nu0 + TIMES) / 2, scale=(s0 + difbeta[d]) / 2, size=1)[0]
 # --------------------------------------------
 #
 ### step4--------------------------------------
 ## '''dlt側の計算'''
 # 現状のθを確保する
 old_sita_dlt = Sita_dlt[hh]
 # 新しいθをサンプリング(酔歩サンプリング)
 new_sita_dlt = Sita_dlt[hh] + ss.norm.rvs(loc=0, scale=sigma_dlt, size=1)[0]
 #
 # 尤度の計算(対数尤度の場合はヤコビアンで調整)
 new_Lsita_dlt = Lsita + hbm.Nkernel(new_sita_dlt, Hdlt, D[hh, :], Vsita_dlt)
 new_Lsita_dlt = math.exp(-0.5 * new_Lsita_dlt)
    for i, component in enumerate(components):
        states[i, :] = component.states_list[0].stateseq

    # Now compute the means
    means = np.matrix(weights)*states
    # Squared summed error
    sse = np.power(observations - means.T, 2).sum(axis=0)


    # Learn the new variances
    new_variances = np.zeros((1, sse.shape[1]))

    for i in xrange(sse.shape[1]):
        alpha = .05 + (observations.shape[0]/ 2.)
        beta = (.05 + (sse[0, i]/2.))
        new_variances[0, i] = invgamma.rvs(alpha, scale=beta)

    # Delete the states to avoid memory leaks
    del states

    for component in components:

        # The number of components plus a bias term
        global_states = np.zeros((COMP+1, observations.shape[0]))
        global_states[-1, :] = 1. # The bias term

        for i, other in enumerate(components):
            # Only if this is another chain
            if other != component:
                seq = other.states_list[0].stateseq
                global_states[i, :] = seq
Example #46
0
 def getUpdates(self):
     return invgamma.rvs(a = self.a, scale = self.scale, size = 1)
Example #47
0
 def sample(self):
     ''' Samples a mean and stdev from the Normal-InverseGamma distribution. '''
     variance = invgamma.rvs(self.a, scale=self.b)
     mean = np.random.normal(self.mu, np.sqrt(variance / self.nu))
     return (mean, np.sqrt(variance))
Example #48
0
    def _sample(self, shape, scale, size):

        return invgamma.rvs(shape, scale=scale, size=size)
    def estimate(self, xs, max_iterations=10000):
        self.n = len(xs)
        self.s2 = invgamma.rvs(self.alpha, scale=self.beta)
        self.means = []
        response_sums = []
        self.counts = []
        assignments = []

        assignments.append(0)
        response_sums.append(xs[0])
        self.counts.append(1)
        self.means.append(self.sample_posterior_mean(response_sums[0], 1,
                                                     self.s2, self.m0,
                                                     self.s20))

        for i in range(1, len(xs)):
            weights = self.__create_weight_vector(xs[i], self.means,
                                                  self.counts, self.s2,
                                                  self.m0, self.s20,
                                                  self.m)
            # Sample an assignment for each item and update statistics
            assignment = multinomial_sampling.sample(weights)
            if assignment < len(self.means):
                response_sums[assignment] += xs[i]
                self.counts[assignment] += 1

            # Create a new component
            elif assignment == len(self.means):
                response_sums.append(xs[i])
                self.counts.append(1)
                self.means.append(self.sample_posterior_mean(xs[i], 1,
                                                             self.s2, self.m0,
                                                             self.s20))
            else:
                print("WWWWW Should never get here. WWWWWW")
            assignments.append(assignment)

        for i in xrange(max_iterations):
            # First sample an assignment for each data item

            for j in xrange(len(xs)):
                old_assignment = assignments[j]
                response_sums[old_assignment] -= xs[j]
                self.counts[old_assignment] -= 1

                if self.counts[old_assignment] == 0:
                    self.__fix_hole(assignments, response_sums, self.means,
                                    self.counts, old_assignment)
                weights = self.__create_weight_vector(xs[j], self.means,
                                                      self.counts,
                                                      self.s2, self.m0,
                                                      self.s20, self.m)
                new_assignment = multinomial_sampling.sample(weights)
                # Create a new component
                if new_assignment < len(self.means):
                    response_sums[new_assignment] += xs[j]
                    self.counts[new_assignment] += 1
                elif new_assignment == len(self.means):
                    response_sums.append(xs[j])
                    self.counts.append(1)
                    self.means.append(self.sample_posterior_mean(xs[j], 1,
                                                                 self.s2,
                                                                 self.m0,
                                                                 self.s20))
                else:
                    print("EEEEEEE This should never happenEEEEEEEEEE")
                assignments[j] = new_assignment

            # Sample new values for the means
            for j in xrange(len(self.means)):
                self.means[j] = self.sample_posterior_mean(response_sums[j],
                                                           self.counts[j],
                                                           self.s2, self.m0,
                                                           self.s20)

            # Sample new value for the component variance
            res_sum_squares = self.calculate_res_sum_squares(xs, assignments,
                                                             self.means)
            self.s2 = self.sample_posterior_var(self.n, res_sum_squares,
                                                self.alpha, self.beta)
        return self
    def sample_posterior_var(self, n, res_sum_squares, alpha, beta):
        alpha_star = alpha + (n / 2.)
        beta_star = beta + (res_sum_squares / 2.)

        return invgamma.rvs(alpha_star, scale=beta_star)
Example #51
0
def rchisquare_inv_scaled(df,scale,size=None):
    a = df/2.0
    b = (df*scale)/2.0
    # size=None returns a scalar
    return invgamma.rvs(a,scale=b,size=size)
Example #52
0
def sample_physics():
  """
  Return a physics object with all the model parameters sampled from their
  appropriate hyperpriors.
  """

  T = 3600                              # an episode is 3600 seconds

  R = 6371                             # the radius of the earth in km
  
  # event rate
  lambda_e = gamma.rvs(6.0, loc=0, scale = 1/(4 * pi * R**2 * T))
  
  # event magnitude
  mu_m = 3.0
  theta_m = 4.0
  gamma_m = 6.0

  # Note: the correct choice of theta_m should be 0.75.
  # We have chosen a large value here to force larger events and hence
  # allow for easier detection. If the full set of 180 stations are ever used
  # then we can correct theta_m.

  # station-specific attributes
  
  mu_d0, mu_d1, mu_d2 = [], [], []
  mu_t, theta_t, mu_z, theta_z, mu_s, theta_s = [], [], [], [], [], []
  mu_a0, mu_a1, mu_a2, sigma_a = [], [], [], []
  lambda_f, mu_f, theta_f = [], [], []
  
  for sta in STATIONS:
    
    # per-station detection probability
    (d0, d1, d2) = mvar_norm_sample(array([-10.4, 3.26, -0.0499]),
                                    array([[13.43,-2.36, -.0122],
                                           [-2.36, 0.452, .000112],
                                           [-.0122, .000112, .000125]]))
    mu_d0.append(d0)
    mu_d1.append(d1)
    mu_d2.append(d2)

    # time
    mu_t.append(0.0)
    theta_t.append(invgamma.rvs(120, 0.0, 118))
    
    # azimuth
    mu_z.append(0.0)
    theta_z.append(invgamma.rvs(5.2, 0.0, 44.0))

    # slowness
    mu_s.append(0.0)
    theta_s.append(invgamma.rvs(6.7, 0.0, 7.5))

    # amplitude
    (a0, a1, a2) = mvar_norm_sample(array([-7.3, 2.03, -.00196]),
                                    array([[1.23, -.227, -.000175],
                                           [-.227, .0461, .0000245],
                                           [-.000175, .0000245,  .000000302]]))
    mu_a0.append(a0)
    mu_a1.append(a1)
    mu_a2.append(a2)
    
    sigma_a.append(sqrt(invgamma.rvs(21.1, 0, 12.6)))
    
    # false arrivals
    lambda_f.append(gamma.rvs(2.1, 0, 0.0013))
    mu_f.append(norm.rvs(-0.68, 0.68))
    theta_f.append(invgamma.rvs(23.5, 0, 12.45))
    
  return Physics(T, R, lambda_e, mu_m, theta_m, gamma_m, mu_d0, mu_d1, mu_d2,
                 mu_t, theta_t, mu_z, theta_z, mu_s, theta_s,
                 mu_a0, mu_a1, mu_a2, sigma_a,
                 lambda_f, mu_f, theta_f)