def ruqaxik_pymc(ri, rnjml): qupinïk = pm.Normal(name='ruqupinïk_' + str(ri), mu=[-1, 0, 1], sd=10, shape=ri.ruchlnl - 1, transform=pm.distributions.transforms.ordered) return pm.OrderedLogistic(name=str(ri), cutpoints=qupinïk, eta=rnjml, observed=ri.tzij)
def graded_response_model(dataset, n_categories): """Defines the mcmc model for the graded response model. Args: dataset: [n_items, n_participants] 2d array of measured responses n_categories: number of polytomous values (i.e. Number of Likert Levels) Returns: model: PyMC3 model to run """ n_items, n_people = dataset.shape n_levels = n_categories - 1 # Need small deviation in offset to # fit into pymc framework mu_value = linspace(-0.1, 0.1, n_levels) # Run through 0, K - 1 observed = dataset - dataset.min() graded_mcmc_model = pm.Model() with graded_mcmc_model: # Ability Parameters ability = pm.Normal("Ability", mu=0, sigma=1, shape=n_people) # Discrimination multilevel prior rayleigh_scale = pm.Lognormal("Rayleigh_Scale", mu=0, sigma=1 / 4, shape=1) discrimination = pm.Bound(Rayleigh, lower=0.25)(name='Discrimination', beta=rayleigh_scale, offset=0.25, shape=n_items) # Threshold multilevel prior sigma_difficulty = pm.HalfNormal('Difficulty_SD', sigma=1, shape=1) for ndx in range(n_items): thresholds = pm.Normal( f"Thresholds{ndx}", mu=mu_value, sigma=sigma_difficulty, shape=n_levels, transform=pm.distributions.transforms.ordered) # Compute the log likelihood kernel = discrimination[ndx] * ability probabilities = pm.OrderedLogistic(f'Log_Likelihood{ndx}', cutpoints=thresholds, eta=kernel, observed=observed[ndx]) return graded_mcmc_model
def update_model(batsmen, bowlers, id1, id2, X, trace): testval = [[-5, 0, 1, 2, 3.5, 5] for i in range(0, 9)] l = [i for i in range(9)] model = pm.Model() with model: delta_1 = from_posterior("delta_1", trace["delta_1"]) delta_2 = from_posterior("delta_2", trace["delta_2"]) # inv_sigma_sqr = from_posterior("sigma^-2", trace["sigma^-2"]) # inv_tau_sqr = from_posterior("tau^-2", trace["tau^-2"]) mu_1 = from_posterior("mu_1", trace["mu_1"]) mu_2 = from_posterior("mu_2", trace["mu_2"]) delta = pm.math.ge(l, 3) * delta_1 + pm.math.ge(l, 6) * delta_2 eta = [ pm.Deterministic("eta_" + str(i), delta[i] + mu_1[id1[i]] - mu_2[id2[i]]) for i in range(9) ] cutpoints = from_posterior("cutpoints", trace["cutpoints"]) X_ = [ pm.OrderedLogistic("X_" + str(i), cutpoints=cutpoints[i], eta=eta[i], observed=X[i] - 1) for i in range(9) ] """ delta_2 = pm.Uniform("delta_2", lower=0, upper=1) inv_sigma_sqr = pm.Gamma("sigma^-2", alpha=1.0, beta=1.0) inv_tau_sqr = pm.Gamma("tau^-2", alpha=1.0, beta=1.0) mu_1 = pm.Normal("mu_1", mu=0, sigma=1/pm.math.sqrt(inv_tau_sqr), shape=len(batsmen)) mu_2 = pm.Normal("mu_2", mu=0, sigma=1/pm.math.sqrt(inv_tau_sqr), shape=len(bowlers)) delta = pm.math.ge(l, 3) * delta_1 + pm.math.ge(l, 6) * delta_2 eta = [pm.Deterministic("eta_" + str(i), delta[i] + mu_1[id1[i]] - mu_2[id2[i]]) for i in range(9)] cutpoints = pm.Normal("cutpoints", mu=0, sigma=1/pm.math.sqrt(inv_sigma_sqr), transform=pm.distributions.transforms.ordered, shape=(9,6), testval=testval) X_ = [pm.OrderedLogistic("X_" + str(i), cutpoints=cutpoints[i], eta=eta[i], observed=X[i]-1) for i in range(9)] """ return model
with pm.Model() as model: mu_beta = pm.Normal('mu_beta', mu=0, sd=2) sd_beta = pm.HalfNormal('sd', sd=sigma_prior) beta = pm.Laplace('beta', mu=mu_beta, b=sd_beta, shape=p) cutpoints = pm.Normal("cutpoints", mu=[-0.001, 0], sd=20, shape=2, transform=pm.distributions.transforms.ordered) lp = pm.Deterministic('lp', pm.math.dot(X_shared, beta)) y_obs = pm.OrderedLogistic("y_obs", eta=lp, cutpoints=cutpoints, observed=y_shared - 1) trace = pm.sample(samples=20000) # In[4]: ## --------------------------------------------------------- ## predict for training data ## --------------------------------------------------------- post_pred_train = pm.sample_ppc(trace, samples=5000, model=model) # exctract predictions y_pred_train_df = post_pred_train['y_obs']
ax.set_xlabel("response", fontsize=14) ax.set_ylabel("log-cumulative-odds", fontsize=14) # %% with pm.Model() as m11_1: a = pm.Normal( "a", 0.0, 10.0, transform=pm.distributions.transforms.ordered, shape=6, testval=np.arange(6) - 2.5, ) resp_obs = pm.OrderedLogistic( "resp_obs", 0.0, a, observed=trolley_df.response.values - 1 ) # %% with m11_1: map_11_1 = pm.find_MAP() # %% map_11_1["a"] # %% sp.special.expit(map_11_1["a"]) # %% with m11_1: trace_11_1 = pm.sample(1000, tune=1000)
def multidimensional_graded_model(dataset, n_categories, n_factors): """Defines the mcmc model for the multidimensional graded response model. Args: dataset: [n_items, n_participants] 2d array of measured responses n_categories: (int) number of polytomous values (i.e. Number of Likert Levels) n_factors: (int) number of factors to extract Returns: model: PyMC3 model to run """ if n_factors < 2: raise AssertionError(f"Multidimensional GRM model requires " f"two or more factors specified!") n_items, n_people = dataset.shape n_levels = n_categories - 1 # Need small deviation in offset to # fit into pymc framework mu_value = linspace(-0.1, 0.1, n_levels) # Run through 0, K - 1 observed = dataset - dataset.min() diagonal_indices, lower_indices = get_discrimination_indices( n_items, n_factors) lower_length = lower_indices[0].shape[0] graded_mcmc_model = pm.Model() with graded_mcmc_model: # Ability Parameters ability = pm.Normal("Ability", mu=0, sigma=1, shape=(n_factors, n_people)) # Multidimensional Discrimination discrimination = tt.zeros((n_items, n_factors), dtype=theano.config.floatX) diagonal_discrimination = pm.Lognormal('Diagonal Discrimination', mu=0, sigma=0.25, shape=n_factors) lower_discrimination = pm.Normal('Lower Discrimination', sigma=1, shape=lower_length) discrimination = tt.set_subtensor(discrimination[diagonal_indices], diagonal_discrimination) discrimination = tt.set_subtensor(discrimination[lower_indices], lower_discrimination) # Threshold multilevel prior sigma_difficulty = pm.HalfNormal('Difficulty_SD', sigma=1, shape=1) for ndx in range(n_items): thresholds = pm.Normal( f"Thresholds{ndx}", mu=mu_value, sigma=sigma_difficulty, shape=n_levels, transform=pm.distributions.transforms.ordered) # Compute the log likelihood kernel = pm.math.dot(discrimination[ndx], ability) probabilities = pm.OrderedLogistic(f'Log_Likelihood{ndx}', cutpoints=thresholds, eta=kernel, observed=observed[ndx]) return graded_mcmc_model