def step(self): def masked_values(stoch): mask = getattr(stoch, 'mask', None) if mask is not None: return stoch.value[mask] else: return stoch.value S_cond = np.concatenate([masked_values(c_) for c_ in self.children]) N_mat = compute_trans_freqs(S_cond, self.stochastic.shape[0], counts_only=True) # We assume, of course, that the parent(s) are the # alpha Dirichlet concentration parameters. alpha_suff_prior = self.stochastic.parents.values()[0] alpha_suff_prior = getattr(alpha_suff_prior, 'value', alpha_suff_prior) alpha_suff_post = alpha_suff_prior + N_mat trans_mat_smpl = np.zeros(self.stochastic.shape) for s, alpha_row in enumerate(alpha_suff_post): trans_mat_smpl[s] = pymc.rdirichlet(alpha_row) self.stochastic.value = trans_mat_smpl
def sim_ordinal(I, J, K, alpha=None, beta=None): # test input params here Is = range(I) Js = range(J) Ks = range(K) N = I * J Ns = range(N) if alpha == None: alpha = alloc_mat(K, K) for k1 in Ks: for k2 in Ks: alpha[k1][k2] = max(1,(K + (0.5 if k1 == k2 else 0) \ - abs(k1 - k2))**4) if beta == None: beta = alloc_vec(K, 2.0) # simulated params beta = alloc_vec(K, 2.0) prevalence = pymc.rdirichlet(beta).tolist() prevalence.append(1.0 - sum(prevalence)) # complete category = [] for i in Is: category.append(pymc.rcategorical(prevalence).tolist()) accuracy = alloc_tens(J, K, K) for j in Js: for k in Ks: accuracy[j][k] = pymc.rdirichlet(alpha[k]).tolist() accuracy[j][k].append(1.0 - sum(accuracy[j][k])) # simulated data item = [] anno = [] label = [] for i in Is: for j in Js: item.append(i) anno.append(j) label.append(pymc.rcategorical(accuracy[j][category[i]]).tolist()) N = len(item) return (prevalence, category, accuracy, item, anno, label)
def trans_mat_random(alpha_trans): """ Sample a shape (K, K-1) transition probability matrix with K-many states given Dirichlet parameters for each row. Parameters ========== alpha_trans: ndarray Dirichlet parameters for each row. Returns ======= A ndarray of the sampled transition probability matrix (without the last column). """ trans_mat_smpl = np.empty((alpha_trans.shape[0], alpha_trans.shape[1] - 1), dtype=np.float) for s, alpha_row in enumerate(alpha_trans): trans_mat_smpl[s] = pymc.rdirichlet(alpha_row, size=1) return trans_mat_smpl
def Ptrans_random(theta): return pymc.rdirichlet(theta, size=len(theta))
import pymc import numpy as np trans = [[80,10,10],[10,80,10],[10,10,80]] n_samples = 1000 means = [pymc.rgamma(alpha,beta,size=n_samples) for alpha,beta in zip([1,2,3],[0.1,0.2,0.3])] variances = [pymc.rgamma(alpha,beta,size=n_samples) for alpha,beta in zip([.2,.3,.4],[0.1,0.1,0.1])] transitions = [pymc.rdirichlet(trans_,size=n_samples) for trans_ in trans] n_gamma = 3 n_modes = n_gamma * 2 mean_params = [pymc.Gamma('mean_param{}'.format(i), alpha=1, beta=.1) for i in range(n_modes)] var_params = [pymc.Gamma('var_param{}'.format(i), alpha=1, beta=.1) for i in range(n_modes)] trans_params = [pymc.Beta('trans_params{}'.format(i), alpha=1,beta=1) for i in range(n_gamma*n_gamma)] mean_obs = [] mean_pred = [] var_obs = [] var_pred = [] trans_obs = [] trans_pred = [] for i in xrange(n_gamma): alpha1 = mean_params[i*2] beta1 = mean_params[i*2+1] mean_obs.append(pymc.Gamma("mean_obs{}".format(i),alpha=alpha1,beta=beta1,value=means[i],observed=True)) mean_pred.append(pymc.Gamma("mean_pred{}".format(i),alpha=alpha1,beta=beta1))
import pymc import numpy as np trans = [[80, 10, 10], [10, 80, 10], [10, 10, 80]] n_samples = 1000 means = [ pymc.rgamma(alpha, beta, size=n_samples) for alpha, beta in zip([1, 2, 3], [0.1, 0.2, 0.3]) ] variances = [ pymc.rgamma(alpha, beta, size=n_samples) for alpha, beta in zip([.2, .3, .4], [0.1, 0.1, 0.1]) ] transitions = [pymc.rdirichlet(trans_, size=n_samples) for trans_ in trans] n_gamma = 3 n_modes = n_gamma * 2 mean_params = [ pymc.Gamma('mean_param{}'.format(i), alpha=1, beta=.1) for i in range(n_modes) ] var_params = [ pymc.Gamma('var_param{}'.format(i), alpha=1, beta=.1) for i in range(n_modes) ] trans_params = [ pymc.Beta('trans_params{}'.format(i), alpha=1, beta=1) for i in range(n_gamma * n_gamma) ] mean_obs = []