Пример #1
0
def simulator(n, numTrials):
	modelLikelihoods = dict()
	posteriors = dict()

	actualD = random.uniform(dFloat[0], dFloat[parameterGridDimension - 1])
	actualTheta = np.random.uniform(thetaFloat[0], thetaFloat[parameterGridDimension - 1])
	actualSigma = np.random.uniform(sigmaFloat[0], sigmaFloat[parameterGridDimension - 1])
	trueD.append(actualD)
	trueTheta.append(actualTheta)
	trueSigma.append(actualSigma)

	numModels = len(parameters)
  	for perm in parameters:
  		modelLikelihoods[tuple(perm)] = 0
  		posteriors[tuple(perm)] = 1.0 / numModels
	trialConditions = []
	for i in xrange(numTrials):
		value_list = []
		for i in xrange(nItems):
			itemValue = random.choice(potential_values)
			value_list.append(itemValue)
		trialConditions.append(value_list)

	# Run the simulation using known parameter values 
	RT, choice, fixItem, fixTime, fixRDV, uninterruptedLastFixTime, lst_of_item_values_dict = \
		run_simulations(nItems, numTrials, trialConditions, actualD, actualTheta, actualSigma)
  	
  	print("Computing Model " + str(n + 1) + ":" + str((actualD, actualTheta, actualSigma)))

	for trial in xrange(numTrials):
		print(trial)
		trialChoice = choice[trial]
		leftValue = lst_of_item_values_dict[0][trial]
		rightValue = lst_of_item_values_dict[1][trial]
		itemFixation = fixItem[trial]
		timeOfFixations = fixTime[trial]

		for perm in parameters:
			trialdScaled, trialthetaScaled, trialsigmaScaled = tuple(perm)
			triald = trialdScaled / 1000.
			trialtheta = trialthetaScaled / 1000.
			trialsigma = trialsigmaScaled / 1000.
			likelihood = get_trial_likelihood(trialChoice, leftValue, rightValue, itemFixation, timeOfFixations, \
				triald, trialtheta, trialsigma)
			modelLikelihoods[tuple(perm)] = likelihood
		 
		# Get the denominator for normalizing the posteriors.
		denominator = 0
		for perm in parameters:
			denominator += posteriors[tuple(perm)] * modelLikelihoods[tuple(perm)]
		if denominator == 0:
			continue

		# Calculate the posteriors after this trial.
		for perm in parameters:
			prior = posteriors[tuple(perm)]
			posteriors[tuple(perm)] = modelLikelihoods[tuple(perm)] * prior / denominator
	optimalValues = max(posteriors.iteritems(), key=operator.itemgetter(1))[0]
	optimald, optimaltheta, optimalsigma = optimalValues
	return optimalValues
Пример #2
0
def main():
  # Parameters 
  parameters = np.vstack(np.meshgrid(d, theta, sigma)).reshape(3,-1).T
  
  numModels = len(parameters)
  for perm in parameters:
     modelLikelihoods[tuple(perm)] = 0
     # We will assume a uniform prior over all models.
     posteriors[tuple(perm)] = 1.0 / numModels

  # Creates the item values to be used in the simulation
  trialConditions = []
  for i in range(numTrials):
        value_list = []
        for i in range(nItems):
             itemValue = random.choice(potential_values)
             value_list.append(itemValue)
        trialConditions.append(value_list)

  # Run the simulation using known parameter values 
  RT, choice, fixItem, fixTime, fixRDV, uninterruptedLastFixTime, lst_of_item_values_dict = \
     run_simulations(nItems, numTrials, trialConditions, actuald, actualTheta, actualSigma)
  
  for trial in xrange(numTrials):
     print(trial)
     trialChoice = choice[trial]
     leftValue = lst_of_item_values_dict[0][trial]
     rightValue = lst_of_item_values_dict[1][trial]
     itemFixation = fixItem[trial]
     timeOfFixations = fixTime[trial]

     den = 0
     for perm in parameters:
        triald, trialtheta, trialsigma = tuple(perm)
        likelihood = get_trial_likelihood(trialChoice, leftValue, rightValue, itemFixation, timeOfFixations,
                                 triald, trialtheta, trialsigma)
        modelLikelihoods[tuple(perm)] += np.log(likelihood)
        prior = posteriors[tuple(perm)]
        posteriors[tuple(perm)] = prior * likelihood
     
     denominator = np.sum(posteriors.values())
     if denominator != 0:
        for perm in parameters:
            posteriors[tuple(perm)] = posteriors[tuple(perm)] / denominator

  # Marginalizing over the 3 parameters to plot separate posteriors. 
  for i in range(parameterGridDimension):
    ds[d[i]] = 0
    thetas[theta[i]] = 0
    sigmas[sigma[i]] = 0

  for perm in parameters:
    x = perm[0] # d 
    y = perm[1] # theta
    z = perm[2] # sigma
    post = posteriors[tuple(perm)]
    ds[x] += post
    thetas[y] += post
    sigmas[z] += post
