Skip to content

Instantly share code, notes, and snippets.

@ChrisCooper
Created October 20, 2012 10:18
Show Gist options
  • Select an option

  • Save ChrisCooper/3922883 to your computer and use it in GitHub Desktop.

Select an option

Save ChrisCooper/3922883 to your computer and use it in GitHub Desktop.
Part of an assignment for a computational neuroscience class
#python 2.7
#Bayesian integration of information assignment
#Chris Cooper
from scipy.stats import norm
#observed
firing_rates = [40.8084, 44.3164, 34.1953, 75.3335, 43.4339,
50.6627, 49.4592, 47.3496, 38.5744, 14.938]
#For left and right
initial_priors = (0.5, 0.5)
reference_means = (60.0, 50.0)
std_dev = 10.0
#For indexing tuples
LEFT = 0; RIGHT = 1
def main():
"""Iteratively integrates the evidence from each neuron to refine a likely
"""
current_priors = initial_priors
print("\nInitial priors: %0.5f (left), %0.5f (right)" % (current_priors[LEFT],
current_priors[RIGHT]))
print("Calculating posteriors by Bayseian inference")
for rate in firing_rates:
current_priors = posteriors(current_priors, rate)
print("Left: %0.5f, right: %0.5f" % (current_priors[LEFT], current_priors[RIGHT]))
print("\nFinal posteriors: %0.5f (left), %0.5f (right)" % (current_priors[LEFT],
current_priors[RIGHT]))
#Thecnically there should be an "inconclusive" conclusion possible,
#but this is fine for the purposes of this assignment.
print("The reach direction appears to be to the %s.\n" %
("left" if current_priors[LEFT] > 0.5 else "right"))
def posteriors(priors, observed_rate):
"""Uses Bayesian inference to return the left and right posterior
resulting from the integration of an observed rate."""
likelihoods = (likelihood(observed_rate, LEFT),
likelihood(observed_rate, RIGHT))
marginal = likelihoods[LEFT] * priors[LEFT] + \
likelihoods[RIGHT] * priors[RIGHT]
return (likelihoods[LEFT] * priors[LEFT] / marginal,
likelihoods[RIGHT] * priors[RIGHT] / marginal)
def likelihood(rate, direction):
"""Uses the known mean firing rates to calculate the likelihood
of an observed rate during a reach in a given direction."""
return norm.pdf(rate, loc=reference_means[direction], scale=std_dev)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment