예제 #1
0
def test():
    # math
    from math import pi, sqrt, exp
    # access the package
    import gsl

    # build a random number generator
    rng = gsl.rng()

    # the order of the distribution
    K = 10
    # the weight vectors
    α = gsl.vector(shape=K).fill(K**(-1/2))
    # build a gaussian distribution
    dirichlet = gsl.pdf.dirichlet(alpha=α, rng=rng)

    # make a vector
    v = gsl.vector(shape=K)
    # fill it with random numbers
    v.random(pdf=dirichlet)

    # make a matrix
    m = gsl.matrix(shape=(50, K))
    # fill it with random numbers
    m.random(pdf=dirichlet)

    return dirichlet
예제 #2
0
def test():
    # access the package
    import gsl

    # build a random number generator
    rng = gsl.rng()
    # build a uniform distribution
    uniform = gsl.pdf.uniform_pos(rng=rng)

    # sample it
    sample = uniform.sample()
    # assert the sample is within (0, 1)
    assert sample > 0 and sample < 1

    density = uniform.density(0)
    assert density == 1

    # make a vector
    v = gsl.vector(1000)
    # fill it with random numbers
    uniform.vector(vector=v)
    # assert all the samples are positive
    for sample in v: assert sample > 0 and sample < 1

    # make a matrix
    m = gsl.matrix(shape=(100, 100))
    # fill it with random numbers
    uniform.matrix(matrix=m)
    # assert all the samples are positive
    for sample in m: assert sample > 0 and sample < 1

    return uniform
예제 #3
0
def test():
    # math
    from math import pi, sqrt, exp
    # access the package
    import gsl

    # the σ of the distribution
    σ = 2
    # build a random number generator
    rng = gsl.rng()
    # build a gaussian distribution
    gaussian = gsl.pdf.gaussian(mean=0.0, sigma=σ, rng=rng)

    # sample it
    sample = gaussian.sample()

    # compute the density
    x = 0
    density = gaussian.density(x)
    assert density == 1 / sqrt(2 * pi * σ**2) * exp(-x**2 / (2 * σ**2))

    # make a vector
    v = gsl.vector(1000)
    # fill it with random numbers
    gaussian.vector(vector=v)

    # make a matrix
    m = gsl.matrix(shape=(50, 200))
    # fill it with random numbers
    gaussian.matrix(matrix=m)

    return gaussian
예제 #4
0
def test():
    # math
    from math import pi, sqrt, exp
    # access the package
    import gsl

    # the σ of the distribution
    σ = 2
    # build a random number generator
    rng = gsl.rng()
    # build a gaussian distribution
    gaussian = gsl.pdf.gaussian(mean=0.0, sigma=σ, rng=rng)

    # sample it
    sample = gaussian.sample()

    # compute the density
    x = 0
    density = gaussian.density(x)
    assert density == 1/sqrt(2*pi*σ**2) * exp(-x**2/ (2*σ**2))

    # make a vector
    v = gsl.vector(1000)
    # fill it with random numbers
    gaussian.vector(vector=v)

    # make a matrix
    m = gsl.matrix(shape=(50, 200))
    # fill it with random numbers
    gaussian.matrix(matrix=m)

    return gaussian
예제 #5
0
def test():
    # access the package
    import gsl

    # the support of the distribution
    support = (-1, 1)
    # build a random number generator
    rng = gsl.rng()
    # build a uniform distribution
    uniform = gsl.pdf.uniform(support=support, rng=rng)

    # sample it
    sample = uniform.sample()
    assert sample >= support[0] and sample < support[1]

    density = uniform.density(0)
    assert density == 1 / (support[1] - support[0])

    # make a vector
    v = gsl.vector(1000)
    # fill it with random numbers
    uniform.vector(vector=v)

    # make a matrix
    m = gsl.matrix(shape=(100, 100))
    # fill it with random numbers
    uniform.matrix(matrix=m)

    return uniform
