Skip to content

Instantly share code, notes, and snippets.

@tanimislam
Created October 23, 2015 21:10
Show Gist options
  • Save tanimislam/9683d932aedcc03fee17 to your computer and use it in GitHub Desktop.
Save tanimislam/9683d932aedcc03fee17 to your computer and use it in GitHub Desktop.
Python module that contains functionality to calculate a FUNKY collection of distribution functions, that sum to 1 over M sample points.
#!/usr/bin/env python
import numpy, pandas, pylab
def _return_prob_dist_sub(M = 5, N = 1000000):
us = numpy.random.random_sample((M-1, N))
ainits = numpy.zeros((M, N))
#
z = numpy.ones( N )
ainits[0,:] = z * us[0,:]
for jdx in xrange(1, M - 1):
z = z * (1 - us[jdx-1,:] )
ainits[jdx,:] = z * us[jdx,:]
z = z * (1 - us[M-2,:] )
ainits[M-1,:] = z
#
alpha_shuffle = numpy.zeros((M, N))
idx_shuffle = numpy.zeros((M, N), dtype=int)
for jdx in xrange(N):
a0 = ainits[:,jdx]
vals = numpy.random.permutation( numpy.linspace( 0, M-1, M, dtype=int))
alpha_shuffle[:,jdx] = a0[vals]
return alpha_shuffle
def calc_prob_dist(M = 5, N = 1000000):
assert(isinstance(M, int))
assert(isinstance(N, int))
assert(M >= 1)
assert(N >= 1)
#
nums, rem = divmod( N, 1000000)
alpha_shuffle = numpy.zeros((M, N))
for idx in xrange(nums):
alpha_shuffle[:,idx*1000000:(idx+1)*1000000] = _return_prob_dist_sub(M, 1000000)
if rem != 0:
alpha_shuffle[:,nums*1000000:] = _return_prob_dist_sub(M, rem)
return alpha_shuffle
def draw_data( alpha_shuffle ):
vars, lengths = alpha_shuffle.shape
df = pandas.DataFrame({ r'alpha_%d' % (idx + 1) : pandas.Series( alpha_shuffle[idx,:] ) for
idx in xrange(vars) })
df.plot(kind = 'hist', bins = 1000, normed = True, alpha = 0.1, linewidth = 0)
pylab.savefig('distrib_%d_%d.pdf' % (vars, lengths), bbox_inches = 'tight')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment