def get_model_nll(params): """ Computes the negative log likelihood of the global data set given the parameters of the aDDM. Args: params: list containing the 3 model parameters, in the following order: d, theta, sigma. Returns: The negative log likelihood for the global data set and the given model. """ d = params[0] theta = params[1] sigma = params[2] logLikelihood = 0 subjects = choice.keys() for subject in subjects: trials = choice[subject].keys() trialSet = np.random.choice(trials, trialsPerSubject, replace=False) for trial in trialSet: try: likelihood = get_trial_likelihood( choice[subject][trial], valueLeft[subject][trial], valueRight[subject][trial], fixItem[subject][trial], fixTime[subject][trial], d, theta, sigma=sigma) except: print("An exception occurred during the likelihood computation " "for subject " + subject + ", trial " + str(trial) + ".") raise if likelihood != 0: logLikelihood += np.log(likelihood) print("NLL for " + str(params) + ": " + str(-logLikelihood)) return -logLikelihood
def get_trial_likelihood_wrapper(params): """ Wrapper for addm.get_trial_likelihood() which takes a single argument. Intended for parallel computation using a thread pool. Args: params: tuple consisting of all arguments required by addm.get_trial_likelihood(). Returns: The output of addm.get_trial_likelihood(). """ return get_trial_likelihood(*params)
def evaluate(individual): """ Computes the negative log likelihood of the global data set given the parameters of the aDDM. Args: individual: list containing the 3 model parameters, in the following order: d, theta, sigma. Returns: A list containing the negative log likelihood for the global data set and the given model. """ d = individual[0] theta = individual[1] sigma = individual[2] logLikelihood = 0 subjects = choice.keys() for subject in subjects: trials = choice[subject].keys() trialSet = np.random.choice(trials, trialsPerSubject, replace=False) for trial in trialSet: try: likelihood = get_trial_likelihood(choice[subject][trial], valueLeft[subject][trial], valueRight[subject][trial], fixItem[subject][trial], fixTime[subject][trial], d, theta, sigma=sigma, plotResults=False) except: print( "An exception occurred during the likelihood computation " "for subject " + subject + ", trial " + str(trial) + ".") raise if likelihood != 0: logLikelihood += np.log(likelihood) print("NLL for " + str(individual) + ": " + str(-logLikelihood)) return -logLikelihood,
def get_model_nll(choice, valueLeft, valueRight, fixItem, fixTime, d, theta, sigma, trialsPerSubject=100, useOddTrials=True, useEvenTrials=True, isCisTrial=None, isTransTrial=None, useCisTrials=True, useTransTrials=True, verbose=True): """ Computes the negative log likelihood of a data set given the parameters of the aDDM. Args: choice: dict of dicts, indexed first by subject then by trial number. Each entry is an integer corresponding to the decision made in that trial. valueLeft: dict of dicts with same indexing as choice. Each entry is an integer corresponding to the value of the left item. valueRight: dict of dicts with same indexing as choice. Each entry is an integer corresponding to the value of the right item. fixItem: dict of dicts with same indexing as choice. Each entry is an ordered list of fixated items in the trial. fixTime: dict of dicts with same indexing as choice. Each entry is an ordered list of fixation durations in the trial. d: float, parameter of the model which controls the speed of integration of the signal. theta: float between 0 and 1, parameter of the model which controls the attentional bias. sigma: float, parameter of the model, standard deviation for the normal distribution. trialsPerSubject: integer, number of trials to be used from each subject. If smaller than one, all trials will be used. useOddTrials: boolean, whether or not to use odd trials in the analysis. useEvenTrials: boolean, whether or not to use even trials in the analysis. isCisTrial: dict of dicts, indexed first by subject then by trial number. Each entry is a boolean indicating if the trial is cis (both bars on the same side of the target). isTransTrial: dict of dicts with same indexing as isCisTrial. Each entry is a boolean indicating if the trial is trans (bars on either side of the target). useCisTrials: boolean, whether or not to use cis trials in the analysis. useTransTrials: boolean, whether or not to use trans trials in the analysis. verbose: boolean, whether or not to print updates during computation. Returns: The negative log likelihood for the given data set and model. """ logLikelihood = 0 subjects = choice.keys() for subject in subjects: trials = choice[subject].keys() if trialsPerSubject < 1: trialsPerSubject = len(trials) if useEvenTrials and useOddTrials: if useCisTrials and useTransTrials: trialSet = np.random.choice(trials, trialsPerSubject, replace=False) elif useCisTrials and not useTransTrials: trialSet = np.random.choice( [trial for trial in trials if isCisTrial[subject][trial]], trialsPerSubject, replace=False) elif not useCisTrials and useTransTrials: trialSet = np.random.choice( [trial for trial in trials if isTransTrial[subject][trial]], trialsPerSubject, replace=False) else: return 0 elif useEvenTrials and not useOddTrials: if useCisTrials and useTransTrials: trialSet = np.random.choice( [trial for trial in trials if not trial % 2], trialsPerSubject, replace=False) elif useCisTrials and not useTransTrials: trialSet = np.random.choice( [trial for trial in trials if not trial % 2 and isCisTrial[subject][trial]], trialsPerSubject, replace=False) elif not useCisTrials and useTransTrials: trialSet = np.random.choice( [trial for trial in trials if not trial % 2 and isTransTrial[subject][trial]], trialsPerSubject, replace=False) else: return 0 elif not useEvenTrials and useOddTrials: if useCisTrials and useTransTrials: trialSet = np.random.choice( [trial for trial in trials if trial % 2], trialsPerSubject, replace=False) elif useCisTrials and not useTransTrials: trialSet = np.random.choice( [trial for trial in trials if trial % 2 and isCisTrial[subject][trial]], trialsPerSubject, replace=False) elif not useCisTrials and useTransTrials: trialSet = np.random.choice( [trial for trial in trials if trial % 2 and isTransTrial[subject][trial]], trialsPerSubject, replace=False) else: return 0 else: return 0 for trial in trialSet: try: likelihood = get_trial_likelihood( choice[subject][trial], valueLeft[subject][trial], valueRight[subject][trial], fixItem[subject][trial], fixTime[subject][trial], d, theta, sigma=sigma) except: print("An exception occurred during the likelihood computation " "for subject " + subject + ", trial " + str(trial) + ".") raise if likelihood != 0: logLikelihood += np.log(likelihood) if verbose: print("NLL for " + str(d) + ", " + str(theta) + ", " + str(sigma) + ": " + str(-logLikelihood)) return -logLikelihood