Пример #3
0
def main():
  # Generates all the permutation of models given a list for d, theta and sigma
  parameters = np.vstack(np.meshgrid(d, theta, sigma)).reshape(3,-1).T
  numModels = len(parameters)
  # Initialize the likelihood and posteriors.
  # - Posteriors are set to a uniform distribution
  # - Likelihoods are set to 0.
  for perm in parameters:
     modelLikelihoods[tuple(perm)] = 0
     # We will assume a uniform prior over all models.
     posteriors[tuple(perm)] = 1.0 / numModels

  # Creates the item values to be used in the simulation
  trialConditions = []
  for i in xrange(numTrials):
        value_list = []
        for i in xrange(nItems):
             itemValue = random.choice(potential_values)
             value_list.append(itemValue)
        trialConditions.append(value_list)

  # Run the simulation using known parameter values 
  RT, choice, fixItem, fixTime, fixRDV, uninterruptedLastFixTime, lst_of_item_values_dict = \
     run_simulations(nItems, numTrials, trialConditions, actuald, actualTheta, actualSigma)
  
  # Extract the data from the above simulation
  for trial in xrange(numTrials):
     print(trial)
     trialChoice = choice[trial]
     leftValue = lst_of_item_values_dict[0][trial]
     rightValue = lst_of_item_values_dict[1][trial]
     itemFixation = fixItem[trial]
     timeOfFixations = fixTime[trial]

     # Get the likelihood for each model and update the model likelihoods
     for perm in parameters:
        triald, trialtheta, trialsigma = tuple(perm)
        likelihood = get_trial_likelihood(trialChoice, leftValue, rightValue, itemFixation, timeOfFixations,
                                 triald, trialtheta, trialsigma)
        modelLikelihoods[tuple(perm)] = likelihood
     
     # Get the denominator for normalizing the posteriors.
     denominator = 0
     for perm in parameters:
        denominator += posteriors[tuple(perm)] * modelLikelihoods[tuple(perm)]
     if denominator == 0:
        continue

     # Calculate the posteriors after this trial.
     for perm in parameters:
        # The new prior is the previous posterior because that is the prior information we have.
        # At the very start this will simply be the uniform distribution. 
        prior = posteriors[tuple(perm)]
        posteriors[tuple(perm)] = modelLikelihoods[tuple(perm)] * prior / denominator
  print("Done with Computation of Posteriors!")
  # Marginalizing over the 3 parameters to plot separate posteriors. 
  for i in range(parameterGridDimension):
    ds[d[i]] = 0 
    thetas[theta[i]] = 0
    sigmas[sigma[i]] = 0

  for perm in parameters:
    x = perm[0] # d 
    y = perm[1] # theta
    z = perm[2] # sigma
    post = posteriors[tuple(perm)]
    ds[x] += post
    thetas[y] += post
    sigmas[z] += post