예제 #6
0
def test():
    # access the package
    import gsl

    # the support of the distribution
    support = (-1,1)
    # build a random number generator
    rng = gsl.rng()
    # build a uniform distribution
    uniform = gsl.pdf.uniform(support=support, rng=rng)

    # sample it
    sample = uniform.sample()
    assert sample >= support[0] and sample < support[1]

    density = uniform.density(0)
    assert density == 1/(support[1]-support[0])

    # make a vector
    v = gsl.vector(1000)
    # fill it with random numbers
    uniform.vector(vector=v)

    # make a matrix
    m = gsl.matrix(shape=(100, 100))
    # fill it with random numbers
    uniform.matrix(matrix=m)

    return uniform
예제 #7
0
def test():
    # access the package
    import gsl

    # build a random number generator
    rng = gsl.rng()
    # build a uniform distribution
    uniform = gsl.pdf.uniform_pos(rng=rng)

    # sample it
    sample = uniform.sample()
    # assert the sample is within (0, 1)
    assert sample > 0 and sample < 1

    density = uniform.density(0)
    assert density == 1

    # make a vector
    v = gsl.vector(1000)
    # fill it with random numbers
    uniform.vector(vector=v)
    # assert all the samples are positive
    for sample in v:
        assert sample > 0 and sample < 1

    # make a matrix
    m = gsl.matrix(shape=(100, 100))
    # fill it with random numbers
    uniform.matrix(matrix=m)
    # assert all the samples are positive
    for sample in m:
        assert sample > 0 and sample < 1

    return uniform
예제 #8
0
def test():
    # package access
    import gsl
    # make a matrix
    m = gsl.matrix(shape=(100, 50))
    # set it to random values
    m.random(pdf=gsl.pdf.gaussian(mean=0.0, sigma=2, rng=gsl.rng()))
    # all done
    return m
예제 #9
0
def test():
    # package access
    import gsl
    # make a vector
    v = gsl.vector(shape=100)
    # set it to random values
    v.random(pdf=gsl.pdf.uniform(support=(-1, 1), rng=gsl.rng()))
    # all done
    return v
예제 #10
0
def test():
    # package access
    import gsl
    # make a vector
    v = gsl.vector(shape=100)
    # set it to random values
    v.random(pdf=gsl.pdf.uniform(support=(-1,1), rng=gsl.rng()))
    # all done
    return v
예제 #11
0
def test():
    # package access
    import gsl
    # make a matrix
    m = gsl.matrix(shape=(100, 50))
    # fill it with random values
    m.random(pdf=gsl.pdf.gaussian(mean=0.0, sigma=2, rng=gsl.rng()))

    # rows
    i = 3
    # set the ith row to all {i}
    for j in range(m.columns):
        m[i, j] = i
    # get the row using the interface
    row = m.getRow(i)
    # check that it is a vector
    assert row.shape == m.columns
    # full of {i}
    assert row == gsl.vector(shape=m.columns).fill(i)

    # columns
    j = 2
    # set the jth column to all {j}
    for i in range(m.rows):
        m[i, j] = j
    # get the column using the interface
    column = m.getColumn(j)
    # check that it is a vector
    assert column.shape == m.rows
    # full of {j}
    assert column == gsl.vector(shape=m.rows).fill(j)

    # shape
    rows = 100
    columns = 200
    # make another matrix
    m = gsl.matrix(shape=(rows, columns)).zero()

    # make a vector of ones
    ones = gsl.vector(shape=columns).fill(1.0)
    # set the middle column
    m.setRow(rows / 2, ones)
    # verify it was done properly
    assert m.getRow(rows / 2) == ones

    # make a vector of twos
    twos = gsl.vector(shape=rows).fill(2.0)
    # set the middle column
    m.setColumn(columns / 2, twos)
    # verify it was done properly
    assert m.getColumn(columns / 2) == twos

    # all done
    return m
예제 #12
0
def test():
    # package access
    import gsl
    # make a matrix
    m = gsl.matrix(shape=(100,50))
    # fill it with random values
    m.random(pdf=gsl.pdf.gaussian(mean=0.0, sigma=2, rng=gsl.rng()))

    # rows
    i = 3
    # set the ith row to all {i}
    for j in range(m.columns) : m[i,j] = i
    # get the row using the interface
    row = m.getRow(i)
    # check that it is a vector
    assert row.shape == m.columns
    # full of {i}
    assert row == gsl.vector(shape=m.columns).fill(i)

    # columns
    j = 2
    # set the jth column to all {j}
    for i in range(m.rows): m[i,j] = j
    # get the column using the interface
    column = m.getColumn(j)
    # check that it is a vector
    assert column.shape == m.rows
    # full of {j}
    assert column == gsl.vector(shape=m.rows).fill(j)

    # shape
    rows = 100
    columns = 200
    # make another matrix
    m = gsl.matrix(shape=(rows,columns)).zero()

    # make a vector of ones
    ones = gsl.vector(shape=columns).fill(1.0)
    # set the middle column
    m.setRow(rows/2, ones)
    # verify it was done properly
    assert m.getRow(rows/2) == ones

    # make a vector of twos
    twos = gsl.vector(shape=rows).fill(2.0)
    # set the middle column
    m.setColumn(columns/2, twos)
    # verify it was done properly
    assert m.getColumn(columns/2) == twos

    # all done
    return m
예제 #13
0
def test():
    import gsl

    # print(" ++ rng_allocate:")
    # loop over all the known algorithms
    for algorithm in gsl.rng.available:
        # print("    {}".format(algorithm))
        # instantiate the rng
        rng = gsl.rng(algorithm=algorithm)
        # check
        assert rng.algorithm == algorithm

    # all done
    return gsl.rng
예제 #14
0
def test():
    # package access
    import gsl
    # make a matrix
    m = gsl.matrix(shape=(100,50))
    # set it to random values
    m.random(pdf=gsl.pdf.gaussian(mean=0.0, sigma=2, rng=gsl.rng()))
    # clone it
    n = m.clone()
    # and check it
    assert m == n

    # all done
    return m, n
예제 #15
0
def test():
    import gsl

    # print(" ++ rng_allocate:")
    # loop over all the known algorithms
    for algorithm in gsl.rng.available:
        # print("    {}".format(algorithm))
        # instantiate the rng
        rng = gsl.rng(algorithm=algorithm)
        # check
        assert rng.algorithm == algorithm

    # all done
    return gsl.rng
예제 #16
0
def test():
    # package access
    import gsl
    # make a vector
    v = gsl.vector(shape=100)
    # set it to some value
    v.random(pdf=gsl.pdf.uniform(support=(-1, 1), rng=gsl.rng()))

    # clone it
    u = v.clone()
    # and check it
    assert u == v

    # all done
    return v, u
예제 #17
0
def test():
    # package access
    import gsl
    # make a vector
    v = gsl.vector(shape=100)
    # set it to some value
    v.random(pdf=gsl.pdf.uniform(support=(-1,1), rng=gsl.rng()))

    # clone it
    u = v.clone()
    # and check it
    assert u == v

    # all done
    return v, u
예제 #18
0
def test():
    # access the package
    import gsl

    # pick an algorithm
    algorithm = 'ranlxs2'

    # instantiate the rng
    rng = gsl.rng(algorithm=algorithm)
    # check its name
    assert rng.algorithm == algorithm
    # and its range
    assert rng.range == (0, 2**24-1) # ranlxs2 provides 24 bits of randomness

    # all done
    return gsl.rng
예제 #19
0
def test():
    # package access
    import gsl
    # make a vector and initialize it
    v = gsl.vector(shape=100)
    # prime
    for index in range(v.shape):
        v[index] = 2 * index + 1
    # shuffle
    vs = v.shuffle(rng=gsl.rng())
    # check it
    assert vs.mean() == v.mean()
    assert vs.max() == v.max()
    assert vs.min() == v.min()
    # all done
    return v
예제 #20
0
파일: rng_range.py 프로젝트: jlmaurer/pyre
def test():
    # access the package
    import gsl

    # pick an algorithm
    algorithm = 'ranlxs2'

    # instantiate the rng
    rng = gsl.rng(algorithm=algorithm)
    # check its name
    assert rng.algorithm == algorithm
    # and its range
    assert rng.range == (0, 2**24-1) # ranlxs2 provides 24 bits of randomness

    # all done
    return gsl.rng
예제 #21
0
def test():
    # access the package
    import gsl

    # pick an algorithm
    algorithm = 'ranlxs2'

    # instantiate the rng
    rng = gsl.rng(algorithm=algorithm)
    # check its name
    assert rng.algorithm == algorithm
    # grab a random number
    sample = rng.float()
    # check that it falls within the range
    assert sample >= 0 and sample < 1

    # all done
    return gsl.rng
예제 #22
0
파일: rng_float.py 프로젝트: jlmaurer/pyre
def test():
    # access the package
    import gsl

    # pick an algorithm
    algorithm = 'ranlxs2'

    # instantiate the rng
    rng = gsl.rng(algorithm=algorithm)
    # check its name
    assert rng.algorithm == algorithm
    # grab a random number
    sample = rng.float()
    # check that it falls within the range
    assert sample >= 0 and sample < 1

    # all done
    return gsl.rng
예제 #23
0
def test():
    # package access
    import gsl
    # make two vectors
    length = 100
    v1 = gsl.vector(shape=length)
    v2 = gsl.vector(shape=length)
    # set them to random values
    rng = gsl.rng()
    v1.random(pdf=gsl.pdf.uniform(support=(-1,1), rng=rng))
    v2.random(pdf=gsl.pdf.uniform(support=(-1,1), rng=rng))

    # call correlation
    covariance = gsl.stats.covariance(v1, v2)

    # covariance between a vector and itself is its variance
    assert gsl.stats.covariance(v1, v1) == v1.variance(mean=v1.mean())

    # all done
    return covariance
예제 #24
0
def test():
    # package access
    import gsl
    # make two vectors
    length = 100
    v1 = gsl.vector(shape=length)
    v2 = gsl.vector(shape=length)
    # set them to random values
    rng = gsl.rng()
    v1.random(pdf=gsl.pdf.uniform(support=(-1,1), rng=rng))
    v2.random(pdf=gsl.pdf.uniform(support=(-1,1), rng=rng))

    # call correlation
    covariance = gsl.stats.covariance(v1, v2)

    # covariance between a vector and itself is its variance
    assert gsl.stats.covariance(v1, v1) == v1.variance(mean=v1.mean())

    # all done
    return covariance
예제 #25
0
def test():
    # package access
    import gsl
    # make two vectors
    length = 100
    v1 = gsl.vector(shape=length)
    v2 = gsl.vector(shape=length)
    # set them to random values
    rng = gsl.rng()
    v1.random(pdf=gsl.pdf.uniform(support=(-1,1), rng=rng))
    v2.random(pdf=gsl.pdf.uniform(support=(-1,1), rng=rng))

    # call correlation
    correlation = gsl.stats.correlation(v1, v2)

    # correlation of a vector with itself should be one
    assert gsl.stats.correlation(v1, v1) == 1.0

    # all done
    return correlation
예제 #26
0
def test():
    # package access
    import gsl
    # make two vectors
    length = 100
    v1 = gsl.vector(shape=length)
    v2 = gsl.vector(shape=length)
    # set them to random values
    rng = gsl.rng()
    v1.random(pdf=gsl.pdf.uniform(support=(-1,1), rng=rng))
    v2.random(pdf=gsl.pdf.uniform(support=(-1,1), rng=rng))

    # call correlation
    correlation = gsl.stats.correlation(v1, v2)

    # correlation of a vector with itself should be one
    assert gsl.stats.correlation(v1, v1) == 1.0

    # all done
    return correlation
예제 #27
0
파일: linalg.py 프로젝트: lijun99/pyre
def test():
    """
    Test Cholesky/trmm/inverse/gemm
    """
    samples = 100
    parameters = 512

    precision = 'float32' # or 'float64'
    #### GSL ####

    # make a sigma and init its values
    sigma = gsl.matrix(shape=(parameters, parameters))
    for i in range(parameters):
        for j in range(parameters):
            sigma[i,j] = 1 if i==j else (i+1)*(j+1)*0.001/(samples*samples)



    # cholesky factorization
    sigma_chol = sigma.clone()
    sigma_chol = gsl.linalg.cholesky_decomposition(sigma_chol)

    # create random gaussian samples
    rng = gsl.rng()
    gaussian = gsl.pdf.gaussian(0, 1, rng)
    random = gsl.matrix(shape=(samples, parameters))
    gaussian.matrix(random)

    # trmm
    jump = random.clone()
    jump = gsl.blas.dtrmm(
            sigma_chol.sideRight, sigma_chol.upperTriangular, sigma_chol.opNoTrans, sigma_chol.nonUnitDiagonal,
            1, sigma_chol, jump)

    #gsl_blas_TYPEtrmm(CblasRight, CblasUpper, CblasNoTrans, CblasNonUnit, 1, chol, delta);

    # gemm
    product = gsl.matrix(shape =(samples, parameters))
    gsl.blas.dgemm(0, 0, 1.0, random, sigma, 0, product)

    # inverse
    sigma_inv = sigma.clone()
    lu = gsl.linalg.LU_decomposition(sigma_inv)
    sigma_inv = gsl.linalg.LU_invert(*lu)


    ##### CUDA ######
    device = cuda.manager.device(0)

    # copy sigma from cpu to hgpu
    dsigma = cuda.matrix(source=sigma, dtype=precision)

    # Cholesky
    dsigma_chol = dsigma.clone()
    dsigma_chol = dsigma_chol.Cholesky(uplo=cuda.cublas.FillModeUpper)

    # trmm
    djump = cuda.matrix(source=random, dtype=precision)
    drandom = cuda.matrix(source=random, dtype=precision)
    djump = cuda.cublas.trmm(dsigma_chol, djump, out=djump, uplo=cuda.cublas.FillModeUpper, side=cuda.cublas.SideRight)

    # gemm
    dproduct = cuda.cublas.gemm(drandom, dsigma)

    # inverse
    dsigma_inv = dsigma.clone()
    dsigma_inv.inverse()


    ### compare ####
    print("Copying between cpu and gpu, maxerror: ",
        cuda.stats.max_diff(dsigma, cuda.matrix(source=sigma, dtype=precision)))

    dsigma_chol.copy_triangle(fill=1)
    print("Cholesky factorization, max difference between cpu/gpu results: ",
          cuda.stats.max_diff(dsigma_chol, cuda.matrix(source=sigma_chol, dtype=precision)))

    print("trmm, max difference between cpu/gpu results: ",
          cuda.stats.max_diff(djump, cuda.matrix(source=jump, dtype=precision)))

    print("gemm, max difference between cpu/gpu results: ",
          cuda.stats.max_diff(dproduct, cuda.matrix(source=product, dtype=precision)))

    print("matrix inverse, max difference between cpu/gpu results: ",
          cuda.stats.max_diff(dsigma_inv, cuda.matrix(source=sigma_inv, dtype=precision)))


    # determinant
    lu = gsl.linalg.LU_decomposition(sigma)
    det =  gsl.linalg.LU_det(*lu)
    gdet = dsigma.determinant(triangular=False)
    print("matrix determinant, (relative) difference between cpu/gpu results: ", abs(gdet/det-1))
    glogdet = dsigma.log_det()
    logdet = gsl.linalg.LU_lndet(*lu)
    print("matrix log determinant, relative difference between cpu/gpu results: ", abs(glogdet/logdet-1), gdet, det)

    return