Skip to content

Instantly share code, notes, and snippets.

/text

Created May 31, 2012 03:19
Show Gist options
  • Save anonymous/2840728 to your computer and use it in GitHub Desktop.
Save anonymous/2840728 to your computer and use it in GitHub Desktop.
def discrete_sample(self,num_samples,burn_in=0):
sample = randint(self.range[0],self.range[1],self.size)
samples = []
prop_dist = 1.0/(self.range[1]**2)
print prop_dist
print self.dist([0,0])
print self.dist([0,1])
print self.dist([1,0])
print self.dist([1,1])
proposal = randint(self.range[0],self.range[1],self.size)
while len(samples) < num_samples:
proposal_prob = min(1, self.dist(proposal)*1.0/self.dist(sample))
# print 'self.dist(prop): %f | self.dist(sample): %f' %(self.dist(proposal),self.dist(sample))
# print self.dist(proposal)*1.0/self.dist(sample)
# print proposal_prob
if proposal_prob > np.random.rand():
print sample
samples.append(sample)
sample = proposal
proposal = randint(self.range[0],self.range[1],self.size)
return samples
input distribution
..[[ 0.05 0.45]
[ 0.25 0.25]]
output distribution after 10,000 samples
[[ 0.3 0.3]
[ 0.2 0.2]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment