def scipy_discrete_sequence(n,distribution=False): """ Return sample sequence of length n from a given discrete distribution. distribution=histogram of values, will be normalized """ try: import scipy.stats as stats except ImportError: print("Import error: not able to import scipy") return import bisect if not distribution: return "no distribution specified" p=distribution random._inst = random.Random() # make CDF out of distribution to use for sample cdf=[] cdf.append(0.0) psum=float(sum(p)) for i in range(0,len(p)): cdf.append(cdf[i]+p[i]/psum) # get a uniform random number stats.seed(random.randint(1,2**30),random.randint(1,2**30)) inputseq=stats.uniform(size=n) # choose from CDF seq=[bisect.bisect_left(cdf,s)-1 for s in inputseq] return seq
def scipy_discrete_sequence(n, distribution=False): """ Return sample sequence of length n from a given discrete distribution distribution=histogram of values, will be normalized """ try: import scipy.stats as stats except ImportError: print "Import error: not able to import scipy" return import bisect if not distribution: return "no distribution specified" p = distribution random._inst = random.Random() # make CDF out of distribution to use for sample cdf = [] cdf.append(0.0) psum = float(sum(p)) for i in range(0, len(p)): cdf.append(cdf[i] + p[i] / psum) # get a uniform random number stats.seed(random.randint(1, 2**30), random.randint(1, 2**30)) inputseq = stats.uniform(size=n) # choose from CDF seq = [bisect.bisect_left(cdf, s) - 1 for s in inputseq] return seq
def scipy_pareto_sequence(n,exponent=1.0): """ Return sample sequence of length n from a Pareto distribution. """ try: import scipy.stats as stats except ImportError: print("Import error: not able to import scipy") return random._inst = random.Random() stats.seed(random.randint(1,2**30),random.randint(1,2**30)) return stats.pareto(exponent,size=n)
def scipy_uniform_sequence(n): """ Return sample sequence of length n from a uniform distribution. """ try: import scipy.stats as stats except ImportError: print("Import error: not able to import scipy") return random._inst = random.Random() stats.seed(random.randint(1,2**30),random.randint(1,2**30)) return stats.uniform(size=n)
def scipy_poisson_sequence(n,mu=1.0): """ Return sample sequence of length n from a Poisson distribution. """ try: import scipy.stats as stats except ImportError: print "Import error: not able to import scipy" return random._inst = random.Random() stats.seed(random.randint(1,2**30),random.randint(1,2**30)) return stats.poisson(mu,size=n)
def scipy_uniform_sequence(n): """ Return sample sequence of length n from a uniform distribution. """ try: import scipy.stats as stats except ImportError: print "Import error: not able to import scipy" return random._inst = random.Random() stats.seed(random.randint(1, 2**30), random.randint(1, 2**30)) return stats.uniform(size=n)
def scipy_poisson_sequence(n, mu=1.0): """ Return sample sequence of length n from a Poisson distribution. """ try: import scipy.stats as stats except ImportError: print "Import error: not able to import scipy" return random._inst = random.Random() stats.seed(random.randint(1, 2**30), random.randint(1, 2**30)) return stats.poisson(mu, size=n)
def scipy_powerlaw_sequence(n, exponent=2.0): """ Return sample sequence of length n from a power law distribution. """ try: import scipy.stats as stats except ImportError: print "Import error: not able to import scipy" return random._inst = random.Random() stats.seed(random.randint(1, 2**30), random.randint(1, 2**30)) return stats.pareto(exponent - 1, size=n)