<jupyter_start><jupyter_text>z & v modelRerun model using HDDM regression - but with the least number of parameters<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import hddm
import numpy as np
import patsy 

samples = 20000

data = hddm.load_csv('../data/DDM_data.csv')
def z_link_func(x, data=data):
    return 1 / (1 + np.exp(-(x)))
v_reg = {'model': 'v ~ 1 + condition + stim', 'link_func': lambda x: x}
z_reg = {'model': 'z ~ 1 + condition', 'link_func': z_link_func}

reg_descr = [v_reg, z_reg]

simpleFull = hddm.models.HDDMRegressor(data, reg_descr,
                                       bias=True, p_outlier=0.05,
                                       group_only_regressors=False)

simpleFull.sample(samples, burn=samples/10, thin=2)
print "Full Model DIC: %f" % simpleFull.dic<jupyter_output><empty_output><jupyter_text>Extract model parameters<jupyter_code>stats = simpleFull.gen_stats()
stats.to_csv('../data/model_outputs/subject_parms/simpleFull_parms.csv')
vc, zc = simpleFull.nodes_db.node[['v_condition', 'z_condition']]
allParms = vc.trace()
allParms = np.column_stack([allParms,zc.trace()])

np.savetxt("../data/model_outputs/trace_processed/simpleFull_trace.csv", allParms , delimiter=",")
# Save all parameters
a, t = simpleFull.nodes_db.node[['a', 't']]

z_m,z_0 = simpleFull.nodes_db.node[['z_condition', 'z_Intercept']] 

v_m,v_s,v_0 = simpleFull.nodes_db.node[['v_condition', 'v_stim', 'v_Intercept']] 

allParms = a.trace()
allParms = np.column_stack([allParms,t.trace()])
allParms = np.column_stack([allParms,z_m.trace(),z_0.trace()])
allParms = np.column_stack([allParms,v_m.trace(),v_s.trace(),v_0.trace()])

np.savetxt("../data/model_outputs/trace_processed/simpleFull_trace_all.csv", allParms , delimiter=",")
# ppc_data = hddm.utils.post_pred_gen(simpleFull,append_data=True)
ppc_data = post_pred_gen(simpleFull, append_data=True)
np.savetxt("../data/model_outputs/ppc/simpleFull_ppc.csv", ppc_data, delimiter=",")<jupyter_output><empty_output><jupyter_text>v model<jupyter_code>v_reg = {'model': 'v ~ 1 + condition + stim', 'link_func': lambda x: x}
z_reg = {'model': 'z ~ 1', 'link_func': z_link_func}

reg_descr = [v_reg, z_reg]

simpleDrift = hddm.models.HDDMRegressor(data, reg_descr,
                                       bias=True, p_outlier=0.05,
                                       group_only_regressors=False)

simpleDrift.sample(samples, burn=samples/10, thin=2)
stats = simpleDrift.gen_stats()
stats.to_csv('../data/model_outputs/subject_parms/simpleDrift_parms.csv')
print "Drift Model DIC: %f" % simpleDrift.dic
# Save all parameters
a, t = simpleDrift.nodes_db.node[['a', 't']]

z_0 = simpleDrift.nodes_db.node['z_Intercept']

v_m,v_s,v_0 = simpleDrift.nodes_db.node[['v_condition', 'v_stim','v_Intercept']] 

allParms = a.trace()
allParms = np.column_stack([allParms,t.trace()])
allParms = np.column_stack([allParms,z_0.trace()])
allParms = np.column_stack([allParms,v_m.trace(),v_s.trace(),v_0.trace()])

np.savetxt("../data/model_outputs/trace_processed/simpleDrift_trace_all.csv", allParms , delimiter=",")
ppc_data = post_pred_gen(simpleDrift, append_data=True)
np.savetxt("../data/model_outputs/ppc/simpleDrift_ppc.csv", ppc_data, delimiter=",")<jupyter_output><empty_output><jupyter_text>z model<jupyter_code>v_reg = {'model': 'v ~ 1 + stim', 'link_func': lambda x: x}
z_reg = {'model': 'z ~ 1 + condition', 'link_func': z_link_func}

reg_descr = [v_reg, z_reg]

simpleBias = hddm.models.HDDMRegressor(data, reg_descr,
                                       bias=True, p_outlier=0.05,
                                       group_only_regressors=False)

simpleBias.sample(samples, burn=samples/10, thin=2)
stats = simpleBias.gen_stats()
stats.to_csv('../data/model_outputs/subject_parms/simpleBias_parms.csv')
print "Bias Model DIC: %f" % simpleBias.dic
# Save all parameters
a, t = simpleBias.nodes_db.node[['a', 't']]

z_m,z_0 = simpleBias.nodes_db.node[['z_condition', 'z_Intercept']] 

v_s,v_0 = simpleBias.nodes_db.node[['v_stim','v_Intercept']] 

allParms = a.trace()
allParms = np.column_stack([allParms,t.trace()])
allParms = np.column_stack([allParms,z_m.trace(),z_0.trace()])
allParms = np.column_stack([allParms,v_s.trace(),v_0.trace()])

np.savetxt("../data/model_outputs/trace_processed/simpleBias_trace_all.csv", allParms , delimiter=",")
ppc_data = post_pred_gen(simpleBias, append_data=True)
np.savetxt("../data/model_outputs/ppc/simpleBias_ppc.csv", ppc_data, delimiter=",")<jupyter_output><empty_output><jupyter_text>Null Model<jupyter_code>v_reg = {'model': 'v ~ 1 + stim', 'link_func': lambda x: x}
z_reg = {'model': 'z ~ 1', 'link_func': z_link_func}

reg_descr = [v_reg, z_reg]

simpleNull = hddm.models.HDDMRegressor(data, reg_descr,
                                       bias=True, p_outlier=0.05,
                                       group_only_regressors=False)

simpleNull.sample(samples, burn=samples/10, thin=2)
stats = simpleNull.gen_stats()
stats.to_csv('../data/model_outputs/subject_parms/simpleNull_parms.csv')
print "Null Model DIC: %f" % simpleNull.dic
# Save all parameters
a, t = simpleNull.nodes_db.node[['a', 't']]

z_0 = simpleNull.nodes_db.node['z_Intercept']

v_s,v_0 = simpleNull.nodes_db.node[['v_stim','v_Intercept']] 

allParms = a.trace()
allParms = np.column_stack([allParms,t.trace()])
allParms = np.column_stack([allParms,z_0.trace()])
allParms = np.column_stack([allParms,v_s.trace(),v_0.trace()])

np.savetxt("../data/model_outputs/trace_processed/simpleNull_trace_all.csv", allParms , delimiter=",")
ppc_data = post_pred_gen(simpleNull, append_data=True)
np.savetxt("../data/model_outputs/ppc/simpleNull_ppc.csv", ppc_data, delimiter=",")<jupyter_output><empty_output><jupyter_text>Patch to run PPC<jupyter_code>import pymc as pm
import numpy as np
import pymc.progressbar as pbar

def _parents_to_random_posterior_sample(bottom_node, pos=None):
    """Walks through parents and sets them to pos sample."""
    for i, parent in enumerate(bottom_node.extended_parents):
        if not isinstance(parent, pm.Node): # Skip non-stochastic nodes
            continue

        if pos is None:
            # Set to random posterior position
            pos = np.random.randint(0, len(parent.trace()))

        assert len(parent.trace()) >= pos, "pos larger than posterior sample size"
        parent.value = parent.trace()[pos]

def _post_pred_generate(bottom_node, samples=500, data=None, append_data=True):
    """Generate posterior predictive data from a single observed node."""
    datasets = []
    ##############################
    # Sample and generate stats
    for sample in range(samples):
        _parents_to_random_posterior_sample(bottom_node)
        # Generate data from bottom node
        sampled_data = bottom_node.random()
        if append_data and data is not None:
            sampled_data.reset_index(inplace=True)  # Only modification of original Kabuki code
            sampled_data = sampled_data.join(data.reset_index(), lsuffix='_sampled')
        datasets.append(sampled_data)
    return datasets

def post_pred_gen(model, groupby=None, samples=500, append_data=False, progress_bar=True):
    results = {}

    # Progress bar
    if progress_bar:
        n_iter = len(model.get_observeds())
        bar = pbar.progress_bar(n_iter)
        bar_iter = 0
    else:
        print("Sampling...")

    if groupby is None:
        iter_data = ((name, model.data.ix[obs['node'].value.index]) for name, obs in model.iter_observeds())
    else:
        iter_data = model.data.groupby(groupby)

    for name, data in iter_data:
        node = model.get_data_nodes(data.index)

        if progress_bar:
            bar_iter += 1
            bar.update(bar_iter)

        if node is None or not hasattr(node, 'random'):
            continue # Skip

        ##############################
        # Sample and generate stats
        datasets = _post_pred_generate(node, samples=samples, data=data, append_data=append_data)
        results[name] = pd.concat(datasets, names=['sample'], keys=list(range(len(datasets))))

    if progress_bar:
        bar_iter += 1
        bar.update(bar_iter)

    return pd.concat(results, names=['node'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14819775937652216, 'fraction_numerical': 0.007915245981490501, 'mean_word_length': 4.136335209505941, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '110412', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 3046, 'n_tokens_neox': 2948, 'n_words': 509, 'path': 'scripts/HDDM.ipynb', 'repo_name': 'ycleong/MotivatedPerception', 'size': '8212'}