Exemple #1
0
def test2():
    mu = 10.0
    sigma = 2.0

    x = Variable("x", float)
    loggauss = -0.5 * math.log(2.0 * math.pi * sigma * sigma) - 0.5 * (
        (x - mu)**2) / (sigma * sigma)

    f = Function(name="foo", params=(x, ), rettype=float, expr=loggauss)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))

    samples = metropolis_hastings(func_ptr, sigma, 0.0, 1000, 2)
    #plt.plot(np.arange(len(samples)), samples)

    n, bins, patches = plt.hist(samples[500:],
                                25,
                                normed=1,
                                histtype='stepfilled')
    plt.setp(patches, 'facecolor', 'g', 'alpha', 0.75)

    # add a line showing the expected distribution
    y = plt.normpdf(bins, mu, sigma)
    l = plt.plot(bins, y, 'k--', linewidth=1.5)
    plt.show()
Exemple #2
0
def test2():
    mu = 10.0
    sigma = 2.0

    x = Variable("x", float)
    loggauss = -0.5 * math.log( 2.0 * math.pi * sigma * sigma ) - 0.5 * ((x - mu) ** 2) / (sigma * sigma)

    f = Function(
            name="foo",
            params=(x,),
            rettype=float,
            expr=loggauss)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))

    samples = metropolis_hastings(func_ptr, sigma, 0.0, 1000, 2)
    #plt.plot(np.arange(len(samples)), samples)

    n, bins, patches = plt.hist(samples[500:], 25, normed=1, histtype='stepfilled')
    plt.setp(patches, 'facecolor', 'g', 'alpha', 0.75)

    # add a line showing the expected distribution
    y = plt.normpdf(bins, mu, sigma)
    l = plt.plot(bins, y, 'k--', linewidth=1.5)
    plt.show()
Exemple #3
0
def test1():

    x = Variable("x", float)
    f = Function(name="foo", params=(x, ), rettype=float, expr=x**2)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    call_and_print_result(func_ptr)
Exemple #4
0
def test4():
    x = Variable("x", float)
    f = Function(name="foo", params=(x, ), rettype=float, expr=x**2)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_double, c_double)(func_ptr)

    assert almost_eq(cfunc(23.45), 23.45 * 23.45)
Exemple #5
0
def test2():
    x = Variable("x", int)
    y = Variable("y", int)
    f = Function(name="foo", params=(x, y), rettype=int, expr=10 * x - y)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_longlong, c_longlong, c_longlong)(func_ptr)
    assert cfunc(5, 3) == 10 * 5 - 3
Exemple #6
0
def test1():
    x = Variable("x", float)
    y = Variable("y", float)
    f = Function(name="foo", params=(x, y), rettype=float, expr=10 * x + y)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_double, c_double, c_double)(func_ptr)
    assert almost_eq(cfunc(1.0, 3.5), 10 * 1.0 + 3.5)
Exemple #7
0
def test1():

    x = Variable("x", float)
    f = Function(
            name="foo",
            params=(x,),
            rettype=float,
            expr=x ** 2)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    call_and_print_result(func_ptr)
Exemple #8
0
def test3():
    x = Variable("x", float)
    f = Function(name="foo",
                 params=(x, ),
                 rettype=float,
                 expr=10 * Log(x) + Exp(x) + 1)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_double, c_double)(func_ptr)

    assert almost_eq(cfunc(3.5), 10.0 * math.log(3.5) + math.exp(3.5) + 1.0)
Exemple #9
0
def test4():
    x = Variable("x", float)
    f = Function(
            name="foo",
            params=(x,),
            rettype=float,
            expr=x ** 2)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_double, c_double)(func_ptr)

    assert almost_eq(cfunc(23.45), 23.45 * 23.45)
Exemple #10
0
def test3():
    x = Variable("x", float)
    f = Function(
            name="foo",
            params=(x,),
            rettype=float,
            expr=10 * Log(x) + Exp(x) + 1)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_double, c_double)(func_ptr)

    assert almost_eq(cfunc(3.5), 10.0 * math.log(3.5) + math.exp(3.5) + 1.0)
Exemple #11
0
def test2():
    x = Variable("x", int)
    y = Variable("y", int)
    f = Function(
            name="foo",
            params=(x, y),
            rettype=int,
            expr=10 * x - y)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_longlong, c_longlong, c_longlong)(func_ptr)
    assert cfunc(5, 3) == 10 * 5 - 3
Exemple #12
0
def test1():
    x = Variable("x", float)
    y = Variable("y", float)
    f = Function(
            name="foo",
            params=(x, y),
            rettype=float,
            expr=10 * x + y)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_double, c_double, c_double)(func_ptr)
    assert almost_eq(cfunc(1.0, 3.5), 10 * 1.0 + 3.5)
Exemple #13
0
def test5():
    # logpdf of Gaussian
    mu = 10.0
    sigma = 2.0

    x = Variable("x", float)
    loggauss = -0.5 * math.log(2.0 * math.pi * sigma * sigma) - 0.5 * (
        (x - mu)**2) / (sigma * sigma)
    f = Function(name="foo", params=(x, ), rettype=float, expr=loggauss)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_double, c_double)(func_ptr)

    from scipy.stats import norm
    assert almost_eq(cfunc(5.0), norm.logpdf(5.0, loc=mu, scale=sigma))
Exemple #14
0
def test5():
    # logpdf of Gaussian
    mu = 10.0
    sigma = 2.0

    x = Variable("x", float)
    loggauss = -0.5 * math.log( 2.0 * math.pi * sigma * sigma ) - 0.5 * ((x - mu) ** 2) / (sigma * sigma)
    f = Function(
            name="foo",
            params=(x,),
            rettype=float,
            expr=loggauss)
    engine = create_execution_engine()
    module = f.compile(engine)
    func_ptr = engine.get_pointer_to_function(module.get_function("foo"))
    cfunc = CFUNCTYPE(c_double, c_double)(func_ptr)

    from scipy.stats import norm
    assert almost_eq(cfunc(5.0), norm.logpdf(5.0, loc=mu, scale=sigma))