Esempio n. 1
0
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
Esempio n. 2
0
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
Esempio n. 3
0
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)
Esempio n. 4
0
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)
Esempio n. 5
0
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)
Esempio n. 6
0
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)
Esempio n. 7
0
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)
Esempio n. 8
0
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)