def _noise_variance(hparams, tau_cmmn, verbose=0): dist_std_noise = hparams.dist_std_noise if dist_std_noise == 'tr_normal': h1 = HalfNormal('h1', tau=np.float32(1 / tau_cmmn[0]), dtype=floatX) h2 = HalfNormal('h2', tau=np.float32(1 / tau_cmmn[1]), dtype=floatX) if 10 <= verbose: print('Truncated normal for prior scales') elif dist_std_noise == 'log_normal': h1 = Lognormal('h1', tau=np.float32(1 / tau_cmmn[0]), dtype=floatX) h2 = Lognormal('h2', tau=np.float32(1 / tau_cmmn[1]), dtype=floatX) if 10 <= verbose: print('Log normal for prior scales') elif dist_std_noise == 'uniform': h1 = Uniform('h1', upper=np.float32(1 / tau_cmmn[0]), dtype=floatX) h2 = Uniform('h2', upper=np.float32(1 / tau_cmmn[1]), dtype=floatX) if 10 <= verbose: print('Uniform for prior scales') else: raise ValueError("Invalid value of dist_std_noise: %s" % dist_std_noise) return h1, h2
def _noise_variance( hparams, tau_cmmn, HalfNormal, Lognormal, Uniform, floatX, verbose): if hparams['prior_scale'] == 'tr_normal': h1 = HalfNormal('h1', tau=np.float32(1 / tau_cmmn[0]), dtype=floatX) h2 = HalfNormal('h2', tau=np.float32(1 / tau_cmmn[1]), dtype=floatX) if 10 <= verbose: print('Truncated normal for prior scales') elif hparams['prior_scale'] == 'log_normal': h1 = Lognormal('h1', tau=np.float32(1 / tau_cmmn[0]), dtype=floatX) h2 = Lognormal('h2', tau=np.float32(1 / tau_cmmn[1]), dtype=floatX) if 10 <= verbose: print('Log normal for prior scales') elif hparams['prior_scale'] == 'uniform': h1 = Uniform('h1', upper=np.float32(1 / tau_cmmn[0]), dtype=floatX) h2 = Uniform('h2', upper=np.float32(1 / tau_cmmn[1]), dtype=floatX) if 10 <= verbose: print('Uniform for prior scales') else: raise ValueError("Invalid value of prior_scale: %s" % hparams['prior_scale']) return h1, h2
def createSignalModelExponential(data): """ Toy model that treats the first ~10% of the waveform as an exponential. Does a good job of finding the start time (t_0) Since I made this as a toy, its super brittle. Waveform must be normalized """ with Model() as signal_model: switchpoint = Uniform('switchpoint', lower=0, upper=len(data), testval=len(data)/2) noise_sigma = HalfNormal('noise_sigma', sd=1.) #Modeling these parameters this way is why wf needs to be normalized exp_rate = Uniform('exp_rate', lower=0, upper=.5, testval = 0.05) exp_scale = Uniform('exp_scale', lower=0, upper=.5, testval = 0.1) timestamp = np.arange(0, len(data), dtype=np.float) rate = switch(switchpoint >= timestamp, 0, exp_rate) baseline_model = Deterministic('baseline_model', exp_scale * (exp( (timestamp-switchpoint)*rate)-1.) ) baseline_observed = Normal("baseline_observed", mu=baseline_model, sd=noise_sigma, observed= data ) return signal_model
def fit(self, base_models_predictions, true_targets, model_identifiers=None): ba = BayesianAverage() weight_vector = ba.fit(base_models_predictions, true_targets) default = True base_models_predictions = base_models_predictions.transpose() n_basemodels = base_models_predictions.shape[2] with Model() as basic_model: #define prior HalfNormal('weights', sd=1, shape=n_basemodels) #define likelihood function ensemble_pred = np.dot(base_models_predictions, weight_vector) Categorical('likelihood', p=ensemble_pred.transpose(), observed=true_targets) with basic_model: start = find_MAP(model=basic_model) if not default: step = Metropolis() step = NUTS() trace = sample(self.n_samples, step=step, start=start) trace = trace[5000:] self.sampled_weights = trace["weights"]
N_GROUPS = comm.Get_size( ) - 1 #Groups used for simulation - one less than used cores if rank == 0: #master print("CHAIN ", CHAIN) print("DRAWS", NDRAWS) print("How many sentences used?", n_sentences) basic_model = Model() #Below we specify the Bayesian model with basic_model: #Priors le = HalfNormal('le', sd=0.5, testval=abs(np.random.randn()) / 2) lf = Gamma('lf', alpha=1, beta=5, testval=abs(np.random.randn() / 4)) #emvt = Gamma('emvt', alpha=1, beta=3, testval=abs(np.random.randn())) emap = HalfNormal('emap', sd=1.0, testval=abs(np.random.randn() / 2)) intercept = Normal('intercept', mu=50, sd=25) sigma = HalfNormal('sigma', sd=20) # Expected value of outcome mu = intercept + model(lf, le, emap, intercept, sigma) # Likelihood (sampling distribution) of observations Normal('Y_obs', mu=mu, sd=50, observed=Y) step = Metropolis(basic_model.vars)
axes[1].scatter(X2, Y) axes[0].set_ylabel('Y'); axes[0].set_xlabel('X1'); axes[1].set_xlabel('X2'); plt.show() basic_model = Model() with basic_model: # Priors for unknown model parameters alpha = Normal('alpha', mu=0, sd=10) beta = Normal('beta', mu=0, sd=10, shape=2) sigma = HalfNormal('sigma', sd=1) # Expected value of outcome mu = alpha + beta[0]*X1 + beta[1]*X2 # Likelihood (sampling distribution) of observations Y_obs = Normal('Y_obs', mu=mu, sd=sigma, observed=Y) # map_estimate = find_MAP(model=basic_model) # print(map_estimate) map_estimate = find_MAP(model=basic_model, fmin=optimize.fmin_powell) print(map_estimate)
def createSignalModelWithLookup(data, wfMax): """ Uses a lookup table to avoid having to call siggen. Lookup locations are along a one-dimensional line from PC to the detector corner. See generate_siggen_lookup.py wfMax: maximum of the input signal. Used as a prior for the for scaling of the simulated pulse """ with Model() as signal_model: switchpoint = DiscreteUniform('switchpoint', lower=0, upper=len(data)) noise_sigma = HalfNormal('noise_sigma', sd=1.) siggen_sigma = HalfNormal('siggen_sigma', sd=10.) timestamp = np.arange(0, len(data), dtype=np.int) uncertainty_model = switch(switchpoint >= timestamp, noise_sigma, siggen_sigma) wf_scale = Normal('wf_scale', sd=10., mu=wfMax) detRad = np.floor(35.41) detZ = np.floor(41.5) dtEstimate = DiscreteUniform('dtEstimate', lower=0, upper=99 ) # radiusEstimate = DiscreteUniform('radiusEstimate', lower=0, upper=35 ) # zEstimate = DiscreteUniform('zEstimate', lower=0, upper=41) @as_op(itypes=[T.lscalar, T.lscalar, T.dscalar], otypes=[T.dvector]) def siggen_model_dt(switchpoint, dtEstimate, wf_scale): siggen_out = dt_array[dtEstimate, :] siggen_out *= wf_scale T.clip(dtEstimate, 0, 99) #THIS IS A DISASTER. NEED to find a better way to handle this out = np.zeros(len(data)) out[switchpoint:] = siggen_out[0:(len(data) - switchpoint)] # print "length of out is %d" % len(out) return out @as_op(itypes=[T.lscalar, T.lscalar, T.lscalar], otypes=[T.dvector]) def siggen_model(switchpoint, r, z): siggen_out = findSiggenWaveform(0,r,z,np.amax(np_data)) out = np.zeros(len(data)) out[switchpoint:] = siggen_out[0:(len(data) - switchpoint)] return out # print "length of data is %d" % len(data) # @as_op(itypes=[T.lscalar, T.dscalar, T.dscalar], otypes=[T.dvector]) # # def crazy_modulo3(switchpoint, exp_scale, exp_rate): # out = np.zeros(len(data)) # out[switchpoint:] = exp_scale * (np.exp( exp_rate * (timestamp[switchpoint:] - switchpoint))-1.) # return out #baseline_model = Deterministic('baseline_model', exp_scale * (exp( (timestamp-switchpoint)*rate)-1.) ) # baseline_model = siggen_model(switchpoint, radiusEstimate, zEstimate) baseline_model_dt = siggen_model_dt(switchpoint, dtEstimate, wf_scale) baseline_observed = Normal("baseline_observed", mu=baseline_model_dt, sd=uncertainty_model, observed= data ) return signal_model #def createSignalModelDynamic(data, wfMax): # """ # Calls siggen in real time # # """ # # with Model() as signal_model: # # switchpoint = DiscreteUniform('switchpoint', lower=0, upper=len(data)) # noise_sigma = HalfNormal('noise_sigma', sd=1.) # siggen_sigma = HalfNormal('siggen_sigma', sd=10.) # # timestamp = np.arange(0, len(data), dtype=np.int) # # uncertainty_model = switch(switchpoint >= timestamp, noise_sigma, siggen_sigma) # # detRad = np.floor(35.41) # detZ = np.floor(41.5) # # dtEstimate = DiscreteUniform('dtEstimate', lower=0, upper=99 ) # # # # radiusEstimate = DiscreteUniform('radiusEstimate', lower=0, upper=35 ) # # zEstimate = DiscreteUniform('zEstimate', lower=0, upper=41) # # # # @as_op(itypes=[T.lscalar, T.lscalar], otypes=[T.dvector]) # def siggen_model_dt(switchpoint, dtEstimate): # siggen_out = dt_array[dtEstimate, :] # siggen_out *= wfMax # # T.clip(dtEstimate, 0, 99) #THIS IS A DISASTER. NEED to find a better way to handle this # # out = np.zeros(len(data)) # out[switchpoint:] = siggen_out[0:(len(data) - switchpoint)] # # # print "length of out is %d" % len(out) # return out # # @as_op(itypes=[T.lscalar, T.lscalar, T.lscalar], otypes=[T.dvector]) # def siggen_model(switchpoint, r, z): # siggen_out = findSiggenWaveform(0,r,z,np.amax(np_data)) # out = np.zeros(len(data)) # out[switchpoint:] = siggen_out[0:(len(data) - switchpoint)] # # return out # # # # print "length of data is %d" % len(data) # # # @as_op(itypes=[T.lscalar, T.dscalar, T.dscalar], otypes=[T.dvector]) # # # # def crazy_modulo3(switchpoint, exp_scale, exp_rate): # # out = np.zeros(len(data)) # # out[switchpoint:] = exp_scale * (np.exp( exp_rate * (timestamp[switchpoint:] - switchpoint))-1.) # # return out # # # #baseline_model = Deterministic('baseline_model', exp_scale * (exp( (timestamp-switchpoint)*rate)-1.) ) # # # baseline_model = siggen_model(switchpoint, radiusEstimate, zEstimate) # baseline_model_dt = siggen_model_dt(switchpoint, dtEstimate) # # # baseline_observed = Normal("baseline_observed", mu=baseline_model_dt, sd=uncertainty_model, observed= data ) # # return signal_model
df = pd.read_csv("../data/data-ss2.txt") T = df.values[:, 0].astype(np.float32) Y = df.values[:, 1].astype(np.float32) n_times = len(df["X"].unique()) #時間の数 L = 4 #トレンド周期 basic_model = Model() #subtensorの使い方↓ #http://deeplearning.net/software/theano/library/tensor/basic.html with basic_model: #事前分布 s_mu = HalfNormal('s_mu', sd=100) #隣接時刻の状態の誤差 s_season = HalfNormal('s_season', sd=100) #季節誤差 s_Y = HalfNormal('s_Y', sd=100) #各時刻における状態と観測の誤差 season_0_1_2 = Normal('season_0_1_2', mu=0, sd=100, shape=3) #季節項の誤差なし初期値 #季節process ##まず初期状態の代入 season = tt.zeros((n_times)) for i in range(L - 1): season = tt.set_subtensor(season[i], season_0_1_2[i]) ##初期状態以降の代入 for t in range(n_times - 3): # sum^{L-1}_{l=1} -season[t-l] のテンソルを作成 Sigma = tt.zeros((1)) for l in range(L - 1):
parser.model_parameters["strength_of_association"] = \ strength_of_association parser.model_parameters["rule_firing"] = rule_firing parser.model_parameters["latency_factor"] = latency_factor sample = np.array(run_fan_exp(), dtype=np.dtype('f8')) * 1000 #print("SAMPLE", sample) return sample fan_model = Model() with fan_model: # Priors buffer_spreading_activation = HalfNormal("bsa", sd=2) strength_of_association = HalfNormal("soa", sd=4) rule_firing = HalfNormal("rf", sd=0.03) latency_factor = HalfNormal("lf", sd=0.2) # Likelihood pyactr_rt = actrmodel_latency(rule_firing, latency_factor, buffer_spreading_activation, strength_of_association) mu_rt = Deterministic('mu_rt', pyactr_rt) rt_observed = Normal('rt_observed', mu=mu_rt, sd=10, observed=RT) #with fan_model: # Compute posteriors #step = pm.SMC(parallel=True) #trace = pm.sample(draws=5000, step=step, njobs=1, cores=50)
import matplotlib.pyplot as plt from pymc3 import Model, Normal, HalfNormal from pymc3 import NUTS, sample from scipy import optimize from pymc3 import traceplot from pymc3 import summary #shape = クラスの数の確率変数に、クラスの値を取るデータ数次元のベクトルを入れる操作がありますが #その詳細な説明は(https://pymc-devs.github.io/pymc3/notebooks/GLM-hierarchical.html)参照 df = pd.read_csv("data-salary-2.txt") X_data = df.values[:, 0] Y_data = df.values[:, 1] Class_data = df.values[:, 2] - 1 n_Class = len(df["KID"].unique()) basic_model = Model() with basic_model: a_0 = Normal('a_0', mu=0, sd=10) b_0 = Normal('b_0', mu=0, sd=10) s_a = HalfNormal('sigma_a', sd=100) s_b = HalfNormal('sigma_b', sd=100) s_Y = HalfNormal('sigma_Y', sd=100) a = a_0 + Normal('a', mu=0, sd=s_a, shape=n_Class) b = b_0 + Normal('b', mu=0, sd=s_b, shape=n_Class) mu = a[Class_data] + b[Class_data] * X_data Y_obs = Normal('Y_obs', mu=mu, sd=s_Y, observed=Y_data) trace = sample(2000) summary(trace)
def mcmc_changepoint(dates, ratings, mcmc_iter=1000, discrete=0, plot_result=1): """This function models Yelp reviews as coming from two normal distributions with a switch point somewhere between them. When left of the switch point then reviews are drawn from the first normal distribution. To the right of the switch point reviews are drawn from the second normal distribution. Normal distributions are used if the reviews have been normalized to the user's average rating; otherwise if analyzing in terms of 1-5 stars set discrete=1 and the function will do the same estimation on Poisson distributions. This function then finds the most likely distribution for where the switchpoint is and the most likely parameters for the two generator distributions by using Metropolis-Hastings sampling and Hamiltonian Monte Carlo.""" # dates: Array of dates when the reviews were posted # ratings: Array of the ratings given by each review # mcmc_iter: How many iterations of the MCMC to run? # discrete: Should I use Normal or Poisson distributions to model the ratings? # (i.e. are the user-averaged or 1-5 stars) # plot_result: Should the function output a plot? number_of_ratings = np.arange(0, len(ratings)) if discrete == 0: with Model() as switch_model: switchpoint = DiscreteUniform('switchpoint', lower=0, upper=len(dates)) before_intensity = Normal('before_intensity', mu=0, sd=1) after_intensity = Normal('after_intensity', mu=0, sd=1) intensity = switch(switchpoint >= number_of_ratings, before_intensity, after_intensity) sigma = HalfNormal('sigma', sd=1) rating = Normal('rating', mu=intensity, sd=sigma, observed=ratings) elif discrete == 1: with Model() as switch_model: switchpoint = DiscreteUniform('switchpoint', lower=0, upper=len(dates)) before_intensity = Exponential('before_intensity', 1) after_intensity = Exponential('after_intensity', 1) intensity = switch(switchpoint >= number_of_ratings, before_intensity, after_intensity) rating = Poisson('rating', intensity, observed=ratings) with switch_model: trace = sample(mcmc_iter) if plot_result == 1: traceplot(trace) plt.show() switch_posterior = trace['switchpoint'] N_MCs = switch_posterior.shape[0] before_intensity_posterior = trace['before_intensity'] after_intensity_posterior = trace['after_intensity'] expected_stars = np.zeros(len(ratings)) for a_rating in number_of_ratings: where_switch = a_rating < switch_posterior expected_stars[a_rating] = ( before_intensity_posterior[where_switch].sum() + after_intensity_posterior[~where_switch].sum()) / N_MCs if plot_result == 1: plt.plot(dates, ratings, 'o') plt.plot(dates, expected_stars, 'b-') plt.show() # Return the mode and it's frequency / mcmc_iter b_mean, b_count = scipy.stats.mode(trace['before_intensity']) a_mean, a_count = scipy.stats.mode(trace['after_intensity']) modal_switch, count = scipy.stats.mode(trace['switchpoint']) sigma_est, sigma_count = scipy.stats.mode(trace['sigma']) differential = b_mean - a_mean return differential, modal_switch, expected_stars, sigma_est, switch_posterior
for x in zip(LEMMA_CHUNKS, activation_from_time) } lex_decision.decmem.activations.update(activation_dict) sample = run_lex_decision_task() return np.array(sample) lex_decision_with_bayes = pm.Model() with lex_decision_with_bayes: # prior for activation decay = Uniform('decay', lower=0, upper=1) # priors for accuracy noise = Uniform('noise', lower=0, upper=5) threshold = Normal('threshold', mu=0, sd=10) # priors for latency lf = HalfNormal('lf', sd=1) le = HalfNormal('le', sd=1) # compute activation scaled_time = time**(-decay) def compute_activation(scaled_time_vector): compare = tt.isinf(scaled_time_vector) subvector = scaled_time_vector[(1 - compare).nonzero()] activation_from_time = tt.log(subvector.sum()) return activation_from_time activation_from_time, _ = theano.scan(fn=compute_activation, sequences=scaled_time) # latency likelihood -- this is where pyactr is used pyactr_rt = actrmodel_latency(lf, le, decay, activation_from_time) mu_rt = Deterministic('mu_rt', pyactr_rt)
# print("predicted RTs:", predicted_ms) return predicted_ms comm = MPI.COMM_WORLD rank = comm.Get_rank() N_GROUPS = comm.Get_size() #Groups used for simulation if rank == 0: #master parser_model = Model() with parser_model: # Priors latency_exponent = HalfNormal('le', sd=0.3) # Likelihood pyactr_rt = actrmodel_latency(latency_exponent) mu_rt = Deterministic('mu_rt', pyactr_rt) rt_observed = Normal('rt_observed', mu=mu_rt, sd=30, observed=RT) # Compute posteriors #step = pm.SMC() #trace = sample(draws=NDRAWS, step=step, njobs=1) else: run_exp(rank) #dump('parser_model_'+str(NDRAWS)+'_iterations', trace) with parser_model: trace = load('../../data/parser_model_' + str(NDRAWS) + '_iterations')
#その詳細な説明は(https://pymc-devs.github.io/pymc3/notebooks/GLM-hierarchical.html)参照 basic_model = Model() with basic_model: #グローバルな変数 b_1 = Normal('b_1', mu=0, sd=10) b_2 = Normal('b_2', mu=0, sd=10) b_3 = Normal('b_3', mu=0, sd=10) b_4 = Normal('b_4', mu=0, sd=10) #天気依存変数 x_weather = b_4 * Weather #科目依存変数 s_c = HalfNormal('s_c', sd=10) x_course = Normal('x_course', mu=0, sd=s_c, shape=n_course) x_course = x_course[C_ID] #学生依存変数 s_p = HalfNormal('s_p', sd=10) b_student_variance = Normal('b_student', mu=0, sd=s_p, shape=n_student) x_student = tt.dot(b_2, A) + tt.dot(b_3, Score) + b_student_variance[P_ID] #likelihood (NormalをBernoulliにするとNUTSにできないので、分散がほとんどない正規分布で対処.) x = b_1 + x_student + x_course + x_weather Y_obs = Normal('Y_obs', mu=tt.nnet.sigmoid(x), sd=0.00001, observed=Y) #サンプリング trace = sample(1000) summary(trace)
# -*- coding: utf-8 -*- import pandas as pd import numpy as np import matplotlib.pyplot as plt from pymc3 import Model, Normal, HalfNormal from pymc3 import NUTS, sample from scipy import optimize from pymc3 import traceplot from pymc3 import summary #shape = クラスの数の確率変数に、クラスの値を取るデータ数次元のベクトルを入れる操作がありますが #その詳細な説明は(https://pymc-devs.github.io/pymc3/notebooks/GLM-hierarchical.html)参照 df = pd.read_csv("data-salary-2.txt") X_data = df.values[:, 0] Y_data = df.values[:, 1] Class_data = df.values[:, 2] - 1 n_Class = len(df["KID"].unique()) basic_model = Model() with basic_model: a = Normal('a', mu=0, sd=10, shape=n_Class) b = Normal('b', mu=0, sd=10, shape=n_Class) epsilon = HalfNormal('sigma', sd=1) #likelihood mu = a[Class_data] + b[Class_data] * X_data Y_obs = Normal('Y_obs', mu=mu, sd=epsilon, observed=Y_data) trace = sample(2000) summary(trace)
sim.step() except simpy.core.EmptySchedule: break if not counting.goal: break return np.repeat( np.array(last_time), size) #what is outputed -- nparray of simulated time points basic_model = Model() with basic_model: # Priors for unknown model parameters rule_firing = HalfNormal('rule_firing', sd=2) sigma = HalfNormal('sigma', sd=1) #Deterministic value, found in the model mu = model(rule_firing) # Likelihood (sampling distribution) of observations Normal('Y_obs', mu=mu, sd=sigma, observed=Y) map_estimate = find_MAP(model=basic_model, fmin=scipy.optimize.fmin_powell) print(map_estimate) print("Estimated rule firing:", math.exp(map_estimate['rule_firing_log_']))
TRP = theano.shared(ReferencePeriod) TBinTimes = theano.shared(ShiftedBinTimes) #TInterpBasis = theano.shared(InterpBasis) #TInterpolatedTime = theano.shared(InterpolatedTime) TFlatTimes = theano.shared(FlatBinTimes) FlatData = (ProfileData.flatten())[:useToAs * 1024] basic_model = Model() with basic_model: # Priors for unknown model parameters amplitude = Normal('amplitude', mu=0, sd=10000, shape=useToAs) offset = Normal('offset', mu=0, sd=10000, shape=useToAs) noise = HalfNormal('noise', sd=10000, shape=useToAs) phase = Uniform('phase', lower=0, upper=ReferencePeriod) #parameters that define a two gaussian model gsep = Savex[1] * ReferencePeriod / 1024 g1width = Savex[2] * ReferencePeriod / 1024 g2width = Savex[3] * ReferencePeriod / 1024 g2amp = Savex[4] Tg1width = theano.shared(g1width) Tg2width = theano.shared(g2width) Tg2amp = theano.shared(g2amp) Tgsep = theano.shared(gsep) #Calculate the X values for first gaussian, these have to wrap on a period of [-ReferencePeriod/2, ReferencePeriod/2] x = TFlatTimes - phase
""" parser.model_parameters["latency_factor"] = lf parser.model_parameters["latency_exponent"] = le parser.model_parameters["rule_firing"] = rf parser.model_parameters["eye_mvt_angle_parameter"] = emap predicted_rts = run_self_paced_task() #print(predicted_rts) return predicted_rts stimuli_csv = load_file(SENTENCES, sep=",") #sentences with frequencies sentences = stimuli_csv.groupby(['item', 'label'], sort=False) parser_with_bayes = pm.Model() with parser_with_bayes: lf = HalfNormal('lf', sd=0.3) le = HalfNormal('le', sd=0.5) rf = HalfNormal('rf', sd=0.05) emap = HalfNormal('emap', sd=1.0) # latency likelihood -- this is where pyactr is used pyactr_rt = actrmodel_latency(lf, le, rf, emap) subj_mu_rt = Deterministic('subj_mu_rt', pyactr_rt[0]) subj_rt_observed = Normal('subj_rt_observed', mu=subj_mu_rt, sd=10, observed=subj_extraction['rt']) obj_mu_rt = Deterministic('obj_mu_rt', pyactr_rt[1]) obj_rt_observed = Normal('obj_rt_observed', mu=obj_mu_rt, sd=10, observed=obj_extraction['rt'])
def hmetad_groupLevel(data: dict, sample_model: bool = True, **kwargs): """Compute hierachical meta-d' at the subject level. This is an internal function. The group level model must be called using :py:func:`metadPy.hierarchical.hmetad`. Parameters ---------- data : dict Response data. sample_model : boolean If `False`, only the model is returned without sampling. **kwargs : keyword arguments All keyword arguments are passed to `func::pymc3.sampling.sample`. Returns ------- model : :py:class:`pymc3.Model` instance The pymc3 model. Encapsulates the variables and likelihood factors. trace : :py:class:`pymc3.backends.base.MultiTrace` or :py:class:`arviz.InferenceData` A `MultiTrace` or `ArviZ InferenceData` object that contains the samples. References ---------- .. [#] Fleming, S.M. (2017) HMeta-d: hierarchical Bayesian estimation of metacognitive efficiency from confidence ratings, Neuroscience of Consciousness, 3(1) nix007, https://doi.org/10.1093/nc/nix007 """ nSubj = data["nSubj"] hits = data["hits"] falsealarms = data["falsealarms"] s = data["s"] n = data["n"] counts = data["counts"] nRatings = data["nRatings"] Tol = data["Tol"] cr = data["cr"] m = data["m"] with Model() as model: # hyperpriors on d, c and c2 mu_c1 = Normal( "mu_c1", mu=0, tau=0.01, shape=(1), testval=np.random.rand() * 0.1 ) mu_c2 = Normal( "mu_c2", mu=0, tau=0.01, shape=(1, 1), testval=np.random.rand() * 0.1 ) mu_d1 = Normal( "mu_d1", mu=0, tau=0.01, shape=(1), testval=np.random.rand() * 0.1 ) sigma_c1 = HalfNormal( "sigma_c1", tau=0.01, shape=(1), testval=np.random.rand() * 0.1 ) sigma_c2 = HalfNormal( "sigma_c2", tau=0.01, shape=(1, 1), testval=np.random.rand() * 0.1 ) sigma_d1 = HalfNormal( "sigma_d1", tau=0.01, shape=(1), testval=np.random.rand() * 0.1 ) # Type 1 priors c1_tilde = Normal("c1_tilde", mu=0, sigma=1, shape=(nSubj, 1)) c1 = Deterministic("c1", mu_c1 + sigma_c1 * c1_tilde) d1_tilde = Normal("d1_tilde", mu=0, sigma=1, shape=(nSubj, 1)) d1 = Deterministic("d1", mu_d1 + sigma_d1 * d1_tilde) # TYPE 1 SDT BINOMIAL MODEL h = cumulative_normal(d1 / 2 - c1) f = cumulative_normal(-d1 / 2 - c1) H = Binomial("H", n=s, p=h, observed=hits) FA = Binomial("FA", n=n, p=f, observed=falsealarms) # Hyperpriors on mRatio mu_logMratio = Normal( "mu_logMratio", mu=0, tau=1, shape=(1), testval=np.random.rand() * 0.1 ) sigma_delta = HalfNormal("sigma_delta", tau=1, shape=(1)) delta_tilde = Normal("delta_tilde", mu=0, sigma=1, shape=(nSubj, 1)) delta = Deterministic("delta", sigma_delta * delta_tilde) epsilon_logMratio = Beta("epsilon_logMratio", 1, 1, shape=(1)) logMratio = Deterministic("logMratio", mu_logMratio + epsilon_logMratio * delta) mRatio = Deterministic("mRatio", math.exp(logMratio)) # Type 2 priors meta_d = Deterministic("meta_d", mRatio * d1) # Specify ordered prior on criteria # bounded above and below by Type 1 c1 cS1_hn = Normal( "cS1_hn", mu=0, sigma=1, shape=(nSubj, nRatings - 1), testval=np.linspace(-1.5, -0.5, nRatings - 1) .reshape(1, nRatings - 1) .repeat(nSubj, axis=0), ) cS1 = Deterministic("cS1", -mu_c2 + (cS1_hn * sigma_c2)) cS2_hn = Normal( "cS2_hn", mu=0, sigma=1, shape=(nSubj, nRatings - 1), testval=np.linspace(0.5, 1.5, nRatings - 1) .reshape(1, nRatings - 1) .repeat(nSubj, axis=0), ) cS2 = Deterministic("cS2", mu_c2 + (cS2_hn * sigma_c2)) # Means of SDT distributions S2mu = meta_d / 2 S1mu = -meta_d / 2 # Calculate normalisation constants C_area_rS1 = cumulative_normal(c1 - S1mu) I_area_rS1 = cumulative_normal(c1 - S2mu) C_area_rS2 = 1 - cumulative_normal(c1 - S2mu) I_area_rS2 = 1 - cumulative_normal(c1 - S1mu) # Get nC_rS1 probs nC_rS1 = cumulative_normal(cS1 - S1mu) / C_area_rS1 nC_rS1 = Deterministic( "nC_rS1", math.concatenate( ( [ cumulative_normal(cS1[:, 0].reshape((nSubj, 1)) - S1mu) / C_area_rS1, nC_rS1[:, 1:] - nC_rS1[:, :-1], ( ( cumulative_normal(c1 - S1mu) - cumulative_normal( cS1[:, nRatings - 2].reshape((nSubj, 1)) - S1mu ) ) / C_area_rS1 ), ] ), axis=1, ), ) # Get nI_rS2 probs nI_rS2 = (1 - cumulative_normal(cS2 - S1mu)) / I_area_rS2 nI_rS2 = Deterministic( "nI_rS2", math.concatenate( ( [ ( (1 - cumulative_normal(c1 - S1mu)) - ( 1 - cumulative_normal( cS2[:, 0].reshape((nSubj, 1)) - S1mu ) ) ) / I_area_rS2, nI_rS2[:, :-1] - (1 - cumulative_normal(cS2[:, 1:] - S1mu)) / I_area_rS2, ( 1 - cumulative_normal( cS2[:, nRatings - 2].reshape((nSubj, 1)) - S1mu ) ) / I_area_rS2, ] ), axis=1, ), ) # Get nI_rS1 probs nI_rS1 = (-cumulative_normal(cS1 - S2mu)) / I_area_rS1 nI_rS1 = Deterministic( "nI_rS1", math.concatenate( ( [ cumulative_normal(cS1[:, 0].reshape((nSubj, 1)) - S2mu) / I_area_rS1, nI_rS1[:, :-1] + (cumulative_normal(cS1[:, 1:] - S2mu)) / I_area_rS1, ( cumulative_normal(c1 - S2mu) - cumulative_normal( cS1[:, nRatings - 2].reshape((nSubj, 1)) - S2mu ) ) / I_area_rS1, ] ), axis=1, ), ) # Get nC_rS2 probs nC_rS2 = (1 - cumulative_normal(cS2 - S2mu)) / C_area_rS2 nC_rS2 = Deterministic( "nC_rS2", math.concatenate( ( [ ( (1 - cumulative_normal(c1 - S2mu)) - ( 1 - cumulative_normal( cS2[:, 0].reshape((nSubj, 1)) - S2mu ) ) ) / C_area_rS2, nC_rS2[:, :-1] - ((1 - cumulative_normal(cS2[:, 1:] - S2mu)) / C_area_rS2), ( 1 - cumulative_normal( cS2[:, nRatings - 2].reshape((nSubj, 1)) - S2mu ) ) / C_area_rS2, ] ), axis=1, ), ) # Avoid underflow of probabilities nC_rS1 = math.switch(nC_rS1 < Tol, Tol, nC_rS1) nI_rS2 = math.switch(nI_rS2 < Tol, Tol, nI_rS2) nI_rS1 = math.switch(nI_rS1 < Tol, Tol, nI_rS1) nC_rS2 = math.switch(nC_rS2 < Tol, Tol, nC_rS2) # TYPE 2 SDT MODEL (META-D) # Multinomial likelihood for response counts ordered as c(nR_S1,nR_S2) Multinomial( "CR_counts", cr, nC_rS1, shape=(nSubj, nRatings), observed=counts[:, :nRatings], ) Multinomial( "FA_counts", FA, nI_rS2, shape=(nSubj, nRatings), observed=counts[:, nRatings : nRatings * 2], ) Multinomial( "M_counts", m, nI_rS1, shape=(nSubj, nRatings), observed=counts[:, nRatings * 2 : nRatings * 3], ) Multinomial( "H_counts", H, nC_rS2, shape=(nSubj, nRatings), observed=counts[:, nRatings * 3 : nRatings * 4], ) if sample_model is True: trace = sample(return_inferencedata=True, **kwargs) return model, trace else: return model
def hmetad_subjectLevel(data, sample_model=True, **kwargs): """Hierachical Bayesian modeling of meta-d' (subject level). This is an internal function. The subject level model must be called using :py:func:`metadPy.hierarchical.hmetad`. Parameters ---------- data : dict Response data. sample_model : boolean If `False`, only the model is returned without sampling. **kwargs : keyword arguments All keyword arguments are passed to `func::pymc3.sampling.sample`. Returns ------- model : :py:class:`pymc3.Model` instance The pymc3 model. Encapsulates the variables and likelihood factors. trace : :py:class:`pymc3.backends.base.MultiTrace` or :py:class:`arviz.InferenceData` A `MultiTrace` or `ArviZ InferenceData` object that contains the samples. References ---------- .. [#] Fleming, S.M. (2017) HMeta-d: hierarchical Bayesian estimation of metacognitive efficiency from confidence ratings, Neuroscience of Consciousness, 3(1) nix007, https://doi.org/10.1093/nc/nix007 """ nRatings = data["nratings"] with Model() as model: # Type 1 priors c1 = Normal("c1", mu=0.0, tau=2) d1 = Normal("d1", mu=0.0, tau=0.5) # TYPE 1 SDT BINOMIAL MODEL h = cumulative_normal(d1 / 2 - c1) f = cumulative_normal(-d1 / 2 - c1) H = Binomial("H", data["S"], h, observed=data["H"]) FA = Binomial("FA", data["N"], f, observed=data["FA"]) # Type 2 priors meta_d = Normal("metad", mu=d1, tau=2) # Specify ordered prior on criteria # bounded above and below by Type 1 c1 cS1_hn = HalfNormal( "cS1_hn", tau=2, shape=nRatings - 1, testval=np.linspace(1.5, 0.5, nRatings - 1), ) cS1 = Deterministic("cS1", -cS1_hn + (c1 - data["Tol"])) cS2_hn = HalfNormal( "cS2_hn", tau=2, shape=nRatings - 1, testval=np.linspace(0.5, 1.5, nRatings - 1), ) cS2 = Deterministic("cS2", cS2_hn + (c1 - data["Tol"])) # Means of SDT distributions S2mu = math.flatten(meta_d / 2, 1) S1mu = math.flatten(-meta_d / 2, 1) # Calculate normalisation constants C_area_rS1 = cumulative_normal(c1 - S1mu) I_area_rS1 = cumulative_normal(c1 - S2mu) C_area_rS2 = 1 - cumulative_normal(c1 - S2mu) I_area_rS2 = 1 - cumulative_normal(c1 - S1mu) # Get nC_rS1 probs nC_rS1 = cumulative_normal(cS1 - S1mu) / C_area_rS1 nC_rS1 = Deterministic( "nC_rS1", math.concatenate( ( [ cumulative_normal(cS1[0] - S1mu) / C_area_rS1, nC_rS1[1:] - nC_rS1[:-1], ( ( cumulative_normal(c1 - S1mu) - cumulative_normal(cS1[(nRatings - 2)] - S1mu) ) / C_area_rS1 ), ] ), axis=0, ), ) # Get nI_rS2 probs nI_rS2 = (1 - cumulative_normal(cS2 - S1mu)) / I_area_rS2 nI_rS2 = Deterministic( "nI_rS2", math.concatenate( ( [ ( (1 - cumulative_normal(c1 - S1mu)) - (1 - cumulative_normal(cS2[0] - S1mu)) ) / I_area_rS2, nI_rS2[:-1] - (1 - cumulative_normal(cS2[1:] - S1mu)) / I_area_rS2, (1 - cumulative_normal(cS2[nRatings - 2] - S1mu)) / I_area_rS2, ] ), axis=0, ), ) # Get nI_rS1 probs nI_rS1 = (-cumulative_normal(cS1 - S2mu)) / I_area_rS1 nI_rS1 = Deterministic( "nI_rS1", math.concatenate( ( [ cumulative_normal(cS1[0] - S2mu) / I_area_rS1, nI_rS1[:-1] + (cumulative_normal(cS1[1:] - S2mu)) / I_area_rS1, ( cumulative_normal(c1 - S2mu) - cumulative_normal(cS1[(nRatings - 2)] - S2mu) ) / I_area_rS1, ] ), axis=0, ), ) # Get nC_rS2 probs nC_rS2 = (1 - cumulative_normal(cS2 - S2mu)) / C_area_rS2 nC_rS2 = Deterministic( "nC_rS2", math.concatenate( ( [ ( (1 - cumulative_normal(c1 - S2mu)) - (1 - cumulative_normal(cS2[0] - S2mu)) ) / C_area_rS2, nC_rS2[:-1] - ((1 - cumulative_normal(cS2[1:] - S2mu)) / C_area_rS2), (1 - cumulative_normal(cS2[nRatings - 2] - S2mu)) / C_area_rS2, ] ), axis=0, ), ) # Avoid underflow of probabilities nC_rS1 = math.switch(nC_rS1 < data["Tol"], data["Tol"], nC_rS1) nI_rS2 = math.switch(nI_rS2 < data["Tol"], data["Tol"], nI_rS2) nI_rS1 = math.switch(nI_rS1 < data["Tol"], data["Tol"], nI_rS1) nC_rS2 = math.switch(nC_rS2 < data["Tol"], data["Tol"], nC_rS2) # TYPE 2 SDT MODEL (META-D) # Multinomial likelihood for response counts ordered as c(nR_S1,nR_S2) Multinomial( "CR_counts", data["CR"], nC_rS1, shape=nRatings, observed=data["counts"][:nRatings], ) Multinomial( "FA_counts", FA, nI_rS2, shape=nRatings, observed=data["counts"][nRatings : nRatings * 2], ) Multinomial( "M_counts", data["M"], nI_rS1, shape=nRatings, observed=data["counts"][nRatings * 2 : nRatings * 3], ) Multinomial( "H_counts", H, nC_rS2, shape=nRatings, observed=data["counts"][nRatings * 3 : nRatings * 4], ) if sample_model is True: trace = sample( trace=[meta_d, cS1, cS2], return_inferencedata=True, **kwargs ) return model, trace else: return model
X['etl_task_time_last7d_sec'] = X['etl_task_time_last7d_sec'].astype('float32') X_INPUT = shared(X.values) # numpy array Y = log(PP[MEASURE]).copy() Y_OUTPUT = shared(Y.values) # numpy array ####################################### # Model definition # MEASURE = exp(ALPHA)*exp(BETA)**log(X) # mu = log(MEASURE) = ALPHA+BETA*log(X) ####################################### with Model() as cost_model: # Priors for unknown cost model parameters ALPHA = Normal('ALPHA', mu=0, sigma=1000) BETA = Normal('BETA', mu=0, sigma=1000, shape=len(ATT)) SIGMA = HalfNormal('SIGMA', sigma=100) # Model MU = ALPHA + dot(X_INPUT, BETA) NU = Deterministic('NU', Exponential('nu_', 1 / 29)) # Likelihood (sampling distribution) of observations # Y_OBS = Normal('Y_OBS', mu=mu, sigma=sigma, observed=Y_OUTPUT) Y_OBS = StudentT('Y_OBS', mu=MU, sigma=SIGMA, observed=Y_OUTPUT, nu=NU) with cost_model: TRACE = sample(SAMPLES, tune=TUNE, cores=6) traceplot(TRACE) with cost_model: Y_PRED = sample_posterior_predictive(TRACE, 1000, cost_model)
def hmetad_rm1way(data: dict, sample_model: bool = True, **kwargs: int): """Compute hierachical meta-d' at the subject level. This is an internal function. The repeated measures model must be called using :py:func:`metadPy.hierarchical.hmetad`. Parameters ---------- data : dict Response data. sample_model : boolean If `False`, only the model is returned without sampling. **kwargs : keyword arguments All keyword arguments are passed to `func::pymc3.sampling.sample`. Returns ------- model : :py:class:`pymc3.Model` instance The pymc3 model. Encapsulates the variables and likelihood factors. trace : :py:class:`pymc3.backends.base.MultiTrace` or :py:class:`arviz.InferenceData` A `MultiTrace` or `ArviZ InferenceData` object that contains the samples. References ---------- .. [#] Fleming, S.M. (2017) HMeta-d: hierarchical Bayesian estimation of metacognitive efficiency from confidence ratings, Neuroscience of Consciousness, 3(1) nix007, https://doi.org/10.1093/nc/nix007 """ nSubj = data["nSubj"] nCond = data["nCond"] nRatings = data["nRatings"] hits = data["hits"].reshape(nSubj, 2) falsealarms = data["falsealarms"].reshape(nSubj, 2) counts = data["counts"] Tol = data["Tol"] cr = data["cr"].reshape(nSubj, 2) m = data["m"].reshape(nSubj, 2) c1 = data["c1"].reshape(nSubj, 2, 1) d1 = data["d1"].reshape(nSubj, 2, 1) with Model() as model: ############# # Hyperpriors ############# mu_c2 = Normal("mu_c2", tau=0.01, shape=(1, ), testval=np.random.rand() * 0.1) sigma_c2 = HalfNormal("sigma_c2", tau=0.01, shape=(1, ), testval=np.random.rand() * 0.1) mu_D = Normal("mu_D", tau=0.001, shape=(1), testval=np.random.rand() * 0.1) sigma_D = HalfNormal("sigma_D", tau=0.1, shape=(1), testval=np.random.rand() * 0.1) mu_Cond1 = Normal("mu_Cond1", mu=0, tau=0.001, shape=(1), testval=np.random.rand() * 0.1) sigma_Cond1 = HalfNormal("sigma_Cond1", tau=0.1, shape=(1), testval=np.random.rand() * 0.1) ############################# # Hyperpriors - Subject level ############################# dbase_tilde = Normal( "dbase_tilde", mu=0, sigma=1, shape=(nSubj, 1, 1), ) dbase = Deterministic("dbase", mu_D + sigma_D * dbase_tilde) Bd_Cond1_tilde = Normal( "Bd_Cond1_tilde", mu=0, sigma=1, shape=(nSubj, 1, 1), ) Bd_Cond1 = Deterministic( "Bd_Cond1", mu_Cond1 + sigma_Cond1 * Bd_Cond1_tilde, ) lambda_logMratio = Gamma( "lambda_logMratio", alpha=0.001, beta=0.001, shape=(nSubj, 1, 1), ) sigma_logMratio = Deterministic("sigma_logMratio", 1 / math.sqrt(lambda_logMratio)) ############################### # Hypterprior - Condition level ############################### mu_regression = [dbase + (Bd_Cond1 * c) for c in range(nCond)] log_mRatio_tilde = Normal("log_mRatio_tilde", mu=0, sigma=1, shape=(nSubj, 1, 1)) log_mRatio = Deterministic( "log_mRatio", tt.stack(mu_regression, axis=1)[:, :, :, 0] + tt.tile(log_mRatio_tilde, (1, 2, 1)) * tt.tile(sigma_logMratio, (1, 2, 1)), ) mRatio = Deterministic("mRatio", tt.exp(log_mRatio)) # Means of SDT distributions metad = Deterministic("metad", mRatio * d1) S2mu = Deterministic("S2mu", metad / 2) S1mu = Deterministic("S1mu", -metad / 2) # TYPE 2 SDT MODEL (META-D) # Multinomial likelihood for response counts # Specify ordered prior on criteria # bounded above and below by Type 1 c cS1_hn = Normal( "cS1_hn", mu=0, sigma=1, shape=(nSubj, nCond, nRatings - 1), testval=np.linspace(-1.5, -0.5, nRatings - 1).reshape( 1, 1, nRatings - 1).repeat(nSubj, axis=0).repeat(nCond, axis=1), ) cS1 = Deterministic("cS1", -mu_c2 + (cS1_hn * sigma_c2)) cS2_hn = Normal( "cS2_hn", mu=0, sigma=1, shape=(nSubj, nCond, nRatings - 1), testval=np.linspace(0.5, 1.5, nRatings - 1).reshape( 1, 1, nRatings - 1).repeat(nSubj, axis=0).repeat(nCond, axis=1), ) cS2 = Deterministic("cS2", mu_c2 + (cS2_hn * sigma_c2)) # Calculate normalisation constants C_area_rS1 = cumulative_normal(c1 - S1mu) I_area_rS1 = cumulative_normal(c1 - S2mu) C_area_rS2 = 1 - cumulative_normal(c1 - S2mu) I_area_rS2 = 1 - cumulative_normal(c1 - S1mu) # Get nC_rS1 probs nC_rS1 = cumulative_normal(cS1 - S1mu) / C_area_rS1 nC_rS1 = Deterministic( "nC_rS1", math.concatenate( ([ cumulative_normal(cS1[:, :, 0].reshape((nSubj, 2, 1)) - S1mu) / C_area_rS1, nC_rS1[:, :, 1:] - nC_rS1[:, :, :-1], ((cumulative_normal(c1 - S1mu) - cumulative_normal(cS1[:, :, (nRatings - 2)].reshape( (nSubj, 2, 1)) - S1mu)) / C_area_rS1), ]), axis=2, ), ) # Get nI_rS2 probs nI_rS2 = (1 - cumulative_normal(cS2 - S1mu)) / I_area_rS2 nI_rS2 = Deterministic( "nI_rS2", math.concatenate( ([ ((1 - cumulative_normal(c1 - S1mu)) - (1 - cumulative_normal(cS2[:, :, 0].reshape( (nSubj, nCond, 1)) - S1mu))) / I_area_rS2, nI_rS2[:, :, :-1] - (1 - cumulative_normal(cS2[:, :, 1:] - S1mu)) / I_area_rS2, (1 - cumulative_normal(cS2[:, :, nRatings - 2].reshape( (nSubj, nCond, 1)) - S1mu)) / I_area_rS2, ]), axis=2, ), ) # Get nI_rS1 probs nI_rS1 = (-cumulative_normal(cS1 - S2mu)) / I_area_rS1 nI_rS1 = Deterministic( "nI_rS1", math.concatenate( ([ cumulative_normal(cS1[:, :, 0].reshape((nSubj, nCond, 1)) - S2mu) / I_area_rS1, nI_rS1[:, :, :-1] + (cumulative_normal(cS1[:, :, 1:] - S2mu)) / I_area_rS1, (cumulative_normal(c1 - S2mu) - cumulative_normal(cS1[:, :, nRatings - 2].reshape( (nSubj, nCond, 1)) - S2mu)) / I_area_rS1, ]), axis=2, ), ) # Get nC_rS2 probs nC_rS2 = (1 - cumulative_normal(cS2 - S2mu)) / C_area_rS2 nC_rS2 = Deterministic( "nC_rS2", math.concatenate( ([ ((1 - cumulative_normal(c1 - S2mu)) - (1 - cumulative_normal(cS2[:, :, 0].reshape( (nSubj, nCond, 1)) - S2mu))) / C_area_rS2, nC_rS2[:, :, :-1] - ((1 - cumulative_normal(cS2[:, :, 1:] - S2mu)) / C_area_rS2), (1 - cumulative_normal(cS2[:, :, nRatings - 2].reshape( (nSubj, nCond, 1)) - S2mu)) / C_area_rS2, ]), axis=2, ), ) # Avoid underflow of probabilities nC_rS1 = math.switch(nC_rS1 < Tol, Tol, nC_rS1) nI_rS2 = math.switch(nI_rS2 < Tol, Tol, nI_rS2) nI_rS1 = math.switch(nI_rS1 < Tol, Tol, nI_rS1) nC_rS2 = math.switch(nC_rS2 < Tol, Tol, nC_rS2) for c in range(nCond): Multinomial( f"CR_counts_{c}", n=cr[:, c], p=nC_rS1[:, c, :], observed=counts[:, c, :nRatings], shape=(nSubj, nRatings), ) Multinomial( f"H_counts_{c}", n=hits[:, c], p=nC_rS2[:, c, :], observed=counts[:, c, nRatings * 3:nRatings * 4], shape=(nSubj, nRatings), ) Multinomial( f"FA_counts_{c}", n=falsealarms[:, c], p=nI_rS2[:, c, :], observed=counts[:, c, nRatings:nRatings * 2], shape=(nSubj, nRatings), ) Multinomial( f"M_counts_{c}", n=m[:, c], p=nI_rS1[:, c, :], observed=counts[:, c, nRatings * 2:nRatings * 3], shape=(nSubj, nRatings), ) if sample_model is True: trace = sample(return_inferencedata=True, **kwargs) return model, trace else: return model
import theano.tensor as tt df = pd.read_csv("data/data-conc-2.txt") df = df.drop("PersonID", axis=1) Y_nt = df.values n_patient = Y_nt.shape[0] times = np.reshape(np.array([1, 2, 4, 8, 12, 24]), (1, -1)) basic_model = Model() with basic_model: #全体平均 a_0 = Normal('a_0', mu=0, sd=10) b_0 = Normal('b_0', mu=0, sd=10) #全体分散 s_ag = HalfNormal('sigma_a', sd=10) s_bg = HalfNormal('sigma_b', sd=10) #個人パラメータ a = Lognormal('a', mu=a_0, sd=s_ag, shape=[n_patient, 1]) b = Lognormal('b', mu=b_0, sd=s_bg, shape=[n_patient, 1]) #個人分散 s_Y = HalfNormal('sigma_Y', sd=10) #likelihood mu = 1 - tt.exp(-tt.dot(b, times)) mu = a * mu Y_obs = Normal('Y_obs', mu=mu, sd=s_Y, observed=Y_nt) #サンプリング
import math df = pd.read_csv("../data/data-changepoint.txt") T = df.values[:100,0].astype(np.float32) Y = df.values[:100,1].astype(np.float32) n_times = 100 #n_times = len(df["X"].unique()) #時間の数 basic_model = Model() #subtensorの使い方↓ #http://deeplearning.net/software/theano/library/tensor/basic.html with basic_model: #事前分布 #コーシー分布は逆関数法にて乱数生成 s_mu = HalfNormal('s_mu', sd=1) #コーシー分布の分散 s_Y = HalfNormal('s_Y', sd=1) #観測誤差 mu_0 = Normal('mu_0',mu=0, sd=1) #初期状態 x = Uniform("x" ,lower=-math.pi/2,upper=math.pi/2, shape=n_times-1) #Cauchyの誤差process c = tt.dot(s_mu,tt.tan(x)) #状態process mu = tt.zeros((n_times)) mu = tt.set_subtensor(mu[0], mu_0) for i in range(n_times-1): mu = tt.set_subtensor(mu[i+1], mu[i]+c[i]) #likelihood Y_obs = Normal('Y_obs', mu=mu, sd=s_Y, observed=Y)
#print X_obs print C print U plt.plot(X_obs[:], np.ones(X_obs.shape), 'o', markersize=8) plt.show() # Infer class labels from pymc3 import Dirichlet, Normal, MvNormal, HalfNormal, Categorical import theano.tensor with Model() as gmm: C = Dirichlet('mixture_coeff', dirichlet_scale * dirichlet_shape, shape=nclusters) S = HalfNormal('S', sd=sd_halfnormal, shape=nclusters) U = Normal('mu', mu=mean_prior_mean, sd=mean_prior_sd, shape=nclusters) Y = Categorical('labels', p=C, shape=nsamples) X = Normal('X', mu=U[Y], sd=S[Y], observed=X_obs) from pymc3 import find_MAP map_estimate = find_MAP(model=gmm) print map_estimate from pymc3 import NUTS, sample, Slice, Metropolis, ElemwiseCategorical, HamiltonianMC modified_map_estimate = copy.deepcopy(map_estimate) modified_map_estimate['mu'] = [ 1 if x < 0.001 else x for x in modified_map_estimate['mu'] ]
cluster_data = df.values[:, 3] - 1 n_company = len(df["KID"].unique()) n_cluster = len(df["GID"].unique()) basic_model = Model() #shape = クラスの数の確率変数に、クラスの値を取るデータ数次元のベクトルを入れる操作がありますが #その詳細な説明は(https://pymc-devs.github.io/pymc3/notebooks/GLM-hierarchical.html)参照 #model8-5に業界毎の分散を追加 with basic_model: #全体平均 a_0 = Normal('a_0', mu=0, sd=10) b_0 = Normal('b_0', mu=0, sd=10) #全体分散 s_ag = HalfNormal('s_ag', sd=100) s_bg = HalfNormal('s_bg', sd=100) #業界平均 a_g = Normal('a_g', mu=a_0, sd=s_ag, shape=n_cluster) b_g = Normal('b_g', mu=b_0, sd=s_bg, shape=n_cluster) #業界毎の誤差分散 s_a = HalfNormal('sigma_a', sd=100, shape=n_cluster) s_a = s_a[cluster_data] s_b = HalfNormal('sigma_b', sd=100, shape=n_cluster) s_b = s_b[cluster_data] #個人の誤差分散 s_Y = HalfNormal('sigma_Y', sd=100) #likelihood #b = Normal('b', mu=b_g[cluster_data], sd=s_b)とするとエラーがでる